Merge branch 'fix/mediaproxy-config-descriptions' into 'develop'
[akkoma] / test / pleroma / web / admin_api / controllers / chat_controller_test.exs
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
5 defmodule Pleroma.Web.AdminAPI.ChatControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 import Pleroma.Factory
9
10 alias Pleroma.Chat
11 alias Pleroma.Chat.MessageReference
12 alias Pleroma.ModerationLog
13 alias Pleroma.Object
14 alias Pleroma.Repo
15 alias Pleroma.Web.CommonAPI
16
17 defp admin_setup do
18 admin = insert(:user, is_admin: true)
19 token = insert(:oauth_admin_token, user: admin)
20
21 conn =
22 build_conn()
23 |> assign(:user, admin)
24 |> assign(:token, token)
25
26 {:ok, %{admin: admin, token: token, conn: conn}}
27 end
28
29 describe "DELETE /api/pleroma/admin/chats/:id/messages/:message_id" do
30 setup do: admin_setup()
31
32 test "it deletes a message from the chat", %{conn: conn, admin: admin} do
33 user = insert(:user)
34 recipient = insert(:user)
35
36 {:ok, message} =
37 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
38
39 object = Object.normalize(message, false)
40
41 chat = Chat.get(user.id, recipient.ap_id)
42 recipient_chat = Chat.get(recipient.id, user.ap_id)
43
44 cm_ref = MessageReference.for_chat_and_object(chat, object)
45 recipient_cm_ref = MessageReference.for_chat_and_object(recipient_chat, object)
46
47 result =
48 conn
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)
52
53 log_entry = Repo.one(ModerationLog)
54
55 assert ModerationLog.get_log_entry_message(log_entry) ==
56 "@#{admin.nickname} deleted chat message ##{cm_ref.id}"
57
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)
62 end
63 end
64
65 describe "GET /api/pleroma/admin/chats/:id/messages" do
66 setup do: admin_setup()
67
68 test "it paginates", %{conn: conn} do
69 user = insert(:user)
70 recipient = insert(:user)
71
72 Enum.each(1..30, fn _ ->
73 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
74 end)
75
76 chat = Chat.get(user.id, recipient.ap_id)
77
78 result =
79 conn
80 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
81 |> json_response_and_validate_schema(200)
82
83 assert length(result) == 20
84
85 result =
86 conn
87 |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
88 |> json_response_and_validate_schema(200)
89
90 assert length(result) == 10
91 end
92
93 test "it returns the messages for a given chat", %{conn: conn} do
94 user = insert(:user)
95 other_user = insert(:user)
96 third_user = insert(:user)
97
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?")
102
103 chat = Chat.get(user.id, other_user.ap_id)
104
105 result =
106 conn
107 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
108 |> json_response_and_validate_schema(200)
109
110 result
111 |> Enum.each(fn message ->
112 assert message["chat_id"] == chat.id |> to_string()
113 end)
114
115 assert length(result) == 3
116 end
117 end
118
119 describe "GET /api/pleroma/admin/chats/:id" do
120 setup do: admin_setup()
121
122 test "it returns a chat", %{conn: conn} do
123 user = insert(:user)
124 other_user = insert(:user)
125
126 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
127
128 result =
129 conn
130 |> get("/api/pleroma/admin/chats/#{chat.id}")
131 |> json_response_and_validate_schema(200)
132
133 assert result["id"] == to_string(chat.id)
134 assert %{} = result["sender"]
135 assert %{} = result["receiver"]
136 refute result["account"]
137 end
138 end
139
140 describe "unauthorized chat moderation" do
141 setup do
142 user = insert(:user)
143 recipient = insert(:user)
144
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)
149
150 %{conn: conn} = oauth_access(["read:chats", "write:chats"])
151 %{conn: conn, chat: chat, cm_ref: cm_ref}
152 end
153
154 test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
155 conn: conn,
156 chat: chat,
157 cm_ref: cm_ref
158 } do
159 conn
160 |> put_req_header("content-type", "application/json")
161 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
162 |> json_response(403)
163
164 assert MessageReference.get_by_id(cm_ref.id) == cm_ref
165 end
166
167 test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
168 conn
169 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
170 |> json_response(403)
171 end
172
173 test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
174 conn
175 |> get("/api/pleroma/admin/chats/#{chat.id}")
176 |> json_response(403)
177 end
178 end
179
180 describe "unauthenticated chat moderation" do
181 setup do
182 user = insert(:user)
183 recipient = insert(:user)
184
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)
189
190 %{conn: build_conn(), chat: chat, cm_ref: cm_ref}
191 end
192
193 test "DELETE /api/pleroma/admin/chats/:id/messages/:message_id", %{
194 conn: conn,
195 chat: chat,
196 cm_ref: cm_ref
197 } do
198 conn
199 |> put_req_header("content-type", "application/json")
200 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{cm_ref.id}")
201 |> json_response(403)
202
203 assert MessageReference.get_by_id(cm_ref.id) == cm_ref
204 end
205
206 test "GET /api/pleroma/admin/chats/:id/messages", %{conn: conn, chat: chat} do
207 conn
208 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
209 |> json_response(403)
210 end
211
212 test "GET /api/pleroma/admin/chats/:id", %{conn: conn, chat: chat} do
213 conn
214 |> get("/api/pleroma/admin/chats/#{chat.id}")
215 |> json_response(403)
216 end
217 end
218 end