total rebase
[anni] / test / pleroma / web / activity_pub / object_validators / attachment_validator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.UnstubbedConfigMock, as: ConfigMock
9   alias Pleroma.Web.ActivityPub.ActivityPub
10   alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
11
12   import Mox
13   import Pleroma.Factory
14
15   describe "attachments" do
16     test "fails without url" do
17       attachment = %{
18         "mediaType" => "",
19         "name" => "",
20         "summary" => "298p3RG7j27tfsZ9RQ.jpg",
21         "type" => "Document"
22       }
23
24       assert {:error, _cng} =
25                AttachmentValidator.cast_and_validate(attachment)
26                |> Ecto.Changeset.apply_action(:insert)
27     end
28
29     test "works with honkerific attachments" do
30       attachment = %{
31         "mediaType" => "",
32         "name" => "",
33         "summary" => "298p3RG7j27tfsZ9RQ.jpg",
34         "type" => "Document",
35         "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
36       }
37
38       assert {:ok, attachment} =
39                AttachmentValidator.cast_and_validate(attachment)
40                |> Ecto.Changeset.apply_action(:insert)
41
42       assert attachment.mediaType == "application/octet-stream"
43     end
44
45     test "works with an unknown but valid mime type" do
46       attachment = %{
47         "mediaType" => "x-custom/x-type",
48         "type" => "Document",
49         "url" => "https://example.org"
50       }
51
52       assert {:ok, attachment} =
53                AttachmentValidator.cast_and_validate(attachment)
54                |> Ecto.Changeset.apply_action(:insert)
55
56       assert attachment.mediaType == "x-custom/x-type"
57     end
58
59     test "works with invalid mime types" do
60       attachment = %{
61         "mediaType" => "x-customx-type",
62         "type" => "Document",
63         "url" => "https://example.org"
64       }
65
66       assert {:ok, attachment} =
67                AttachmentValidator.cast_and_validate(attachment)
68                |> Ecto.Changeset.apply_action(:insert)
69
70       assert attachment.mediaType == "application/octet-stream"
71
72       attachment = %{
73         "mediaType" => "https://example.org",
74         "type" => "Document",
75         "url" => "https://example.org"
76       }
77
78       assert {:ok, attachment} =
79                AttachmentValidator.cast_and_validate(attachment)
80                |> Ecto.Changeset.apply_action(:insert)
81
82       assert attachment.mediaType == "application/octet-stream"
83     end
84
85     test "it turns mastodon attachments into our attachments" do
86       attachment = %{
87         "url" =>
88           "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
89         "type" => "Document",
90         "name" => nil,
91         "mediaType" => "image/jpeg",
92         "blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
93       }
94
95       {:ok, attachment} =
96         AttachmentValidator.cast_and_validate(attachment)
97         |> Ecto.Changeset.apply_action(:insert)
98
99       assert [
100                %{
101                  href:
102                    "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
103                  type: "Link",
104                  mediaType: "image/jpeg"
105                }
106              ] = attachment.url
107
108       assert attachment.mediaType == "image/jpeg"
109       assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
110     end
111
112     test "it handles our own uploads" do
113       user = insert(:user)
114
115       file = %Plug.Upload{
116         content_type: "image/jpeg",
117         path: Path.absname("test/fixtures/image.jpg"),
118         filename: "an_image.jpg"
119       }
120
121       ConfigMock
122       |> stub_with(Pleroma.Test.StaticConfig)
123
124       {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
125
126       {:ok, attachment} =
127         attachment.data
128         |> AttachmentValidator.cast_and_validate()
129         |> Ecto.Changeset.apply_action(:insert)
130
131       assert attachment.mediaType == "image/jpeg"
132     end
133
134     test "it handles image dimensions" do
135       attachment = %{
136         "url" => [
137           %{
138             "type" => "Link",
139             "mediaType" => "image/jpeg",
140             "href" => "https://example.com/images/1.jpg",
141             "width" => 200,
142             "height" => 100
143           }
144         ],
145         "type" => "Document",
146         "name" => nil,
147         "mediaType" => "image/jpeg"
148       }
149
150       {:ok, attachment} =
151         AttachmentValidator.cast_and_validate(attachment)
152         |> Ecto.Changeset.apply_action(:insert)
153
154       assert [
155                %{
156                  href: "https://example.com/images/1.jpg",
157                  type: "Link",
158                  mediaType: "image/jpeg",
159                  width: 200,
160                  height: 100
161                }
162              ] = attachment.url
163
164       assert attachment.mediaType == "image/jpeg"
165     end
166
167     test "it transforms image dimensions to our internal format" do
168       attachment = %{
169         "type" => "Document",
170         "name" => "Hello world",
171         "url" => "https://media.example.tld/1.jpg",
172         "width" => 880,
173         "height" => 960,
174         "mediaType" => "image/jpeg",
175         "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
176       }
177
178       expected = %AttachmentValidator{
179         type: "Document",
180         name: "Hello world",
181         mediaType: "image/jpeg",
182         blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of",
183         url: [
184           %AttachmentValidator.UrlObjectValidator{
185             type: "Link",
186             mediaType: "image/jpeg",
187             href: "https://media.example.tld/1.jpg",
188             width: 880,
189             height: 960
190           }
191         ]
192       }
193
194       {:ok, ^expected} =
195         AttachmentValidator.cast_and_validate(attachment)
196         |> Ecto.Changeset.apply_action(:insert)
197     end
198   end
199 end