1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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)
86 describe "mix pleroma.relay list" do
87 test "Prints relay subscription list" do
88 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
90 refute_receive {:mix_shell, :info, _}
92 relay_user = Relay.get_actor()
94 ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
95 |> Enum.each(fn ap_id ->
96 {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
97 User.follow(relay_user, user)
100 :ok = Mix.Tasks.Pleroma.Relay.run(["list"])
102 assert_receive {:mix_shell, :info, ["https://mstdn.io/users/mayuutann"]}
103 assert_receive {:mix_shell, :info, ["http://mastodon.example.org/users/admin"]}