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