1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Instances
12 alias Pleroma.Web.ActivityPub.ObjectView
13 alias Pleroma.Web.ActivityPub.UserView
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 test "with the relay active, it returns the relay user", %{conn: conn} do
24 |> get(activity_pub_path(conn, :relay))
27 assert res["id"] =~ "/relay"
30 test "with the relay disabled, it returns 404", %{conn: conn} do
31 Pleroma.Config.put([:instance, :allow_relay], false)
34 |> get(activity_pub_path(conn, :relay))
38 Pleroma.Config.put([:instance, :allow_relay], true)
42 describe "/users/:nickname" do
43 test "it returns a json representation of the user with accept application/json", %{
50 |> put_req_header("accept", "application/json")
51 |> get("/users/#{user.nickname}")
53 user = User.get_by_id(user.id)
55 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
58 test "it returns a json representation of the user with accept application/activity+json", %{
65 |> put_req_header("accept", "application/activity+json")
66 |> get("/users/#{user.nickname}")
68 user = User.get_by_id(user.id)
70 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
73 test "it returns a json representation of the user with accept application/ld+json", %{
82 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
84 |> get("/users/#{user.nickname}")
86 user = User.get_by_id(user.id)
88 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
92 describe "/object/:uuid" do
93 test "it returns a json representation of the object with accept application/json", %{
97 uuid = String.split(note.data["id"], "/") |> List.last()
101 |> put_req_header("accept", "application/json")
102 |> get("/objects/#{uuid}")
104 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
107 test "it returns a json representation of the object with accept application/activity+json",
110 uuid = String.split(note.data["id"], "/") |> List.last()
114 |> put_req_header("accept", "application/activity+json")
115 |> get("/objects/#{uuid}")
117 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
120 test "it returns a json representation of the object with accept application/ld+json", %{
124 uuid = String.split(note.data["id"], "/") |> List.last()
130 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
132 |> get("/objects/#{uuid}")
134 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
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()
143 |> put_req_header("accept", "application/activity+json")
144 |> get("/objects/#{uuid}")
146 assert json_response(conn, 404)
149 test "it returns 404 for tombstone objects", %{conn: conn} do
150 tombstone = insert(:tombstone)
151 uuid = String.split(tombstone.data["id"], "/") |> List.last()
155 |> put_req_header("accept", "application/activity+json")
156 |> get("/objects/#{uuid}")
158 assert json_response(conn, 404)
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()
169 |> put_req_header("accept", "application/activity+json")
170 |> get("/objects/#{uuid}/likes")
171 |> json_response(200)
173 assert List.first(result["first"]["orderedItems"])["id"] == like.data["id"]
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()
184 |> put_req_header("accept", "application/activity+json")
185 |> get("/activities/#{uuid}")
187 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
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()
196 |> put_req_header("accept", "application/activity+json")
197 |> get("/activities/#{uuid}")
199 assert json_response(conn, 404)
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!()
209 |> assign(:valid_signature, true)
210 |> put_req_header("content-type", "application/activity+json")
211 |> post("/inbox", data)
213 assert "ok" == json_response(conn, 200)
215 assert Activity.get_by_ap_id(data["id"])
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!()
221 sender_url = data["actor"]
222 Instances.set_consistently_unreachable(sender_url)
223 refute Instances.reachable?(sender_url)
227 |> assign(:valid_signature, true)
228 |> put_req_header("content-type", "application/activity+json")
229 |> post("/inbox", data)
231 assert "ok" == json_response(conn, 200)
232 assert Instances.reachable?(sender_url)
236 describe "/users/:nickname/inbox" do
237 test "it inserts an incoming activity into the database", %{conn: conn} do
241 File.read!("test/fixtures/mastodon-post-activity.json")
243 |> Map.put("bcc", [user.ap_id])
247 |> assign(:valid_signature, true)
248 |> put_req_header("content-type", "application/activity+json")
249 |> post("/users/#{user.nickname}/inbox", data)
251 assert "ok" == json_response(conn, 200)
253 assert Activity.get_by_ap_id(data["id"])
256 test "it rejects reads from other users", %{conn: conn} do
258 otheruser = insert(:user)
262 |> assign(:user, otheruser)
263 |> put_req_header("accept", "application/activity+json")
264 |> get("/users/#{user.nickname}/inbox")
266 assert json_response(conn, 403)
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"]))
275 |> assign(:user, user)
276 |> put_req_header("accept", "application/activity+json")
277 |> get("/users/#{user.nickname}/inbox")
279 assert response(conn, 200) =~ note_activity.data["object"]["content"]
282 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
286 File.read!("test/fixtures/mastodon-post-activity.json")
288 |> Map.put("bcc", [user.ap_id])
290 sender_host = URI.parse(data["actor"]).host
291 Instances.set_consistently_unreachable(sender_host)
292 refute Instances.reachable?(sender_host)
296 |> assign(:valid_signature, true)
297 |> put_req_header("content-type", "application/activity+json")
298 |> post("/users/#{user.nickname}/inbox", data)
300 assert "ok" == json_response(conn, 200)
301 assert Instances.reachable?(sender_host)
305 describe "/users/:nickname/outbox" do
306 test "it will not bomb when there is no activity", %{conn: conn} do
311 |> put_req_header("accept", "application/activity+json")
312 |> get("/users/#{user.nickname}/outbox")
314 result = json_response(conn, 200)
315 assert user.ap_id <> "/outbox" == result["id"]
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"])
324 |> put_req_header("accept", "application/activity+json")
325 |> get("/users/#{user.nickname}/outbox")
327 assert response(conn, 200) =~ note_activity.data["object"]["content"]
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"])
336 |> put_req_header("accept", "application/activity+json")
337 |> get("/users/#{user.nickname}/outbox")
339 assert response(conn, 200) =~ announce_activity.data["object"]
342 test "it rejects posts from other users", %{conn: conn} do
343 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
345 otheruser = insert(:user)
349 |> assign(:user, otheruser)
350 |> put_req_header("content-type", "application/activity+json")
351 |> post("/users/#{user.nickname}/outbox", data)
353 assert json_response(conn, 403)
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!()
362 |> assign(:user, user)
363 |> put_req_header("content-type", "application/activity+json")
364 |> post("/users/#{user.nickname}/outbox", data)
366 result = json_response(conn, 201)
367 assert Activity.get_by_ap_id(result["id"])
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!()
376 |> Map.put("type", "BadType")
380 |> assign(:user, user)
381 |> put_req_header("content-type", "application/activity+json")
382 |> post("/users/#{user.nickname}/outbox", data)
384 assert json_response(conn, 400)
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"])
394 id: note_activity.data["object"]["id"]
400 |> assign(:user, user)
401 |> put_req_header("content-type", "application/activity+json")
402 |> post("/users/#{user.nickname}/outbox", data)
404 result = json_response(conn, 201)
405 assert Activity.get_by_ap_id(result["id"])
407 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
409 assert object.data["type"] == "Tombstone"
412 test "it rejects delete activity of object from other actor", %{conn: conn} do
413 note_activity = insert(:note_activity)
419 id: note_activity.data["object"]["id"]
425 |> assign(:user, user)
426 |> put_req_header("content-type", "application/activity+json")
427 |> post("/users/#{user.nickname}/outbox", data)
429 assert json_response(conn, 400)
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"])
439 id: note_activity.data["object"]["id"]
445 |> assign(:user, user)
446 |> put_req_header("content-type", "application/activity+json")
447 |> post("/users/#{user.nickname}/outbox", data)
449 result = json_response(conn, 201)
450 assert Activity.get_by_ap_id(result["id"])
452 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
454 assert object.data["like_count"] == 1
458 describe "/users/:nickname/followers" do
459 test "it returns the followers in a collection", %{conn: conn} do
461 user_two = insert(:user)
462 User.follow(user, user_two)
466 |> get("/users/#{user_two.nickname}/followers")
467 |> json_response(200)
469 assert result["first"]["orderedItems"] == [user.ap_id]
472 test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
474 user_two = insert(:user, %{info: %{hide_followers: true}})
475 User.follow(user, user_two)
479 |> get("/users/#{user_two.nickname}/followers")
480 |> json_response(200)
482 assert result["first"]["orderedItems"] == []
483 assert result["totalItems"] == 0
486 test "it works for more than 10 users", %{conn: conn} do
489 Enum.each(1..15, fn _ ->
490 other_user = insert(:user)
491 User.follow(other_user, user)
496 |> get("/users/#{user.nickname}/followers")
497 |> json_response(200)
499 assert length(result["first"]["orderedItems"]) == 10
500 assert result["first"]["totalItems"] == 15
501 assert result["totalItems"] == 15
505 |> get("/users/#{user.nickname}/followers?page=2")
506 |> json_response(200)
508 assert length(result["orderedItems"]) == 5
509 assert result["totalItems"] == 15
513 describe "/users/:nickname/following" do
514 test "it returns the following in a collection", %{conn: conn} do
516 user_two = insert(:user)
517 User.follow(user, user_two)
521 |> get("/users/#{user.nickname}/following")
522 |> json_response(200)
524 assert result["first"]["orderedItems"] == [user_two.ap_id]
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)
534 |> get("/users/#{user.nickname}/following")
535 |> json_response(200)
537 assert result["first"]["orderedItems"] == []
538 assert result["totalItems"] == 0
541 test "it works for more than 10 users", %{conn: conn} do
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)
552 |> get("/users/#{user.nickname}/following")
553 |> json_response(200)
555 assert length(result["first"]["orderedItems"]) == 10
556 assert result["first"]["totalItems"] == 15
557 assert result["totalItems"] == 15
561 |> get("/users/#{user.nickname}/following?page=2")
562 |> json_response(200)
564 assert length(result["orderedItems"]) == 5
565 assert result["totalItems"] == 15