1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
7 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Delivery
11 alias Pleroma.Instances
13 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.ObjectView
17 alias Pleroma.Web.ActivityPub.Relay
18 alias Pleroma.Web.ActivityPub.UserView
19 alias Pleroma.Web.ActivityPub.Utils
20 alias Pleroma.Web.CommonAPI
21 alias Pleroma.Web.Endpoint
22 alias Pleroma.Workers.ReceiverWorker
24 import Pleroma.Factory
26 require Pleroma.Constants
29 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
33 setup do: clear_config([:instance, :federating], true)
36 setup do: clear_config([:instance, :allow_relay])
38 test "with the relay active, it returns the relay user", %{conn: conn} do
41 |> get(activity_pub_path(conn, :relay))
44 assert res["id"] =~ "/relay"
47 test "with the relay disabled, it returns 404", %{conn: conn} do
48 clear_config([:instance, :allow_relay], false)
51 |> get(activity_pub_path(conn, :relay))
55 test "on non-federating instance, it returns 404", %{conn: conn} do
56 clear_config([:instance, :federating], false)
60 |> assign(:user, user)
61 |> get(activity_pub_path(conn, :relay))
66 describe "/internal/fetch" do
67 test "it returns the internal fetch user", %{conn: conn} do
70 |> get(activity_pub_path(conn, :internal_fetch))
73 assert res["id"] =~ "/fetch"
76 test "on non-federating instance, it returns 404", %{conn: conn} do
77 clear_config([:instance, :federating], false)
81 |> assign(:user, user)
82 |> get(activity_pub_path(conn, :internal_fetch))
87 describe "/users/:nickname" do
88 test "it returns a json representation of the user with accept application/json", %{
95 |> put_req_header("accept", "application/json")
96 |> get("/users/#{user.nickname}")
98 user = User.get_cached_by_id(user.id)
100 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
103 test "it returns a json representation of the user with accept application/activity+json", %{
110 |> put_req_header("accept", "application/activity+json")
111 |> get("/users/#{user.nickname}")
113 user = User.get_cached_by_id(user.id)
115 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
118 test "it returns a json representation of the user with accept application/ld+json", %{
127 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
129 |> get("/users/#{user.nickname}")
131 user = User.get_cached_by_id(user.id)
133 assert json_response(conn, 200) == UserView.render("user.json", %{user: user})
136 test "it returns 404 for remote users", %{
139 user = insert(:user, local: false, nickname: "remoteuser@example.com")
143 |> put_req_header("accept", "application/json")
144 |> get("/users/#{user.nickname}.json")
146 assert json_response(conn, 404)
149 test "it returns error when user is not found", %{conn: conn} do
152 |> put_req_header("accept", "application/json")
153 |> get("/users/jimm")
154 |> json_response(404)
156 assert response == "Not found"
160 describe "mastodon compatibility routes" do
161 test "it returns a json representation of the object with accept application/json", %{
168 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
169 "actor" => Endpoint.url() <> "/users/raymoo",
170 "to" => [Pleroma.Constants.as_public()]
176 |> put_req_header("accept", "application/json")
177 |> get("/users/raymoo/statuses/999999999")
179 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: object})
182 test "it returns a json representation of the activity with accept application/json", %{
189 "id" => Endpoint.url() <> "/users/raymoo/statuses/999999999",
190 "actor" => Endpoint.url() <> "/users/raymoo",
191 "to" => [Pleroma.Constants.as_public()]
197 "id" => object.data["id"] <> "/activity",
199 "object" => object.data["id"],
200 "actor" => object.data["actor"],
201 "to" => object.data["to"]
203 |> ActivityPub.persist(local: true)
207 |> put_req_header("accept", "application/json")
208 |> get("/users/raymoo/statuses/999999999/activity")
210 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
214 describe "/objects/:uuid" do
215 test "it doesn't return a local-only object", %{conn: conn} do
217 {:ok, post} = CommonAPI.post(user, %{status: "test", visibility: "local"})
219 assert Pleroma.Web.ActivityPub.Visibility.is_local_public?(post)
221 object = Object.normalize(post, fetch: false)
222 uuid = String.split(object.data["id"], "/") |> List.last()
226 |> put_req_header("accept", "application/json")
227 |> get("/objects/#{uuid}")
229 assert json_response(conn, 404)
232 test "returns local-only objects when authenticated", %{conn: conn} do
234 {:ok, post} = CommonAPI.post(user, %{status: "test", visibility: "local"})
236 assert Pleroma.Web.ActivityPub.Visibility.is_local_public?(post)
238 object = Object.normalize(post, fetch: false)
239 uuid = String.split(object.data["id"], "/") |> List.last()
243 |> assign(:user, user)
244 |> put_req_header("accept", "application/activity+json")
245 |> get("/objects/#{uuid}")
247 assert json_response(response, 200) == ObjectView.render("object.json", %{object: object})
250 test "it returns a json representation of the object with accept application/json", %{
254 uuid = String.split(note.data["id"], "/") |> List.last()
258 |> put_req_header("accept", "application/json")
259 |> get("/objects/#{uuid}")
261 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
264 test "it returns a json representation of the object with accept application/activity+json",
267 uuid = String.split(note.data["id"], "/") |> List.last()
271 |> put_req_header("accept", "application/activity+json")
272 |> get("/objects/#{uuid}")
274 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
277 test "it returns a json representation of the object with accept application/ld+json", %{
281 uuid = String.split(note.data["id"], "/") |> List.last()
287 "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
289 |> get("/objects/#{uuid}")
291 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note})
294 test "it returns 404 for non-public messages", %{conn: conn} do
295 note = insert(:direct_note)
296 uuid = String.split(note.data["id"], "/") |> List.last()
300 |> put_req_header("accept", "application/activity+json")
301 |> get("/objects/#{uuid}")
303 assert json_response(conn, 404)
306 test "returns visible non-public messages when authenticated", %{conn: conn} do
307 note = insert(:direct_note)
308 uuid = String.split(note.data["id"], "/") |> List.last()
309 user = User.get_by_ap_id(note.data["actor"])
310 marisa = insert(:user)
313 |> assign(:user, marisa)
314 |> put_req_header("accept", "application/activity+json")
315 |> get("/objects/#{uuid}")
316 |> json_response(404)
320 |> assign(:user, user)
321 |> put_req_header("accept", "application/activity+json")
322 |> get("/objects/#{uuid}")
323 |> json_response(200)
325 assert response == ObjectView.render("object.json", %{object: note})
328 test "it returns 404 for tombstone objects", %{conn: conn} do
329 tombstone = insert(:tombstone)
330 uuid = String.split(tombstone.data["id"], "/") |> List.last()
334 |> put_req_header("accept", "application/activity+json")
335 |> get("/objects/#{uuid}")
337 assert json_response(conn, 404)
340 test "it caches a response", %{conn: conn} do
342 uuid = String.split(note.data["id"], "/") |> List.last()
346 |> put_req_header("accept", "application/activity+json")
347 |> get("/objects/#{uuid}")
349 assert json_response(conn1, :ok)
350 assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
354 |> put_req_header("accept", "application/activity+json")
355 |> get("/objects/#{uuid}")
357 assert json_response(conn1, :ok) == json_response(conn2, :ok)
358 assert Enum.any?(conn2.resp_headers, &(&1 == {"x-cache", "HIT from Pleroma"}))
361 test "cached purged after object deletion", %{conn: conn} do
363 uuid = String.split(note.data["id"], "/") |> List.last()
367 |> put_req_header("accept", "application/activity+json")
368 |> get("/objects/#{uuid}")
370 assert json_response(conn1, :ok)
371 assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
377 |> put_req_header("accept", "application/activity+json")
378 |> get("/objects/#{uuid}")
380 assert "Not found" == json_response(conn2, :not_found)
384 describe "/activities/:uuid" do
385 test "it doesn't return a local-only activity", %{conn: conn} do
387 {:ok, post} = CommonAPI.post(user, %{status: "test", visibility: "local"})
389 assert Pleroma.Web.ActivityPub.Visibility.is_local_public?(post)
391 uuid = String.split(post.data["id"], "/") |> List.last()
395 |> put_req_header("accept", "application/json")
396 |> get("/activities/#{uuid}")
398 assert json_response(conn, 404)
401 test "returns local-only activities when authenticated", %{conn: conn} do
403 {:ok, post} = CommonAPI.post(user, %{status: "test", visibility: "local"})
405 assert Pleroma.Web.ActivityPub.Visibility.is_local_public?(post)
407 uuid = String.split(post.data["id"], "/") |> List.last()
411 |> assign(:user, user)
412 |> put_req_header("accept", "application/activity+json")
413 |> get("/activities/#{uuid}")
415 assert json_response(response, 200) == ObjectView.render("object.json", %{object: post})
418 test "it returns a json representation of the activity", %{conn: conn} do
419 activity = insert(:note_activity)
420 uuid = String.split(activity.data["id"], "/") |> List.last()
424 |> put_req_header("accept", "application/activity+json")
425 |> get("/activities/#{uuid}")
427 assert json_response(conn, 200) == ObjectView.render("object.json", %{object: activity})
430 test "it returns 404 for non-public activities", %{conn: conn} do
431 activity = insert(:direct_note_activity)
432 uuid = String.split(activity.data["id"], "/") |> List.last()
436 |> put_req_header("accept", "application/activity+json")
437 |> get("/activities/#{uuid}")
439 assert json_response(conn, 404)
442 test "returns visible non-public messages when authenticated", %{conn: conn} do
443 note = insert(:direct_note_activity)
444 uuid = String.split(note.data["id"], "/") |> List.last()
445 user = User.get_by_ap_id(note.data["actor"])
446 marisa = insert(:user)
449 |> assign(:user, marisa)
450 |> put_req_header("accept", "application/activity+json")
451 |> get("/activities/#{uuid}")
452 |> json_response(404)
456 |> assign(:user, user)
457 |> put_req_header("accept", "application/activity+json")
458 |> get("/activities/#{uuid}")
459 |> json_response(200)
461 assert response == ObjectView.render("object.json", %{object: note})
464 test "it caches a response", %{conn: conn} do
465 activity = insert(:note_activity)
466 uuid = String.split(activity.data["id"], "/") |> List.last()
470 |> put_req_header("accept", "application/activity+json")
471 |> get("/activities/#{uuid}")
473 assert json_response(conn1, :ok)
474 assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
478 |> put_req_header("accept", "application/activity+json")
479 |> get("/activities/#{uuid}")
481 assert json_response(conn1, :ok) == json_response(conn2, :ok)
482 assert Enum.any?(conn2.resp_headers, &(&1 == {"x-cache", "HIT from Pleroma"}))
485 test "cached purged after activity deletion", %{conn: conn} do
487 {:ok, activity} = CommonAPI.post(user, %{status: "cofe"})
489 uuid = String.split(activity.data["id"], "/") |> List.last()
493 |> put_req_header("accept", "application/activity+json")
494 |> get("/activities/#{uuid}")
496 assert json_response(conn1, :ok)
497 assert Enum.any?(conn1.resp_headers, &(&1 == {"x-cache", "MISS from Pleroma"}))
499 Activity.delete_all_by_object_ap_id(activity.object.data["id"])
503 |> put_req_header("accept", "application/activity+json")
504 |> get("/activities/#{uuid}")
506 assert "Not found" == json_response(conn2, :not_found)
511 test "it inserts an incoming activity into the database", %{conn: conn} do
512 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
516 |> assign(:valid_signature, true)
517 |> put_req_header("content-type", "application/activity+json")
518 |> post("/inbox", data)
520 assert "ok" == json_response(conn, 200)
522 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
523 assert Activity.get_by_ap_id(data["id"])
526 @tag capture_log: true
527 test "it inserts an incoming activity into the database" <>
528 "even if we can't fetch the user but have it in our db",
532 ap_id: "https://mastodon.example.org/users/raymoo",
535 last_refreshed_at: nil
539 File.read!("test/fixtures/mastodon-post-activity.json")
541 |> Map.put("actor", user.ap_id)
542 |> put_in(["object", "attributedTo"], user.ap_id)
546 |> assign(:valid_signature, true)
547 |> put_req_header("content-type", "application/activity+json")
548 |> post("/inbox", data)
550 assert "ok" == json_response(conn, 200)
552 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
553 assert Activity.get_by_ap_id(data["id"])
556 test "it clears `unreachable` federation status of the sender", %{conn: conn} do
557 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
559 sender_url = data["actor"]
560 Instances.set_consistently_unreachable(sender_url)
561 refute Instances.reachable?(sender_url)
565 |> assign(:valid_signature, true)
566 |> put_req_header("content-type", "application/activity+json")
567 |> post("/inbox", data)
569 assert "ok" == json_response(conn, 200)
570 assert Instances.reachable?(sender_url)
573 test "accept follow activity", %{conn: conn} do
574 clear_config([:instance, :federating], true)
575 relay = Relay.get_actor()
577 assert {:ok, %Activity{} = activity} = Relay.follow("https://relay.mastodon.host/actor")
579 followed_relay = Pleroma.User.get_by_ap_id("https://relay.mastodon.host/actor")
580 relay = refresh_record(relay)
583 File.read!("test/fixtures/relay/accept-follow.json")
584 |> String.replace("{{ap_id}}", relay.ap_id)
585 |> String.replace("{{activity_id}}", activity.data["id"])
589 |> assign(:valid_signature, true)
590 |> put_req_header("content-type", "application/activity+json")
591 |> post("/inbox", accept)
592 |> json_response(200)
594 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
596 assert Pleroma.FollowingRelationship.following?(
601 Mix.shell(Mix.Shell.Process)
604 Mix.shell(Mix.Shell.IO)
607 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
608 assert_receive {:mix_shell, :info, ["https://relay.mastodon.host/actor"]}
611 @tag capture_log: true
612 test "without valid signature, " <>
613 "it only accepts Create activities and requires enabled federation",
615 data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
616 non_create_data = File.read!("test/fixtures/mastodon-announce.json") |> Jason.decode!()
618 conn = put_req_header(conn, "content-type", "application/activity+json")
620 clear_config([:instance, :federating], false)
623 |> post("/inbox", data)
624 |> json_response(403)
627 |> post("/inbox", non_create_data)
628 |> json_response(403)
630 clear_config([:instance, :federating], true)
632 ret_conn = post(conn, "/inbox", data)
633 assert "ok" == json_response(ret_conn, 200)
636 |> post("/inbox", non_create_data)
637 |> json_response(400)
641 describe "/users/:nickname/inbox" do
644 File.read!("test/fixtures/mastodon-post-activity.json")
650 test "it inserts an incoming activity into the database", %{conn: conn, data: data} do
655 |> Map.put("bcc", [user.ap_id])
656 |> Kernel.put_in(["object", "bcc"], [user.ap_id])
660 |> assign(:valid_signature, true)
661 |> put_req_header("content-type", "application/activity+json")
662 |> post("/users/#{user.nickname}/inbox", data)
664 assert "ok" == json_response(conn, 200)
665 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
666 assert Activity.get_by_ap_id(data["id"])
669 test "it accepts messages with to as string instead of array", %{conn: conn, data: data} do
674 |> Map.put("to", user.ap_id)
676 |> Kernel.put_in(["object", "to"], user.ap_id)
677 |> Kernel.put_in(["object", "cc"], [])
681 |> assign(:valid_signature, true)
682 |> put_req_header("content-type", "application/activity+json")
683 |> post("/users/#{user.nickname}/inbox", data)
685 assert "ok" == json_response(conn, 200)
686 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
687 assert Activity.get_by_ap_id(data["id"])
690 test "it accepts messages with cc as string instead of array", %{conn: conn, data: data} do
696 |> Map.put("cc", user.ap_id)
697 |> Kernel.put_in(["object", "to"], [])
698 |> Kernel.put_in(["object", "cc"], user.ap_id)
702 |> assign(:valid_signature, true)
703 |> put_req_header("content-type", "application/activity+json")
704 |> post("/users/#{user.nickname}/inbox", data)
706 assert "ok" == json_response(conn, 200)
707 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
708 %Activity{} = activity = Activity.get_by_ap_id(data["id"])
709 assert user.ap_id in activity.recipients
712 test "it accepts messages with bcc as string instead of array", %{conn: conn, data: data} do
719 |> Map.put("bcc", user.ap_id)
720 |> Kernel.put_in(["object", "to"], [])
721 |> Kernel.put_in(["object", "cc"], [])
722 |> Kernel.put_in(["object", "bcc"], user.ap_id)
726 |> assign(:valid_signature, true)
727 |> put_req_header("content-type", "application/activity+json")
728 |> post("/users/#{user.nickname}/inbox", data)
730 assert "ok" == json_response(conn, 200)
731 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
732 assert Activity.get_by_ap_id(data["id"])
735 test "it accepts announces with to as string instead of array", %{conn: conn} do
738 {:ok, post} = CommonAPI.post(user, %{status: "hey"})
739 announcer = insert(:user, local: false)
742 "@context" => "https://www.w3.org/ns/activitystreams",
743 "actor" => announcer.ap_id,
744 "id" => "#{announcer.ap_id}/statuses/19512778738411822/activity",
745 "object" => post.data["object"],
746 "to" => "https://www.w3.org/ns/activitystreams#Public",
747 "cc" => [user.ap_id],
753 |> assign(:valid_signature, true)
754 |> put_req_header("content-type", "application/activity+json")
755 |> post("/users/#{user.nickname}/inbox", data)
757 assert "ok" == json_response(conn, 200)
758 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
759 %Activity{} = activity = Activity.get_by_ap_id(data["id"])
760 assert "https://www.w3.org/ns/activitystreams#Public" in activity.recipients
763 test "it accepts messages from actors that are followed by the user", %{
767 recipient = insert(:user)
768 actor = insert(:user, %{ap_id: "http://mastodon.example.org/users/actor"})
770 {:ok, recipient, actor} = User.follow(recipient, actor)
774 |> Map.put("attributedTo", actor.ap_id)
778 |> Map.put("actor", actor.ap_id)
779 |> Map.put("object", object)
783 |> assign(:valid_signature, true)
784 |> put_req_header("content-type", "application/activity+json")
785 |> post("/users/#{recipient.nickname}/inbox", data)
787 assert "ok" == json_response(conn, 200)
788 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
789 assert Activity.get_by_ap_id(data["id"])
792 test "it rejects reads from other users", %{conn: conn} do
794 other_user = insert(:user)
798 |> assign(:user, other_user)
799 |> put_req_header("accept", "application/activity+json")
800 |> get("/users/#{user.nickname}/inbox")
802 assert json_response(conn, 403)
805 test "it returns a note activity in a collection", %{conn: conn} do
806 note_activity = insert(:direct_note_activity)
807 note_object = Object.normalize(note_activity, fetch: false)
808 user = User.get_cached_by_ap_id(hd(note_activity.data["to"]))
812 |> assign(:user, user)
813 |> put_req_header("accept", "application/activity+json")
814 |> get("/users/#{user.nickname}/inbox?page=true")
816 assert response(conn, 200) =~ note_object.data["content"]
819 test "it clears `unreachable` federation status of the sender", %{conn: conn, data: data} do
821 data = Map.put(data, "bcc", [user.ap_id])
823 sender_host = URI.parse(data["actor"]).host
824 Instances.set_consistently_unreachable(sender_host)
825 refute Instances.reachable?(sender_host)
829 |> assign(:valid_signature, true)
830 |> put_req_header("content-type", "application/activity+json")
831 |> post("/users/#{user.nickname}/inbox", data)
833 assert "ok" == json_response(conn, 200)
834 assert Instances.reachable?(sender_host)
837 @tag capture_log: true
838 test "it removes all follower collections but actor's", %{conn: conn} do
839 [actor, recipient] = insert_pair(:user)
843 recipient.follower_address,
844 "https://www.w3.org/ns/activitystreams#Public"
847 cc = [recipient.follower_address, actor.follower_address]
850 "@context" => ["https://www.w3.org/ns/activitystreams"],
852 "id" => Utils.generate_activity_id(),
855 "actor" => actor.ap_id,
860 "content" => "It's a note",
861 "attributedTo" => actor.ap_id,
862 "id" => Utils.generate_object_id()
867 |> assign(:valid_signature, true)
868 |> put_req_header("content-type", "application/activity+json")
869 |> post("/users/#{recipient.nickname}/inbox", data)
870 |> json_response(200)
872 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
874 assert activity = Activity.get_by_ap_id(data["id"])
877 assert actor.follower_address in activity.recipients
878 assert actor.follower_address in activity.data["cc"]
880 refute recipient.follower_address in activity.recipients
881 refute recipient.follower_address in activity.data["cc"]
882 refute recipient.follower_address in activity.data["to"]
885 test "it requires authentication", %{conn: conn} do
887 conn = put_req_header(conn, "accept", "application/activity+json")
889 ret_conn = get(conn, "/users/#{user.nickname}/inbox")
890 assert json_response(ret_conn, 403)
894 |> assign(:user, user)
895 |> get("/users/#{user.nickname}/inbox")
897 assert json_response(ret_conn, 200)
900 @tag capture_log: true
901 test "forwarded report", %{conn: conn} do
902 admin = insert(:user, is_admin: true)
903 actor = insert(:user, local: false)
904 remote_domain = URI.parse(actor.ap_id).host
905 reported_user = insert(:user)
907 note = insert(:note_activity, user: reported_user)
911 "https://www.w3.org/ns/activitystreams",
912 "https://#{remote_domain}/schemas/litepub-0.1.jsonld",
917 "actor" => actor.ap_id,
922 "context" => "context",
923 "id" => "http://#{remote_domain}/activities/02be56cf-35e3-46b4-b2c6-47ae08dfee9e",
924 "nickname" => reported_user.nickname,
929 "actor_type" => "Person",
930 "approval_pending" => false,
932 "confirmation_pending" => false,
933 "deactivated" => false,
934 "display_name" => "test user",
935 "id" => reported_user.id,
937 "nickname" => reported_user.nickname,
938 "registration_reason" => nil,
944 "url" => reported_user.ap_id
947 "id" => note.data["id"],
948 "published" => note.data["published"],
952 "published" => note.data["published"],
959 |> assign(:valid_signature, true)
960 |> put_req_header("content-type", "application/activity+json")
961 |> post("/users/#{reported_user.nickname}/inbox", data)
962 |> json_response(200)
964 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
966 assert Pleroma.Repo.aggregate(Activity, :count, :id) == 2
968 ObanHelpers.perform_all()
970 Swoosh.TestAssertions.assert_email_sent(
971 to: {admin.name, admin.email},
972 html_body: ~r/Reported Account:/i
976 @tag capture_log: true
977 test "forwarded report from mastodon", %{conn: conn} do
978 admin = insert(:user, is_admin: true)
979 actor = insert(:user, local: false)
980 remote_domain = URI.parse(actor.ap_id).host
981 remote_actor = "https://#{remote_domain}/actor"
982 [reported_user, another] = insert_list(2, :user)
984 note = insert(:note_activity, user: reported_user)
986 Pleroma.Web.CommonAPI.favorite(another, note.id)
989 "test/fixtures/mastodon/application_actor.json"
991 |> String.replace("{{DOMAIN}}", remote_domain)
993 Tesla.Mock.mock(fn %{url: ^remote_actor} ->
996 body: mock_json_body,
997 headers: [{"content-type", "application/activity+json"}]
1002 "@context" => "https://www.w3.org/ns/activitystreams",
1003 "actor" => remote_actor,
1004 "content" => "test report",
1005 "id" => "https://#{remote_domain}/e3b12fd1-948c-446e-b93b-a5e67edbe1d8",
1007 reported_user.ap_id,
1014 |> assign(:valid_signature, true)
1015 |> put_req_header("content-type", "application/activity+json")
1016 |> post("/users/#{reported_user.nickname}/inbox", data)
1017 |> json_response(200)
1019 ObanHelpers.perform(all_enqueued(worker: ReceiverWorker))
1021 flag_activity = "Flag" |> Pleroma.Activity.Queries.by_type() |> Pleroma.Repo.one()
1022 reported_user_ap_id = reported_user.ap_id
1024 [^reported_user_ap_id, flag_data] = flag_activity.data["object"]
1026 Enum.each(~w(actor content id published type), &Map.has_key?(flag_data, &1))
1027 ObanHelpers.perform_all()
1029 Swoosh.TestAssertions.assert_email_sent(
1030 to: {admin.name, admin.email},
1031 html_body: ~r/#{note.data["object"]}/i
1036 describe "GET /users/:nickname/outbox" do
1037 test "it paginates correctly", %{conn: conn} do
1038 user = insert(:user)
1039 conn = assign(conn, :user, user)
1040 outbox_endpoint = user.ap_id <> "/outbox"
1044 {:ok, activity} = CommonAPI.post(user, %{status: "post #{i}"})
1050 |> put_req_header("accept", "application/activity+json")
1051 |> get(outbox_endpoint <> "?page=true")
1052 |> json_response(200)
1054 result_ids = Enum.map(result["orderedItems"], fn x -> x["id"] end)
1055 assert length(result["orderedItems"]) == 20
1056 assert length(result_ids) == 20
1057 assert result["next"]
1058 assert String.starts_with?(result["next"], outbox_endpoint)
1062 |> put_req_header("accept", "application/activity+json")
1063 |> get(result["next"])
1064 |> json_response(200)
1066 result_next_ids = Enum.map(result_next["orderedItems"], fn x -> x["id"] end)
1067 assert length(result_next["orderedItems"]) == 6
1068 assert length(result_next_ids) == 6
1069 refute Enum.find(result_next_ids, fn x -> x in result_ids end)
1070 refute Enum.find(result_ids, fn x -> x in result_next_ids end)
1071 assert String.starts_with?(result["id"], outbox_endpoint)
1075 |> put_req_header("accept", "application/activity+json")
1076 |> get(result_next["id"])
1077 |> json_response(200)
1079 assert result_next == result_next_again
1082 test "it returns 200 even if there're no activities", %{conn: conn} do
1083 user = insert(:user)
1084 outbox_endpoint = user.ap_id <> "/outbox"
1088 |> assign(:user, user)
1089 |> put_req_header("accept", "application/activity+json")
1090 |> get(outbox_endpoint)
1092 result = json_response(conn, 200)
1093 assert outbox_endpoint == result["id"]
1096 test "it returns a note activity in a collection", %{conn: conn} do
1097 note_activity = insert(:note_activity)
1098 note_object = Object.normalize(note_activity, fetch: false)
1099 user = User.get_cached_by_ap_id(note_activity.data["actor"])
1103 |> assign(:user, user)
1104 |> put_req_header("accept", "application/activity+json")
1105 |> get("/users/#{user.nickname}/outbox?page=true")
1107 assert response(conn, 200) =~ note_object.data["content"]
1110 test "it returns an announce activity in a collection", %{conn: conn} do
1111 announce_activity = insert(:announce_activity)
1112 user = User.get_cached_by_ap_id(announce_activity.data["actor"])
1116 |> assign(:user, user)
1117 |> put_req_header("accept", "application/activity+json")
1118 |> get("/users/#{user.nickname}/outbox?page=true")
1120 assert response(conn, 200) =~ announce_activity.data["object"]
1123 test "It returns poll Answers when authenticated", %{conn: conn} do
1124 poller = insert(:user)
1125 voter = insert(:user)
1128 CommonAPI.post(poller, %{
1130 poll: %{options: ["suya", "suya.", "suya.."], expires_in: 10}
1133 assert question = Object.normalize(activity, fetch: false)
1135 {:ok, [activity], _object} = CommonAPI.vote(voter, question, [1])
1139 |> assign(:user, voter)
1140 |> put_req_header("accept", "application/activity+json")
1141 |> get(voter.ap_id <> "/outbox?page=true")
1142 |> json_response(200)
1144 assert [answer_outbox] = outbox_get["orderedItems"]
1145 assert answer_outbox["id"] == activity.data["id"]
1149 describe "POST /users/:nickname/outbox (C2S)" do
1150 setup do: clear_config([:instance, :limit])
1155 "@context" => "https://www.w3.org/ns/activitystreams",
1157 "object" => %{"type" => "Note", "content" => "AP C2S test"},
1158 "to" => "https://www.w3.org/ns/activitystreams#Public",
1164 test "it rejects posts from other users / unauthenticated users", %{
1168 user = insert(:user)
1169 other_user = insert(:user)
1170 conn = put_req_header(conn, "content-type", "application/activity+json")
1173 |> post("/users/#{user.nickname}/outbox", activity)
1174 |> json_response(403)
1177 |> assign(:user, other_user)
1178 |> post("/users/#{user.nickname}/outbox", activity)
1179 |> json_response(403)
1182 test "it inserts an incoming create activity into the database", %{
1186 user = insert(:user)
1190 |> assign(:user, user)
1191 |> put_req_header("content-type", "application/activity+json")
1192 |> post("/users/#{user.nickname}/outbox", activity)
1193 |> json_response(201)
1195 assert Activity.get_by_ap_id(result["id"])
1196 assert result["object"]
1197 assert %Object{data: object} = Object.normalize(result["object"], fetch: false)
1198 assert object["content"] == activity["object"]["content"]
1201 test "it rejects anything beyond 'Note' creations", %{conn: conn, activity: activity} do
1202 user = insert(:user)
1206 |> put_in(["object", "type"], "Benis")
1210 |> assign(:user, user)
1211 |> put_req_header("content-type", "application/activity+json")
1212 |> post("/users/#{user.nickname}/outbox", activity)
1213 |> json_response(400)
1216 test "it inserts an incoming sensitive activity into the database", %{
1220 user = insert(:user)
1221 conn = assign(conn, :user, user)
1222 object = Map.put(activity["object"], "sensitive", true)
1223 activity = Map.put(activity, "object", object)
1227 |> put_req_header("content-type", "application/activity+json")
1228 |> post("/users/#{user.nickname}/outbox", activity)
1229 |> json_response(201)
1231 assert Activity.get_by_ap_id(response["id"])
1232 assert response["object"]
1233 assert %Object{data: response_object} = Object.normalize(response["object"], fetch: false)
1234 assert response_object["sensitive"] == true
1235 assert response_object["content"] == activity["object"]["content"]
1239 |> put_req_header("accept", "application/activity+json")
1240 |> get(response["id"])
1241 |> json_response(200)
1243 assert representation["object"]["sensitive"] == true
1246 test "it rejects an incoming activity with bogus type", %{conn: conn, activity: activity} do
1247 user = insert(:user)
1248 activity = Map.put(activity, "type", "BadType")
1252 |> assign(:user, user)
1253 |> put_req_header("content-type", "application/activity+json")
1254 |> post("/users/#{user.nickname}/outbox", activity)
1256 assert json_response(conn, 400)
1259 test "it erects a tombstone when receiving a delete activity", %{conn: conn} do
1260 note_activity = insert(:note_activity)
1261 note_object = Object.normalize(note_activity, fetch: false)
1262 user = User.get_cached_by_ap_id(note_activity.data["actor"])
1267 id: note_object.data["id"]
1273 |> assign(:user, user)
1274 |> put_req_header("content-type", "application/activity+json")
1275 |> post("/users/#{user.nickname}/outbox", data)
1277 result = json_response(conn, 201)
1278 assert Activity.get_by_ap_id(result["id"])
1280 assert object = Object.get_by_ap_id(note_object.data["id"])
1281 assert object.data["type"] == "Tombstone"
1284 test "it rejects delete activity of object from other actor", %{conn: conn} do
1285 note_activity = insert(:note_activity)
1286 note_object = Object.normalize(note_activity, fetch: false)
1287 user = insert(:user)
1292 id: note_object.data["id"]
1298 |> assign(:user, user)
1299 |> put_req_header("content-type", "application/activity+json")
1300 |> post("/users/#{user.nickname}/outbox", data)
1302 assert json_response(conn, 400)
1305 test "it increases like count when receiving a like action", %{conn: conn} do
1306 note_activity = insert(:note_activity)
1307 note_object = Object.normalize(note_activity, fetch: false)
1308 user = User.get_cached_by_ap_id(note_activity.data["actor"])
1313 id: note_object.data["id"]
1319 |> assign(:user, user)
1320 |> put_req_header("content-type", "application/activity+json")
1321 |> post("/users/#{user.nickname}/outbox", data)
1323 result = json_response(conn, 201)
1324 assert Activity.get_by_ap_id(result["id"])
1326 assert object = Object.get_by_ap_id(note_object.data["id"])
1327 assert object.data["like_count"] == 1
1330 test "it doesn't spreads faulty attributedTo or actor fields", %{
1334 reimu = insert(:user, nickname: "reimu")
1335 cirno = insert(:user, nickname: "cirno")
1342 |> put_in(["object", "actor"], reimu.ap_id)
1343 |> put_in(["object", "attributedTo"], reimu.ap_id)
1344 |> put_in(["actor"], reimu.ap_id)
1345 |> put_in(["attributedTo"], reimu.ap_id)
1349 |> assign(:user, cirno)
1350 |> put_req_header("content-type", "application/activity+json")
1351 |> post("/users/#{reimu.nickname}/outbox", activity)
1352 |> json_response(403)
1356 |> assign(:user, cirno)
1357 |> put_req_header("content-type", "application/activity+json")
1358 |> post("/users/#{cirno.nickname}/outbox", activity)
1359 |> json_response(201)
1361 assert cirno_outbox["attributedTo"] == nil
1362 assert cirno_outbox["actor"] == cirno.ap_id
1364 assert cirno_object = Object.normalize(cirno_outbox["object"], fetch: false)
1365 assert cirno_object.data["actor"] == cirno.ap_id
1366 assert cirno_object.data["attributedTo"] == cirno.ap_id
1369 test "Character limitation", %{conn: conn, activity: activity} do
1370 clear_config([:instance, :limit], 5)
1371 user = insert(:user)
1375 |> assign(:user, user)
1376 |> put_req_header("content-type", "application/activity+json")
1377 |> post("/users/#{user.nickname}/outbox", activity)
1378 |> json_response(400)
1380 assert result == "Note is over the character limit"
1384 describe "/relay/followers" do
1385 test "it returns relay followers", %{conn: conn} do
1386 relay_actor = Relay.get_actor()
1387 user = insert(:user)
1388 User.follow(user, relay_actor)
1392 |> get("/relay/followers")
1393 |> json_response(200)
1395 assert result["first"]["orderedItems"] == [user.ap_id]
1398 test "on non-federating instance, it returns 404", %{conn: conn} do
1399 clear_config([:instance, :federating], false)
1400 user = insert(:user)
1403 |> assign(:user, user)
1404 |> get("/relay/followers")
1405 |> json_response(404)
1409 describe "/relay/following" do
1410 test "it returns relay following", %{conn: conn} do
1413 |> get("/relay/following")
1414 |> json_response(200)
1416 assert result["first"]["orderedItems"] == []
1419 test "on non-federating instance, it returns 404", %{conn: conn} do
1420 clear_config([:instance, :federating], false)
1421 user = insert(:user)
1424 |> assign(:user, user)
1425 |> get("/relay/following")
1426 |> json_response(404)
1430 describe "/users/:nickname/followers" do
1431 test "it returns the followers in a collection", %{conn: conn} do
1432 user = insert(:user)
1433 user_two = insert(:user)
1434 User.follow(user, user_two)
1438 |> assign(:user, user_two)
1439 |> get("/users/#{user_two.nickname}/followers")
1440 |> json_response(200)
1442 assert result["first"]["orderedItems"] == [user.ap_id]
1445 test "it returns a uri if the user has 'hide_followers' set", %{conn: conn} do
1446 user = insert(:user)
1447 user_two = insert(:user, hide_followers: true)
1448 User.follow(user, user_two)
1452 |> assign(:user, user)
1453 |> get("/users/#{user_two.nickname}/followers")
1454 |> json_response(200)
1456 assert is_binary(result["first"])
1459 test "it returns a 403 error on pages, if the user has 'hide_followers' set and the request is from another user",
1461 user = insert(:user)
1462 other_user = insert(:user, hide_followers: true)
1466 |> assign(:user, user)
1467 |> get("/users/#{other_user.nickname}/followers?page=1")
1469 assert result.status == 403
1470 assert result.resp_body == ""
1473 test "it renders the page, if the user has 'hide_followers' set and the request is authenticated with the same user",
1475 user = insert(:user, hide_followers: true)
1476 other_user = insert(:user)
1477 {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
1481 |> assign(:user, user)
1482 |> get("/users/#{user.nickname}/followers?page=1")
1483 |> json_response(200)
1485 assert result["totalItems"] == 1
1486 assert result["orderedItems"] == [other_user.ap_id]
1489 test "it works for more than 10 users", %{conn: conn} do
1490 user = insert(:user)
1492 Enum.each(1..15, fn _ ->
1493 other_user = insert(:user)
1494 User.follow(other_user, user)
1499 |> assign(:user, user)
1500 |> get("/users/#{user.nickname}/followers")
1501 |> json_response(200)
1503 assert length(result["first"]["orderedItems"]) == 10
1504 assert result["first"]["totalItems"] == 15
1505 assert result["totalItems"] == 15
1509 |> assign(:user, user)
1510 |> get("/users/#{user.nickname}/followers?page=2")
1511 |> json_response(200)
1513 assert length(result["orderedItems"]) == 5
1514 assert result["totalItems"] == 15
1517 test "does not require authentication", %{conn: conn} do
1518 user = insert(:user)
1521 |> get("/users/#{user.nickname}/followers")
1522 |> json_response(200)
1526 describe "/users/:nickname/following" do
1527 test "it returns the following in a collection", %{conn: conn} do
1528 user = insert(:user)
1529 user_two = insert(:user)
1530 User.follow(user, user_two)
1534 |> assign(:user, user)
1535 |> get("/users/#{user.nickname}/following")
1536 |> json_response(200)
1538 assert result["first"]["orderedItems"] == [user_two.ap_id]
1541 test "it returns a uri if the user has 'hide_follows' set", %{conn: conn} do
1542 user = insert(:user)
1543 user_two = insert(:user, hide_follows: true)
1544 User.follow(user, user_two)
1548 |> assign(:user, user)
1549 |> get("/users/#{user_two.nickname}/following")
1550 |> json_response(200)
1552 assert is_binary(result["first"])
1555 test "it returns a 403 error on pages, if the user has 'hide_follows' set and the request is from another user",
1557 user = insert(:user)
1558 user_two = insert(:user, hide_follows: true)
1562 |> assign(:user, user)
1563 |> get("/users/#{user_two.nickname}/following?page=1")
1565 assert result.status == 403
1566 assert result.resp_body == ""
1569 test "it renders the page, if the user has 'hide_follows' set and the request is authenticated with the same user",
1571 user = insert(:user, hide_follows: true)
1572 other_user = insert(:user)
1573 {:ok, user, _other_user, _activity} = CommonAPI.follow(user, other_user)
1577 |> assign(:user, user)
1578 |> get("/users/#{user.nickname}/following?page=1")
1579 |> json_response(200)
1581 assert result["totalItems"] == 1
1582 assert result["orderedItems"] == [other_user.ap_id]
1585 test "it works for more than 10 users", %{conn: conn} do
1586 user = insert(:user)
1588 Enum.each(1..15, fn _ ->
1589 user = User.get_cached_by_id(user.id)
1590 other_user = insert(:user)
1591 User.follow(user, other_user)
1596 |> assign(:user, user)
1597 |> get("/users/#{user.nickname}/following")
1598 |> json_response(200)
1600 assert length(result["first"]["orderedItems"]) == 10
1601 assert result["first"]["totalItems"] == 15
1602 assert result["totalItems"] == 15
1606 |> assign(:user, user)
1607 |> get("/users/#{user.nickname}/following?page=2")
1608 |> json_response(200)
1610 assert length(result["orderedItems"]) == 5
1611 assert result["totalItems"] == 15
1614 test "does not require authentication", %{conn: conn} do
1615 user = insert(:user)
1618 |> get("/users/#{user.nickname}/following")
1619 |> json_response(200)
1623 describe "delivery tracking" do
1624 test "it tracks a signed object fetch", %{conn: conn} do
1625 user = insert(:user, local: false)
1626 activity = insert(:note_activity)
1627 object = Object.normalize(activity, fetch: false)
1629 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
1632 |> put_req_header("accept", "application/activity+json")
1633 |> assign(:user, user)
1635 |> json_response(200)
1637 assert Delivery.get(object.id, user.id)
1640 test "it tracks a signed activity fetch", %{conn: conn} do
1641 user = insert(:user, local: false)
1642 activity = insert(:note_activity)
1643 object = Object.normalize(activity, fetch: false)
1645 activity_path = String.trim_leading(activity.data["id"], Pleroma.Web.Endpoint.url())
1648 |> put_req_header("accept", "application/activity+json")
1649 |> assign(:user, user)
1650 |> get(activity_path)
1651 |> json_response(200)
1653 assert Delivery.get(object.id, user.id)
1656 test "it tracks a signed object fetch when the json is cached", %{conn: conn} do
1657 user = insert(:user, local: false)
1658 other_user = insert(:user, local: false)
1659 activity = insert(:note_activity)
1660 object = Object.normalize(activity, fetch: false)
1662 object_path = String.trim_leading(object.data["id"], Pleroma.Web.Endpoint.url())
1665 |> put_req_header("accept", "application/activity+json")
1666 |> assign(:user, user)
1668 |> json_response(200)
1671 |> put_req_header("accept", "application/activity+json")
1672 |> assign(:user, other_user)
1674 |> json_response(200)
1676 assert Delivery.get(object.id, user.id)
1677 assert Delivery.get(object.id, other_user.id)
1680 test "it tracks a signed activity fetch when the json is cached", %{conn: conn} do
1681 user = insert(:user, local: false)
1682 other_user = insert(:user, local: false)
1683 activity = insert(:note_activity)
1684 object = Object.normalize(activity, fetch: false)
1686 activity_path = String.trim_leading(activity.data["id"], Pleroma.Web.Endpoint.url())
1689 |> put_req_header("accept", "application/activity+json")
1690 |> assign(:user, user)
1691 |> get(activity_path)
1692 |> json_response(200)
1695 |> put_req_header("accept", "application/activity+json")
1696 |> assign(:user, other_user)
1697 |> get(activity_path)
1698 |> json_response(200)
1700 assert Delivery.get(object.id, user.id)
1701 assert Delivery.get(object.id, other_user.id)
1705 describe "Additional ActivityPub C2S endpoints" do
1706 test "GET /api/ap/whoami", %{conn: conn} do
1707 user = insert(:user)
1711 |> assign(:user, user)
1712 |> get("/api/ap/whoami")
1714 user = User.get_cached_by_id(user.id)
1716 assert UserView.render("user.json", %{user: user}) == json_response(conn, 200)
1719 |> get("/api/ap/whoami")
1720 |> json_response(403)
1723 setup do: clear_config([:media_proxy])
1724 setup do: clear_config([Pleroma.Upload])
1726 test "POST /api/ap/upload_media", %{conn: conn} do
1727 user = insert(:user)
1729 desc = "Description of the image"
1731 image = %Plug.Upload{
1732 content_type: "image/jpeg",
1733 path: Path.absname("test/fixtures/image.jpg"),
1734 filename: "an_image.jpg"
1739 |> assign(:user, user)
1740 |> post("/api/ap/upload_media", %{"file" => image, "description" => desc})
1741 |> json_response(:created)
1743 assert object["name"] == desc
1744 assert object["type"] == "Document"
1745 assert object["actor"] == user.ap_id
1746 assert [%{"href" => object_href, "mediaType" => object_mediatype}] = object["url"]
1747 assert is_binary(object_href)
1748 assert object_mediatype == "image/jpeg"
1749 assert String.ends_with?(object_href, ".jpg")
1751 activity_request = %{
1752 "@context" => "https://www.w3.org/ns/activitystreams",
1756 "content" => "AP C2S test, attachment",
1757 "attachment" => [object]
1759 "to" => "https://www.w3.org/ns/activitystreams#Public",
1765 |> assign(:user, user)
1766 |> post("/users/#{user.nickname}/outbox", activity_request)
1767 |> json_response(:created)
1769 assert activity_response["id"]
1770 assert activity_response["object"]
1771 assert activity_response["actor"] == user.ap_id
1773 assert %Object{data: %{"attachment" => [attachment]}} =
1774 Object.normalize(activity_response["object"], fetch: false)
1776 assert attachment["type"] == "Document"
1777 assert attachment["name"] == desc
1781 "href" => ^object_href,
1783 "mediaType" => ^object_mediatype
1785 ] = attachment["url"]
1787 # Fails if unauthenticated
1789 |> post("/api/ap/upload_media", %{"file" => image, "description" => desc})
1790 |> json_response(403)