logo

pleroma

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

report_controller_test.exs (5456B)


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