f419770f2e46e7c0cac2bff08cb2037227c17c2c
[akkoma] / test / pleroma / web / activity_pub / object_validators / article_note_page_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.ArticleNotePageValidatorTest do
6 use Pleroma.DataCase, async: true
7
8 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator
9 alias Pleroma.Web.ActivityPub.Utils
10
11 import Pleroma.Factory
12
13 setup_all do
14 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
15
16 :ok
17 end
18
19 describe "Notes" do
20 setup do
21 user = insert(:user)
22
23 note = %{
24 "id" => Utils.generate_activity_id(),
25 "type" => "Note",
26 "actor" => user.ap_id,
27 "to" => [user.follower_address],
28 "cc" => [],
29 "content" => "Hellow this is content.",
30 "context" => "xxx",
31 "summary" => "a post"
32 }
33
34 %{user: user, note: note}
35 end
36
37 test "a basic note validates", %{note: note} do
38 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
39 end
40
41 test "a note with a remote replies collection should validate", _ do
42 insert(:user, %{ap_id: "https://bookwyrm.com/user/TestUser"})
43 collection = File.read!("test/fixtures/bookwyrm-replies-collection.json")
44
45 Tesla.Mock.mock(fn %{
46 method: :get,
47 url: "https://bookwyrm.com/user/TestUser/review/17/replies?page=1"
48 } ->
49 %Tesla.Env{
50 status: 200,
51 body: collection,
52 headers: HttpRequestMock.activitypub_object_headers()
53 }
54 end)
55
56 note = Jason.decode!(File.read!("test/fixtures/bookwyrm-article.json"))
57
58 %{valid?: true, changes: %{replies: ["https://bookwyrm.com/user/TestUser/status/18"]}} =
59 ArticleNotePageValidator.cast_and_validate(note)
60 end
61
62 test "a note with an attachment should work", _ do
63 insert(:user, %{ap_id: "https://owncast.localhost.localdomain/federation/user/streamer"})
64
65 note =
66 "test/fixtures/owncast-note-with-attachment.json"
67 |> File.read!()
68 |> Jason.decode!()
69
70 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
71 end
72
73 test "a misskey MFM status with a content field should work and be linked", _ do
74 local_user =
75 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
76
77 remote_user =
78 insert(:user, %{
79 nickname: "remote_user",
80 ap_id: "http://misskey.local.live/users/remote_user"
81 })
82
83 full_tag_remote_user =
84 insert(:user, %{
85 nickname: "full_tag_remote_user",
86 ap_id: "http://misskey.local.live/users/full_tag_remote_user"
87 })
88
89 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
90
91 note =
92 "test/fixtures/misskey/mfm_x_format.json"
93 |> File.read!()
94 |> Jason.decode!()
95
96 %{
97 valid?: true,
98 changes: %{
99 content: "this does not get replaced",
100 source: %{
101 "content" => content,
102 "mediaType" => "text/x.misskeymarkdown"
103 }
104 }
105 } = ArticleNotePageValidator.cast_and_validate(note)
106
107 assert content =~
108 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{local_user.id}\" href=\"#{local_user.ap_id}\" rel=\"ugc\">@<span>akkoma_user</span></a></span>"
109
110 assert content =~
111 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{remote_user.id}\" href=\"#{remote_user.ap_id}\" rel=\"ugc\">@<span>remote_user</span></a></span>"
112
113 assert content =~
114 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{full_tag_remote_user.id}\" href=\"#{full_tag_remote_user.ap_id}\" rel=\"ugc\">@<span>full_tag_remote_user</span></a></span>"
115
116 assert content =~ "@oops_not_a_mention"
117 assert content =~ "$[jelly mfm goes here] <br><br>## aaa"
118 end
119
120 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
121 local_user =
122 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
123
124 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
125
126 note =
127 "test/fixtures/misskey/mfm_underscore_format.json"
128 |> File.read!()
129 |> Jason.decode!()
130
131 changes = ArticleNotePageValidator.cast_and_validate(note)
132
133 %{
134 valid?: true,
135 changes: %{
136 source: %{
137 "content" => content,
138 "mediaType" => "text/x.misskeymarkdown"
139 }
140 }
141 } = changes
142
143 assert content =~
144 "<span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{local_user.id}\" href=\"#{local_user.ap_id}\" rel=\"ugc\">@<span>akkoma_user</span></a></span>"
145 end
146 end
147 end