1 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Web.ActivityPub.Utils
5 alias Pleroma.Web.CommonAPI
6 alias Pleroma.{Activity, Object, User}
7 alias Pleroma.Builders.ActivityBuilder
11 describe "building a user from his ap id" do
12 test "it returns a user" do
13 user_id = "http://mastodon.example.org/users/admin"
14 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
15 assert user.ap_id == user_id
16 assert user.nickname == "admin@mastodon.example.org"
17 assert user.info["source_data"]
18 assert user.info["ap_enabled"]
19 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
23 describe "insertion" do
24 test "returns the activity if one with the same id is already in" do
25 activity = insert(:note_activity)
26 {:ok, new_activity} = ActivityPub.insert(activity.data)
28 assert activity == new_activity
31 test "inserts a given map into the activity database, giving it an id if it has none." do
36 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
37 assert activity.data["ok"] == data["ok"]
38 assert is_binary(activity.data["id"])
48 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
49 assert activity.data["ok"] == data["ok"]
50 assert activity.data["id"] == given_id
51 assert activity.data["context"] == "blabla"
52 assert activity.data["context_id"]
55 test "adds a context when none is there" do
63 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
65 assert is_binary(activity.data["context"])
66 assert is_binary(activity.data["object"]["context"])
67 assert activity.data["context_id"]
68 assert activity.data["object"]["context_id"]
71 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
79 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
80 assert is_binary(activity.data["object"]["id"])
81 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
85 describe "create activities" do
86 test "removes doubled 'to' recipients" do
91 to: ["user1", "user1", "user2"],
97 assert activity.data["to"] == ["user1", "user2"]
98 assert activity.actor == user.ap_id
99 assert activity.recipients == ["user1", "user2"]
103 describe "fetch activities for recipients" do
104 test "retrieve the activities for certain recipients" do
105 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
106 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
107 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
109 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
110 assert length(activities) == 2
111 assert activities == [activity_one, activity_two]
115 describe "fetch activities in context" do
116 test "retrieves activities that have a given context" do
117 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
118 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
119 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
120 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
121 activity_five = insert(:note_activity)
124 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
126 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
127 assert activities == [activity_two, activity]
131 test "doesn't return blocked activities" do
132 activity_one = insert(:note_activity)
133 activity_two = insert(:note_activity)
134 activity_three = insert(:note_activity)
136 booster = insert(:user)
137 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
139 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
141 assert Enum.member?(activities, activity_two)
142 assert Enum.member?(activities, activity_three)
143 refute Enum.member?(activities, activity_one)
145 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
147 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
149 assert Enum.member?(activities, activity_two)
150 assert Enum.member?(activities, activity_three)
151 assert Enum.member?(activities, activity_one)
153 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
154 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
155 %Activity{} = boost_activity = Activity.get_create_activity_by_object_ap_id(id)
156 activity_three = Repo.get(Activity, activity_three.id)
158 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
160 assert Enum.member?(activities, activity_two)
161 refute Enum.member?(activities, activity_three)
162 refute Enum.member?(activities, boost_activity)
163 assert Enum.member?(activities, activity_one)
165 activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
167 assert Enum.member?(activities, activity_two)
168 assert Enum.member?(activities, activity_three)
169 assert Enum.member?(activities, boost_activity)
170 assert Enum.member?(activities, activity_one)
173 describe "public fetch activities" do
174 test "doesn't retrieve unlisted activities" do
177 {:ok, unlisted_activity} =
178 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
180 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
182 [activity] = ActivityPub.fetch_public_activities()
184 assert activity == listed_activity
187 test "retrieves public activities" do
188 _activities = ActivityPub.fetch_public_activities()
190 %{public: public} = ActivityBuilder.public_and_non_public()
192 activities = ActivityPub.fetch_public_activities()
193 assert length(activities) == 1
194 assert Enum.at(activities, 0) == public
197 test "retrieves a maximum of 20 activities" do
198 activities = ActivityBuilder.insert_list(30)
199 last_expected = List.last(activities)
201 activities = ActivityPub.fetch_public_activities()
202 last = List.last(activities)
204 assert length(activities) == 20
205 assert last == last_expected
208 test "retrieves ids starting from a since_id" do
209 activities = ActivityBuilder.insert_list(30)
210 later_activities = ActivityBuilder.insert_list(10)
211 since_id = List.last(activities).id
212 last_expected = List.last(later_activities)
214 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
215 last = List.last(activities)
217 assert length(activities) == 10
218 assert last == last_expected
221 test "retrieves ids up to max_id" do
222 _first_activities = ActivityBuilder.insert_list(10)
223 activities = ActivityBuilder.insert_list(20)
224 later_activities = ActivityBuilder.insert_list(10)
225 max_id = List.first(later_activities).id
226 last_expected = List.last(activities)
228 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
229 last = List.last(activities)
231 assert length(activities) == 20
232 assert last == last_expected
236 describe "like an object" do
237 test "adds a like activity to the db" do
238 note_activity = insert(:note_activity)
239 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
241 user_two = insert(:user)
243 {:ok, like_activity, object} = ActivityPub.like(user, object)
245 assert like_activity.data["actor"] == user.ap_id
246 assert like_activity.data["type"] == "Like"
247 assert like_activity.data["object"] == object.data["id"]
248 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
249 assert like_activity.data["context"] == object.data["context"]
250 assert object.data["like_count"] == 1
251 assert object.data["likes"] == [user.ap_id]
253 # Just return the original activity if the user already liked it.
254 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
256 assert like_activity == same_like_activity
257 assert object.data["likes"] == [user.ap_id]
259 [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
260 assert note_activity.data["object"]["like_count"] == 1
262 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
263 assert object.data["like_count"] == 2
267 describe "unliking" do
268 test "unliking a previously liked object" do
269 note_activity = insert(:note_activity)
270 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
273 # Unliking something that hasn't been liked does nothing
274 {:ok, object} = ActivityPub.unlike(user, object)
275 assert object.data["like_count"] == 0
277 {:ok, like_activity, object} = ActivityPub.like(user, object)
278 assert object.data["like_count"] == 1
280 {:ok, _, _, object} = ActivityPub.unlike(user, object)
281 assert object.data["like_count"] == 0
283 assert Repo.get(Activity, like_activity.id) == nil
287 describe "announcing an object" do
288 test "adds an announce activity to the db" do
289 note_activity = insert(:note_activity)
290 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
293 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
294 assert object.data["announcement_count"] == 1
295 assert object.data["announcements"] == [user.ap_id]
297 assert announce_activity.data["to"] == [
298 User.ap_followers(user),
299 note_activity.data["actor"]
302 assert announce_activity.data["object"] == object.data["id"]
303 assert announce_activity.data["actor"] == user.ap_id
304 assert announce_activity.data["context"] == object.data["context"]
308 describe "unannouncing an object" do
309 test "unannouncing a previously announced object" do
310 note_activity = insert(:note_activity)
311 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
314 # Unannouncing an object that is not announced does nothing
315 # {:ok, object} = ActivityPub.unannounce(user, object)
316 # assert object.data["announcement_count"] == 0
318 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
319 assert object.data["announcement_count"] == 1
321 {:ok, unannounce_activity, activity, object} = ActivityPub.unannounce(user, object)
322 assert object.data["announcement_count"] == 0
324 assert activity == announce_activity
326 assert unannounce_activity.data["to"] == [
327 User.ap_followers(user),
328 announce_activity.data["actor"]
331 assert unannounce_activity.data["type"] == "Undo"
332 assert unannounce_activity.data["object"] == announce_activity.data
333 assert unannounce_activity.data["actor"] == user.ap_id
334 assert unannounce_activity.data["context"] == announce_activity.data["context"]
336 assert Repo.get(Activity, announce_activity.id) == nil
340 describe "uploading files" do
341 test "copies the file to the configured folder" do
343 content_type: "image/jpg",
344 path: Path.absname("test/fixtures/image.jpg"),
345 filename: "an_image.jpg"
348 {:ok, %Object{} = object} = ActivityPub.upload(file)
349 assert object.data["name"] == "an_image.jpg"
352 test "works with base64 encoded images" do
357 {:ok, %Object{}} = ActivityPub.upload(file)
361 describe "fetch the latest Follow" do
362 test "fetches the latest Follow activity" do
363 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
364 follower = Repo.get_by(User, ap_id: activity.data["actor"])
365 followed = Repo.get_by(User, ap_id: activity.data["object"])
367 assert activity == Utils.fetch_latest_follow(follower, followed)
371 describe "fetching an object" do
372 test "it fetches an object" do
374 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
376 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
377 assert activity.data["id"]
379 {:ok, object_again} =
380 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
382 assert [attachment] = object.data["attachment"]
383 assert is_list(attachment["url"])
385 assert object == object_again
388 test "it works with objects only available via Ostatus" do
389 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
390 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
391 assert activity.data["id"]
393 {:ok, object_again} =
394 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
396 assert object == object_again
399 test "it correctly stitches up conversations between ostatus and ap" do
400 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
401 {:ok, object} = ActivityPub.fetch_object_from_id(last)
403 object = Object.get_by_ap_id(object.data["inReplyTo"])
408 describe "following / unfollowing" do
409 test "creates a follow activity" do
410 follower = insert(:user)
411 followed = insert(:user)
413 {:ok, activity} = ActivityPub.follow(follower, followed)
414 assert activity.data["type"] == "Follow"
415 assert activity.data["actor"] == follower.ap_id
416 assert activity.data["object"] == followed.ap_id
419 test "creates an undo activity for the last follow" do
420 follower = insert(:user)
421 followed = insert(:user)
423 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
424 {:ok, activity} = ActivityPub.unfollow(follower, followed)
426 assert activity.data["type"] == "Undo"
427 assert activity.data["actor"] == follower.ap_id
428 assert activity.data["object"]["id"] == follow_activity.data["id"]
432 describe "deletion" do
433 test "it creates a delete activity and deletes the original object" do
434 note = insert(:note_activity)
435 object = Object.get_by_ap_id(note.data["object"]["id"])
436 {:ok, delete} = ActivityPub.delete(object)
438 assert delete.data["type"] == "Delete"
439 assert delete.data["actor"] == note.data["actor"]
440 assert delete.data["object"] == note.data["object"]["id"]
442 assert Repo.get(Activity, delete.id) != nil
444 assert Repo.get(Object, object.id) == nil
449 test "it creates an update activity with the new user data" do
451 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
452 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
455 ActivityPub.update(%{
456 actor: user_data["id"],
457 to: [user.follower_address],
462 assert update.data["actor"] == user.ap_id
463 assert update.data["to"] == [user.follower_address]
464 assert update.data["object"]["id"] == user_data["id"]
465 assert update.data["object"]["type"] == user_data["type"]
470 File.read!("test/fixtures/avatar_data_uri")