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.StatusControllerTest do
6 use Pleroma.Web.ConnCase, async: true
10 alias Pleroma.Activity
11 alias Pleroma.ModerationLog
14 alias Pleroma.Web.CommonAPI
17 admin = insert(:user, is_admin: true)
18 token = insert(:oauth_admin_token, user: admin)
22 |> assign(:user, admin)
23 |> assign(:token, token)
25 {:ok, %{admin: admin, token: token, conn: conn}}
28 describe "GET /api/pleroma/admin/statuses/:id" do
29 test "not found", %{conn: conn} do
31 |> get("/api/pleroma/admin/statuses/not_found")
32 |> json_response_and_validate_schema(:not_found)
35 test "shows activity", %{conn: conn} do
36 activity = insert(:note_activity)
40 |> get("/api/pleroma/admin/statuses/#{activity.id}")
41 |> json_response_and_validate_schema(200)
43 assert response["id"] == activity.id
45 account = response["account"]
46 actor = User.get_by_ap_id(activity.actor)
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
55 describe "PUT /api/pleroma/admin/statuses/:id" do
57 activity = insert(:note_activity)
62 test "toggle sensitive flag", %{conn: conn, id: id, admin: admin} do
65 |> put_req_header("content-type", "application/json")
66 |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "true"})
67 |> json_response_and_validate_schema(:ok)
69 assert response["sensitive"]
71 log_entry = Repo.one(ModerationLog)
73 assert ModerationLog.get_log_entry_message(log_entry) ==
74 "@#{admin.nickname} updated status ##{id}, set sensitive: 'true'"
78 |> put_req_header("content-type", "application/json")
79 |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "false"})
80 |> json_response_and_validate_schema(:ok)
82 refute response["sensitive"]
85 test "change visibility flag", %{conn: conn, id: id, admin: admin} do
88 |> put_req_header("content-type", "application/json")
89 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "public"})
90 |> json_response_and_validate_schema(:ok)
92 assert response["visibility"] == "public"
94 log_entry = Repo.one(ModerationLog)
96 assert ModerationLog.get_log_entry_message(log_entry) ==
97 "@#{admin.nickname} updated status ##{id}, set visibility: 'public'"
101 |> put_req_header("content-type", "application/json")
102 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "private"})
103 |> json_response_and_validate_schema(:ok)
105 assert response["visibility"] == "private"
109 |> put_req_header("content-type", "application/json")
110 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "unlisted"})
111 |> json_response_and_validate_schema(:ok)
113 assert response["visibility"] == "unlisted"
116 test "returns 400 when visibility is unknown", %{conn: conn, id: id} do
119 |> put_req_header("content-type", "application/json")
120 |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "test"})
122 assert %{"error" => "test - Invalid value for enum."} =
123 json_response_and_validate_schema(conn, :bad_request)
127 describe "DELETE /api/pleroma/admin/statuses/:id" do
129 activity = insert(:note_activity)
134 test "deletes status", %{conn: conn, id: id, admin: admin} do
136 |> delete("/api/pleroma/admin/statuses/#{id}")
137 |> json_response_and_validate_schema(:ok)
139 refute Activity.get_by_id(id)
141 log_entry = Repo.one(ModerationLog)
143 assert ModerationLog.get_log_entry_message(log_entry) ==
144 "@#{admin.nickname} deleted status ##{id}"
147 test "returns 404 when the status does not exist", %{conn: conn} do
148 conn = delete(conn, "/api/pleroma/admin/statuses/test")
150 assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
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)
158 User.block(admin, blocked)
160 {:ok, _} = CommonAPI.post(user, %{status: "@#{admin.nickname}", visibility: "direct"})
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"})
169 |> get("/api/pleroma/admin/statuses")
170 |> json_response_and_validate_schema(200)
172 refute "private" in Enum.map(response, & &1["visibility"])
173 assert length(response) == 3
176 test "returns only local statuses with local_only on", %{conn: conn} do
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)
184 |> get("/api/pleroma/admin/statuses?local_only=true")
185 |> json_response_and_validate_schema(200)
187 assert length(response) == 1
190 test "returns private and direct statuses with godmode on", %{conn: conn, admin: admin} do
193 {:ok, _} = CommonAPI.post(user, %{status: "@#{admin.nickname}", visibility: "direct"})
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