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 Mix.Tasks.Pleroma.RelayTest do
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.Relay
10 alias Pleroma.Web.ActivityPub.Utils
13 import Pleroma.Factory
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 Mix.shell(Mix.Shell.Process)
21 Mix.shell(Mix.Shell.IO)
27 describe "running follow" do
28 test "relay is followed" do
29 target_instance = "http://mastodon.example.org/users/admin"
31 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
33 local_user = Relay.get_actor()
34 assert local_user.ap_id =~ "/relay"
36 target_user = User.get_cached_by_ap_id(target_instance)
37 refute target_user.local
39 activity = Utils.fetch_latest_follow(local_user, target_user)
40 assert activity.data["type"] == "Follow"
41 assert activity.data["actor"] == local_user.ap_id
42 assert activity.data["object"] == target_user.ap_id
44 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
46 assert_receive {:mix_shell, :info,
48 "http://mastodon.example.org/users/admin - no Accept received (relay didn't follow back)"
53 describe "running unfollow" do
54 test "relay is unfollowed" do
56 target_instance = user.ap_id
58 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
60 %User{ap_id: follower_id} = local_user = Relay.get_actor()
61 target_user = User.get_cached_by_ap_id(target_instance)
62 follow_activity = Utils.fetch_latest_follow(local_user, target_user)
63 User.follow(local_user, target_user)
64 assert "#{target_instance}/followers" in User.following(local_user)
65 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
67 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
68 assert cancelled_activity.data["state"] == "cancelled"
71 ActivityPub.fetch_activities([], %{
73 actor_id: follower_id,
76 invisible_actors: true
79 assert undo_activity.data["type"] == "Undo"
80 assert undo_activity.data["actor"] == local_user.ap_id
81 assert undo_activity.data["object"]["id"] == cancelled_activity.data["id"]
82 refute "#{target_instance}/followers" in User.following(local_user)
85 test "unfollow when relay is dead" do
87 target_instance = user.ap_id
89 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
91 %User{ap_id: follower_id} = local_user = Relay.get_actor()
92 target_user = User.get_cached_by_ap_id(target_instance)
93 follow_activity = Utils.fetch_latest_follow(local_user, target_user)
94 User.follow(local_user, target_user)
96 assert "#{target_instance}/followers" in User.following(local_user)
98 Tesla.Mock.mock(fn %{method: :get, url: ^target_instance} ->
99 %Tesla.Env{status: 404}
102 Pleroma.Repo.delete(user)
103 User.invalidate_cache(user)
105 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
107 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
108 assert cancelled_activity.data["state"] == "accept"
111 ActivityPub.fetch_activities(
115 actor_id: follower_id,
117 invisible_actors: true
122 test "force unfollow when relay is dead" do
124 target_instance = user.ap_id
126 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
128 %User{ap_id: follower_id} = local_user = Relay.get_actor()
129 target_user = User.get_cached_by_ap_id(target_instance)
130 follow_activity = Utils.fetch_latest_follow(local_user, target_user)
131 User.follow(local_user, target_user)
133 assert "#{target_instance}/followers" in User.following(local_user)
135 Tesla.Mock.mock(fn %{method: :get, url: ^target_instance} ->
136 %Tesla.Env{status: 404}
139 Pleroma.Repo.delete(user)
140 User.invalidate_cache(user)
142 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance, "--force"])
144 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
145 assert cancelled_activity.data["state"] == "cancelled"
148 ActivityPub.fetch_activities(
150 %{type: "Undo", actor_id: follower_id, skip_preload: true, invisible_actors: true}
153 assert undo_activity.data["type"] == "Undo"
154 assert undo_activity.data["actor"] == local_user.ap_id
155 assert undo_activity.data["object"]["id"] == cancelled_activity.data["id"]
156 refute "#{target_instance}/followers" in User.following(local_user)
160 describe "mix pleroma.relay list" do
161 test "Prints relay subscription list" do
162 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
164 refute_receive {:mix_shell, :info, _}
166 relay_user = Relay.get_actor()
168 ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
169 |> Enum.each(fn ap_id ->
170 {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
171 User.follow(relay_user, user)
174 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
176 assert_receive {:mix_shell, :info, ["https://mstdn.io/users/mayuutann"]}
177 assert_receive {:mix_shell, :info, ["http://mastodon.example.org/users/admin"]}