Use `User.get_cached*` everywhere
[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_cached_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_cached_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_cached_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 accepts messages from actors that are followed by the user", %{conn: conn} do
257 recipient = insert(:user)
258 actor = insert(:user, %{ap_id: "http://mastodon.example.org/users/actor"})
259
260 {:ok, recipient} = User.follow(recipient, actor)
261
262 data =
263 File.read!("test/fixtures/mastodon-post-activity.json")
264 |> Poison.decode!()
265
266 object =
267 data["object"]
268 |> Map.put("attributedTo", actor.ap_id)
269
270 data =
271 data
272 |> Map.put("actor", actor.ap_id)
273 |> Map.put("object", object)
274
275 conn =
276 conn
277 |> assign(:valid_signature, true)
278 |> put_req_header("content-type", "application/activity+json")
279 |> post("/users/#{recipient.nickname}/inbox", data)
280
281 assert "ok" == json_response(conn, 200)
282 :timer.sleep(500)
283 assert Activity.get_by_ap_id(data["id"])
284 end
285
286 test "it rejects reads from other users", %{conn: conn} do
287 user = insert(:user)
288 otheruser = insert(:user)
289
290 conn =
291 conn
292 |> assign(:user, otheruser)
293 |> put_req_header("accept", "application/activity+json")
294 |> get("/users/#{user.nickname}/inbox")
295
296 assert json_response(conn, 403)
297 end
298
299 test "it returns a note activity in a collection", %{conn: conn} do
300 note_activity = insert(:direct_note_activity)
301 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
302
303 conn =
304 conn
305 |> assign(:user, user)
306 |> put_req_header("accept", "application/activity+json")
307 |> get("/users/#{user.nickname}/inbox")
308
309 assert response(conn, 200) =~ note_activity.data["object"]["content"]
310 end
311
312 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
313 user = insert(:user)
314
315 data =
316 File.read!("test/fixtures/mastodon-post-activity.json")
317 |> Poison.decode!()
318 |> Map.put("bcc", [user.ap_id])
319
320 sender_host = URI.parse(data["actor"]).host
321 Instances.set_consistently_unreachable(sender_host)
322 refute Instances.reachable?(sender_host)
323
324 conn =
325 conn
326 |> assign(:valid_signature, true)
327 |> put_req_header("content-type", "application/activity+json")
328 |> post("/users/#{user.nickname}/inbox", data)
329
330 assert "ok" == json_response(conn, 200)
331 assert Instances.reachable?(sender_host)
332 end
333 end
334
335 describe "/users/:nickname/outbox" do
336 test "it will not bomb when there is no activity", %{conn: conn} do
337 user = insert(:user)
338
339 conn =
340 conn
341 |> put_req_header("accept", "application/activity+json")
342 |> get("/users/#{user.nickname}/outbox")
343
344 result = json_response(conn, 200)
345 assert user.ap_id <> "/outbox" == result["id"]
346 end
347
348 test "it returns a note activity in a collection", %{conn: conn} do
349 note_activity = insert(:note_activity)
350 user = User.get_cached_by_ap_id(note_activity.data["actor"])
351
352 conn =
353 conn
354 |> put_req_header("accept", "application/activity+json")
355 |> get("/users/#{user.nickname}/outbox")
356
357 assert response(conn, 200) =~ note_activity.data["object"]["content"]
358 end
359
360 test "it returns an announce activity in a collection", %{conn: conn} do
361 announce_activity = insert(:announce_activity)
362 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
363
364 conn =
365 conn
366 |> put_req_header("accept", "application/activity+json")
367 |> get("/users/#{user.nickname}/outbox")
368
369 assert response(conn, 200) =~ announce_activity.data["object"]
370 end
371
372 test "it rejects posts from other users", %{conn: conn} do
373 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
374 user = insert(:user)
375 otheruser = insert(:user)
376
377 conn =
378 conn
379 |> assign(:user, otheruser)
380 |> put_req_header("content-type", "application/activity+json")
381 |> post("/users/#{user.nickname}/outbox", data)
382
383 assert json_response(conn, 403)
384 end
385
386 test "it inserts an incoming create activity into the database", %{conn: conn} do
387 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
388 user = insert(:user)
389
390 conn =
391 conn
392 |> assign(:user, user)
393 |> put_req_header("content-type", "application/activity+json")
394 |> post("/users/#{user.nickname}/outbox", data)
395
396 result = json_response(conn, 201)
397 assert Activity.get_by_ap_id(result["id"])
398 end
399
400 test "it rejects an incoming activity with bogus type", %{conn: conn} do
401 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
402 user = insert(:user)
403
404 data =
405 data
406 |> Map.put("type", "BadType")
407
408 conn =
409 conn
410 |> assign(:user, user)
411 |> put_req_header("content-type", "application/activity+json")
412 |> post("/users/#{user.nickname}/outbox", data)
413
414 assert json_response(conn, 400)
415 end
416
417 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
418 note_activity = insert(:note_activity)
419 user = User.get_cached_by_ap_id(note_activity.data["actor"])
420
421 data = %{
422 type: "Delete",
423 object: %{
424 id: note_activity.data["object"]["id"]
425 }
426 }
427
428 conn =
429 conn
430 |> assign(:user, user)
431 |> put_req_header("content-type", "application/activity+json")
432 |> post("/users/#{user.nickname}/outbox", data)
433
434 result = json_response(conn, 201)
435 assert Activity.get_by_ap_id(result["id"])
436
437 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
438 assert object
439 assert object.data["type"] == "Tombstone"
440 end
441
442 test "it rejects delete activity of object from other actor", %{conn: conn} do
443 note_activity = insert(:note_activity)
444 user = insert(:user)
445
446 data = %{
447 type: "Delete",
448 object: %{
449 id: note_activity.data["object"]["id"]
450 }
451 }
452
453 conn =
454 conn
455 |> assign(:user, user)
456 |> put_req_header("content-type", "application/activity+json")
457 |> post("/users/#{user.nickname}/outbox", data)
458
459 assert json_response(conn, 400)
460 end
461
462 test "it increases like count when receiving a like action", %{conn: conn} do
463 note_activity = insert(:note_activity)
464 user = User.get_cached_by_ap_id(note_activity.data["actor"])
465
466 data = %{
467 type: "Like",
468 object: %{
469 id: note_activity.data["object"]["id"]
470 }
471 }
472
473 conn =
474 conn
475 |> assign(:user, user)
476 |> put_req_header("content-type", "application/activity+json")
477 |> post("/users/#{user.nickname}/outbox", data)
478
479 result = json_response(conn, 201)
480 assert Activity.get_by_ap_id(result["id"])
481
482 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
483 assert object
484 assert object.data["like_count"] == 1
485 end
486 end
487
488 describe "/users/:nickname/followers" do
489 test "it returns the followers in a collection", %{conn: conn} do
490 user = insert(:user)
491 user_two = insert(:user)
492 User.follow(user, user_two)
493
494 result =
495 conn
496 |> get("/users/#{user_two.nickname}/followers")
497 |> json_response(200)
498
499 assert result["first"]["orderedItems"] == [user.ap_id]
500 end
501
502 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
503 user = insert(:user)
504 user_two = insert(:user, %{info: %{hide_followers: true}})
505 User.follow(user, user_two)
506
507 result =
508 conn
509 |> get("/users/#{user_two.nickname}/followers")
510 |> json_response(200)
511
512 assert result["first"]["orderedItems"] == []
513 assert result["totalItems"] == 0
514 end
515
516 test "it works for more than 10 users", %{conn: conn} do
517 user = insert(:user)
518
519 Enum.each(1..15, fn _ ->
520 other_user = insert(:user)
521 User.follow(other_user, user)
522 end)
523
524 result =
525 conn
526 |> get("/users/#{user.nickname}/followers")
527 |> json_response(200)
528
529 assert length(result["first"]["orderedItems"]) == 10
530 assert result["first"]["totalItems"] == 15
531 assert result["totalItems"] == 15
532
533 result =
534 conn
535 |> get("/users/#{user.nickname}/followers?page=2")
536 |> json_response(200)
537
538 assert length(result["orderedItems"]) == 5
539 assert result["totalItems"] == 15
540 end
541 end
542
543 describe "/users/:nickname/following" do
544 test "it returns the following in a collection", %{conn: conn} do
545 user = insert(:user)
546 user_two = insert(:user)
547 User.follow(user, user_two)
548
549 result =
550 conn
551 |> get("/users/#{user.nickname}/following")
552 |> json_response(200)
553
554 assert result["first"]["orderedItems"] == [user_two.ap_id]
555 end
556
557 test "it returns returns empty if the user has 'hide_follows' set", %{conn: conn} do
558 user = insert(:user, %{info: %{hide_follows: true}})
559 user_two = insert(:user)
560 User.follow(user, user_two)
561
562 result =
563 conn
564 |> get("/users/#{user.nickname}/following")
565 |> json_response(200)
566
567 assert result["first"]["orderedItems"] == []
568 assert result["totalItems"] == 0
569 end
570
571 test "it works for more than 10 users", %{conn: conn} do
572 user = insert(:user)
573
574 Enum.each(1..15, fn _ ->
575 user = User.get_cached_by_id(user.id)
576 other_user = insert(:user)
577 User.follow(user, other_user)
578 end)
579
580 result =
581 conn
582 |> get("/users/#{user.nickname}/following")
583 |> json_response(200)
584
585 assert length(result["first"]["orderedItems"]) == 10
586 assert result["first"]["totalItems"] == 15
587 assert result["totalItems"] == 15
588
589 result =
590 conn
591 |> get("/users/#{user.nickname}/following?page=2")
592 |> json_response(200)
593
594 assert length(result["orderedItems"]) == 5
595 assert result["totalItems"] == 15
596 end
597 end
598 end