1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
6 use Pleroma.Web.ConnCase
11 alias Pleroma.Chat.MessageReference
13 alias Pleroma.ModerationLog
16 alias Pleroma.Web.CommonAPI
19 admin = insert(:user, is_admin: true)
20 token = insert(:oauth_admin_token, user: admin)
24 |> assign(:user, admin)
25 |> assign(:token, token)
27 {:ok, %{admin: admin, token: token, conn: conn}}
30 describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
31 setup do: admin_setup()
33 test "it deletes a message from the chat", %{conn: conn, admin: admin} do
35 recipient = insert(:user)
38 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
40 object = Object.normalize(message, false)
42 chat = Chat.get(user.id, recipient.ap_id)
44 cm_ref = MessageReference.for_chat_and_object(chat, object)
48 |> put_req_header("content-type", "application/json")
49 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
50 |> json_response_and_validate_schema(200)
52 log_entry = Repo.one(ModerationLog)
54 assert ModerationLog.get_log_entry_message(log_entry) ==
55 "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
57 assert result["id"] == cm_ref.id
58 refute MessageReference.get_by_id(cm_ref.id)
59 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
63 describe "GET /api/pleroma/admin/chats/:id/messages" do
64 setup do: admin_setup()
66 test "it paginates", %{conn: conn} do
68 recipient = insert(:user)
70 Enum.each(1..30, fn _ ->
71 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
74 chat = Chat.get(user.id, recipient.ap_id)
78 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
79 |> json_response_and_validate_schema(200)
81 assert length(result) == 20
85 |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
86 |> json_response_and_validate_schema(200)
88 assert length(result) == 10
91 test "it returns the messages for a given chat", %{conn: conn} do
93 other_user = insert(:user)
94 third_user = insert(:user)
96 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
97 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
98 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
99 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
101 chat = Chat.get(user.id, other_user.ap_id)
105 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
106 |> json_response_and_validate_schema(200)
109 |> Enum.each(fn message ->
110 assert message["chat_id"] == chat.id |> to_string()
113 assert length(result) == 3
117 describe "GET /api/pleroma/admin/chats/:id" do
118 setup do: admin_setup()
120 test "it returns a chat", %{conn: conn} do
122 other_user = insert(:user)
124 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
128 |> get("/api/pleroma/admin/chats/#{chat.id}")
129 |> json_response_and_validate_schema(200)
131 assert result["id"] == to_string(chat.id)
132 assert %{} = result["sender"]
133 assert %{} = result["receiver"]
134 refute result["account"]
138 describe "unauthorized chat moderation" do
141 recipient = insert(:user)
143 {:ok, message} = CommonAPI.post_chat_message(user, recipient, "Yo")
144 object = Object.normalize(message, false)
145 chat = Chat.get(user.id, recipient.ap_id)
146 cm_ref = MessageReference.for_chat_and_object(chat, object)
148 %{conn: conn} = oauth_access(["read:chats", "write:chats"])
149 %{conn: conn, chat: chat, cm_ref: cm_ref}
152 test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
158 |> put_req_header("content-type", "application/json")
159 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
160 |> json_response(403)
162 assert MessageReference.get_by_id(cm_ref.id) == cm_ref
165 test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
167 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
168 |> json_response(403)
171 test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
173 |> get("/api/pleroma/admin/chats/#{chat.id}")
174 |> json_response(403)
178 describe "unauthenticated chat moderation" do
181 recipient = insert(:user)
183 {:ok, message} = CommonAPI.post_chat_message(user, recipient, "Yo")
184 object = Object.normalize(message, false)
185 chat = Chat.get(user.id, recipient.ap_id)
186 cm_ref = MessageReference.for_chat_and_object(chat, object)
188 %{conn: build_conn(), chat: chat, cm_ref: cm_ref}
191 test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
197 |> put_req_header("content-type", "application/json")
198 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
199 |> json_response(403)
201 assert MessageReference.get_by_id(cm_ref.id) == cm_ref
204 test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
206 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
207 |> json_response(403)
210 test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
212 |> get("/api/pleroma/admin/chats/#{chat.id}")
213 |> json_response(403)