b775515e0f85945b947b4b232b2e0b5d8020ad33
[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 end
76 end