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, async: true
11 alias Pleroma.Chat.MessageReference
12 alias Pleroma.ModerationLog
15 alias Pleroma.Web.CommonAPI
18 admin = insert(:user, is_admin: true)
19 token = insert(:oauth_admin_token, user: admin)
23 |> assign(:user, admin)
24 |> assign(:token, token)
26 {:ok, %{admin: admin, token: token, conn: conn}}
29 describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
30 setup do: admin_setup()
32 test "it deletes a message from the chat", %{conn: conn, admin: admin} do
34 recipient = insert(:user)
37 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
39 object = Object.normalize(message, false)
41 chat = Chat.get(user.id, recipient.ap_id)
42 recipient_chat = Chat.get(recipient.id, user.ap_id)
44 cm_ref = MessageReference.for_chat_and_object(chat, object)
45 recipient_cm_ref = MessageReference.for_chat_and_object(recipient_chat, object)
49 |> put_req_header("content-type", "application/json")
50 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
51 |> json_response_and_validate_schema(200)
53 log_entry = Repo.one(ModerationLog)
55 assert ModerationLog.get_log_entry_message(log_entry) ==
56 "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
58 assert result["id"] == cm_ref.id
59 refute MessageReference.get_by_id(cm_ref.id)
60 refute MessageReference.get_by_id(recipient_cm_ref.id)
61 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
65 describe "GET /api/pleroma/admin/chats/:id/messages" do
66 setup do: admin_setup()
68 test "it paginates", %{conn: conn} do
70 recipient = insert(:user)
72 Enum.each(1..30, fn _ ->
73 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
76 chat = Chat.get(user.id, recipient.ap_id)
80 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
81 |> json_response_and_validate_schema(200)
83 assert length(result) == 20
87 |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
88 |> json_response_and_validate_schema(200)
90 assert length(result) == 10
93 test "it returns the messages for a given chat", %{conn: conn} do
95 other_user = insert(:user)
96 third_user = insert(:user)
98 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
99 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
100 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
101 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
103 chat = Chat.get(user.id, other_user.ap_id)
107 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
108 |> json_response_and_validate_schema(200)
111 |> Enum.each(fn message ->
112 assert message["chat_id"] == chat.id |> to_string()
115 assert length(result) == 3
119 describe "GET /api/pleroma/admin/chats/:id" do
120 setup do: admin_setup()
122 test "it returns a chat", %{conn: conn} do
124 other_user = insert(:user)
126 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
130 |> get("/api/pleroma/admin/chats/#{chat.id}")
131 |> json_response_and_validate_schema(200)
133 assert result["id"] == to_string(chat.id)
134 assert %{} = result["sender"]
135 assert %{} = result["receiver"]
136 refute result["account"]
140 describe "unauthorized chat moderation" do
143 recipient = insert(:user)
145 {:ok, message} = CommonAPI.post_chat_message(user, recipient, "Yo")
146 object = Object.normalize(message, false)
147 chat = Chat.get(user.id, recipient.ap_id)
148 cm_ref = MessageReference.for_chat_and_object(chat, object)
150 %{conn: conn} = oauth_access(["read:chats", "write:chats"])
151 %{conn: conn, chat: chat, cm_ref: cm_ref}
154 test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
160 |> put_req_header("content-type", "application/json")
161 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
162 |> json_response(403)
164 assert MessageReference.get_by_id(cm_ref.id) == cm_ref
167 test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
169 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
170 |> json_response(403)
173 test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
175 |> get("/api/pleroma/admin/chats/#{chat.id}")
176 |> json_response(403)
180 describe "unauthenticated chat moderation" do
183 recipient = insert(:user)
185 {:ok, message} = CommonAPI.post_chat_message(user, recipient, "Yo")
186 object = Object.normalize(message, false)
187 chat = Chat.get(user.id, recipient.ap_id)
188 cm_ref = MessageReference.for_chat_and_object(chat, object)
190 %{conn: build_conn(), chat: chat, cm_ref: cm_ref}
193 test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
199 |> put_req_header("content-type", "application/json")
200 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
201 |> json_response(403)
203 assert MessageReference.get_by_id(cm_ref.id) == cm_ref
206 test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
208 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
209 |> json_response(403)
212 test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
214 |> get("/api/pleroma/admin/chats/#{chat.id}")
215 |> json_response(403)