Merge branch 'relay-list-change' into 'develop'
[akkoma] / test / tasks / 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 setup_all do
14 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
15
16 Mix.shell(Mix.Shell.Process)
17
18 on_exit(fn ->
19 Mix.shell(Mix.Shell.IO)
20 end)
21
22 :ok
23 end
24
25 describe "running follow" do
26 test "relay is followed" do
27 target_instance = "http://mastodon.example.org/users/admin"
28
29 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
30
31 local_user = Relay.get_actor()
32 assert local_user.ap_id =~ "/relay"
33
34 target_user = User.get_cached_by_ap_id(target_instance)
35 refute target_user.local
36
37 activity = Utils.fetch_latest_follow(local_user, target_user)
38 assert activity.data["type"] == "Follow"
39 assert activity.data["actor"] == local_user.ap_id
40 assert activity.data["object"] == target_user.ap_id
41
42 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
43 assert_receive {:mix_shell, :info, ["mastodon.example.org (no Accept received)"]}
44 end
45 end
46
47 describe "running unfollow" do
48 test "relay is unfollowed" do
49 target_instance = "http://mastodon.example.org/users/admin"
50
51 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
52
53 %User{ap_id: follower_id} = local_user = Relay.get_actor()
54 target_user = User.get_cached_by_ap_id(target_instance)
55 follow_activity = Utils.fetch_latest_follow(local_user, target_user)
56 User.follow(local_user, target_user)
57 assert "#{target_instance}/followers" in User.following(local_user)
58 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
59
60 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
61 assert cancelled_activity.data["state"] == "cancelled"
62
63 [undo_activity] =
64 ActivityPub.fetch_activities([], %{
65 "type" => "Undo",
66 "actor_id" => follower_id,
67 "limit" => 1,
68 "skip_preload" => true
69 })
70
71 assert undo_activity.data["type"] == "Undo"
72 assert undo_activity.data["actor"] == local_user.ap_id
73 assert undo_activity.data["object"] == cancelled_activity.data
74 refute "#{target_instance}/followers" in User.following(local_user)
75 end
76 end
77
78 describe "mix pleroma.relay list" do
79 test "Prints relay subscription list" do
80 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
81
82 refute_receive {:mix_shell, :info, _}
83
84 relay_user = Relay.get_actor()
85
86 ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
87 |> Enum.each(fn ap_id ->
88 {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
89 User.follow(relay_user, user)
90 end)
91
92 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
93
94 assert_receive {:mix_shell, :info, ["mstdn.io"]}
95 assert_receive {:mix_shell, :info, ["mastodon.example.org"]}
96 end
97 end
98 end