Merge branch 'issue/1177' into 'develop'
[akkoma] / test / tasks / relay_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 end
42 end
43
44 describe "running unfollow" do
45 test "relay is unfollowed" do
46 target_instance = "http://mastodon.example.org/users/admin"
47
48 Mix.Tasks.Pleroma.Relay.run(["follow", target_instance])
49
50 %User{ap_id: follower_id} = local_user = Relay.get_actor()
51 target_user = User.get_cached_by_ap_id(target_instance)
52 follow_activity = Utils.fetch_latest_follow(local_user, target_user)
53 User.follow(local_user, target_user)
54 assert "#{target_instance}/followers" in refresh_record(local_user).following
55 Mix.Tasks.Pleroma.Relay.run(["unfollow", target_instance])
56
57 cancelled_activity = Activity.get_by_ap_id(follow_activity.data["id"])
58 assert cancelled_activity.data["state"] == "cancelled"
59
60 [undo_activity] =
61 ActivityPub.fetch_activities([], %{
62 "type" => "Undo",
63 "actor_id" => follower_id,
64 "limit" => 1,
65 "skip_preload" => true
66 })
67
68 assert undo_activity.data["type"] == "Undo"
69 assert undo_activity.data["actor"] == local_user.ap_id
70 assert undo_activity.data["object"] == cancelled_activity.data
71 refute "#{target_instance}/followers" in refresh_record(local_user).following
72 end
73 end
74
75 describe "mix pleroma.relay list" do
76 test "Prints relay subscription list" do
77 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
78
79 refute_receive {:mix_shell, :info, _}
80
81 Pleroma.Web.ActivityPub.Relay.get_actor()
82 |> Ecto.Changeset.change(
83 following: [
84 "http://test-app.com/user/test1",
85 "http://test-app.com/user/test1",
86 "http://test-app-42.com/user/test1"
87 ]
88 )
89 |> Pleroma.User.update_and_set_cache()
90
91 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
92
93 assert_receive {:mix_shell, :info, ["test-app.com"]}
94 assert_receive {:mix_shell, :info, ["test-app-42.com"]}
95 end
96 end
97 end