1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidatorTest do
6 use Pleroma.DataCase, async: true
8 alias Pleroma.UnstubbedConfigMock, as: ConfigMock
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
13 import Pleroma.Factory
15 describe "attachments" do
16 test "fails without url" do
20 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
24 assert {:error, _cng} =
25 AttachmentValidator.cast_and_validate(attachment)
26 |> Ecto.Changeset.apply_action(:insert)
29 test "works with honkerific attachments" do
33 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
35 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
38 assert {:ok, attachment} =
39 AttachmentValidator.cast_and_validate(attachment)
40 |> Ecto.Changeset.apply_action(:insert)
42 assert attachment.mediaType == "application/octet-stream"
45 test "works with an unknown but valid mime type" do
47 "mediaType" => "x-custom/x-type",
49 "url" => "https://example.org"
52 assert {:ok, attachment} =
53 AttachmentValidator.cast_and_validate(attachment)
54 |> Ecto.Changeset.apply_action(:insert)
56 assert attachment.mediaType == "x-custom/x-type"
59 test "works with invalid mime types" do
61 "mediaType" => "x-customx-type",
63 "url" => "https://example.org"
66 assert {:ok, attachment} =
67 AttachmentValidator.cast_and_validate(attachment)
68 |> Ecto.Changeset.apply_action(:insert)
70 assert attachment.mediaType == "application/octet-stream"
73 "mediaType" => "https://example.org",
75 "url" => "https://example.org"
78 assert {:ok, attachment} =
79 AttachmentValidator.cast_and_validate(attachment)
80 |> Ecto.Changeset.apply_action(:insert)
82 assert attachment.mediaType == "application/octet-stream"
85 test "it turns mastodon attachments into our attachments" do
88 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
91 "mediaType" => "image/jpeg",
92 "blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
96 AttachmentValidator.cast_and_validate(attachment)
97 |> Ecto.Changeset.apply_action(:insert)
102 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
104 mediaType: "image/jpeg"
108 assert attachment.mediaType == "image/jpeg"
109 assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
112 test "it handles our own uploads" do
116 content_type: "image/jpeg",
117 path: Path.absname("test/fixtures/image.jpg"),
118 filename: "an_image.jpg"
122 |> stub_with(Pleroma.Test.StaticConfig)
124 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
128 |> AttachmentValidator.cast_and_validate()
129 |> Ecto.Changeset.apply_action(:insert)
131 assert attachment.mediaType == "image/jpeg"
134 test "it handles image dimensions" do
139 "mediaType" => "image/jpeg",
140 "href" => "https://example.com/images/1.jpg",
145 "type" => "Document",
147 "mediaType" => "image/jpeg"
151 AttachmentValidator.cast_and_validate(attachment)
152 |> Ecto.Changeset.apply_action(:insert)
156 href: "https://example.com/images/1.jpg",
158 mediaType: "image/jpeg",
164 assert attachment.mediaType == "image/jpeg"
167 test "it transforms image dimensions to our internal format" do
169 "type" => "Document",
170 "name" => "Hello world",
171 "url" => "https://media.example.tld/1.jpg",
174 "mediaType" => "image/jpeg",
175 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
178 expected = %AttachmentValidator{
181 mediaType: "image/jpeg",
182 blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of",
184 %AttachmentValidator.UrlObjectValidator{
186 mediaType: "image/jpeg",
187 href: "https://media.example.tld/1.jpg",
195 AttachmentValidator.cast_and_validate(attachment)
196 |> Ecto.Changeset.apply_action(:insert)