total rebase
[anni] / test / pleroma / web / mastodon_api / controllers / report_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.MastodonAPI.ReportControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Activity
9   alias Pleroma.Repo
10   alias Pleroma.Web.CommonAPI
11
12   import Pleroma.Factory
13
14   setup do: oauth_access(["write:reports"])
15
16   setup do
17     target_user = insert(:user)
18
19     {:ok, activity} = CommonAPI.post(target_user, %{status: "foobar"})
20
21     [target_user: target_user, activity: activity]
22   end
23
24   test "submit a basic report", %{conn: conn, target_user: target_user} do
25     assert %{"action_taken" => false, "id" => _} =
26              conn
27              |> put_req_header("content-type", "application/json")
28              |> post("/api/v1/reports", %{"account_id" => target_user.id})
29              |> json_response_and_validate_schema(200)
30   end
31
32   test "submit a report with a fake Create", %{
33     conn: conn
34   } do
35     target_user = insert(:user)
36
37     note = insert(:note, user: target_user)
38
39     activity_params = %{
40       "object" => note.data["id"],
41       "actor" => note.data["actor"],
42       "to" => note.data["to"] || [],
43       "cc" => note.data["cc"] || [],
44       "type" => "Create"
45     }
46
47     {:ok, fake_activity} =
48       Repo.insert(%Activity{
49         data: activity_params,
50         recipients: activity_params["to"] ++ activity_params["cc"],
51         local: true,
52         actor: activity_params["actor"]
53       })
54
55     assert %{"action_taken" => false, "id" => _} =
56              conn
57              |> put_req_header("content-type", "application/json")
58              |> post("/api/v1/reports", %{
59                "account_id" => target_user.id,
60                "status_ids" => [fake_activity.id],
61                "comment" => "bad status!",
62                "forward" => "false"
63              })
64              |> json_response_and_validate_schema(200)
65   end
66
67   test "submit a report with statuses and comment", %{
68     conn: conn,
69     target_user: target_user,
70     activity: activity
71   } do
72     assert %{"action_taken" => false, "id" => _} =
73              conn
74              |> put_req_header("content-type", "application/json")
75              |> post("/api/v1/reports", %{
76                "account_id" => target_user.id,
77                "status_ids" => [activity.id],
78                "comment" => "bad status!",
79                "forward" => "false"
80              })
81              |> json_response_and_validate_schema(200)
82   end
83
84   test "account_id is required", %{
85     conn: conn,
86     activity: activity
87   } do
88     assert %{"error" => "Missing field: account_id."} =
89              conn
90              |> put_req_header("content-type", "application/json")
91              |> post("/api/v1/reports", %{"status_ids" => [activity.id]})
92              |> json_response_and_validate_schema(400)
93   end
94
95   test "comment must be up to the size specified in the config", %{
96     conn: conn,
97     target_user: target_user
98   } do
99     max_size = Pleroma.Config.get([:instance, :max_report_comment_size], 1000)
100     comment = String.pad_trailing("a", max_size + 1, "a")
101
102     error = %{"error" => "Comment must be up to #{max_size} characters"}
103
104     assert ^error =
105              conn
106              |> put_req_header("content-type", "application/json")
107              |> post("/api/v1/reports", %{"account_id" => target_user.id, "comment" => comment})
108              |> json_response_and_validate_schema(400)
109   end
110
111   test "returns error when account is not exist", %{
112     conn: conn,
113     activity: activity
114   } do
115     conn =
116       conn
117       |> put_req_header("content-type", "application/json")
118       |> post("/api/v1/reports", %{"status_ids" => [activity.id], "account_id" => "foo"})
119
120     assert json_response_and_validate_schema(conn, 400) == %{"error" => "Account not found"}
121   end
122
123   test "doesn't fail if an admin has no email", %{conn: conn, target_user: target_user} do
124     insert(:user, %{is_admin: true, email: nil})
125
126     assert %{"action_taken" => false, "id" => _} =
127              conn
128              |> put_req_header("content-type", "application/json")
129              |> post("/api/v1/reports", %{"account_id" => target_user.id})
130              |> json_response_and_validate_schema(200)
131   end
132 end