logo

pleroma

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

status_controller_test.exs (6564B)


  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.StatusControllerTest 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.User
  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/statuses/:id" do
  23. test "not found", %{conn: conn} do
  24. assert conn
  25. |> get("/api/pleroma/admin/statuses/not_found")
  26. |> json_response_and_validate_schema(:not_found)
  27. end
  28. test "shows activity", %{conn: conn} do
  29. activity = insert(:note_activity)
  30. response =
  31. conn
  32. |> get("/api/pleroma/admin/statuses/#{activity.id}")
  33. |> json_response_and_validate_schema(200)
  34. assert response["id"] == activity.id
  35. account = response["account"]
  36. actor = User.get_by_ap_id(activity.actor)
  37. assert account["id"] == actor.id
  38. assert account["nickname"] == actor.nickname
  39. assert account["deactivated"] == actor.deactivated
  40. assert account["confirmation_pending"] == actor.confirmation_pending
  41. end
  42. end
  43. describe "PUT /api/pleroma/admin/statuses/:id" do
  44. setup do
  45. activity = insert(:note_activity)
  46. %{id: activity.id}
  47. end
  48. test "toggle sensitive flag", %{conn: conn, id: id, admin: admin} do
  49. response =
  50. conn
  51. |> put_req_header("content-type", "application/json")
  52. |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "true"})
  53. |> json_response_and_validate_schema(:ok)
  54. assert response["sensitive"]
  55. log_entry = Repo.one(ModerationLog)
  56. assert ModerationLog.get_log_entry_message(log_entry) ==
  57. "@#{admin.nickname} updated status ##{id}, set sensitive: 'true'"
  58. response =
  59. conn
  60. |> put_req_header("content-type", "application/json")
  61. |> put("/api/pleroma/admin/statuses/#{id}", %{"sensitive" => "false"})
  62. |> json_response_and_validate_schema(:ok)
  63. refute response["sensitive"]
  64. end
  65. test "change visibility flag", %{conn: conn, id: id, admin: admin} do
  66. response =
  67. conn
  68. |> put_req_header("content-type", "application/json")
  69. |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "public"})
  70. |> json_response_and_validate_schema(:ok)
  71. assert response["visibility"] == "public"
  72. log_entry = Repo.one(ModerationLog)
  73. assert ModerationLog.get_log_entry_message(log_entry) ==
  74. "@#{admin.nickname} updated status ##{id}, set visibility: 'public'"
  75. response =
  76. conn
  77. |> put_req_header("content-type", "application/json")
  78. |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "private"})
  79. |> json_response_and_validate_schema(:ok)
  80. assert response["visibility"] == "private"
  81. response =
  82. conn
  83. |> put_req_header("content-type", "application/json")
  84. |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "unlisted"})
  85. |> json_response_and_validate_schema(:ok)
  86. assert response["visibility"] == "unlisted"
  87. end
  88. test "returns 400 when visibility is unknown", %{conn: conn, id: id} do
  89. conn =
  90. conn
  91. |> put_req_header("content-type", "application/json")
  92. |> put("/api/pleroma/admin/statuses/#{id}", %{visibility: "test"})
  93. assert %{"error" => "test - Invalid value for enum."} =
  94. json_response_and_validate_schema(conn, :bad_request)
  95. end
  96. end
  97. describe "DELETE /api/pleroma/admin/statuses/:id" do
  98. setup do
  99. activity = insert(:note_activity)
  100. %{id: activity.id}
  101. end
  102. test "deletes status", %{conn: conn, id: id, admin: admin} do
  103. conn
  104. |> delete("/api/pleroma/admin/statuses/#{id}")
  105. |> json_response_and_validate_schema(:ok)
  106. refute Activity.get_by_id(id)
  107. log_entry = Repo.one(ModerationLog)
  108. assert ModerationLog.get_log_entry_message(log_entry) ==
  109. "@#{admin.nickname} deleted status ##{id}"
  110. end
  111. test "returns 404 when the status does not exist", %{conn: conn} do
  112. conn = delete(conn, "/api/pleroma/admin/statuses/test")
  113. assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
  114. end
  115. end
  116. describe "GET /api/pleroma/admin/statuses" do
  117. test "returns all public and unlisted statuses", %{conn: conn, admin: admin} do
  118. blocked = insert(:user)
  119. user = insert(:user)
  120. User.block(admin, blocked)
  121. {:ok, _} = CommonAPI.post(user, %{status: "@#{admin.nickname}", visibility: "direct"})
  122. {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "unlisted"})
  123. {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "private"})
  124. {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "public"})
  125. {:ok, _} = CommonAPI.post(blocked, %{status: ".", visibility: "public"})
  126. response =
  127. conn
  128. |> get("/api/pleroma/admin/statuses")
  129. |> json_response_and_validate_schema(200)
  130. refute "private" in Enum.map(response, & &1["visibility"])
  131. assert length(response) == 3
  132. end
  133. test "returns only local statuses with local_only on", %{conn: conn} do
  134. user = insert(:user)
  135. remote_user = insert(:user, local: false, nickname: "archaeme@archae.me")
  136. insert(:note_activity, user: user, local: true)
  137. insert(:note_activity, user: remote_user, local: false)
  138. response =
  139. conn
  140. |> get("/api/pleroma/admin/statuses?local_only=true")
  141. |> json_response_and_validate_schema(200)
  142. assert length(response) == 1
  143. end
  144. test "returns private and direct statuses with godmode on", %{conn: conn, admin: admin} do
  145. user = insert(:user)
  146. {:ok, _} = CommonAPI.post(user, %{status: "@#{admin.nickname}", visibility: "direct"})
  147. {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "private"})
  148. {:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "public"})
  149. conn = get(conn, "/api/pleroma/admin/statuses?godmode=true")
  150. assert json_response_and_validate_schema(conn, 200) |> length() == 3
  151. end
  152. end
  153. end