summaryrefslogtreecommitdiff
path: root/test/pleroma/upload/filter/exiftool/read_description_test.exs
blob: 7389fda4717bddb52bccf10236b2eca65b2e2ef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Upload.Filter.Exiftool.ReadDescriptionTest do
  use Pleroma.DataCase, async: true
  alias Pleroma.Upload.Filter

  @uploads %Pleroma.Upload{
    name: "image_with_imagedescription_and_caption-abstract.jpg",
    content_type: "image/jpeg",
    path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
    tempfile: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
    description: nil
  }

  test "keeps description when not empty" do
    uploads = %Pleroma.Upload{
      name: "image_with_imagedescription_and_caption-abstract.jpg",
      content_type: "image/jpeg",
      path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
      tempfile:
        Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
      description: "Some description"
    }

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

  test "otherwise returns ImageDescription when present" do
    uploads_after = %Pleroma.Upload{
      name: "image_with_imagedescription_and_caption-abstract.jpg",
      content_type: "image/jpeg",
      path: Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
      tempfile:
        Path.absname("test/fixtures/image_with_imagedescription_and_caption-abstract.jpg"),
      description: "a descriptive white pixel"
    }

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

  test "otherwise returns iptc:Caption-Abstract when present" do
    upload = %Pleroma.Upload{
      name: "image_with_caption-abstract.jpg",
      content_type: "image/jpeg",
      path: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
      tempfile: Path.absname("test/fixtures/image_with_caption-abstract.jpg"),
      description: nil
    }

    upload_after = %Pleroma.Upload{
      name: "image_with_caption-abstract.jpg",
      content_type: "image/jpeg",
      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"
    }

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

  test "otherwise returns nil" do
    uploads = %Pleroma.Upload{
      name: "image_with_no_description.jpg",
      content_type: "image/jpeg",
      path: Path.absname("test/fixtures/image_with_no_description.jpg"),
      tempfile: Path.absname("test/fixtures/image_with_no_description.jpg"),
      description: nil
    }

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

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

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

  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

  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
    }

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