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.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
11 import Pleroma.Factory
13 describe "attachments" do
14 test "fails without url" do
18 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
22 assert {:error, _cng} =
23 AttachmentValidator.cast_and_validate(attachment)
24 |> Ecto.Changeset.apply_action(:insert)
27 test "works with honkerific attachments" do
31 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
33 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
36 assert {:ok, attachment} =
37 AttachmentValidator.cast_and_validate(attachment)
38 |> Ecto.Changeset.apply_action(:insert)
40 assert attachment.mediaType == "application/octet-stream"
43 test "works with an unknown but valid mime type" do
45 "mediaType" => "x-custom/x-type",
47 "url" => "https://example.org"
50 assert {:ok, attachment} =
51 AttachmentValidator.cast_and_validate(attachment)
52 |> Ecto.Changeset.apply_action(:insert)
54 assert attachment.mediaType == "x-custom/x-type"
57 test "works with invalid mime types" do
59 "mediaType" => "x-customx-type",
61 "url" => "https://example.org"
64 assert {:ok, attachment} =
65 AttachmentValidator.cast_and_validate(attachment)
66 |> Ecto.Changeset.apply_action(:insert)
68 assert attachment.mediaType == "application/octet-stream"
71 "mediaType" => "https://example.org",
73 "url" => "https://example.org"
76 assert {:ok, attachment} =
77 AttachmentValidator.cast_and_validate(attachment)
78 |> Ecto.Changeset.apply_action(:insert)
80 assert attachment.mediaType == "application/octet-stream"
83 test "it turns mastodon attachments into our attachments" do
86 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
89 "mediaType" => "image/jpeg",
90 "blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
94 AttachmentValidator.cast_and_validate(attachment)
95 |> Ecto.Changeset.apply_action(:insert)
100 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
102 mediaType: "image/jpeg"
106 assert attachment.mediaType == "image/jpeg"
107 assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
110 test "it handles our own uploads" do
114 content_type: "image/jpeg",
115 path: Path.absname("test/fixtures/image.jpg"),
116 filename: "an_image.jpg"
119 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
123 |> AttachmentValidator.cast_and_validate()
124 |> Ecto.Changeset.apply_action(:insert)
126 assert attachment.mediaType == "image/jpeg"
129 test "it handles image dimensions" do
134 "mediaType" => "image/jpeg",
135 "href" => "https://example.com/images/1.jpg",
140 "type" => "Document",
142 "mediaType" => "image/jpeg"
146 AttachmentValidator.cast_and_validate(attachment)
147 |> Ecto.Changeset.apply_action(:insert)
151 href: "https://example.com/images/1.jpg",
153 mediaType: "image/jpeg",
159 assert attachment.mediaType == "image/jpeg"
162 test "it transforms image dimentions to our internal format" do
164 "type" => "Document",
165 "name" => "Hello world",
166 "url" => "https://media.example.tld/1.jpg",
169 "mediaType" => "image/jpeg",
170 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
173 expected = %AttachmentValidator{
176 mediaType: "image/jpeg",
177 blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of",
179 %AttachmentValidator.UrlObjectValidator{
181 mediaType: "image/jpeg",
182 href: "https://media.example.tld/1.jpg",
190 AttachmentValidator.cast_and_validate(attachment)
191 |> Ecto.Changeset.apply_action(:insert)