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