1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.RelayTest do
10 alias Pleroma.Web.ActivityPub.Relay
11 alias Pleroma.Web.CommonAPI
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
17 test "gets an actor for the relay" do
18 user = Relay.get_actor()
19 assert user.ap_id == "#{Pleroma.Web.Endpoint.url()}/relay"
22 test "relay actor is invisible" do
23 user = Relay.get_actor()
24 assert User.invisible?(user)
27 describe "follow/1" do
28 test "returns errors when user not found" do
29 assert capture_log(fn ->
30 {:error, _} = Relay.follow("test-ap-id")
31 end) =~ "Could not decode user at fetch"
34 test "returns activity" do
36 service_actor = Relay.get_actor()
37 assert {:ok, %Activity{} = activity} = Relay.follow(user.ap_id)
38 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
39 assert user.ap_id in activity.recipients
40 assert activity.data["type"] == "Follow"
41 assert activity.data["actor"] == service_actor.ap_id
42 assert activity.data["object"] == user.ap_id
46 describe "unfollow/1" do
47 test "returns errors when user not found" do
48 assert capture_log(fn ->
49 {:error, _} = Relay.unfollow("test-ap-id")
50 end) =~ "Could not decode user at fetch"
53 test "returns activity" do
55 service_actor = Relay.get_actor()
56 CommonAPI.follow(service_actor, user)
57 assert "#{user.ap_id}/followers" in User.following(service_actor)
58 assert {:ok, %Activity{} = activity} = Relay.unfollow(user.ap_id)
59 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
60 assert user.ap_id in activity.recipients
61 assert activity.data["type"] == "Undo"
62 assert activity.data["actor"] == service_actor.ap_id
63 assert activity.data["to"] == [user.ap_id]
64 refute "#{user.ap_id}/followers" in User.following(service_actor)
67 test "force unfollow when target service is dead" do
69 user_ap_id = user.ap_id
72 Tesla.Mock.mock(fn %{method: :get, url: ^user_ap_id} ->
73 %Tesla.Env{status: 404}
76 service_actor = Relay.get_actor()
77 CommonAPI.follow(service_actor, user)
78 assert "#{user.ap_id}/followers" in User.following(service_actor)
80 assert Pleroma.Repo.get_by(
81 Pleroma.FollowingRelationship,
82 follower_id: service_actor.id,
86 Pleroma.Repo.delete(user)
87 User.invalidate_cache(user)
89 assert {:ok, %Activity{} = activity} = Relay.unfollow(user_ap_id, %{force: true})
91 assert refresh_record(service_actor).following_count == 0
93 refute Pleroma.Repo.get_by(
94 Pleroma.FollowingRelationship,
95 follower_id: service_actor.id,
99 assert activity.actor == "#{Pleroma.Web.Endpoint.url()}/relay"
100 assert user.ap_id in activity.recipients
101 assert activity.data["type"] == "Undo"
102 assert activity.data["actor"] == service_actor.ap_id
103 assert activity.data["to"] == [user_ap_id]
104 refute "#{user.ap_id}/followers" in User.following(service_actor)
108 describe "publish/1" do
109 setup do: clear_config([:instance, :federating])
111 test "returns error when activity not `Create` type" do
112 activity = insert(:like_activity)
113 assert Relay.publish(activity) == {:error, "Not implemented"}
116 @tag capture_log: true
117 test "returns error when activity not public" do
118 activity = insert(:direct_note_activity)
119 assert Relay.publish(activity) == {:error, false}
122 test "returns error when object is unknown" do
124 insert(:note_activity,
127 "object" => "http://mastodon.example.org/eee/99541947525187367"
132 %{method: :get, url: "http://mastodon.example.org/eee/99541947525187367"} ->
133 %Tesla.Env{status: 500, body: ""}
136 assert capture_log(fn ->
137 assert Relay.publish(activity) == {:error, false}
138 end) =~ "[error] error: false"
141 test_with_mock "returns announce activity and publish to federate",
142 Pleroma.Web.Federator,
145 clear_config([:instance, :federating], true)
146 service_actor = Relay.get_actor()
147 note = insert(:note_activity)
148 assert {:ok, %Activity{} = activity} = Relay.publish(note)
149 assert activity.data["type"] == "Announce"
150 assert activity.data["actor"] == service_actor.ap_id
151 assert service_actor.follower_address in activity.data["to"]
152 assert called(Pleroma.Web.Federator.publish(activity))
155 test_with_mock "returns announce activity and not publish to federate",
156 Pleroma.Web.Federator,
159 clear_config([:instance, :federating], false)
160 service_actor = Relay.get_actor()
161 note = insert(:note_activity)
162 assert {:ok, %Activity{} = activity} = Relay.publish(note)
163 assert activity.data["type"] == "Announce"
164 assert activity.data["actor"] == service_actor.ap_id
165 refute called(Pleroma.Web.Federator.publish(activity))