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