1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.RelayTest do
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.Relay
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
18 test "gets an actor for the relay" do
19 user = Relay.get_actor()
20 assert user.ap_id == "#{Pleroma.Web.Endpoint.url()}/relay"
23 test "relay actor is invisible" do
24 user = Relay.get_actor()
25 assert User.invisible?(user)
28 describe "follow/1" do
29 test "returns errors when user not found" do
30 assert capture_log(fn ->
31 {:error, _} = Relay.follow("test-ap-id")
32 end) =~ "Could not decode user at fetch"
35 test "returns activity" do
37 service_actor = Relay.get_actor()
38 assert {:ok, %Activity{} = activity} = Relay.follow(user.ap_id)
39 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
40 assert user.ap_id in activity.recipients
41 assert activity.data["type"] == "Follow"
42 assert activity.data["actor"] == service_actor.ap_id
43 assert activity.data["object"] == user.ap_id
47 describe "unfollow/1" do
48 test "returns errors when user not found" do
49 assert capture_log(fn ->
50 {:error, _} = Relay.unfollow("test-ap-id")
51 end) =~ "Could not decode user at fetch"
54 test "returns activity" do
56 service_actor = Relay.get_actor()
57 ActivityPub.follow(service_actor, user)
58 Pleroma.User.follow(service_actor, user)
59 assert "#{user.ap_id}/followers" in User.following(service_actor)
60 assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
61 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
62 assert user.ap_id in activity.recipients
63 assert activity.data["type"] == "Undo"
64 assert activity.data["actor"] == service_actor.ap_id
65 assert activity.data["to"] == [user.ap_id]
66 refute "#{user.ap_id}/followers" in User.following(service_actor)
70 describe "publish/1" do
71 clear_config([:instance, :federating])
73 test "returns error when activity not `Create` type" do
74 activity = insert(:like_activity)
75 assert Relay.publish(activity) == {:error, "Not implemented"}
78 test "returns error when activity not public" do
79 activity = insert(:direct_note_activity)
80 assert Relay.publish(activity) == {:error, false}
83 test "returns error when object is unknown" do
85 insert(:note_activity,
88 "object" => "http://mastodon.example.org/eee/99541947525187367"
92 assert capture_log(fn ->
93 assert Relay.publish(activity) == {:error, nil}
94 end) =~ "[error] error: nil"
97 test_with_mock "returns announce activity and publish to federate",
98 Pleroma.Web.Federator,
101 Pleroma.Config.put([:instance, :federating], true)
102 service_actor = Relay.get_actor()
103 note = insert(:note_activity)
104 assert {:ok, %Activity{} = activity, %Object{} = obj} = Relay.publish(note)
105 assert activity.data["type"] == "Announce"
106 assert activity.data["actor"] == service_actor.ap_id
107 assert activity.data["object"] == obj.data["id"]
108 assert called(Pleroma.Web.Federator.publish(activity))
111 test_with_mock "returns announce activity and not publish to federate",
112 Pleroma.Web.Federator,
115 Pleroma.Config.put([:instance, :federating], false)
116 service_actor = Relay.get_actor()
117 note = insert(:note_activity)
118 assert {:ok, %Activity{} = activity, %Object{} = obj} = Relay.publish(note)
119 assert activity.data["type"] == "Announce"
120 assert activity.data["actor"] == service_actor.ap_id
121 assert activity.data["object"] == obj.data["id"]
122 refute called(Pleroma.Web.Federator.publish(activity))