First
[anni] / test / pleroma / upload / filter / exiftool / read_description_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Upload.Filter.Exiftool.ReadDescriptionTest do
6   use Pleroma.DataCase, async: true
7   alias Pleroma.Upload.Filter
8
9   @uploads %Pleroma.Upload{
10     name: "image_with_imagedescription_and_caption-abstract.jpg",
11     content_type: "image/jpeg",
12     path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
13     tempfile: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
14     description: nil
15   }
16
17   test "keeps description when not empty" do
18     uploads = %Pleroma.Upload{
19       name: "image_with_imagedescription_and_caption-abstract.jpg",
20       content_type: "image/jpeg",
21       path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
22       tempfile:
23         Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
24       description: "Some description"
25     }
26
27     assert Filter.Exiftool.ReadDescription.filter(uploads) ==
28              {:ok, :noop}
29   end
30
31   test "otherwise returns ImageDescription when present" do
32     uploads_after = %Pleroma.Upload{
33       name: "image_with_imagedescription_and_caption-abstract.jpg",
34       content_type: "image/jpeg",
35       path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
36       tempfile:
37         Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
38       description: "a descriptive white pixel"
39     }
40
41     assert Filter.Exiftool.ReadDescription.filter(@uploads) ==
42              {:ok, :filtered, uploads_after}
43   end
44
45   test "Ignores warnings" do
46     uploads = %Pleroma.Upload{
47       name: "image_with_imagedescription_and_caption-abstract_and_stray_data_after.png",
48       content_type: "image/png",
49       path:
50         Path.absname(
51           "test/fixtures/image_with_imagedescription_and_caption-abstract_and_stray_data_after.png"
52         ),
53       tempfile:
54         Path.absname(
55           "test/fixtures/image_with_imagedescription_and_caption-abstract_and_stray_data_after.png"
56         )
57     }
58
59     assert {:ok, :filtered, %{description: "a descriptive white pixel"}} =
60              Filter.Exiftool.ReadDescription.filter(uploads)
61
62     uploads = %Pleroma.Upload{
63       name: "image_with_stray_data_after.png",
64       content_type: "image/png",
65       path: Path.absname("test/fixtures/image_with_stray_data_after.png"),
66       tempfile: Path.absname("test/fixtures/image_with_stray_data_after.png")
67     }
68
69     assert {:ok, :filtered, %{description: nil}} = Filter.Exiftool.ReadDescription.filter(uploads)
70   end
71
72   test "otherwise returns iptc:Caption-Abstract when present" do
73     upload = %Pleroma.Upload{
74       name: "image_with_caption-abstract.jpg",
75       content_type: "image/jpeg",
76       path: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
77       tempfile: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
78       description: nil
79     }
80
81     upload_after = %Pleroma.Upload{
82       name: "image_with_caption-abstract.jpg",
83       content_type: "image/jpeg",
84       path: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
85       tempfile: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
86       description: "an abstract white pixel"
87     }
88
89     assert Filter.Exiftool.ReadDescription.filter(upload) ==
90              {:ok, :filtered, upload_after}
91   end
92
93   test "otherwise returns nil" do
94     uploads = %Pleroma.Upload{
95       name: "image_with_no_description.jpg",
96       content_type: "image/jpeg",
97       path: Path.absname("test/fixtures/image_with_no_description.jpg"),
98       tempfile: Path.absname("test/fixtures/image_with_no_description.jpg"),
99       description: nil
100     }
101
102     assert Filter.Exiftool.ReadDescription.filter(uploads) ==
103              {:ok, :filtered, uploads}
104   end
105
106   test "Return nil when image description from EXIF data exceeds the maximum length" do
107     clear_config([:instance, :description_limit], 5)
108
109     assert Filter.Exiftool.ReadDescription.filter(@uploads) ==
110              {:ok, :filtered, @uploads}
111   end
112
113   test "Ignores content with only whitespace" do
114     uploads = %Pleroma.Upload{
115       name: "non-existant.jpg",
116       content_type: "image/jpeg",
117       path:
118         Path.absname(
119           "test/fixtures/image_with_imagedescription_and_caption-abstract_whitespaces.jpg"
120         ),
121       tempfile:
122         Path.absname(
123           "test/fixtures/image_with_imagedescription_and_caption-abstract_whitespaces.jpg"
124         ),
125       description: nil
126     }
127
128     assert Filter.Exiftool.ReadDescription.filter(uploads) ==
129              {:ok, :filtered, uploads}
130   end
131
132   test "Return nil when image description from EXIF data can't be read" do
133     uploads = %Pleroma.Upload{
134       name: "non-existant.jpg",
135       content_type: "image/jpeg",
136       path: Path.absname("test/fixtures/non-existant.jpg"),
137       tempfile: Path.absname("test/fixtures/non-existant_tmp.jpg"),
138       description: nil
139     }
140
141     assert Filter.Exiftool.ReadDescription.filter(uploads) ==
142              {:ok, :filtered, uploads}
143   end
144 end