Merge branch 'develop' into refactor/notification_settings
[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 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 assert_receive {:mix_shell, :info, ["mastodon.example.org (no Accept received)"]}
46 end
47 end
48
49 describe "running unfollow" do
50 test "relay is unfollowed" do
51 user = insert(:user)
52 target_instance = user.ap_id
53
54 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
55
56 %User{ap_id: follower_id} = local_user = Relay.get_actor()
57 target_user = User.get_cached_by_ap_id(target_instance)
58 follow_activity = Utils.fetch_latest_follow(local_user, target_user)
59 User.follow(local_user, target_user)
60 assert "#{target_instance}/followers" in User.following(local_user)
61 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
62
63 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
64 assert cancelled_activity.data["state"] == "cancelled"
65
66 [undo_activity] =
67 ActivityPub.fetch_activities([], %{
68 type: "Undo",
69 actor_id: follower_id,
70 limit: 1,
71 skip_preload: true,
72 invisible_actors: true
73 })
74
75 assert undo_activity.data["type"] == "Undo"
76 assert undo_activity.data["actor"] == local_user.ap_id
77 assert undo_activity.data["object"]["id"] == cancelled_activity.data["id"]
78 refute "#{target_instance}/followers" in User.following(local_user)
79 end
80 end
81
82 describe "mix pleroma.relay list" do
83 test "Prints relay subscription list" do
84 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
85
86 refute_receive {:mix_shell, :info, _}
87
88 relay_user = Relay.get_actor()
89
90 ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
91 |> Enum.each(fn ap_id ->
92 {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
93 User.follow(relay_user, user)
94 end)
95
96 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
97
98 assert_receive {:mix_shell, :info, ["mstdn.io"]}
99 assert_receive {:mix_shell, :info, ["mastodon.example.org"]}
100 end
101 end
102 end