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