total rebase
[anni] / test / pleroma / web / admin_api / controllers / instance_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.InstanceControllerTest do
6   use Pleroma.Web.ConnCase, async: false
7   use Oban.Testing, repo: Pleroma.Repo
8
9   import Pleroma.Factory
10
11   alias Pleroma.Repo
12   alias Pleroma.Tests.ObanHelpers
13   alias Pleroma.Web.CommonAPI
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   test "GET /instances/:instance/statuses", %{conn: conn} do
34     clear_config([:instance, :admin_privileges], [:messages_read])
35     user = insert(:user, local: false, ap_id: "https://archae.me/users/archaeme")
36     user2 = insert(:user, local: false, ap_id: "https://test.com/users/test")
37     insert_pair(:note_activity, user: user)
38     activity = insert(:note_activity, user: user2)
39
40     %{"total" => 2, "activities" => activities} =
41       conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
42
43     assert length(activities) == 2
44
45     %{"total" => 1, "activities" => [_]} =
46       conn |> get("/api/pleroma/admin/instances/test.com/statuses") |> json_response(200)
47
48     %{"total" => 0, "activities" => []} =
49       conn |> get("/api/pleroma/admin/instances/nonexistent.com/statuses") |> json_response(200)
50
51     CommonAPI.repeat(activity.id, user)
52
53     %{"total" => 2, "activities" => activities} =
54       conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(200)
55
56     assert length(activities) == 2
57
58     %{"total" => 3, "activities" => activities} =
59       conn
60       |> get("/api/pleroma/admin/instances/archae.me/statuses?with_reblogs=true")
61       |> json_response(200)
62
63     assert length(activities) == 3
64
65     clear_config([:instance, :admin_privileges], [])
66
67     conn |> get("/api/pleroma/admin/instances/archae.me/statuses") |> json_response(:forbidden)
68   end
69
70   test "DELETE /instances/:instance", %{conn: conn} do
71     clear_config([:instance, :admin_privileges], [:instances_delete])
72     user = insert(:user, nickname: "lain@lain.com")
73     post = insert(:note_activity, user: user)
74
75     response =
76       conn
77       |> delete("/api/pleroma/admin/instances/lain.com")
78       |> json_response(200)
79
80     [:ok] = ObanHelpers.perform_all()
81
82     assert response == "lain.com"
83     refute Repo.reload(user).is_active
84     refute Repo.reload(post)
85
86     clear_config([:instance, :admin_privileges], [])
87
88     conn
89     |> delete("/api/pleroma/admin/instances/lain.com")
90     |> json_response(:forbidden)
91   end
92 end