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