1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 "works with honkerific attachments" do
18 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
20 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
23 assert {:ok, attachment} =
24 AttachmentValidator.cast_and_validate(attachment)
25 |> Ecto.Changeset.apply_action(:insert)
27 assert attachment.mediaType == "application/octet-stream"
30 test "it turns mastodon attachments into our attachments" do
33 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
36 "mediaType" => "image/jpeg",
37 "blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
41 AttachmentValidator.cast_and_validate(attachment)
42 |> Ecto.Changeset.apply_action(:insert)
47 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
49 mediaType: "image/jpeg"
53 assert attachment.mediaType == "image/jpeg"
54 assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
57 test "it handles our own uploads" do
61 content_type: "image/jpeg",
62 path: Path.absname("test/fixtures/image.jpg"),
63 filename: "an_image.jpg"
66 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
70 |> AttachmentValidator.cast_and_validate()
71 |> Ecto.Changeset.apply_action(:insert)
73 assert attachment.mediaType == "image/jpeg"
76 test "it handles image dimensions" do
81 "mediaType" => "image/jpeg",
82 "href" => "https://example.com/images/1.jpg",
89 "mediaType" => "image/jpeg"
93 AttachmentValidator.cast_and_validate(attachment)
94 |> Ecto.Changeset.apply_action(:insert)
98 href: "https://example.com/images/1.jpg",
100 mediaType: "image/jpeg",
106 assert attachment.mediaType == "image/jpeg"
109 test "it transforms image dimentions to our internal format" do
111 "type" => "Document",
112 "name" => "Hello world",
113 "url" => "https://media.example.tld/1.jpg",
116 "mediaType" => "image/jpeg",
117 "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of"
120 expected = %AttachmentValidator{
123 mediaType: "image/jpeg",
124 blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of",
126 %AttachmentValidator.UrlObjectValidator{
128 mediaType: "image/jpeg",
129 href: "https://media.example.tld/1.jpg",
137 AttachmentValidator.cast_and_validate(attachment)
138 |> Ecto.Changeset.apply_action(:insert)