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.Relay
14 alias Pleroma.Web.ActivityPub.UserView
15 alias Pleroma.Web.ActivityPub.Utils
16 alias Pleroma.Web.CommonAPI
19 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 clear_config_all([:instance, :federating],
24 do: Pleroma.Config.put([:instance, :federating], true)
28 clear_config([:instance, :allow_relay])
30 test "with the relay active, it returns the relay user", %{conn: conn} do
33 |> get(activity_pub_path(conn, :relay))
36 assert res["id"] =~ "/relay"
39 test "with the relay disabled, it returns 404", %{conn: conn} do
40 Pleroma.Config.put([:instance, :allow_relay], false)
43 |> get(activity_pub_path(conn, :relay))
49 describe "/internal/fetch" do
50 test "it returns the internal fetch user", %{conn: conn} do
53 |> get(activity_pub_path(conn, :internal_fetch))
56 assert res["id"] =~ "/fetch"
60 describe "/users/:nickname" do
61 test "it returns a json representation of the user with accept application/json", %{
68 |> put_req_header("accept", "application/json")
69 |> get("/users/#{user.nickname}")
71 user = User.get_cached_by_id(user.id)
73 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
76 test "it returns a json representation of the user with accept application/activity+json", %{
83 |> put_req_header("accept", "application/activity+json")
84 |> get("/users/#{user.nickname}")
86 user = User.get_cached_by_id(user.id)
88 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
91 test "it returns a json representation of the user with accept application/ld+json", %{
100 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
102 |> get("/users/#{user.nickname}")
104 user = User.get_cached_by_id(user.id)
106 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
110 describe "/object/:uuid" do
111 test "it returns a json representation of the object with accept application/json", %{
115 uuid = String.split(note.data["id"], "/") |> List.last()
119 |> put_req_header("accept", "application/json")
120 |> get("/objects/#{uuid}")
122 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
125 test "it returns a json representation of the object with accept application/activity+json",
128 uuid = String.split(note.data["id"], "/") |> List.last()
132 |> put_req_header("accept", "application/activity+json")
133 |> get("/objects/#{uuid}")
135 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
138 test "it returns a json representation of the object with accept application/ld+json", %{
142 uuid = String.split(note.data["id"], "/") |> List.last()
148 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
150 |> get("/objects/#{uuid}")
152 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
155 test "it returns 404 for non-public messages", %{conn: conn} do
156 note = insert(:direct_note)
157 uuid = String.split(note.data["id"], "/") |> List.last()
161 |> put_req_header("accept", "application/activity+json")
162 |> get("/objects/#{uuid}")
164 assert json_response(conn, 404)
167 test "it returns 404 for tombstone objects", %{conn: conn} do
168 tombstone = insert(:tombstone)
169 uuid = String.split(tombstone.data["id"], "/") |> List.last()
173 |> put_req_header("accept", "application/activity+json")
174 |> get("/objects/#{uuid}")
176 assert json_response(conn, 404)
180 describe "/object/:uuid/likes" do
182 like = insert(:like_activity)
183 like_object_ap_id = Object.normalize(like).data["id"]
190 [id: like.data["id"], uuid: uuid]
193 test "it returns the like activities in a collection", %{conn: conn, id: id, uuid: uuid} do
196 |> put_req_header("accept", "application/activity+json")
197 |> get("/objects/#{uuid}/likes")
198 |> json_response(200)
200 assert List.first(result["first"]["orderedItems"])["id"] == id
201 assert result["type"] == "OrderedCollection"
202 assert result["totalItems"] == 1
203 refute result["first"]["next"]
206 test "it does not crash when page number is exceeded total pages", %{conn: conn, uuid: uuid} do
209 |> put_req_header("accept", "application/activity+json")
210 |> get("/objects/#{uuid}/likes?page=2")
211 |> json_response(200)
213 assert result["type"] == "OrderedCollectionPage"
214 assert result["totalItems"] == 1
215 refute result["next"]
216 assert Enum.empty?(result["orderedItems"])
219 test "it contains the next key when likes count is more than 10", %{conn: conn} do
220 note = insert(:note_activity)
221 insert_list(11, :like_activity, note_activity: note)
225 |> Object.normalize()
233 |> put_req_header("accept", "application/activity+json")
234 |> get("/objects/#{uuid}/likes?page=1")
235 |> json_response(200)
237 assert result["totalItems"] == 11
238 assert length(result["orderedItems"]) == 10
239 assert result["next"]
243 describe "/activities/:uuid" do
244 test "it returns a json representation of the activity", %{conn: conn} do
245 activity = insert(:note_activity)
246 uuid = String.split(activity.data["id"], "/") |> List.last()
250 |> put_req_header("accept", "application/activity+json")
251 |> get("/activities/#{uuid}")
253 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
256 test "it returns 404 for non-public activities", %{conn: conn} do
257 activity = insert(:direct_note_activity)
258 uuid = String.split(activity.data["id"], "/") |> List.last()
262 |> put_req_header("accept", "application/activity+json")
263 |> get("/activities/#{uuid}")
265 assert json_response(conn, 404)
270 test "it inserts an incoming activity into the database", %{conn: conn} do
271 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
275 |> assign(:valid_signature, true)
276 |> put_req_header("content-type", "application/activity+json")
277 |> post("/inbox", data)
279 assert "ok" == json_response(conn, 200)
281 assert Activity.get_by_ap_id(data["id"])
284 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
285 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!()
287 sender_url = data["actor"]
288 Instances.set_consistently_unreachable(sender_url)
289 refute Instances.reachable?(sender_url)
293 |> assign(:valid_signature, true)
294 |> put_req_header("content-type", "application/activity+json")
295 |> post("/inbox", data)
297 assert "ok" == json_response(conn, 200)
298 assert Instances.reachable?(sender_url)
302 describe "/users/:nickname/inbox" do
305 File.read!("test/fixtures/mastodon-post-activity.json")
311 test "it inserts an incoming activity into the database", %{conn: conn, data: data} do
313 data = Map.put(data, "bcc", [user.ap_id])
317 |> assign(:valid_signature, true)
318 |> put_req_header("content-type", "application/activity+json")
319 |> post("/users/#{user.nickname}/inbox", data)
321 assert "ok" == json_response(conn, 200)
323 assert Activity.get_by_ap_id(data["id"])
326 test "it accepts messages from actors that are followed by the user", %{
330 recipient = insert(:user)
331 actor = insert(:user, %{ap_id: "http://mastodon.example.org/users/actor"})
333 {:ok, recipient} = User.follow(recipient, actor)
337 |> Map.put("attributedTo", actor.ap_id)
341 |> Map.put("actor", actor.ap_id)
342 |> Map.put("object", object)
346 |> assign(:valid_signature, true)
347 |> put_req_header("content-type", "application/activity+json")
348 |> post("/users/#{recipient.nickname}/inbox", data)
350 assert "ok" == json_response(conn, 200)
352 assert Activity.get_by_ap_id(data["id"])
355 test "it rejects reads from other users", %{conn: conn} do
357 otheruser = insert(:user)
361 |> assign(:user, otheruser)
362 |> put_req_header("accept", "application/activity+json")
363 |> get("/users/#{user.nickname}/inbox")
365 assert json_response(conn, 403)
368 test "it returns a note activity in a collection", %{conn: conn} do
369 note_activity = insert(:direct_note_activity)
370 note_object = Object.normalize(note_activity)
371 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
375 |> assign(:user, user)
376 |> put_req_header("accept", "application/activity+json")
377 |> get("/users/#{user.nickname}/inbox")
379 assert response(conn, 200) =~ note_object.data["content"]
382 test "it clears `unreachable` federation status of the sender", %{conn: conn, data: data} do
384 data = Map.put(data, "bcc", [user.ap_id])
386 sender_host = URI.parse(data["actor"]).host
387 Instances.set_consistently_unreachable(sender_host)
388 refute Instances.reachable?(sender_host)
392 |> assign(:valid_signature, true)
393 |> put_req_header("content-type", "application/activity+json")
394 |> post("/users/#{user.nickname}/inbox", data)
396 assert "ok" == json_response(conn, 200)
397 assert Instances.reachable?(sender_host)
400 test "it removes all follower collections but actor's", %{conn: conn} do
401 [actor, recipient] = insert_pair(:user)
404 File.read!("test/fixtures/activitypub-client-post-activity.json")
407 object = Map.put(data["object"], "attributedTo", actor.ap_id)
411 |> Map.put("id", Utils.generate_object_id())
412 |> Map.put("actor", actor.ap_id)
413 |> Map.put("object", object)
415 recipient.follower_address,
416 actor.follower_address
420 recipient.follower_address,
421 "https://www.w3.org/ns/activitystreams#Public"
425 |> assign(:valid_signature, true)
426 |> put_req_header("content-type", "application/activity+json")
427 |> post("/users/#{recipient.nickname}/inbox", data)
428 |> json_response(200)
430 activity = Activity.get_by_ap_id(data["id"])
433 assert actor.follower_address in activity.recipients
434 assert actor.follower_address in activity.data["cc"]
436 refute recipient.follower_address in activity.recipients
437 refute recipient.follower_address in activity.data["cc"]
438 refute recipient.follower_address in activity.data["to"]
442 describe "/users/:nickname/outbox" do
443 test "it will not bomb when there is no activity", %{conn: conn} do
448 |> put_req_header("accept", "application/activity+json")
449 |> get("/users/#{user.nickname}/outbox")
451 result = json_response(conn, 200)
452 assert user.ap_id <> "/outbox" == result["id"]
455 test "it returns a note activity in a collection", %{conn: conn} do
456 note_activity = insert(:note_activity)
457 note_object = Object.normalize(note_activity)
458 user = User.get_cached_by_ap_id(note_activity.data["actor"])
462 |> put_req_header("accept", "application/activity+json")
463 |> get("/users/#{user.nickname}/outbox")
465 assert response(conn, 200) =~ note_object.data["content"]
468 test "it returns an announce activity in a collection", %{conn: conn} do
469 announce_activity = insert(:announce_activity)
470 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
474 |> put_req_header("accept", "application/activity+json")
475 |> get("/users/#{user.nickname}/outbox")
477 assert response(conn, 200) =~ announce_activity.data["object"]
480 test "it rejects posts from other users", %{conn: conn} do
481 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
483 otheruser = insert(:user)
487 |> assign(:user, otheruser)
488 |> put_req_header("content-type", "application/activity+json")
489 |> post("/users/#{user.nickname}/outbox", data)
491 assert json_response(conn, 403)
494 test "it inserts an incoming create activity into the database", %{conn: conn} do
495 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
500 |> assign(:user, user)
501 |> put_req_header("content-type", "application/activity+json")
502 |> post("/users/#{user.nickname}/outbox", data)
504 result = json_response(conn, 201)
505 assert Activity.get_by_ap_id(result["id"])
508 test "it rejects an incoming activity with bogus type", %{conn: conn} do
509 data = File.read!("test/fixtures/activitypub-client-post-activity.json") |> Poison.decode!()
514 |> Map.put("type", "BadType")
518 |> assign(:user, user)
519 |> put_req_header("content-type", "application/activity+json")
520 |> post("/users/#{user.nickname}/outbox", data)
522 assert json_response(conn, 400)
525 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
526 note_activity = insert(:note_activity)
527 note_object = Object.normalize(note_activity)
528 user = User.get_cached_by_ap_id(note_activity.data["actor"])
533 id: note_object.data["id"]
539 |> assign(:user, user)
540 |> put_req_header("content-type", "application/activity+json")
541 |> post("/users/#{user.nickname}/outbox", data)
543 result = json_response(conn, 201)
544 assert Activity.get_by_ap_id(result["id"])
546 assert object = Object.get_by_ap_id(note_object.data["id"])
547 assert object.data["type"] == "Tombstone"
550 test "it rejects delete activity of object from other actor", %{conn: conn} do
551 note_activity = insert(:note_activity)
552 note_object = Object.normalize(note_activity)
558 id: note_object.data["id"]
564 |> assign(:user, user)
565 |> put_req_header("content-type", "application/activity+json")
566 |> post("/users/#{user.nickname}/outbox", data)
568 assert json_response(conn, 400)
571 test "it increases like count when receiving a like action", %{conn: conn} do
572 note_activity = insert(:note_activity)
573 note_object = Object.normalize(note_activity)
574 user = User.get_cached_by_ap_id(note_activity.data["actor"])
579 id: note_object.data["id"]
585 |> assign(:user, user)
586 |> put_req_header("content-type", "application/activity+json")
587 |> post("/users/#{user.nickname}/outbox", data)
589 result = json_response(conn, 201)
590 assert Activity.get_by_ap_id(result["id"])
592 assert object = Object.get_by_ap_id(note_object.data["id"])
593 assert object.data["like_count"] == 1
597 describe "/relay/followers" do
598 test "it returns relay followers", %{conn: conn} do
599 relay_actor = Relay.get_actor()
601 User.follow(user, relay_actor)
605 |> assign(:relay, true)
606 |> get("/relay/followers")
607 |> json_response(200)
609 assert result["first"]["orderedItems"] == [user.ap_id]
613 describe "/relay/following" do
614 test "it returns relay following", %{conn: conn} do
617 |> assign(:relay, true)
618 |> get("/relay/following")
619 |> json_response(200)
621 assert result["first"]["orderedItems"] == []
625 describe "/users/:nickname/followers" do
626 test "it returns the followers in a collection", %{conn: conn} do
628 user_two = insert(:user)
629 User.follow(user, user_two)
633 |> get("/users/#{user_two.nickname}/followers")
634 |> json_response(200)
636 assert result["first"]["orderedItems"] == [user.ap_id]
639 test "it returns returns a uri if the user has 'hide_followers' set", %{conn: conn} do
641 user_two = insert(:user, %{info: %{hide_followers: true}})
642 User.follow(user, user_two)
646 |> get("/users/#{user_two.nickname}/followers")
647 |> json_response(200)
649 assert is_binary(result["first"])
652 test "it returns a 403 error on pages, if the user has 'hide_followers' set and the request is not authenticated",
654 user = insert(:user, %{info: %{hide_followers: true}})
658 |> get("/users/#{user.nickname}/followers?page=1")
660 assert result.status == 403
661 assert result.resp_body == ""
664 test "it renders the page, if the user has 'hide_followers' set and the request is authenticated with the same user",
666 user = insert(:user, %{info: %{hide_followers: true}})
667 other_user = insert(:user)
668 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
672 |> assign(:user, user)
673 |> get("/users/#{user.nickname}/followers?page=1")
674 |> json_response(200)
676 assert result["totalItems"] == 1
677 assert result["orderedItems"] == [other_user.ap_id]
680 test "it works for more than 10 users", %{conn: conn} do
683 Enum.each(1..15, fn _ ->
684 other_user = insert(:user)
685 User.follow(other_user, user)
690 |> get("/users/#{user.nickname}/followers")
691 |> json_response(200)
693 assert length(result["first"]["orderedItems"]) == 10
694 assert result["first"]["totalItems"] == 15
695 assert result["totalItems"] == 15
699 |> get("/users/#{user.nickname}/followers?page=2")
700 |> json_response(200)
702 assert length(result["orderedItems"]) == 5
703 assert result["totalItems"] == 15
707 describe "/users/:nickname/following" do
708 test "it returns the following in a collection", %{conn: conn} do
710 user_two = insert(:user)
711 User.follow(user, user_two)
715 |> get("/users/#{user.nickname}/following")
716 |> json_response(200)
718 assert result["first"]["orderedItems"] == [user_two.ap_id]
721 test "it returns a uri if the user has 'hide_follows' set", %{conn: conn} do
722 user = insert(:user, %{info: %{hide_follows: true}})
723 user_two = insert(:user)
724 User.follow(user, user_two)
728 |> get("/users/#{user.nickname}/following")
729 |> json_response(200)
731 assert is_binary(result["first"])
734 test "it returns a 403 error on pages, if the user has 'hide_follows' set and the request is not authenticated",
736 user = insert(:user, %{info: %{hide_follows: true}})
740 |> get("/users/#{user.nickname}/following?page=1")
742 assert result.status == 403
743 assert result.resp_body == ""
746 test "it renders the page, if the user has 'hide_follows' set and the request is authenticated with the same user",
748 user = insert(:user, %{info: %{hide_follows: true}})
749 other_user = insert(:user)
750 {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
754 |> assign(:user, user)
755 |> get("/users/#{user.nickname}/following?page=1")
756 |> json_response(200)
758 assert result["totalItems"] == 1
759 assert result["orderedItems"] == [other_user.ap_id]
762 test "it works for more than 10 users", %{conn: conn} do
765 Enum.each(1..15, fn _ ->
766 user = User.get_cached_by_id(user.id)
767 other_user = insert(:user)
768 User.follow(user, other_user)
773 |> get("/users/#{user.nickname}/following")
774 |> json_response(200)
776 assert length(result["first"]["orderedItems"]) == 10
777 assert result["first"]["totalItems"] == 15
778 assert result["totalItems"] == 15
782 |> get("/users/#{user.nickname}/following?page=2")
783 |> json_response(200)
785 assert length(result["orderedItems"]) == 5
786 assert result["totalItems"] == 15