35f0deffebd65cb3cfec8b87bf3940e478396544
[akkoma] / test / web / activity_pub / activity_pub_test.exs
1 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
2 use Pleroma.DataCase
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
8
9 import Pleroma.Factory
10
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"
20 end
21 end
22
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)
27
28 assert activity == new_activity
29 end
30
31 test "inserts a given map into the activity database, giving it an id if it has none." do
32 data = %{
33 "ok" => true
34 }
35
36 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
37 assert activity.data["ok"] == data["ok"]
38 assert is_binary(activity.data["id"])
39
40 given_id = "bla"
41
42 data = %{
43 "ok" => true,
44 "id" => given_id,
45 "context" => "blabla"
46 }
47
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"]
53 end
54
55 test "adds a context when none is there" do
56 data = %{
57 "id" => "some_id",
58 "object" => %{
59 "id" => "object_id"
60 }
61 }
62
63 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
64
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"]
69 end
70
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
72 data = %{
73 "object" => %{
74 "type" => "Note",
75 "ok" => true
76 }
77 }
78
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"])
82 end
83 end
84
85 describe "create activities" do
86 test "removes doubled 'to' recipients" do
87 user = insert(:user)
88
89 {:ok, activity} =
90 ActivityPub.create(%{
91 to: ["user1", "user1", "user2"],
92 actor: user,
93 context: "",
94 object: %{}
95 })
96
97 assert activity.data["to"] == ["user1", "user2"]
98 assert activity.actor == user.ap_id
99 assert activity.recipients == ["user1", "user2"]
100 end
101 end
102
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"]})
108
109 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
110 assert length(activities) == 2
111 assert activities == [activity_one, activity_two]
112 end
113 end
114
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)
122 user = insert(:user)
123
124 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
125
126 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
127 assert activities == [activity_two, activity]
128 end
129 end
130
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)
135 user = insert(:user)
136 booster = insert(:user)
137 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
138
139 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
140
141 assert Enum.member?(activities, activity_two)
142 assert Enum.member?(activities, activity_three)
143 refute Enum.member?(activities, activity_one)
144
145 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
146
147 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
148
149 assert Enum.member?(activities, activity_two)
150 assert Enum.member?(activities, activity_three)
151 assert Enum.member?(activities, activity_one)
152
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)
157
158 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
159
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)
164
165 activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
166
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)
171 end
172
173 describe "public fetch activities" do
174 test "doesn't retrieve unlisted activities" do
175 user = insert(:user)
176
177 {:ok, unlisted_activity} =
178 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
179
180 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
181
182 [activity] = ActivityPub.fetch_public_activities()
183
184 assert activity == listed_activity
185 end
186
187 test "retrieves public activities" do
188 _activities = ActivityPub.fetch_public_activities()
189
190 %{public: public} = ActivityBuilder.public_and_non_public()
191
192 activities = ActivityPub.fetch_public_activities()
193 assert length(activities) == 1
194 assert Enum.at(activities, 0) == public
195 end
196
197 test "retrieves a maximum of 20 activities" do
198 activities = ActivityBuilder.insert_list(30)
199 last_expected = List.last(activities)
200
201 activities = ActivityPub.fetch_public_activities()
202 last = List.last(activities)
203
204 assert length(activities) == 20
205 assert last == last_expected
206 end
207
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)
213
214 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
215 last = List.last(activities)
216
217 assert length(activities) == 10
218 assert last == last_expected
219 end
220
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)
227
228 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
229 last = List.last(activities)
230
231 assert length(activities) == 20
232 assert last == last_expected
233 end
234 end
235
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"])
240 user = insert(:user)
241 user_two = insert(:user)
242
243 {:ok, like_activity, object} = ActivityPub.like(user, object)
244
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]
252
253 # Just return the original activity if the user already liked it.
254 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
255
256 assert like_activity == same_like_activity
257 assert object.data["likes"] == [user.ap_id]
258
259 [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
260 assert note_activity.data["object"]["like_count"] == 1
261
262 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
263 assert object.data["like_count"] == 2
264 end
265 end
266
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"])
271 user = insert(:user)
272
273 # Unliking something that hasn't been liked does nothing
274 {:ok, object} = ActivityPub.unlike(user, object)
275 assert object.data["like_count"] == 0
276
277 {:ok, like_activity, object} = ActivityPub.like(user, object)
278 assert object.data["like_count"] == 1
279
280 {:ok, _, _, object} = ActivityPub.unlike(user, object)
281 assert object.data["like_count"] == 0
282
283 assert Repo.get(Activity, like_activity.id) == nil
284 end
285 end
286
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"])
291 user = insert(:user)
292
293 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
294 assert object.data["announcement_count"] == 1
295 assert object.data["announcements"] == [user.ap_id]
296
297 assert announce_activity.data["to"] == [
298 User.ap_followers(user),
299 note_activity.data["actor"]
300 ]
301
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"]
305 end
306 end
307
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"])
312 user = insert(:user)
313
314 # Unannouncing an object that is not announced does nothing
315 # {:ok, object} = ActivityPub.unannounce(user, object)
316 # assert object.data["announcement_count"] == 0
317
318 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
319 assert object.data["announcement_count"] == 1
320
321 {:ok, unannounce_activity, activity, object} = ActivityPub.unannounce(user, object)
322 assert object.data["announcement_count"] == 0
323
324 assert activity == announce_activity
325
326 assert unannounce_activity.data["to"] == [
327 User.ap_followers(user),
328 announce_activity.data["actor"]
329 ]
330
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"]
335
336 assert Repo.get(Activity, announce_activity.id) == nil
337 end
338 end
339
340 describe "uploading files" do
341 test "copies the file to the configured folder" do
342 file = %Plug.Upload{
343 content_type: "image/jpg",
344 path: Path.absname("test/fixtures/image.jpg"),
345 filename: "an_image.jpg"
346 }
347
348 {:ok, %Object{} = object} = ActivityPub.upload(file)
349 assert object.data["name"] == "an_image.jpg"
350 end
351
352 test "works with base64 encoded images" do
353 file = %{
354 "img" => data_uri()
355 }
356
357 {:ok, %Object{}} = ActivityPub.upload(file)
358 end
359 end
360
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"])
366
367 assert activity == Utils.fetch_latest_follow(follower, followed)
368 end
369 end
370
371 describe "fetching an object" do
372 test "it fetches an object" do
373 {:ok, object} =
374 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
375
376 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
377 assert activity.data["id"]
378
379 {:ok, object_again} =
380 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
381
382 assert [attachment] = object.data["attachment"]
383 assert is_list(attachment["url"])
384
385 assert object == object_again
386 end
387
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"]
392
393 {:ok, object_again} =
394 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
395
396 assert object == object_again
397 end
398
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)
402
403 object = Object.get_by_ap_id(object.data["inReplyTo"])
404 assert object
405 end
406 end
407
408 describe "following / unfollowing" do
409 test "creates a follow activity" do
410 follower = insert(:user)
411 followed = insert(:user)
412
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
417 end
418
419 test "creates an undo activity for the last follow" do
420 follower = insert(:user)
421 followed = insert(:user)
422
423 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
424 {:ok, activity} = ActivityPub.unfollow(follower, followed)
425
426 assert activity.data["type"] == "Undo"
427 assert activity.data["actor"] == follower.ap_id
428 assert activity.data["object"]["id"] == follow_activity.data["id"]
429 end
430 end
431
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)
437
438 assert delete.data["type"] == "Delete"
439 assert delete.data["actor"] == note.data["actor"]
440 assert delete.data["object"] == note.data["object"]["id"]
441
442 assert Repo.get(Activity, delete.id) != nil
443
444 assert Repo.get(Object, object.id) == nil
445 end
446 end
447
448 describe "update" do
449 test "it creates an update activity with the new user data" do
450 user = insert(:user)
451 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
452 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
453
454 {:ok, update} =
455 ActivityPub.update(%{
456 actor: user_data["id"],
457 to: [user.follower_address],
458 cc: [],
459 object: user_data
460 })
461
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"]
466 end
467 end
468
469 def data_uri do
470 File.read!("test/fixtures/avatar_data_uri")
471 end
472 end