1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.RelayControllerTest do
6 use Pleroma.Web.ConnCase
10 alias Pleroma.ModerationLog
15 Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 admin = insert(:user, is_admin: true)
22 token = insert(:oauth_admin_token, user: admin)
26 |> assign(:user, admin)
27 |> assign(:token, token)
29 {:ok, %{admin: admin, token: token, conn: conn}}
33 test "POST /relay", %{conn: conn, admin: admin} do
36 |> put_req_header("content-type", "application/json")
37 |> post("/api/pleroma/admin/relay", %{
38 relay_url: "http://mastodon.example.org/users/admin"
41 assert json_response_and_validate_schema(conn, 200) == %{
42 "actor" => "http://mastodon.example.org/users/admin",
43 "followed_back" => false
46 log_entry = Repo.one(ModerationLog)
48 assert ModerationLog.get_log_entry_message(log_entry) ==
49 "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
52 test "GET /relay", %{conn: conn} do
53 relay_user = Pleroma.Web.ActivityPub.Relay.get_actor()
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)
61 conn = get(conn, "/api/pleroma/admin/relay")
63 assert json_response_and_validate_schema(conn, 200)["relays"] |> Enum.sort() == [
65 "actor" => "http://mastodon.example.org/users/admin",
66 "followed_back" => true
68 %{"actor" => "https://mstdn.io/users/mayuutann", "followed_back" => true}
72 test "DELETE /relay", %{conn: conn, admin: admin} do
74 |> put_req_header("content-type", "application/json")
75 |> post("/api/pleroma/admin/relay", %{
76 relay_url: "http://mastodon.example.org/users/admin"
81 |> put_req_header("content-type", "application/json")
82 |> delete("/api/pleroma/admin/relay", %{
83 relay_url: "http://mastodon.example.org/users/admin"
86 assert json_response_and_validate_schema(conn, 200) ==
87 "http://mastodon.example.org/users/admin"
89 [log_entry_one, log_entry_two] = Repo.all(ModerationLog)
91 assert ModerationLog.get_log_entry_message(log_entry_one) ==
92 "@#{admin.nickname} followed relay: http://mastodon.example.org/users/admin"
94 assert ModerationLog.get_log_entry_message(log_entry_two) ==
95 "@#{admin.nickname} unfollowed relay: http://mastodon.example.org/users/admin"