tests: add legal boilerplate
[akkoma] / test / web / activity_pub / activity_pub_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
6 use Pleroma.DataCase
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.ActivityPub.Utils
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.{Activity, Object, User}
11 alias Pleroma.Builders.ActivityBuilder
12
13 import Pleroma.Factory
14 import Tesla.Mock
15
16 setup do
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 describe "building a user from his ap id" do
22 test "it returns a user" do
23 user_id = "http://mastodon.example.org/users/admin"
24 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
25 assert user.ap_id == user_id
26 assert user.nickname == "admin@mastodon.example.org"
27 assert user.info.source_data
28 assert user.info.ap_enabled
29 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
30 end
31 end
32
33 describe "insertion" do
34 test "returns the activity if one with the same id is already in" do
35 activity = insert(:note_activity)
36 {:ok, new_activity} = ActivityPub.insert(activity.data)
37
38 assert activity == new_activity
39 end
40
41 test "inserts a given map into the activity database, giving it an id if it has none." do
42 data = %{
43 "ok" => true
44 }
45
46 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
47 assert activity.data["ok"] == data["ok"]
48 assert is_binary(activity.data["id"])
49
50 given_id = "bla"
51
52 data = %{
53 "ok" => true,
54 "id" => given_id,
55 "context" => "blabla"
56 }
57
58 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
59 assert activity.data["ok"] == data["ok"]
60 assert activity.data["id"] == given_id
61 assert activity.data["context"] == "blabla"
62 assert activity.data["context_id"]
63 end
64
65 test "adds a context when none is there" do
66 data = %{
67 "id" => "some_id",
68 "object" => %{
69 "id" => "object_id"
70 }
71 }
72
73 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
74
75 assert is_binary(activity.data["context"])
76 assert is_binary(activity.data["object"]["context"])
77 assert activity.data["context_id"]
78 assert activity.data["object"]["context_id"]
79 end
80
81 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
82 data = %{
83 "object" => %{
84 "type" => "Note",
85 "ok" => true
86 }
87 }
88
89 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
90 assert is_binary(activity.data["object"]["id"])
91 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
92 end
93 end
94
95 describe "create activities" do
96 test "removes doubled 'to' recipients" do
97 user = insert(:user)
98
99 {:ok, activity} =
100 ActivityPub.create(%{
101 to: ["user1", "user1", "user2"],
102 actor: user,
103 context: "",
104 object: %{}
105 })
106
107 assert activity.data["to"] == ["user1", "user2"]
108 assert activity.actor == user.ap_id
109 assert activity.recipients == ["user1", "user2"]
110 end
111 end
112
113 describe "fetch activities for recipients" do
114 test "retrieve the activities for certain recipients" do
115 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
116 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
117 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
118
119 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
120 assert length(activities) == 2
121 assert activities == [activity_one, activity_two]
122 end
123 end
124
125 describe "fetch activities in context" do
126 test "retrieves activities that have a given context" do
127 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
128 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
129 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
130 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
131 activity_five = insert(:note_activity)
132 user = insert(:user)
133
134 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
135
136 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
137 assert activities == [activity_two, activity]
138 end
139 end
140
141 test "doesn't return blocked activities" do
142 activity_one = insert(:note_activity)
143 activity_two = insert(:note_activity)
144 activity_three = insert(:note_activity)
145 user = insert(:user)
146 booster = insert(:user)
147 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
148
149 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
150
151 assert Enum.member?(activities, activity_two)
152 assert Enum.member?(activities, activity_three)
153 refute Enum.member?(activities, activity_one)
154
155 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
156
157 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
158
159 assert Enum.member?(activities, activity_two)
160 assert Enum.member?(activities, activity_three)
161 assert Enum.member?(activities, activity_one)
162
163 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
164 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
165 %Activity{} = boost_activity = Activity.get_create_activity_by_object_ap_id(id)
166 activity_three = Repo.get(Activity, activity_three.id)
167
168 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
169
170 assert Enum.member?(activities, activity_two)
171 refute Enum.member?(activities, activity_three)
172 refute Enum.member?(activities, boost_activity)
173 assert Enum.member?(activities, activity_one)
174
175 activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
176
177 assert Enum.member?(activities, activity_two)
178 assert Enum.member?(activities, activity_three)
179 assert Enum.member?(activities, boost_activity)
180 assert Enum.member?(activities, activity_one)
181 end
182
183 describe "public fetch activities" do
184 test "doesn't retrieve unlisted activities" do
185 user = insert(:user)
186
187 {:ok, _unlisted_activity} =
188 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
189
190 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
191
192 [activity] = ActivityPub.fetch_public_activities()
193
194 assert activity == listed_activity
195 end
196
197 test "retrieves public activities" do
198 _activities = ActivityPub.fetch_public_activities()
199
200 %{public: public} = ActivityBuilder.public_and_non_public()
201
202 activities = ActivityPub.fetch_public_activities()
203 assert length(activities) == 1
204 assert Enum.at(activities, 0) == public
205 end
206
207 test "retrieves a maximum of 20 activities" do
208 activities = ActivityBuilder.insert_list(30)
209 last_expected = List.last(activities)
210
211 activities = ActivityPub.fetch_public_activities()
212 last = List.last(activities)
213
214 assert length(activities) == 20
215 assert last == last_expected
216 end
217
218 test "retrieves ids starting from a since_id" do
219 activities = ActivityBuilder.insert_list(30)
220 later_activities = ActivityBuilder.insert_list(10)
221 since_id = List.last(activities).id
222 last_expected = List.last(later_activities)
223
224 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
225 last = List.last(activities)
226
227 assert length(activities) == 10
228 assert last == last_expected
229 end
230
231 test "retrieves ids up to max_id" do
232 _first_activities = ActivityBuilder.insert_list(10)
233 activities = ActivityBuilder.insert_list(20)
234 later_activities = ActivityBuilder.insert_list(10)
235 max_id = List.first(later_activities).id
236 last_expected = List.last(activities)
237
238 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
239 last = List.last(activities)
240
241 assert length(activities) == 20
242 assert last == last_expected
243 end
244 end
245
246 describe "like an object" do
247 test "adds a like activity to the db" do
248 note_activity = insert(:note_activity)
249 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
250 user = insert(:user)
251 user_two = insert(:user)
252
253 {:ok, like_activity, object} = ActivityPub.like(user, object)
254
255 assert like_activity.data["actor"] == user.ap_id
256 assert like_activity.data["type"] == "Like"
257 assert like_activity.data["object"] == object.data["id"]
258 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
259 assert like_activity.data["context"] == object.data["context"]
260 assert object.data["like_count"] == 1
261 assert object.data["likes"] == [user.ap_id]
262
263 # Just return the original activity if the user already liked it.
264 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
265
266 assert like_activity == same_like_activity
267 assert object.data["likes"] == [user.ap_id]
268
269 [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
270 assert note_activity.data["object"]["like_count"] == 1
271
272 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
273 assert object.data["like_count"] == 2
274 end
275 end
276
277 describe "unliking" do
278 test "unliking a previously liked object" do
279 note_activity = insert(:note_activity)
280 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
281 user = insert(:user)
282
283 # Unliking something that hasn't been liked does nothing
284 {:ok, object} = ActivityPub.unlike(user, object)
285 assert object.data["like_count"] == 0
286
287 {:ok, like_activity, object} = ActivityPub.like(user, object)
288 assert object.data["like_count"] == 1
289
290 {:ok, _, _, object} = ActivityPub.unlike(user, object)
291 assert object.data["like_count"] == 0
292
293 assert Repo.get(Activity, like_activity.id) == nil
294 end
295 end
296
297 describe "announcing an object" do
298 test "adds an announce activity to the db" do
299 note_activity = insert(:note_activity)
300 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
301 user = insert(:user)
302
303 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
304 assert object.data["announcement_count"] == 1
305 assert object.data["announcements"] == [user.ap_id]
306
307 assert announce_activity.data["to"] == [
308 User.ap_followers(user),
309 note_activity.data["actor"]
310 ]
311
312 assert announce_activity.data["object"] == object.data["id"]
313 assert announce_activity.data["actor"] == user.ap_id
314 assert announce_activity.data["context"] == object.data["context"]
315 end
316 end
317
318 describe "unannouncing an object" do
319 test "unannouncing a previously announced object" do
320 note_activity = insert(:note_activity)
321 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
322 user = insert(:user)
323
324 # Unannouncing an object that is not announced does nothing
325 # {:ok, object} = ActivityPub.unannounce(user, object)
326 # assert object.data["announcement_count"] == 0
327
328 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
329 assert object.data["announcement_count"] == 1
330
331 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
332 assert object.data["announcement_count"] == 0
333
334 assert unannounce_activity.data["to"] == [
335 User.ap_followers(user),
336 announce_activity.data["actor"]
337 ]
338
339 assert unannounce_activity.data["type"] == "Undo"
340 assert unannounce_activity.data["object"] == announce_activity.data
341 assert unannounce_activity.data["actor"] == user.ap_id
342 assert unannounce_activity.data["context"] == announce_activity.data["context"]
343
344 assert Repo.get(Activity, announce_activity.id) == nil
345 end
346 end
347
348 describe "uploading files" do
349 test "copies the file to the configured folder" do
350 file = %Plug.Upload{
351 content_type: "image/jpg",
352 path: Path.absname("test/fixtures/image.jpg"),
353 filename: "an_image.jpg"
354 }
355
356 {:ok, %Object{} = object} = ActivityPub.upload(file)
357 assert object.data["name"] == "an_image.jpg"
358 end
359
360 test "works with base64 encoded images" do
361 file = %{
362 "img" => data_uri()
363 }
364
365 {:ok, %Object{}} = ActivityPub.upload(file)
366 end
367 end
368
369 describe "fetch the latest Follow" do
370 test "fetches the latest Follow activity" do
371 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
372 follower = Repo.get_by(User, ap_id: activity.data["actor"])
373 followed = Repo.get_by(User, ap_id: activity.data["object"])
374
375 assert activity == Utils.fetch_latest_follow(follower, followed)
376 end
377 end
378
379 describe "fetching an object" do
380 test "it fetches an object" do
381 {:ok, object} =
382 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
383
384 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
385 assert activity.data["id"]
386
387 {:ok, object_again} =
388 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
389
390 assert [attachment] = object.data["attachment"]
391 assert is_list(attachment["url"])
392
393 assert object == object_again
394 end
395
396 test "it works with objects only available via Ostatus" do
397 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
398 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
399 assert activity.data["id"]
400
401 {:ok, object_again} =
402 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
403
404 assert object == object_again
405 end
406
407 test "it correctly stitches up conversations between ostatus and ap" do
408 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
409 {:ok, object} = ActivityPub.fetch_object_from_id(last)
410
411 object = Object.get_by_ap_id(object.data["inReplyTo"])
412 assert object
413 end
414 end
415
416 describe "following / unfollowing" do
417 test "creates a follow activity" do
418 follower = insert(:user)
419 followed = insert(:user)
420
421 {:ok, activity} = ActivityPub.follow(follower, followed)
422 assert activity.data["type"] == "Follow"
423 assert activity.data["actor"] == follower.ap_id
424 assert activity.data["object"] == followed.ap_id
425 end
426
427 test "creates an undo activity for the last follow" do
428 follower = insert(:user)
429 followed = insert(:user)
430
431 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
432 {:ok, activity} = ActivityPub.unfollow(follower, followed)
433
434 assert activity.data["type"] == "Undo"
435 assert activity.data["actor"] == follower.ap_id
436
437 assert is_map(activity.data["object"])
438 assert activity.data["object"]["type"] == "Follow"
439 assert activity.data["object"]["object"] == followed.ap_id
440 assert activity.data["object"]["id"] == follow_activity.data["id"]
441 end
442 end
443
444 describe "blocking / unblocking" do
445 test "creates a block activity" do
446 blocker = insert(:user)
447 blocked = insert(:user)
448
449 {:ok, activity} = ActivityPub.block(blocker, blocked)
450
451 assert activity.data["type"] == "Block"
452 assert activity.data["actor"] == blocker.ap_id
453 assert activity.data["object"] == blocked.ap_id
454 end
455
456 test "creates an undo activity for the last block" do
457 blocker = insert(:user)
458 blocked = insert(:user)
459
460 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
461 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
462
463 assert activity.data["type"] == "Undo"
464 assert activity.data["actor"] == blocker.ap_id
465
466 assert is_map(activity.data["object"])
467 assert activity.data["object"]["type"] == "Block"
468 assert activity.data["object"]["object"] == blocked.ap_id
469 assert activity.data["object"]["id"] == block_activity.data["id"]
470 end
471 end
472
473 describe "deletion" do
474 test "it creates a delete activity and deletes the original object" do
475 note = insert(:note_activity)
476 object = Object.get_by_ap_id(note.data["object"]["id"])
477 {:ok, delete} = ActivityPub.delete(object)
478
479 assert delete.data["type"] == "Delete"
480 assert delete.data["actor"] == note.data["actor"]
481 assert delete.data["object"] == note.data["object"]["id"]
482
483 assert Repo.get(Activity, delete.id) != nil
484
485 assert Repo.get(Object, object.id) == nil
486 end
487 end
488
489 describe "timeline post-processing" do
490 test "it filters broken threads" do
491 user1 = insert(:user)
492 user2 = insert(:user)
493 user3 = insert(:user)
494
495 {:ok, user1} = User.follow(user1, user3)
496 assert User.following?(user1, user3)
497
498 {:ok, user2} = User.follow(user2, user3)
499 assert User.following?(user2, user3)
500
501 {:ok, user3} = User.follow(user3, user2)
502 assert User.following?(user3, user2)
503
504 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
505
506 {:ok, private_activity_1} =
507 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
508
509 {:ok, private_activity_2} =
510 CommonAPI.post(user2, %{
511 "status" => "hi 3",
512 "visibility" => "private",
513 "in_reply_to_status_id" => private_activity_1.id
514 })
515
516 {:ok, private_activity_3} =
517 CommonAPI.post(user3, %{
518 "status" => "hi 4",
519 "visibility" => "private",
520 "in_reply_to_status_id" => private_activity_2.id
521 })
522
523 assert user1.following == [user3.ap_id <> "/followers", user1.ap_id]
524
525 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
526
527 assert [public_activity, private_activity_1, private_activity_3] == activities
528 assert length(activities) == 3
529
530 activities = ActivityPub.contain_timeline(activities, user1)
531
532 assert [public_activity, private_activity_1] == activities
533 assert length(activities) == 2
534 end
535 end
536
537 test "it can fetch plume articles" do
538 {:ok, object} =
539 ActivityPub.fetch_object_from_id(
540 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
541 )
542
543 assert object
544 end
545
546 describe "update" do
547 test "it creates an update activity with the new user data" do
548 user = insert(:user)
549 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
550 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
551
552 {:ok, update} =
553 ActivityPub.update(%{
554 actor: user_data["id"],
555 to: [user.follower_address],
556 cc: [],
557 object: user_data
558 })
559
560 assert update.data["actor"] == user.ap_id
561 assert update.data["to"] == [user.follower_address]
562 assert update.data["object"]["id"] == user_data["id"]
563 assert update.data["object"]["type"] == user_data["type"]
564 end
565 end
566
567 test "it can fetch peertube videos" do
568 {:ok, object} =
569 ActivityPub.fetch_object_from_id(
570 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
571 )
572
573 assert object
574 end
575
576 def data_uri do
577 File.read!("test/fixtures/avatar_data_uri")
578 end
579 end