Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / pleroma / web / admin_api / controllers / relay_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.RelayControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9
10 alias Pleroma.ModerationLog
11 alias Pleroma.Repo
12 alias Pleroma.User
13
14 setup_all do
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
16
17 :ok
18 end
19
20 setup do
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
23
24 conn =
25 build_conn()
26 |> assign(:user, admin)
27 |> assign(:token, token)
28
29 {:ok, %{admin: admin, token: token, conn: conn}}
30 end
31
32 describe "relays" do
33 test "POST /relay", %{conn: conn, admin: admin} do
34 conn =
35 conn
36 |> put_req_header("content-type", "application/json")
37 |> post("/api/pleroma/admin/relay", %{
38 relay_url: "http://mastodon.example.org/users/admin"
39 })
40
41 assert json_response_and_validate_schema(conn, 200) == %{
42 "actor" => "http://mastodon.example.org/users/admin",
43 "followed_back" => false
44 }
45
46 log_entry = Repo.one(ModerationLog)
47
48 assert ModerationLog.get_log_entry_message(log_entry) ==
49 "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
50 end
51
52 test "GET /relay", %{conn: conn} do
53 relay_user = Pleroma.Web.ActivityPub.Relay.get_actor()
54
55 ["http://mastodon.example.org/users/admin", "https://mstdn.io/users/mayuutann"]
56 |> Enum.each(fn ap_id ->
57 {:ok, user} = User.get_or_fetch_by_ap_id(ap_id)
58 User.follow(relay_user, user)
59 end)
60
61 conn = get(conn, "/api/pleroma/admin/relay")
62
63 assert json_response_and_validate_schema(conn, 200)["relays"] |> Enum.sort() == [
64 %{
65 "actor" => "http://mastodon.example.org/users/admin",
66 "followed_back" => true
67 },
68 %{"actor" => "https://mstdn.io/users/mayuutann", "followed_back" => true}
69 ]
70 end
71
72 test "DELETE /relay", %{conn: conn, admin: admin} do
73 conn
74 |> put_req_header("content-type", "application/json")
75 |> post("/api/pleroma/admin/relay", %{
76 relay_url: "http://mastodon.example.org/users/admin"
77 })
78
79 conn =
80 conn
81 |> put_req_header("content-type", "application/json")
82 |> delete("/api/pleroma/admin/relay", %{
83 relay_url: "http://mastodon.example.org/users/admin"
84 })
85
86 assert json_response_and_validate_schema(conn, 200) ==
87 "http://mastodon.example.org/users/admin"
88
89 [log_entry_one, log_entry_two] = Repo.all(ModerationLog)
90
91 assert ModerationLog.get_log_entry_message(log_entry_one) ==
92 "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
93
94 assert ModerationLog.get_log_entry_message(log_entry_two) ==
95 "@#{admin.nickname} unfollowed relay: http://mastodon.example.org/users/admin"
96 end
97 end
98 end