1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
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
13 import Pleroma.Factory
17 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
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"
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)
38 assert activity == new_activity
41 test "inserts a given map into the activity database, giving it an id if it has none." do
46 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
47 assert activity.data["ok"] == data["ok"]
48 assert is_binary(activity.data["id"])
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"]
65 test "adds a context when none is there" do
73 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
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"]
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
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"])
95 describe "create activities" do
96 test "removes doubled 'to' recipients" do
100 ActivityPub.create(%{
101 to: ["user1", "user1", "user2"],
107 assert activity.data["to"] == ["user1", "user2"]
108 assert activity.actor == user.ap_id
109 assert activity.recipients == ["user1", "user2"]
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"]})
119 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
120 assert length(activities) == 2
121 assert activities == [activity_one, activity_two]
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)
134 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
136 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
137 assert activities == [activity_two, activity]
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)
146 booster = insert(:user)
147 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
149 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
151 assert Enum.member?(activities, activity_two)
152 assert Enum.member?(activities, activity_three)
153 refute Enum.member?(activities, activity_one)
155 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
157 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
159 assert Enum.member?(activities, activity_two)
160 assert Enum.member?(activities, activity_three)
161 assert Enum.member?(activities, activity_one)
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)
168 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
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)
175 activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
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)
183 describe "public fetch activities" do
184 test "doesn't retrieve unlisted activities" do
187 {:ok, _unlisted_activity} =
188 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
190 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
192 [activity] = ActivityPub.fetch_public_activities()
194 assert activity == listed_activity
197 test "retrieves public activities" do
198 _activities = ActivityPub.fetch_public_activities()
200 %{public: public} = ActivityBuilder.public_and_non_public()
202 activities = ActivityPub.fetch_public_activities()
203 assert length(activities) == 1
204 assert Enum.at(activities, 0) == public
207 test "retrieves a maximum of 20 activities" do
208 activities = ActivityBuilder.insert_list(30)
209 last_expected = List.last(activities)
211 activities = ActivityPub.fetch_public_activities()
212 last = List.last(activities)
214 assert length(activities) == 20
215 assert last == last_expected
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)
224 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
225 last = List.last(activities)
227 assert length(activities) == 10
228 assert last == last_expected
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)
238 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
239 last = List.last(activities)
241 assert length(activities) == 20
242 assert last == last_expected
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"])
251 user_two = insert(:user)
253 {:ok, like_activity, object} = ActivityPub.like(user, object)
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]
263 # Just return the original activity if the user already liked it.
264 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
266 assert like_activity == same_like_activity
267 assert object.data["likes"] == [user.ap_id]
269 [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
270 assert note_activity.data["object"]["like_count"] == 1
272 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
273 assert object.data["like_count"] == 2
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"])
283 # Unliking something that hasn't been liked does nothing
284 {:ok, object} = ActivityPub.unlike(user, object)
285 assert object.data["like_count"] == 0
287 {:ok, like_activity, object} = ActivityPub.like(user, object)
288 assert object.data["like_count"] == 1
290 {:ok, _, _, object} = ActivityPub.unlike(user, object)
291 assert object.data["like_count"] == 0
293 assert Repo.get(Activity, like_activity.id) == nil
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"])
303 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
304 assert object.data["announcement_count"] == 1
305 assert object.data["announcements"] == [user.ap_id]
307 assert announce_activity.data["to"] == [
308 User.ap_followers(user),
309 note_activity.data["actor"]
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"]
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"])
324 # Unannouncing an object that is not announced does nothing
325 # {:ok, object} = ActivityPub.unannounce(user, object)
326 # assert object.data["announcement_count"] == 0
328 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
329 assert object.data["announcement_count"] == 1
331 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
332 assert object.data["announcement_count"] == 0
334 assert unannounce_activity.data["to"] == [
335 User.ap_followers(user),
336 announce_activity.data["actor"]
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"]
344 assert Repo.get(Activity, announce_activity.id) == nil
348 describe "uploading files" do
349 test "copies the file to the configured folder" do
351 content_type: "image/jpg",
352 path: Path.absname("test/fixtures/image.jpg"),
353 filename: "an_image.jpg"
356 {:ok, %Object{} = object} = ActivityPub.upload(file)
357 assert object.data["name"] == "an_image.jpg"
360 test "works with base64 encoded images" do
365 {:ok, %Object{}} = ActivityPub.upload(file)
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"])
375 assert activity == Utils.fetch_latest_follow(follower, followed)
379 describe "fetching an object" do
380 test "it fetches an object" do
382 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
384 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
385 assert activity.data["id"]
387 {:ok, object_again} =
388 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
390 assert [attachment] = object.data["attachment"]
391 assert is_list(attachment["url"])
393 assert object == object_again
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"]
401 {:ok, object_again} =
402 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
404 assert object == object_again
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)
411 object = Object.get_by_ap_id(object.data["inReplyTo"])
416 describe "following / unfollowing" do
417 test "creates a follow activity" do
418 follower = insert(:user)
419 followed = insert(:user)
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
427 test "creates an undo activity for the last follow" do
428 follower = insert(:user)
429 followed = insert(:user)
431 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
432 {:ok, activity} = ActivityPub.unfollow(follower, followed)
434 assert activity.data["type"] == "Undo"
435 assert activity.data["actor"] == follower.ap_id
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"]
444 describe "blocking / unblocking" do
445 test "creates a block activity" do
446 blocker = insert(:user)
447 blocked = insert(:user)
449 {:ok, activity} = ActivityPub.block(blocker, blocked)
451 assert activity.data["type"] == "Block"
452 assert activity.data["actor"] == blocker.ap_id
453 assert activity.data["object"] == blocked.ap_id
456 test "creates an undo activity for the last block" do
457 blocker = insert(:user)
458 blocked = insert(:user)
460 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
461 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
463 assert activity.data["type"] == "Undo"
464 assert activity.data["actor"] == blocker.ap_id
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"]
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)
479 assert delete.data["type"] == "Delete"
480 assert delete.data["actor"] == note.data["actor"]
481 assert delete.data["object"] == note.data["object"]["id"]
483 assert Repo.get(Activity, delete.id) != nil
485 assert Repo.get(Object, object.id) == nil
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)
495 {:ok, user1} = User.follow(user1, user3)
496 assert User.following?(user1, user3)
498 {:ok, user2} = User.follow(user2, user3)
499 assert User.following?(user2, user3)
501 {:ok, user3} = User.follow(user3, user2)
502 assert User.following?(user3, user2)
504 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
506 {:ok, private_activity_1} =
507 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
509 {:ok, private_activity_2} =
510 CommonAPI.post(user2, %{
512 "visibility" => "private",
513 "in_reply_to_status_id" => private_activity_1.id
516 {:ok, private_activity_3} =
517 CommonAPI.post(user3, %{
519 "visibility" => "private",
520 "in_reply_to_status_id" => private_activity_2.id
523 assert user1.following == [user3.ap_id <> "/followers", user1.ap_id]
525 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
527 assert [public_activity, private_activity_1, private_activity_3] == activities
528 assert length(activities) == 3
530 activities = ActivityPub.contain_timeline(activities, user1)
532 assert [public_activity, private_activity_1] == activities
533 assert length(activities) == 2
537 test "it can fetch plume articles" do
539 ActivityPub.fetch_object_from_id(
540 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
547 test "it creates an update activity with the new user data" do
549 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
550 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
553 ActivityPub.update(%{
554 actor: user_data["id"],
555 to: [user.follower_address],
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"]
567 test "it can fetch peertube videos" do
569 ActivityPub.fetch_object_from_id(
570 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
577 File.read!("test/fixtures/avatar_data_uri")