[#3213] Hashtag-filtering functions in ActivityPub. Mix task for migrating hashtags...
[akkoma] / test / mix / tasks / pleroma / relay_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.RelayTest do
6 alias Pleroma.Activity
7 alias Pleroma.User
8 alias Pleroma.Web.ActivityPub.ActivityPub
9 alias Pleroma.Web.ActivityPub.Relay
10 alias Pleroma.Web.ActivityPub.Utils
11 use Pleroma.DataCase
12
13 import Pleroma.Factory
14
15 setup_all do
16 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
17
18 Mix.shell(Mix.Shell.Process)
19
20 on_exit(fn ->
21 Mix.shell(Mix.Shell.IO)
22 end)
23
24 :ok
25 end
26
27 describe "running follow" do
28 test "relay is followed" do
29 target_instance = "http://mastodon.example.org/users/admin"
30
31 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
32
33 local_user = Relay.get_actor()
34 assert local_user.ap_id =~ "/relay"
35
36 target_user = User.get_cached_by_ap_id(target_instance)
37 refute target_user.local
38
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
43
44 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
45
46 assert_receive {:mix_shell, :info,
47 [
48 "http://mastodon.example.org/users/admin - no Accept received (relay didn't follow back)"
49 ]}
50 end
51 end
52
53 describe "running unfollow" do
54 test "relay is unfollowed" do
55 user = insert(:user)
56 target_instance = user.ap_id
57
58 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
59
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])
66
67 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
68 assert cancelled_activity.data["state"] == "cancelled"
69
70 [undo_activity] =
71 ActivityPub.fetch_activities([], %{
72 type: "Undo",
73 actor_id: follower_id,
74 limit: 1,
75 skip_preload: true,
76 invisible_actors: true
77 })
78
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)
83 end
84
85 test "unfollow when relay is dead" do
86 user = insert(:user)
87 target_instance = user.ap_id
88
89 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
90
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)
95
96 assert "#{target_instance}/followers" in User.following(local_user)
97
98 Tesla.Mock.mock(fn %{method: :get, url: ^target_instance} ->
99 %Tesla.Env{status: 404}
100 end)
101
102 Pleroma.Repo.delete(user)
103 Cachex.clear(:user_cache)
104
105 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
106
107 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
108 assert cancelled_activity.data["state"] == "accept"
109
110 assert [] ==
111 ActivityPub.fetch_activities(
112 [],
113 %{
114 type: "Undo",
115 actor_id: follower_id,
116 skip_preload: true,
117 invisible_actors: true
118 }
119 )
120 end
121
122 test "force unfollow when relay is dead" do
123 user = insert(:user)
124 target_instance = user.ap_id
125
126 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
127
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)
132
133 assert "#{target_instance}/followers" in User.following(local_user)
134
135 Tesla.Mock.mock(fn %{method: :get, url: ^target_instance} ->
136 %Tesla.Env{status: 404}
137 end)
138
139 Pleroma.Repo.delete(user)
140 Cachex.clear(:user_cache)
141
142 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance, "--force"])
143
144 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
145 assert cancelled_activity.data["state"] == "cancelled"
146
147 [undo_activity] =
148 ActivityPub.fetch_activities(
149 [],
150 %{type: "Undo", actor_id: follower_id, skip_preload: true, invisible_actors: true}
151 )
152
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)
157 end
158 end
159
160 describe "mix pleroma.relay list" do
161 test "Prints relay subscription list" do
162 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
163
164 refute_receive {:mix_shell, :info, _}
165
166 relay_user = Relay.get_actor()
167
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)
172 end)
173
174 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
175
176 assert_receive {:mix_shell, :info, ["https://mstdn.io/users/mayuutann"]}
177 assert_receive {:mix_shell, :info, ["http://mastodon.example.org/users/admin"]}
178 end
179 end
180 end