read_description_test.exs 4.11 KB
Newer Older
1
2
3
4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

Ilja's avatar
Ilja committed
5
defmodule Pleroma.Upload.Filter.Exiftool.ReadDescriptionTest do
6
7
8
9
  use Pleroma.DataCase, async: true
  alias Pleroma.Upload.Filter

  @uploads %Pleroma.Upload{
Ilja's avatar
Ilja committed
10
    name: "image_with_imagedescription_and_caption-abstract.jpg",
11
    content_type: "image/jpeg",
Ilja's avatar
Ilja committed
12
13
    path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
    tempfile: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
14
15
16
17
18
    description: nil
  }

  test "keeps description when not empty" do
    uploads = %Pleroma.Upload{
Ilja's avatar
Ilja committed
19
      name: "image_with_imagedescription_and_caption-abstract.jpg",
20
      content_type: "image/jpeg",
Ilja's avatar
Ilja committed
21
      path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
22
      tempfile:
Ilja's avatar
Ilja committed
23
24
        Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
      description: "Some description"
25
26
    }

Ilja's avatar
Ilja committed
27
    assert Filter.Exiftool.ReadDescription.filter(uploads) ==
28
29
30
31
32
             {:ok, :noop}
  end

  test "otherwise returns ImageDescription when present" do
    uploads_after = %Pleroma.Upload{
Ilja's avatar
Ilja committed
33
      name: "image_with_imagedescription_and_caption-abstract.jpg",
34
      content_type: "image/jpeg",
Ilja's avatar
Ilja committed
35
      path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
36
      tempfile:
Ilja's avatar
Ilja committed
37
38
        Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
      description: "a descriptive white pixel"
39
40
    }

Ilja's avatar
Ilja committed
41
    assert Filter.Exiftool.ReadDescription.filter(@uploads) ==
42
43
44
45
46
             {:ok, :filtered, uploads_after}
  end

  test "otherwise returns iptc:Caption-Abstract when present" do
    upload = %Pleroma.Upload{
Ilja's avatar
Ilja committed
47
      name: "image_with_caption-abstract.jpg",
48
      content_type: "image/jpeg",
Ilja's avatar
Ilja committed
49
50
      path: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
      tempfile: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
51
52
53
54
      description: nil
    }

    upload_after = %Pleroma.Upload{
Ilja's avatar
Ilja committed
55
      name: "image_with_caption-abstract.jpg",
56
      content_type: "image/jpeg",
Ilja's avatar
Ilja committed
57
58
59
      path: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
      tempfile: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
      description: "an abstract white pixel"
60
61
    }

Ilja's avatar
Ilja committed
62
    assert Filter.Exiftool.ReadDescription.filter(upload) ==
63
64
65
66
67
             {:ok, :filtered, upload_after}
  end

  test "otherwise returns nil" do
    uploads = %Pleroma.Upload{
Ilja's avatar
Ilja committed
68
      name: "image_with_no_description.jpg",
69
      content_type: "image/jpeg",
Ilja's avatar
Ilja committed
70
71
      path: Path.absname("test/fixtures/image_with_no_description.jpg"),
      tempfile: Path.absname("test/fixtures/image_with_no_description.jpg"),
72
73
74
      description: nil
    }

Ilja's avatar
Ilja committed
75
    assert Filter.Exiftool.ReadDescription.filter(uploads) ==
76
77
78
79
80
81
             {:ok, :filtered, uploads}
  end

  test "Return nil when image description from EXIF data exceeds the maximum length" do
    clear_config([:instance, :description_limit], 5)

Ilja's avatar
Ilja committed
82
    assert Filter.Exiftool.ReadDescription.filter(@uploads) ==
83
84
85
             {:ok, :filtered, @uploads}
  end

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  test "Ignores content with only whitespace" do
    uploads = %Pleroma.Upload{
      name: "non-existant.jpg",
      content_type: "image/jpeg",
      path:
        Path.absname(
          "test/fixtures/image_with_imagedescription_and_caption-abstract_whitespaces.jpg"
        ),
      tempfile:
        Path.absname(
          "test/fixtures/image_with_imagedescription_and_caption-abstract_whitespaces.jpg"
        ),
      description: nil
    }

    assert Filter.Exiftool.ReadDescription.filter(uploads) ==
             {:ok, :filtered, uploads}
  end

105
106
107
108
109
110
111
112
113
  test "Return nil when image description from EXIF data can't be read" do
    uploads = %Pleroma.Upload{
      name: "non-existant.jpg",
      content_type: "image/jpeg",
      path: Path.absname("test/fixtures/non-existant.jpg"),
      tempfile: Path.absname("test/fixtures/non-existant_tmp.jpg"),
      description: nil
    }

Ilja's avatar
Ilja committed
114
    assert Filter.Exiftool.ReadDescription.filter(uploads) ==
115
116
117
             {:ok, :filtered, uploads}
  end
end