use correct versions for oban migrations
[akkoma] / test / pleroma / web / admin_api / controllers / status_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.StatusControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 import Pleroma.Factory
9
10 alias Pleroma.Activity
11 alias Pleroma.ModerationLog
12 alias Pleroma.Repo
13 alias Pleroma.User
14 alias Pleroma.Web.CommonAPI
15
16 setup do
17 admin = insert(:user, is_admin: true)
18 token = insert(:oauth_admin_token, user: admin)
19
20 conn =
21 build_conn()
22 |> assign(:user, admin)
23 |> assign(:token, token)
24
25 {:ok, %{admin: admin, token: token, conn: conn}}
26 end
27
28 describe "GET /api/pleroma/admin/statuses/:id" do
29 test "not found", %{conn: conn} do
30 assert conn
31 |> get("/api/pleroma/admin/statuses/not_found")
32 |> json_response_and_validate_schema(:not_found)
33 end
34
35 test "shows activity", %{conn: conn} do
36 activity = insert(:note_activity)
37
38 response =
39 conn
40 |> get("/api/pleroma/admin/statuses/#{activity.id}")
41 |> json_response_and_validate_schema(200)
42
43 assert response["id"] == activity.id
44
45 account = response["account"]
46 actor = User.get_by_ap_id(activity.actor)
47
48 assert account["id"] == actor.id
49 assert account["nickname"] == actor.nickname
50 assert account["deactivated"] == actor.deactivated
51 assert account["confirmation_pending"] == actor.confirmation_pending
52 end
53 end
54
55 describe "PUT /api/pleroma/admin/statuses/:id" do
56 setup do
57 activity = insert(:note_activity)
58
59 %{id: activity.id}
60 end
61
62 test "toggle sensitive flag", %{conn: conn, id: id, admin: admin} do
63 response =
64 conn
65 |> put_req_header("content-type", "application/json")
66 |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "true"})
67 |> json_response_and_validate_schema(:ok)
68
69 assert response["sensitive"]
70
71 log_entry = Repo.one(ModerationLog)
72
73 assert ModerationLog.get_log_entry_message(log_entry) ==
74 "@#{admin.nickname} updated status ##{id}, set sensitive: 'true'"
75
76 response =
77 conn
78 |> put_req_header("content-type", "application/json")
79 |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "false"})
80 |> json_response_and_validate_schema(:ok)
81
82 refute response["sensitive"]
83 end
84
85 test "change visibility flag", %{conn: conn, id: id, admin: admin} do
86 response =
87 conn
88 |> put_req_header("content-type", "application/json")
89 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "public"})
90 |> json_response_and_validate_schema(:ok)
91
92 assert response["visibility"] == "public"
93
94 log_entry = Repo.one(ModerationLog)
95
96 assert ModerationLog.get_log_entry_message(log_entry) ==
97 "@#{admin.nickname} updated status ##{id}, set visibility: 'public'"
98
99 response =
100 conn
101 |> put_req_header("content-type", "application/json")
102 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "private"})
103 |> json_response_and_validate_schema(:ok)
104
105 assert response["visibility"] == "private"
106
107 response =
108 conn
109 |> put_req_header("content-type", "application/json")
110 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "unlisted"})
111 |> json_response_and_validate_schema(:ok)
112
113 assert response["visibility"] == "unlisted"
114 end
115
116 test "returns 400 when visibility is unknown", %{conn: conn, id: id} do
117 conn =
118 conn
119 |> put_req_header("content-type", "application/json")
120 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "test"})
121
122 assert %{"error" => "test - Invalid value for enum."} =
123 json_response_and_validate_schema(conn, :bad_request)
124 end
125 end
126
127 describe "DELETE /api/pleroma/admin/statuses/:id" do
128 setup do
129 activity = insert(:note_activity)
130
131 %{id: activity.id}
132 end
133
134 test "deletes status", %{conn: conn, id: id, admin: admin} do
135 conn
136 |> delete("/api/pleroma/admin/statuses/#{id}")
137 |> json_response_and_validate_schema(:ok)
138
139 refute Activity.get_by_id(id)
140
141 log_entry = Repo.one(ModerationLog)
142
143 assert ModerationLog.get_log_entry_message(log_entry) ==
144 "@#{admin.nickname} deleted status ##{id}"
145 end
146
147 test "returns 404 when the status does not exist", %{conn: conn} do
148 conn = delete(conn, "/api/pleroma/admin/statuses/test")
149
150 assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
151 end
152 end
153
154 describe "GET /api/pleroma/admin/statuses" do
155 test "returns all public and unlisted statuses", %{conn: conn, admin: admin} do
156 blocked = insert(:user)
157 user = insert(:user)
158 User.block(admin, blocked)
159
160 {:ok, _} = CommonAPI.post(user, %{status: "@#{admin.nickname}", visibility: "direct"})
161
162 {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
163 {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "private"})
164 {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "public"})
165 {:ok, _} = CommonAPI.post(blocked, %{status: ".", visibility: "public"})
166
167 response =
168 conn
169 |> get("/api/pleroma/admin/statuses")
170 |> json_response_and_validate_schema(200)
171
172 refute "private" in Enum.map(response, & &1["visibility"])
173 assert length(response) == 3
174 end
175
176 test "returns only local statuses with local_only on", %{conn: conn} do
177 user = insert(:user)
178 remote_user = insert(:user, local: false, nickname: "archaeme@archae.me")
179 insert(:note_activity, user: user, local: true)
180 insert(:note_activity, user: remote_user, local: false)
181
182 response =
183 conn
184 |> get("/api/pleroma/admin/statuses?local_only=true")
185 |> json_response_and_validate_schema(200)
186
187 assert length(response) == 1
188 end
189
190 test "returns private and direct statuses with godmode on", %{conn: conn, admin: admin} do
191 user = insert(:user)
192
193 {:ok, _} = CommonAPI.post(user, %{status: "@#{admin.nickname}", visibility: "direct"})
194
195 {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "private"})
196 {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "public"})
197 conn = get(conn, "/api/pleroma/admin/statuses?godmode=true")
198 assert json_response_and_validate_schema(conn, 200) |> length() == 3
199 end
200 end
201 end