logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

report_controller_test.exs (4243B)


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