logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

report_controller_test.exs (11315B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.AdminAPI.ReportControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. alias Pleroma.Activity
  8. alias Pleroma.Config
  9. alias Pleroma.ModerationLog
  10. alias Pleroma.Repo
  11. alias Pleroma.ReportNote
  12. alias Pleroma.Web.CommonAPI
  13. setup do
  14. admin = insert(:user, is_admin: true)
  15. token = insert(:oauth_admin_token, user: admin)
  16. conn =
  17. build_conn()
  18. |> assign(:user, admin)
  19. |> assign(:token, token)
  20. {:ok, %{admin: admin, token: token, conn: conn}}
  21. end
  22. describe "GET /api/pleroma/admin/reports/:id" do
  23. test "returns report by its id", %{conn: conn} do
  24. [reporter, target_user] = insert_pair(:user)
  25. activity = insert(:note_activity, user: target_user)
  26. {:ok, %{id: report_id}} =
  27. CommonAPI.report(reporter, %{
  28. account_id: target_user.id,
  29. comment: "I feel offended",
  30. status_ids: [activity.id]
  31. })
  32. response =
  33. conn
  34. |> get("/api/pleroma/admin/reports/#{report_id}")
  35. |> json_response_and_validate_schema(:ok)
  36. assert response["id"] == report_id
  37. end
  38. test "returns 404 when report id is invalid", %{conn: conn} do
  39. conn = get(conn, "/api/pleroma/admin/reports/test")
  40. assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
  41. end
  42. end
  43. describe "PATCH /api/pleroma/admin/reports" do
  44. setup do
  45. [reporter, target_user] = insert_pair(:user)
  46. activity = insert(:note_activity, user: target_user)
  47. {:ok, %{id: report_id}} =
  48. CommonAPI.report(reporter, %{
  49. account_id: target_user.id,
  50. comment: "I feel offended",
  51. status_ids: [activity.id]
  52. })
  53. {:ok, %{id: second_report_id}} =
  54. CommonAPI.report(reporter, %{
  55. account_id: target_user.id,
  56. comment: "I feel very offended",
  57. status_ids: [activity.id]
  58. })
  59. %{
  60. id: report_id,
  61. second_report_id: second_report_id
  62. }
  63. end
  64. test "requires admin:write:reports scope", %{conn: conn, id: id, admin: admin} do
  65. read_token = insert(:oauth_token, user: admin, scopes: ["admin:read"])
  66. write_token = insert(:oauth_token, user: admin, scopes: ["admin:write:reports"])
  67. response =
  68. conn
  69. |> assign(:token, read_token)
  70. |> put_req_header("content-type", "application/json")
  71. |> patch("/api/pleroma/admin/reports", %{
  72. "reports" => [%{"state" => "resolved", "id" => id}]
  73. })
  74. |> json_response_and_validate_schema(403)
  75. assert response == %{
  76. "error" => "Insufficient permissions: admin:write:reports."
  77. }
  78. conn
  79. |> assign(:token, write_token)
  80. |> put_req_header("content-type", "application/json")
  81. |> patch("/api/pleroma/admin/reports", %{
  82. "reports" => [%{"state" => "resolved", "id" => id}]
  83. })
  84. |> json_response_and_validate_schema(:no_content)
  85. end
  86. test "mark report as resolved", %{conn: conn, id: id, admin: admin} do
  87. conn
  88. |> put_req_header("content-type", "application/json")
  89. |> patch("/api/pleroma/admin/reports", %{
  90. "reports" => [
  91. %{"state" => "resolved", "id" => id}
  92. ]
  93. })
  94. |> json_response_and_validate_schema(:no_content)
  95. activity = Activity.get_by_id(id)
  96. assert activity.data["state"] == "resolved"
  97. log_entry = Repo.one(ModerationLog)
  98. assert ModerationLog.get_log_entry_message(log_entry) ==
  99. "@#{admin.nickname} updated report ##{id} with 'resolved' state"
  100. end
  101. test "closes report", %{conn: conn, id: id, admin: admin} do
  102. conn
  103. |> put_req_header("content-type", "application/json")
  104. |> patch("/api/pleroma/admin/reports", %{
  105. "reports" => [
  106. %{"state" => "closed", "id" => id}
  107. ]
  108. })
  109. |> json_response_and_validate_schema(:no_content)
  110. activity = Activity.get_by_id(id)
  111. assert activity.data["state"] == "closed"
  112. log_entry = Repo.one(ModerationLog)
  113. assert ModerationLog.get_log_entry_message(log_entry) ==
  114. "@#{admin.nickname} updated report ##{id} with 'closed' state"
  115. end
  116. test "returns 400 when state is unknown", %{conn: conn, id: id} do
  117. conn =
  118. conn
  119. |> put_req_header("content-type", "application/json")
  120. |> patch("/api/pleroma/admin/reports", %{
  121. "reports" => [
  122. %{"state" => "test", "id" => id}
  123. ]
  124. })
  125. assert "Unsupported state" =
  126. hd(json_response_and_validate_schema(conn, :bad_request))["error"]
  127. end
  128. test "returns 404 when report is not exist", %{conn: conn} do
  129. conn =
  130. conn
  131. |> put_req_header("content-type", "application/json")
  132. |> patch("/api/pleroma/admin/reports", %{
  133. "reports" => [
  134. %{"state" => "closed", "id" => "test"}
  135. ]
  136. })
  137. assert hd(json_response_and_validate_schema(conn, :bad_request))["error"] == "not_found"
  138. end
  139. test "updates state of multiple reports", %{
  140. conn: conn,
  141. id: id,
  142. admin: admin,
  143. second_report_id: second_report_id
  144. } do
  145. conn
  146. |> put_req_header("content-type", "application/json")
  147. |> patch("/api/pleroma/admin/reports", %{
  148. "reports" => [
  149. %{"state" => "resolved", "id" => id},
  150. %{"state" => "closed", "id" => second_report_id}
  151. ]
  152. })
  153. |> json_response_and_validate_schema(:no_content)
  154. activity = Activity.get_by_id(id)
  155. second_activity = Activity.get_by_id(second_report_id)
  156. assert activity.data["state"] == "resolved"
  157. assert second_activity.data["state"] == "closed"
  158. [first_log_entry, second_log_entry] = Repo.all(ModerationLog)
  159. assert ModerationLog.get_log_entry_message(first_log_entry) ==
  160. "@#{admin.nickname} updated report ##{id} with 'resolved' state"
  161. assert ModerationLog.get_log_entry_message(second_log_entry) ==
  162. "@#{admin.nickname} updated report ##{second_report_id} with 'closed' state"
  163. end
  164. end
  165. describe "GET /api/pleroma/admin/reports" do
  166. test "returns empty response when no reports created", %{conn: conn} do
  167. response =
  168. conn
  169. |> get(report_path(conn, :index))
  170. |> json_response_and_validate_schema(:ok)
  171. assert Enum.empty?(response["reports"])
  172. assert response["total"] == 0
  173. end
  174. test "returns reports", %{conn: conn} do
  175. [reporter, target_user] = insert_pair(:user)
  176. activity = insert(:note_activity, user: target_user)
  177. {:ok, %{id: report_id}} =
  178. CommonAPI.report(reporter, %{
  179. account_id: target_user.id,
  180. comment: "I feel offended",
  181. status_ids: [activity.id]
  182. })
  183. response =
  184. conn
  185. |> get(report_path(conn, :index))
  186. |> json_response_and_validate_schema(:ok)
  187. [report] = response["reports"]
  188. assert length(response["reports"]) == 1
  189. assert report["id"] == report_id
  190. assert response["total"] == 1
  191. end
  192. test "returns reports with specified state", %{conn: conn} do
  193. [reporter, target_user] = insert_pair(:user)
  194. activity = insert(:note_activity, user: target_user)
  195. {:ok, %{id: first_report_id}} =
  196. CommonAPI.report(reporter, %{
  197. account_id: target_user.id,
  198. comment: "I feel offended",
  199. status_ids: [activity.id]
  200. })
  201. {:ok, %{id: second_report_id}} =
  202. CommonAPI.report(reporter, %{
  203. account_id: target_user.id,
  204. comment: "I don't like this user"
  205. })
  206. CommonAPI.update_report_state(second_report_id, "closed")
  207. response =
  208. conn
  209. |> get(report_path(conn, :index, %{state: "open"}))
  210. |> json_response_and_validate_schema(:ok)
  211. assert [open_report] = response["reports"]
  212. assert length(response["reports"]) == 1
  213. assert open_report["id"] == first_report_id
  214. assert response["total"] == 1
  215. response =
  216. conn
  217. |> get(report_path(conn, :index, %{state: "closed"}))
  218. |> json_response_and_validate_schema(:ok)
  219. assert [closed_report] = response["reports"]
  220. assert length(response["reports"]) == 1
  221. assert closed_report["id"] == second_report_id
  222. assert response["total"] == 1
  223. assert %{"total" => 0, "reports" => []} ==
  224. conn
  225. |> get(report_path(conn, :index, %{state: "resolved"}))
  226. |> json_response_and_validate_schema(:ok)
  227. end
  228. test "returns 403 when requested by a non-admin" do
  229. user = insert(:user)
  230. token = insert(:oauth_token, user: user)
  231. conn =
  232. build_conn()
  233. |> assign(:user, user)
  234. |> assign(:token, token)
  235. |> get("/api/pleroma/admin/reports")
  236. assert json_response(conn, :forbidden) ==
  237. %{"error" => "User is not an admin."}
  238. end
  239. test "returns 403 when requested by anonymous" do
  240. conn = get(build_conn(), "/api/pleroma/admin/reports")
  241. assert json_response(conn, :forbidden) == %{
  242. "error" => "Invalid credentials."
  243. }
  244. end
  245. end
  246. describe "POST /api/pleroma/admin/reports/:id/notes" do
  247. setup %{conn: conn, admin: admin} do
  248. [reporter, target_user] = insert_pair(:user)
  249. activity = insert(:note_activity, user: target_user)
  250. {:ok, %{id: report_id}} =
  251. CommonAPI.report(reporter, %{
  252. account_id: target_user.id,
  253. comment: "I feel offended",
  254. status_ids: [activity.id]
  255. })
  256. conn
  257. |> put_req_header("content-type", "application/json")
  258. |> post("/api/pleroma/admin/reports/#{report_id}/notes", %{
  259. content: "this is disgusting!"
  260. })
  261. conn
  262. |> put_req_header("content-type", "application/json")
  263. |> post("/api/pleroma/admin/reports/#{report_id}/notes", %{
  264. content: "this is disgusting2!"
  265. })
  266. %{
  267. admin_id: admin.id,
  268. report_id: report_id
  269. }
  270. end
  271. test "it creates report note", %{admin_id: admin_id, report_id: report_id} do
  272. assert [note, _] = Repo.all(ReportNote)
  273. assert %{
  274. activity_id: ^report_id,
  275. content: "this is disgusting!",
  276. user_id: ^admin_id
  277. } = note
  278. end
  279. test "it returns reports with notes", %{conn: conn, admin: admin} do
  280. conn = get(conn, "/api/pleroma/admin/reports")
  281. response = json_response_and_validate_schema(conn, 200)
  282. notes = hd(response["reports"])["notes"]
  283. [note, _] = notes
  284. assert note["user"]["nickname"] == admin.nickname
  285. assert note["content"] == "this is disgusting!"
  286. assert note["created_at"]
  287. assert response["total"] == 1
  288. end
  289. test "it deletes the note", %{conn: conn, report_id: report_id} do
  290. assert ReportNote |> Repo.all() |> length() == 2
  291. assert [note, _] = Repo.all(ReportNote)
  292. delete(conn, "/api/pleroma/admin/reports/#{report_id}/notes/#{note.id}")
  293. assert ReportNote |> Repo.all() |> length() == 1
  294. end
  295. end
  296. end