Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / activity_pub / activity_pub_controller_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.ActivityPubControllerTest do
6 use Pleroma.Web.ConnCase
7 import Pleroma.Factory
8 alias Pleroma.Activity
9 alias Pleroma.Instances
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ObjectView
13 alias Pleroma.Web.ActivityPub.UserView
14
15 setup_all do
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 describe "/relay" do
21 test "with the relay active, it returns the relay user", %{conn: conn} do
22 res =
23 conn
24 |> get(activity_pub_path(conn, :relay))
25 |> json_response(200)
26
27 assert res["id"] =~ "/relay"
28 end
29
30 test "with the relay disabled, it returns 404", %{conn: conn} do
31 Pleroma.Config.put([:instance, :allow_relay], false)
32
33 conn
34 |> get(activity_pub_path(conn, :relay))
35 |> json_response(404)
36 |> assert
37
38 Pleroma.Config.put([:instance, :allow_relay], true)
39 end
40 end
41
42 describe "/users/:nickname" do
43 test "it returns a json representation of the user with accept application/json", %{
44 conn: conn
45 } do
46 user = insert(:user)
47
48 conn =
49 conn
50 |> put_req_header("accept", "application/json")
51 |> get("/users/#{user.nickname}")
52
53 user = User.get_by_id(user.id)
54
55 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
56 end
57
58 test "it returns a json representation of the user with accept application/activity+json", %{
59 conn: conn
60 } do
61 user = insert(:user)
62
63 conn =
64 conn
65 |> put_req_header("accept", "application/activity+json")
66 |> get("/users/#{user.nickname}")
67
68 user = User.get_by_id(user.id)
69
70 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
71 end
72
73 test "it returns a json representation of the user with accept application/ld+json", %{
74 conn: conn
75 } do
76 user = insert(:user)
77
78 conn =
79 conn
80 |> put_req_header(
81 "accept",
82 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
83 )
84 |> get("/users/#{user.nickname}")
85
86 user = User.get_by_id(user.id)
87
88 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
89 end
90 end
91
92 describe "/object/:uuid" do
93 test "it returns a json representation of the object with accept application/json", %{
94 conn: conn
95 } do
96 note = insert(:note)
97 uuid = String.split(note.data["id"], "/") |> List.last()
98
99 conn =
100 conn
101 |> put_req_header("accept", "application/json")
102 |> get("/objects/#{uuid}")
103
104 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
105 end
106
107 test "it returns a json representation of the object with accept application/activity+json",
108 %{conn: conn} do
109 note = insert(:note)
110 uuid = String.split(note.data["id"], "/") |> List.last()
111
112 conn =
113 conn
114 |> put_req_header("accept", "application/activity+json")
115 |> get("/objects/#{uuid}")
116
117 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
118 end
119
120 test "it returns a json representation of the object with accept application/ld+json", %{
121 conn: conn
122 } do
123 note = insert(:note)
124 uuid = String.split(note.data["id"], "/") |> List.last()
125
126 conn =
127 conn
128 |> put_req_header(
129 "accept",
130 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
131 )
132 |> get("/objects/#{uuid}")
133
134 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
135 end
136
137 test "it returns 404 for non-public messages", %{conn: conn} do
138 note = insert(:direct_note)
139 uuid = String.split(note.data["id"], "/") |> List.last()
140
141 conn =
142 conn
143 |> put_req_header("accept", "application/activity+json")
144 |> get("/objects/#{uuid}")
145
146 assert json_response(conn, 404)
147 end
148
149 test "it returns 404 for tombstone objects", %{conn: conn} do
150 tombstone = insert(:tombstone)
151 uuid = String.split(tombstone.data["id"], "/") |> List.last()
152
153 conn =
154 conn
155 |> put_req_header("accept", "application/activity+json")
156 |> get("/objects/#{uuid}")
157
158 assert json_response(conn, 404)
159 end
160 end
161
162 describe "/object/:uuid/likes" do
163 test "it returns the like activities in a collection", %{conn: conn} do
164 like = insert(:like_activity)
165 uuid = String.split(like.data["object"], "/") |> List.last()
166
167 result =
168 conn
169 |> put_req_header("accept", "application/activity+json")
170 |> get("/objects/#{uuid}/likes")
171 |> json_response(200)
172
173 assert List.first(result["first"]["orderedItems"])["id"] == like.data["id"]
174 end
175 end
176
177 describe "/activities/:uuid" do
178 test "it returns a json representation of the activity", %{conn: conn} do
179 activity = insert(:note_activity)
180 uuid = String.split(activity.data["id"], "/") |> List.last()
181
182 conn =
183 conn
184 |> put_req_header("accept", "application/activity+json")
185 |> get("/activities/#{uuid}")
186
187 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
188 end
189
190 test "it returns 404 for non-public activities", %{conn: conn} do
191 activity = insert(:direct_note_activity)
192 uuid = String.split(activity.data["id"], "/") |> List.last()
193
194 conn =
195 conn
196 |> put_req_header("accept", "application/activity+json")
197 |> get("/activities/#{uuid}")
198
199 assert json_response(conn, 404)
200 end
201 end
202
203 describe "/inbox" do
204 test "it inserts an incoming activity into the database", %{conn: conn} do
205 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
206
207 conn =
208 conn
209 |> assign(:valid_signature, true)
210 |> put_req_header("content-type", "application/activity+json")
211 |> post("/inbox", data)
212
213 assert "ok" == json_response(conn, 200)
214 :timer.sleep(500)
215 assert Activity.get_by_ap_id(data["id"])
216 end
217
218 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
219 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
220
221 sender_url = data["actor"]
222 Instances.set_consistently_unreachable(sender_url)
223 refute Instances.reachable?(sender_url)
224
225 conn =
226 conn
227 |> assign(:valid_signature, true)
228 |> put_req_header("content-type", "application/activity+json")
229 |> post("/inbox", data)
230
231 assert "ok" == json_response(conn, 200)
232 assert Instances.reachable?(sender_url)
233 end
234 end
235
236 describe "/users/:nickname/inbox" do
237 test "it inserts an incoming activity into the database", %{conn: conn} do
238 user = insert(:user)
239
240 data =
241 File.read!("test/fixtures/mastodon-post-activity.json")
242 |> Poison.decode!()
243 |> Map.put("bcc", [user.ap_id])
244
245 conn =
246 conn
247 |> assign(:valid_signature, true)
248 |> put_req_header("content-type", "application/activity+json")
249 |> post("/users/#{user.nickname}/inbox", data)
250
251 assert "ok" == json_response(conn, 200)
252 :timer.sleep(500)
253 assert Activity.get_by_ap_id(data["id"])
254 end
255
256 test "it rejects reads from other users", %{conn: conn} do
257 user = insert(:user)
258 otheruser = insert(:user)
259
260 conn =
261 conn
262 |> assign(:user, otheruser)
263 |> put_req_header("accept", "application/activity+json")
264 |> get("/users/#{user.nickname}/inbox")
265
266 assert json_response(conn, 403)
267 end
268
269 test "it returns a note activity in a collection", %{conn: conn} do
270 note_activity = insert(:direct_note_activity)
271 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
272
273 conn =
274 conn
275 |> assign(:user, user)
276 |> put_req_header("accept", "application/activity+json")
277 |> get("/users/#{user.nickname}/inbox")
278
279 assert response(conn, 200) =~ note_activity.data["object"]["content"]
280 end
281
282 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
283 user = insert(:user)
284
285 data =
286 File.read!("test/fixtures/mastodon-post-activity.json")
287 |> Poison.decode!()
288 |> Map.put("bcc", [user.ap_id])
289
290 sender_host = URI.parse(data["actor"]).host
291 Instances.set_consistently_unreachable(sender_host)
292 refute Instances.reachable?(sender_host)
293
294 conn =
295 conn
296 |> assign(:valid_signature, true)
297 |> put_req_header("content-type", "application/activity+json")
298 |> post("/users/#{user.nickname}/inbox", data)
299
300 assert "ok" == json_response(conn, 200)
301 assert Instances.reachable?(sender_host)
302 end
303 end
304
305 describe "/users/:nickname/outbox" do
306 test "it will not bomb when there is no activity", %{conn: conn} do
307 user = insert(:user)
308
309 conn =
310 conn
311 |> put_req_header("accept", "application/activity+json")
312 |> get("/users/#{user.nickname}/outbox")
313
314 result = json_response(conn, 200)
315 assert user.ap_id <> "/outbox" == result["id"]
316 end
317
318 test "it returns a note activity in a collection", %{conn: conn} do
319 note_activity = insert(:note_activity)
320 user = User.get_cached_by_ap_id(note_activity.data["actor"])
321
322 conn =
323 conn
324 |> put_req_header("accept", "application/activity+json")
325 |> get("/users/#{user.nickname}/outbox")
326
327 assert response(conn, 200) =~ note_activity.data["object"]["content"]
328 end
329
330 test "it returns an announce activity in a collection", %{conn: conn} do
331 announce_activity = insert(:announce_activity)
332 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
333
334 conn =
335 conn
336 |> put_req_header("accept", "application/activity+json")
337 |> get("/users/#{user.nickname}/outbox")
338
339 assert response(conn, 200) =~ announce_activity.data["object"]
340 end
341
342 test "it rejects posts from other users", %{conn: conn} do
343 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
344 user = insert(:user)
345 otheruser = insert(:user)
346
347 conn =
348 conn
349 |> assign(:user, otheruser)
350 |> put_req_header("content-type", "application/activity+json")
351 |> post("/users/#{user.nickname}/outbox", data)
352
353 assert json_response(conn, 403)
354 end
355
356 test "it inserts an incoming create activity into the database", %{conn: conn} do
357 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
358 user = insert(:user)
359
360 conn =
361 conn
362 |> assign(:user, user)
363 |> put_req_header("content-type", "application/activity+json")
364 |> post("/users/#{user.nickname}/outbox", data)
365
366 result = json_response(conn, 201)
367 assert Activity.get_by_ap_id(result["id"])
368 end
369
370 test "it rejects an incoming activity with bogus type", %{conn: conn} do
371 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
372 user = insert(:user)
373
374 data =
375 data
376 |> Map.put("type", "BadType")
377
378 conn =
379 conn
380 |> assign(:user, user)
381 |> put_req_header("content-type", "application/activity+json")
382 |> post("/users/#{user.nickname}/outbox", data)
383
384 assert json_response(conn, 400)
385 end
386
387 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
388 note_activity = insert(:note_activity)
389 user = User.get_cached_by_ap_id(note_activity.data["actor"])
390
391 data = %{
392 type: "Delete",
393 object: %{
394 id: note_activity.data["object"]["id"]
395 }
396 }
397
398 conn =
399 conn
400 |> assign(:user, user)
401 |> put_req_header("content-type", "application/activity+json")
402 |> post("/users/#{user.nickname}/outbox", data)
403
404 result = json_response(conn, 201)
405 assert Activity.get_by_ap_id(result["id"])
406
407 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
408 assert object
409 assert object.data["type"] == "Tombstone"
410 end
411
412 test "it rejects delete activity of object from other actor", %{conn: conn} do
413 note_activity = insert(:note_activity)
414 user = insert(:user)
415
416 data = %{
417 type: "Delete",
418 object: %{
419 id: note_activity.data["object"]["id"]
420 }
421 }
422
423 conn =
424 conn
425 |> assign(:user, user)
426 |> put_req_header("content-type", "application/activity+json")
427 |> post("/users/#{user.nickname}/outbox", data)
428
429 assert json_response(conn, 400)
430 end
431
432 test "it increases like count when receiving a like action", %{conn: conn} do
433 note_activity = insert(:note_activity)
434 user = User.get_cached_by_ap_id(note_activity.data["actor"])
435
436 data = %{
437 type: "Like",
438 object: %{
439 id: note_activity.data["object"]["id"]
440 }
441 }
442
443 conn =
444 conn
445 |> assign(:user, user)
446 |> put_req_header("content-type", "application/activity+json")
447 |> post("/users/#{user.nickname}/outbox", data)
448
449 result = json_response(conn, 201)
450 assert Activity.get_by_ap_id(result["id"])
451
452 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
453 assert object
454 assert object.data["like_count"] == 1
455 end
456 end
457
458 describe "/users/:nickname/followers" do
459 test "it returns the followers in a collection", %{conn: conn} do
460 user = insert(:user)
461 user_two = insert(:user)
462 User.follow(user, user_two)
463
464 result =
465 conn
466 |> get("/users/#{user_two.nickname}/followers")
467 |> json_response(200)
468
469 assert result["first"]["orderedItems"] == [user.ap_id]
470 end
471
472 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
473 user = insert(:user)
474 user_two = insert(:user, %{info: %{hide_followers: true}})
475 User.follow(user, user_two)
476
477 result =
478 conn
479 |> get("/users/#{user_two.nickname}/followers")
480 |> json_response(200)
481
482 assert result["first"]["orderedItems"] == []
483 assert result["totalItems"] == 0
484 end
485
486 test "it works for more than 10 users", %{conn: conn} do
487 user = insert(:user)
488
489 Enum.each(1..15, fn _ ->
490 other_user = insert(:user)
491 User.follow(other_user, user)
492 end)
493
494 result =
495 conn
496 |> get("/users/#{user.nickname}/followers")
497 |> json_response(200)
498
499 assert length(result["first"]["orderedItems"]) == 10
500 assert result["first"]["totalItems"] == 15
501 assert result["totalItems"] == 15
502
503 result =
504 conn
505 |> get("/users/#{user.nickname}/followers?page=2")
506 |> json_response(200)
507
508 assert length(result["orderedItems"]) == 5
509 assert result["totalItems"] == 15
510 end
511 end
512
513 describe "/users/:nickname/following" do
514 test "it returns the following in a collection", %{conn: conn} do
515 user = insert(:user)
516 user_two = insert(:user)
517 User.follow(user, user_two)
518
519 result =
520 conn
521 |> get("/users/#{user.nickname}/following")
522 |> json_response(200)
523
524 assert result["first"]["orderedItems"] == [user_two.ap_id]
525 end
526
527 test "it returns returns empty if the user has 'hide_follows' set", %{conn: conn} do
528 user = insert(:user, %{info: %{hide_follows: true}})
529 user_two = insert(:user)
530 User.follow(user, user_two)
531
532 result =
533 conn
534 |> get("/users/#{user.nickname}/following")
535 |> json_response(200)
536
537 assert result["first"]["orderedItems"] == []
538 assert result["totalItems"] == 0
539 end
540
541 test "it works for more than 10 users", %{conn: conn} do
542 user = insert(:user)
543
544 Enum.each(1..15, fn _ ->
545 user = User.get_by_id(user.id)
546 other_user = insert(:user)
547 User.follow(user, other_user)
548 end)
549
550 result =
551 conn
552 |> get("/users/#{user.nickname}/following")
553 |> json_response(200)
554
555 assert length(result["first"]["orderedItems"]) == 10
556 assert result["first"]["totalItems"] == 15
557 assert result["totalItems"] == 15
558
559 result =
560 conn
561 |> get("/users/#{user.nickname}/following?page=2")
562 |> json_response(200)
563
564 assert length(result["orderedItems"]) == 5
565 assert result["totalItems"] == 15
566 end
567 end
568 end