a79e50a29e812d83246ca9123ff9d50ec8101490
[akkoma] / test / web / activity_pub / object_validator_test.exs
1 defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Object
5 alias Pleroma.Web.ActivityPub.ActivityPub
6 alias Pleroma.Web.ActivityPub.Builder
7 alias Pleroma.Web.ActivityPub.ObjectValidator
8 alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
9 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 describe "attachments" do
16 test "works with honkerific attachments" do
17 attachment = %{
18 "mediaType" => "image/jpeg",
19 "name" => "298p3RG7j27tfsZ9RQ.jpg",
20 "summary" => "298p3RG7j27tfsZ9RQ.jpg",
21 "type" => "Document",
22 "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
23 }
24
25 assert {:ok, attachment} =
26 AttachmentValidator.cast_and_validate(attachment)
27 |> Ecto.Changeset.apply_action(:insert)
28 end
29
30 test "it turns mastodon attachments into our attachments" do
31 attachment = %{
32 "url" =>
33 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
34 "type" => "Document",
35 "name" => nil,
36 "mediaType" => "image/jpeg"
37 }
38
39 {:ok, attachment} =
40 AttachmentValidator.cast_and_validate(attachment)
41 |> Ecto.Changeset.apply_action(:insert)
42
43 assert [
44 %{
45 href:
46 "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg",
47 type: "Link",
48 mediaType: "image/jpeg"
49 }
50 ] = attachment.url
51 end
52 end
53
54 describe "chat message create activities" do
55 test "it is invalid if the object already exists" do
56 user = insert(:user)
57 recipient = insert(:user)
58 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "hey")
59 object = Object.normalize(activity, false)
60
61 {:ok, create_data, _} = Builder.create(user, object.data, [recipient.ap_id])
62
63 {:error, cng} = ObjectValidator.validate(create_data, [])
64
65 assert {:object, {"The object to create already exists", []}} in cng.errors
66 end
67
68 test "it is invalid if the object data has a different `to` or `actor` field" do
69 user = insert(:user)
70 recipient = insert(:user)
71 {:ok, object_data, _} = Builder.chat_message(recipient, user.ap_id, "Hey")
72
73 {:ok, create_data, _} = Builder.create(user, object_data, [recipient.ap_id])
74
75 {:error, cng} = ObjectValidator.validate(create_data, [])
76
77 assert {:to, {"Recipients don't match with object recipients", []}} in cng.errors
78 assert {:actor, {"Actor doesn't match with object actor", []}} in cng.errors
79 end
80 end
81
82 describe "chat messages" do
83 setup do
84 clear_config([:instance, :remote_limit])
85 user = insert(:user)
86 recipient = insert(:user, local: false)
87
88 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
89
90 %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
91 end
92
93 test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
94 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
95
96 assert Map.put(valid_chat_message, "attachment", nil) == object
97 end
98
99 test "validates for a basic object with an attachment", %{
100 valid_chat_message: valid_chat_message,
101 user: user
102 } do
103 file = %Plug.Upload{
104 content_type: "image/jpg",
105 path: Path.absname("test/fixtures/image.jpg"),
106 filename: "an_image.jpg"
107 }
108
109 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
110
111 valid_chat_message =
112 valid_chat_message
113 |> Map.put("attachment", attachment.data)
114
115 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
116
117 assert object["attachment"]
118 end
119
120 test "validates for a basic object with an attachment in an array", %{
121 valid_chat_message: valid_chat_message,
122 user: user
123 } do
124 file = %Plug.Upload{
125 content_type: "image/jpg",
126 path: Path.absname("test/fixtures/image.jpg"),
127 filename: "an_image.jpg"
128 }
129
130 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
131
132 valid_chat_message =
133 valid_chat_message
134 |> Map.put("attachment", [attachment.data])
135
136 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
137
138 assert object["attachment"]
139 end
140
141 test "validates for a basic object with an attachment but without content", %{
142 valid_chat_message: valid_chat_message,
143 user: user
144 } do
145 file = %Plug.Upload{
146 content_type: "image/jpg",
147 path: Path.absname("test/fixtures/image.jpg"),
148 filename: "an_image.jpg"
149 }
150
151 {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
152
153 valid_chat_message =
154 valid_chat_message
155 |> Map.put("attachment", attachment.data)
156 |> Map.delete("content")
157
158 assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
159
160 assert object["attachment"]
161 end
162
163 test "does not validate if the message has no content", %{
164 valid_chat_message: valid_chat_message
165 } do
166 contentless =
167 valid_chat_message
168 |> Map.delete("content")
169
170 refute match?({:ok, _object, _meta}, ObjectValidator.validate(contentless, []))
171 end
172
173 test "does not validate if the message is longer than the remote_limit", %{
174 valid_chat_message: valid_chat_message
175 } do
176 Pleroma.Config.put([:instance, :remote_limit], 2)
177 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
178 end
179
180 test "does not validate if the recipient is blocking the actor", %{
181 valid_chat_message: valid_chat_message,
182 user: user,
183 recipient: recipient
184 } do
185 Pleroma.User.block(recipient, user)
186 refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
187 end
188
189 test "does not validate if the actor or the recipient is not in our system", %{
190 valid_chat_message: valid_chat_message
191 } do
192 chat_message =
193 valid_chat_message
194 |> Map.put("actor", "https://raymoo.com/raymoo")
195
196 {:error, _} = ObjectValidator.validate(chat_message, [])
197
198 chat_message =
199 valid_chat_message
200 |> Map.put("to", ["https://raymoo.com/raymoo"])
201
202 {:error, _} = ObjectValidator.validate(chat_message, [])
203 end
204
205 test "does not validate for a message with multiple recipients", %{
206 valid_chat_message: valid_chat_message,
207 user: user,
208 recipient: recipient
209 } do
210 chat_message =
211 valid_chat_message
212 |> Map.put("to", [user.ap_id, recipient.ap_id])
213
214 assert {:error, _} = ObjectValidator.validate(chat_message, [])
215 end
216
217 test "does not validate if it doesn't concern local users" do
218 user = insert(:user, local: false)
219 recipient = insert(:user, local: false)
220
221 {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
222 assert {:error, _} = ObjectValidator.validate(valid_chat_message, [])
223 end
224 end
225
226 describe "EmojiReacts" do
227 setup do
228 user = insert(:user)
229 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
230
231 object = Pleroma.Object.get_by_ap_id(post_activity.data["object"])
232
233 {:ok, valid_emoji_react, []} = Builder.emoji_react(user, object, "👌")
234
235 %{user: user, post_activity: post_activity, valid_emoji_react: valid_emoji_react}
236 end
237
238 test "it validates a valid EmojiReact", %{valid_emoji_react: valid_emoji_react} do
239 assert {:ok, _, _} = ObjectValidator.validate(valid_emoji_react, [])
240 end
241
242 test "it is not valid without a 'content' field", %{valid_emoji_react: valid_emoji_react} do
243 without_content =
244 valid_emoji_react
245 |> Map.delete("content")
246
247 {:error, cng} = ObjectValidator.validate(without_content, [])
248
249 refute cng.valid?
250 assert {:content, {"can't be blank", [validation: :required]}} in cng.errors
251 end
252
253 test "it is not valid with a non-emoji content field", %{valid_emoji_react: valid_emoji_react} do
254 without_emoji_content =
255 valid_emoji_react
256 |> Map.put("content", "x")
257
258 {:error, cng} = ObjectValidator.validate(without_emoji_content, [])
259
260 refute cng.valid?
261
262 assert {:content, {"must be a single character emoji", []}} in cng.errors
263 end
264 end
265
266 describe "Undos" do
267 setup do
268 user = insert(:user)
269 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
270 {:ok, like} = CommonAPI.favorite(user, post_activity.id)
271 {:ok, valid_like_undo, []} = Builder.undo(user, like)
272
273 %{user: user, like: like, valid_like_undo: valid_like_undo}
274 end
275
276 test "it validates a basic like undo", %{valid_like_undo: valid_like_undo} do
277 assert {:ok, _, _} = ObjectValidator.validate(valid_like_undo, [])
278 end
279
280 test "it does not validate if the actor of the undo is not the actor of the object", %{
281 valid_like_undo: valid_like_undo
282 } do
283 other_user = insert(:user, ap_id: "https://gensokyo.2hu/users/raymoo")
284
285 bad_actor =
286 valid_like_undo
287 |> Map.put("actor", other_user.ap_id)
288
289 {:error, cng} = ObjectValidator.validate(bad_actor, [])
290
291 assert {:actor, {"not the same as object actor", []}} in cng.errors
292 end
293
294 test "it does not validate if the object is missing", %{valid_like_undo: valid_like_undo} do
295 missing_object =
296 valid_like_undo
297 |> Map.put("object", "https://gensokyo.2hu/objects/1")
298
299 {:error, cng} = ObjectValidator.validate(missing_object, [])
300
301 assert {:object, {"can't find object", []}} in cng.errors
302 assert length(cng.errors) == 1
303 end
304 end
305
306 describe "deletes" do
307 setup do
308 user = insert(:user)
309 {:ok, post_activity} = CommonAPI.post(user, %{status: "cancel me daddy"})
310
311 {:ok, valid_post_delete, _} = Builder.delete(user, post_activity.data["object"])
312 {:ok, valid_user_delete, _} = Builder.delete(user, user.ap_id)
313
314 %{user: user, valid_post_delete: valid_post_delete, valid_user_delete: valid_user_delete}
315 end
316
317 test "it is valid for a post deletion", %{valid_post_delete: valid_post_delete} do
318 {:ok, valid_post_delete, _} = ObjectValidator.validate(valid_post_delete, [])
319
320 assert valid_post_delete["deleted_activity_id"]
321 end
322
323 test "it is invalid if the object isn't in a list of certain types", %{
324 valid_post_delete: valid_post_delete
325 } do
326 object = Object.get_by_ap_id(valid_post_delete["object"])
327
328 data =
329 object.data
330 |> Map.put("type", "Like")
331
332 {:ok, _object} =
333 object
334 |> Ecto.Changeset.change(%{data: data})
335 |> Object.update_and_set_cache()
336
337 {:error, cng} = ObjectValidator.validate(valid_post_delete, [])
338 assert {:object, {"object not in allowed types", []}} in cng.errors
339 end
340
341 test "it is valid for a user deletion", %{valid_user_delete: valid_user_delete} do
342 assert match?({:ok, _, _}, ObjectValidator.validate(valid_user_delete, []))
343 end
344
345 test "it's invalid if the id is missing", %{valid_post_delete: valid_post_delete} do
346 no_id =
347 valid_post_delete
348 |> Map.delete("id")
349
350 {:error, cng} = ObjectValidator.validate(no_id, [])
351
352 assert {:id, {"can't be blank", [validation: :required]}} in cng.errors
353 end
354
355 test "it's invalid if the object doesn't exist", %{valid_post_delete: valid_post_delete} do
356 missing_object =
357 valid_post_delete
358 |> Map.put("object", "http://does.not/exist")
359
360 {:error, cng} = ObjectValidator.validate(missing_object, [])
361
362 assert {:object, {"can't find object", []}} in cng.errors
363 end
364
365 test "it's invalid if the actor of the object and the actor of delete are from different domains",
366 %{valid_post_delete: valid_post_delete} do
367 valid_user = insert(:user)
368
369 valid_other_actor =
370 valid_post_delete
371 |> Map.put("actor", valid_user.ap_id)
372
373 assert match?({:ok, _, _}, ObjectValidator.validate(valid_other_actor, []))
374
375 invalid_other_actor =
376 valid_post_delete
377 |> Map.put("actor", "https://gensokyo.2hu/users/raymoo")
378
379 {:error, cng} = ObjectValidator.validate(invalid_other_actor, [])
380
381 assert {:actor, {"is not allowed to delete object", []}} in cng.errors
382 end
383
384 test "it's valid if the actor of the object is a local superuser",
385 %{valid_post_delete: valid_post_delete} do
386 user =
387 insert(:user, local: true, is_moderator: true, ap_id: "https://gensokyo.2hu/users/raymoo")
388
389 valid_other_actor =
390 valid_post_delete
391 |> Map.put("actor", user.ap_id)
392
393 {:ok, _, meta} = ObjectValidator.validate(valid_other_actor, [])
394 assert meta[:do_not_federate]
395 end
396 end
397
398 describe "likes" do
399 setup do
400 user = insert(:user)
401 {:ok, post_activity} = CommonAPI.post(user, %{status: "uguu"})
402
403 valid_like = %{
404 "to" => [user.ap_id],
405 "cc" => [],
406 "type" => "Like",
407 "id" => Utils.generate_activity_id(),
408 "object" => post_activity.data["object"],
409 "actor" => user.ap_id,
410 "context" => "a context"
411 }
412
413 %{valid_like: valid_like, user: user, post_activity: post_activity}
414 end
415
416 test "returns ok when called in the ObjectValidator", %{valid_like: valid_like} do
417 {:ok, object, _meta} = ObjectValidator.validate(valid_like, [])
418
419 assert "id" in Map.keys(object)
420 end
421
422 test "is valid for a valid object", %{valid_like: valid_like} do
423 assert LikeValidator.cast_and_validate(valid_like).valid?
424 end
425
426 test "sets the 'to' field to the object actor if no recipients are given", %{
427 valid_like: valid_like,
428 user: user
429 } do
430 without_recipients =
431 valid_like
432 |> Map.delete("to")
433
434 {:ok, object, _meta} = ObjectValidator.validate(without_recipients, [])
435
436 assert object["to"] == [user.ap_id]
437 end
438
439 test "sets the context field to the context of the object if no context is given", %{
440 valid_like: valid_like,
441 post_activity: post_activity
442 } do
443 without_context =
444 valid_like
445 |> Map.delete("context")
446
447 {:ok, object, _meta} = ObjectValidator.validate(without_context, [])
448
449 assert object["context"] == post_activity.data["context"]
450 end
451
452 test "it errors when the actor is missing or not known", %{valid_like: valid_like} do
453 without_actor = Map.delete(valid_like, "actor")
454
455 refute LikeValidator.cast_and_validate(without_actor).valid?
456
457 with_invalid_actor = Map.put(valid_like, "actor", "invalidactor")
458
459 refute LikeValidator.cast_and_validate(with_invalid_actor).valid?
460 end
461
462 test "it errors when the object is missing or not known", %{valid_like: valid_like} do
463 without_object = Map.delete(valid_like, "object")
464
465 refute LikeValidator.cast_and_validate(without_object).valid?
466
467 with_invalid_object = Map.put(valid_like, "object", "invalidobject")
468
469 refute LikeValidator.cast_and_validate(with_invalid_object).valid?
470 end
471
472 test "it errors when the actor has already like the object", %{
473 valid_like: valid_like,
474 user: user,
475 post_activity: post_activity
476 } do
477 _like = CommonAPI.favorite(user, post_activity.id)
478
479 refute LikeValidator.cast_and_validate(valid_like).valid?
480 end
481
482 test "it works when actor or object are wrapped in maps", %{valid_like: valid_like} do
483 wrapped_like =
484 valid_like
485 |> Map.put("actor", %{"id" => valid_like["actor"]})
486 |> Map.put("object", %{"id" => valid_like["object"]})
487
488 validated = LikeValidator.cast_and_validate(wrapped_like)
489
490 assert validated.valid?
491
492 assert {:actor, valid_like["actor"]} in validated.changes
493 assert {:object, valid_like["object"]} in validated.changes
494 end
495 end
496 end