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