logo

pleroma

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

chat_controller_test.exs (6570B)


  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.ChatControllerTest do
  5. use Pleroma.Web.ConnCase
  6. import Pleroma.Factory
  7. alias Pleroma.Chat
  8. alias Pleroma.Chat.MessageReference
  9. alias Pleroma.Config
  10. alias Pleroma.ModerationLog
  11. alias Pleroma.Object
  12. alias Pleroma.Repo
  13. alias Pleroma.Web.CommonAPI
  14. defp admin_setup do
  15. admin = insert(:user, is_admin: true)
  16. token = insert(:oauth_admin_token, user: admin)
  17. conn =
  18. build_conn()
  19. |> assign(:user, admin)
  20. |> assign(:token, token)
  21. {:ok, %{admin: admin, token: token, conn: conn}}
  22. end
  23. describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
  24. setup do: admin_setup()
  25. test "it deletes a message from the chat", %{conn: conn, admin: admin} do
  26. user = insert(:user)
  27. recipient = insert(:user)
  28. {:ok, message} =
  29. CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
  30. object = Object.normalize(message, false)
  31. chat = Chat.get(user.id, recipient.ap_id)
  32. recipient_chat = Chat.get(recipient.id, user.ap_id)
  33. cm_ref = MessageReference.for_chat_and_object(chat, object)
  34. recipient_cm_ref = MessageReference.for_chat_and_object(recipient_chat, object)
  35. result =
  36. conn
  37. |> put_req_header("content-type", "application/json")
  38. |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
  39. |> json_response_and_validate_schema(200)
  40. log_entry = Repo.one(ModerationLog)
  41. assert ModerationLog.get_log_entry_message(log_entry) ==
  42. "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
  43. assert result["id"] == cm_ref.id
  44. refute MessageReference.get_by_id(cm_ref.id)
  45. refute MessageReference.get_by_id(recipient_cm_ref.id)
  46. assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
  47. end
  48. end
  49. describe "GET /api/pleroma/admin/chats/:id/messages" do
  50. setup do: admin_setup()
  51. test "it paginates", %{conn: conn} do
  52. user = insert(:user)
  53. recipient = insert(:user)
  54. Enum.each(1..30, fn _ ->
  55. {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
  56. end)
  57. chat = Chat.get(user.id, recipient.ap_id)
  58. result =
  59. conn
  60. |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
  61. |> json_response_and_validate_schema(200)
  62. assert length(result) == 20
  63. result =
  64. conn
  65. |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
  66. |> json_response_and_validate_schema(200)
  67. assert length(result) == 10
  68. end
  69. test "it returns the messages for a given chat", %{conn: conn} do
  70. user = insert(:user)
  71. other_user = insert(:user)
  72. third_user = insert(:user)
  73. {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
  74. {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
  75. {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
  76. {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
  77. chat = Chat.get(user.id, other_user.ap_id)
  78. result =
  79. conn
  80. |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
  81. |> json_response_and_validate_schema(200)
  82. result
  83. |> Enum.each(fn message ->
  84. assert message["chat_id"] == chat.id |> to_string()
  85. end)
  86. assert length(result) == 3
  87. end
  88. end
  89. describe "GET /api/pleroma/admin/chats/:id" do
  90. setup do: admin_setup()
  91. test "it returns a chat", %{conn: conn} do
  92. user = insert(:user)
  93. other_user = insert(:user)
  94. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  95. result =
  96. conn
  97. |> get("/api/pleroma/admin/chats/#{chat.id}")
  98. |> json_response_and_validate_schema(200)
  99. assert result["id"] == to_string(chat.id)
  100. assert %{} = result["sender"]
  101. assert %{} = result["receiver"]
  102. refute result["account"]
  103. end
  104. end
  105. describe "unauthorized chat moderation" do
  106. setup do
  107. user = insert(:user)
  108. recipient = insert(:user)
  109. {:ok, message} = CommonAPI.post_chat_message(user, recipient, "Yo")
  110. object = Object.normalize(message, false)
  111. chat = Chat.get(user.id, recipient.ap_id)
  112. cm_ref = MessageReference.for_chat_and_object(chat, object)
  113. %{conn: conn} = oauth_access(["read:chats", "write:chats"])
  114. %{conn: conn, chat: chat, cm_ref: cm_ref}
  115. end
  116. test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
  117. conn: conn,
  118. chat: chat,
  119. cm_ref: cm_ref
  120. } do
  121. conn
  122. |> put_req_header("content-type", "application/json")
  123. |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
  124. |> json_response(403)
  125. assert MessageReference.get_by_id(cm_ref.id) == cm_ref
  126. end
  127. test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
  128. conn
  129. |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
  130. |> json_response(403)
  131. end
  132. test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
  133. conn
  134. |> get("/api/pleroma/admin/chats/#{chat.id}")
  135. |> json_response(403)
  136. end
  137. end
  138. describe "unauthenticated chat moderation" do
  139. setup do
  140. user = insert(:user)
  141. recipient = insert(:user)
  142. {:ok, message} = CommonAPI.post_chat_message(user, recipient, "Yo")
  143. object = Object.normalize(message, false)
  144. chat = Chat.get(user.id, recipient.ap_id)
  145. cm_ref = MessageReference.for_chat_and_object(chat, object)
  146. %{conn: build_conn(), chat: chat, cm_ref: cm_ref}
  147. end
  148. test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
  149. conn: conn,
  150. chat: chat,
  151. cm_ref: cm_ref
  152. } do
  153. conn
  154. |> put_req_header("content-type", "application/json")
  155. |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
  156. |> json_response(403)
  157. assert MessageReference.get_by_id(cm_ref.id) == cm_ref
  158. end
  159. test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
  160. conn
  161. |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
  162. |> json_response(403)
  163. end
  164. test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
  165. conn
  166. |> get("/api/pleroma/admin/chats/#{chat.id}")
  167. |> json_response(403)
  168. end
  169. end
  170. end