Ingest remote attachment width/height
[akkoma] / test / pleroma / web / activity_pub / object_validators / attachment_validator_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 "works with honkerific attachments" do
15 attachment = %{
16 "mediaType" => "",
17 "name" => "",
18 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
19 "type" => "Document",
20 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
21 }
22
23 assert {:ok, attachment} =
24 AttachmentValidator.cast_and_validate(attachment)
25 |> Ecto.Changeset.apply_action(:insert)
26
27 assert attachment.mediaType == "application/octet-stream"
28 end
29
30 test "it turns mastodon attachments into our attachments" do
31 attachment = %{
32 "url" =>
33 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
34 "type" => "Document",
35 "name" => nil,
36 "mediaType" => "image/jpeg",
37 "blurhash" => "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
38 }
39
40 {:ok, attachment} =
41 AttachmentValidator.cast_and_validate(attachment)
42 |> Ecto.Changeset.apply_action(:insert)
43
44 assert [
45 %{
46 href:
47 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
48 type: "Link",
49 mediaType: "image/jpeg"
50 }
51 ] = attachment.url
52
53 assert attachment.mediaType == "image/jpeg"
54 assert attachment.blurhash == "UD9jJz~VSbR#xT$~%KtQX9R,WAs9RjWBs:of"
55 end
56
57 test "it handles our own uploads" do
58 user = insert(:user)
59
60 file = %Plug.Upload{
61 content_type: "image/jpeg",
62 path: Path.absname("test/fixtures/image.jpg"),
63 filename: "an_image.jpg"
64 }
65
66 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
67
68 {:ok, attachment} =
69 attachment.data
70 |> AttachmentValidator.cast_and_validate()
71 |> Ecto.Changeset.apply_action(:insert)
72
73 assert attachment.mediaType == "image/jpeg"
74 end
75
76 test "it handles image dimensions" do
77 attachment = %{
78 "url" => [
79 %{
80 "type" => "Link",
81 "mediaType" => "image/jpeg",
82 "href" => "https://example.com/images/1.jpg",
83 "width" => 200,
84 "height" => 100
85 }
86 ],
87 "type" => "Document",
88 "name" => nil,
89 "mediaType" => "image/jpeg"
90 }
91
92 {:ok, attachment} =
93 AttachmentValidator.cast_and_validate(attachment)
94 |> Ecto.Changeset.apply_action(:insert)
95
96 assert [
97 %{
98 href: "https://example.com/images/1.jpg",
99 type: "Link",
100 mediaType: "image/jpeg",
101 width: 200,
102 height: 100
103 }
104 ] = attachment.url
105
106 assert attachment.mediaType == "image/jpeg"
107 end
108 end
109 end