ade2256c3475d428f42de0a09cd51df3c867b1f3
[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.ObjectValidator
9 alias Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator
10 alias Pleroma.Web.ActivityPub.Utils
11
12 import Pleroma.Factory
13
14 setup_all do
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
16
17 :ok
18 end
19
20 describe "Notes" do
21 setup do
22 user = insert(:user)
23
24 note = %{
25 "id" => Utils.generate_activity_id(),
26 "type" => "Note",
27 "actor" => user.ap_id,
28 "to" => [user.follower_address],
29 "cc" => [],
30 "content" => "Hellow this is content.",
31 "context" => "xxx",
32 "summary" => "a post"
33 }
34
35 %{user: user, note: note}
36 end
37
38 test "a basic note validates", %{note: note} do
39 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
40 end
41
42 test "a note with a language validates" do
43 user = insert(:user, %{ap_id: "https://mastodon.social/users/akkoma_ap_integration_tester"})
44 note = File.read!("test/fixtures/mastodon/note_with_language.json") |> Jason.decode!()
45 %{valid?: true, changes: %{ contentMap: %{
46 "ja" => "<p>tag</p>",
47 }}} = ArticleNotePageValidator.cast_and_validate(note)
48 end
49
50 test "a note from factory validates" do
51 note = insert(:note)
52 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note.data)
53 end
54
55 test "a note with a remote replies collection should validate", _ do
56 insert(:user, %{ap_id: "https://bookwyrm.com/user/TestUser"})
57 collection = File.read!("test/fixtures/bookwyrm-replies-collection.json")
58
59 Tesla.Mock.mock(fn %{
60 method: :get,
61 url: "https://bookwyrm.com/user/TestUser/review/17/replies?page=1"
62 } ->
63 %Tesla.Env{
64 status: 200,
65 body: collection,
66 headers: HttpRequestMock.activitypub_object_headers()
67 }
68 end)
69
70 note = Jason.decode!(File.read!("test/fixtures/bookwyrm-article.json"))
71
72 %{valid?: true, changes: %{replies: ["https://bookwyrm.com/user/TestUser/status/18"]}} =
73 ArticleNotePageValidator.cast_and_validate(note)
74 end
75
76 test "a note with an attachment should work", _ do
77 insert(:user, %{ap_id: "https://owncast.localhost.localdomain/federation/user/streamer"})
78
79 note =
80 "test/fixtures/owncast-note-with-attachment.json"
81 |> File.read!()
82 |> Jason.decode!()
83
84 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
85 end
86
87 test "a misskey MFM status with a content field should work and be linked", _ do
88 local_user =
89 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
90
91 remote_user =
92 insert(:user, %{
93 nickname: "remote_user",
94 ap_id: "http://misskey.local.live/users/remote_user"
95 })
96
97 full_tag_remote_user =
98 insert(:user, %{
99 nickname: "full_tag_remote_user",
100 ap_id: "http://misskey.local.live/users/full_tag_remote_user"
101 })
102
103 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
104
105 note =
106 "test/fixtures/misskey/mfm_x_format.json"
107 |> File.read!()
108 |> Jason.decode!()
109
110 %{
111 valid?: true,
112 changes: %{
113 content: content,
114 source: %{
115 "mediaType" => "text/x.misskeymarkdown"
116 }
117 }
118 } = ArticleNotePageValidator.cast_and_validate(note)
119
120 assert content =~
121 "<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>"
122
123 assert content =~
124 "<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>"
125
126 assert content =~
127 "<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>"
128
129 assert content =~ "@oops_not_a_mention"
130
131 assert content =~
132 "<span class=\"mfm _mfm_jelly_\" style=\"display: inline-block; animation: 1s linear 0s infinite normal both running mfm-rubberBand;\">mfm goes here</span> </p>aaa"
133 end
134
135 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
136 local_user =
137 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
138
139 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
140
141 note =
142 "test/fixtures/misskey/mfm_underscore_format.json"
143 |> File.read!()
144 |> Jason.decode!()
145
146 changes = ArticleNotePageValidator.cast_and_validate(note)
147
148 %{
149 valid?: true,
150 changes: %{
151 content: content,
152 source: %{
153 "mediaType" => "text/x.misskeymarkdown",
154 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa"
155 }
156 }
157 } = changes
158
159 assert content =~
160 "<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>"
161 end
162 end
163
164 test "a Note without replies/first/items validates" do
165 insert(:user, ap_id: "https://mastodon.social/users/emelie")
166
167 note =
168 "test/fixtures/tesla_mock/status.emelie.json"
169 |> File.read!()
170 |> Jason.decode!()
171 |> pop_in(["replies", "first", "items"])
172 |> elem(1)
173
174 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
175 end
176
177 describe "Note with history" do
178 setup do
179 user = insert(:user)
180 {:ok, activity} = Pleroma.Web.CommonAPI.post(user, %{status: "mew mew :dinosaur:"})
181 {:ok, edit} = Pleroma.Web.CommonAPI.update(user, activity, %{status: "edited :blank:"})
182
183 {:ok, %{"object" => external_rep}} =
184 Pleroma.Web.ActivityPub.Transmogrifier.prepare_outgoing(edit.data)
185
186 %{external_rep: external_rep}
187 end
188
189 test "edited note", %{external_rep: external_rep} do
190 assert %{"formerRepresentations" => %{"orderedItems" => [%{"tag" => [_]}]}} = external_rep
191
192 {:ok, validate_res, []} = ObjectValidator.validate(external_rep, [])
193
194 assert %{"formerRepresentations" => %{"orderedItems" => [%{"emoji" => %{"dinosaur" => _}}]}} =
195 validate_res
196 end
197
198 test "edited note, badly-formed formerRepresentations", %{external_rep: external_rep} do
199 external_rep = Map.put(external_rep, "formerRepresentations", %{})
200
201 assert {:error, _} = ObjectValidator.validate(external_rep, [])
202 end
203
204 test "edited note, badly-formed history item", %{external_rep: external_rep} do
205 history_item =
206 Enum.at(external_rep["formerRepresentations"]["orderedItems"], 0)
207 |> Map.put("type", "Foo")
208
209 external_rep =
210 put_in(
211 external_rep,
212 ["formerRepresentations", "orderedItems"],
213 [history_item]
214 )
215
216 assert {:error, _} = ObjectValidator.validate(external_rep, [])
217 end
218 end
219 end