5b95ebc51f61877867ad1b76bd3327a4e85b9897
[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 from factory validates" do
43 note = insert(:note)
44 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note.data)
45 end
46
47 test "a note with a remote replies collection should validate", _ do
48 insert(:user, %{ap_id: "https://bookwyrm.com/user/TestUser"})
49 collection = File.read!("test/fixtures/bookwyrm-replies-collection.json")
50
51 Tesla.Mock.mock(fn %{
52 method: :get,
53 url: "https://bookwyrm.com/user/TestUser/review/17/replies?page=1"
54 } ->
55 %Tesla.Env{
56 status: 200,
57 body: collection,
58 headers: HttpRequestMock.activitypub_object_headers()
59 }
60 end)
61
62 note = Jason.decode!(File.read!("test/fixtures/bookwyrm-article.json"))
63
64 %{valid?: true, changes: %{replies: ["https://bookwyrm.com/user/TestUser/status/18"]}} =
65 ArticleNotePageValidator.cast_and_validate(note)
66 end
67
68 test "a note with an attachment should work", _ do
69 insert(:user, %{ap_id: "https://owncast.localhost.localdomain/federation/user/streamer"})
70
71 note =
72 "test/fixtures/owncast-note-with-attachment.json"
73 |> File.read!()
74 |> Jason.decode!()
75
76 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
77 end
78
79 test "a misskey MFM status with a content field should work and be linked", _ do
80 local_user =
81 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
82
83 remote_user =
84 insert(:user, %{
85 nickname: "remote_user",
86 ap_id: "http://misskey.local.live/users/remote_user"
87 })
88
89 full_tag_remote_user =
90 insert(:user, %{
91 nickname: "full_tag_remote_user",
92 ap_id: "http://misskey.local.live/users/full_tag_remote_user"
93 })
94
95 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
96
97 note =
98 "test/fixtures/misskey/mfm_x_format.json"
99 |> File.read!()
100 |> Jason.decode!()
101
102 %{
103 valid?: true,
104 changes: %{
105 content: content,
106 source: %{
107 "mediaType" => "text/x.misskeymarkdown"
108 }
109 }
110 } = ArticleNotePageValidator.cast_and_validate(note)
111
112 assert content =~
113 "<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>"
114
115 assert content =~
116 "<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>"
117
118 assert content =~
119 "<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>"
120
121 assert content =~ "@oops_not_a_mention"
122
123 assert content =~
124 "<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"
125 end
126
127 test "a misskey MFM status with a _misskey_content field should work and be linked", _ do
128 local_user =
129 insert(:user, %{nickname: "akkoma_user", ap_id: "http://localhost:4001/users/akkoma_user"})
130
131 insert(:user, %{ap_id: "https://misskey.local.live/users/92hzkskwgy"})
132
133 note =
134 "test/fixtures/misskey/mfm_underscore_format.json"
135 |> File.read!()
136 |> Jason.decode!()
137
138 changes = ArticleNotePageValidator.cast_and_validate(note)
139
140 %{
141 valid?: true,
142 changes: %{
143 content: content,
144 source: %{
145 "mediaType" => "text/x.misskeymarkdown",
146 "content" => "@akkoma_user linkifylink #dancedance $[jelly mfm goes here] \n\n## aaa"
147 }
148 }
149 } = changes
150
151 assert content =~
152 "<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>"
153 end
154 end
155
156 test "a Note without replies/first/items validates" do
157 insert(:user, ap_id: "https://mastodon.social/users/emelie")
158
159 note =
160 "test/fixtures/tesla_mock/status.emelie.json"
161 |> File.read!()
162 |> Jason.decode!()
163 |> pop_in(["replies", "first", "items"])
164 |> elem(1)
165
166 %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note)
167 end
168
169 describe "Note with history" do
170 setup do
171 user = insert(:user)
172 {:ok, activity} = Pleroma.Web.CommonAPI.post(user, %{status: "mew mew :dinosaur:"})
173 {:ok, edit} = Pleroma.Web.CommonAPI.update(user, activity, %{status: "edited :blank:"})
174
175 {:ok, %{"object" => external_rep}} =
176 Pleroma.Web.ActivityPub.Transmogrifier.prepare_outgoing(edit.data)
177
178 %{external_rep: external_rep}
179 end
180
181 test "edited note", %{external_rep: external_rep} do
182 assert %{"formerRepresentations" => %{"orderedItems" => [%{"tag" => [_]}]}} = external_rep
183
184 {:ok, validate_res, []} = ObjectValidator.validate(external_rep, [])
185
186 assert %{"formerRepresentations" => %{"orderedItems" => [%{"emoji" => %{"dinosaur" => _}}]}} =
187 validate_res
188 end
189
190 test "edited note, badly-formed formerRepresentations", %{external_rep: external_rep} do
191 external_rep = Map.put(external_rep, "formerRepresentations", %{})
192
193 assert {:error, _} = ObjectValidator.validate(external_rep, [])
194 end
195
196 test "edited note, badly-formed history item", %{external_rep: external_rep} do
197 history_item =
198 Enum.at(external_rep["formerRepresentations"]["orderedItems"], 0)
199 |> Map.put("type", "Foo")
200
201 external_rep =
202 put_in(
203 external_rep,
204 ["formerRepresentations", "orderedItems"],
205 [history_item]
206 )
207
208 assert {:error, _} = ObjectValidator.validate(external_rep, [])
209 end
210 end
211 end