f61e2a1fa01f4e6a79d927fa580bf725ecde355d
[akkoma] / test / 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
7
8 import Pleroma.Factory
9
10 alias Pleroma.Activity
11 alias Pleroma.Chat
12 alias Pleroma.Config
13 alias Pleroma.ModerationLog
14 alias Pleroma.Repo
15 alias Pleroma.Web.CommonAPI
16
17 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
31 chat = insert(:chat)
32 message = insert(:chat_message_activity, chat: chat)
33 %{chat: chat, message: message}
34 end
35
36 test "deletes chat message", %{conn: conn, chat: chat, message: message, admin: admin} do
37 conn
38 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{message.id}")
39 |> json_response_and_validate_schema(:ok)
40
41 refute Activity.get_by_id(message.id)
42
43 log_entry = Repo.one(ModerationLog)
44
45 assert ModerationLog.get_log_entry_message(log_entry) ==
46 "@#{admin.nickname} deleted chat message ##{message.id}"
47 end
48
49 test "returns 404 when the chat message does not exist", %{conn: conn} do
50 conn = delete(conn, "/api/pleroma/admin/chats/test/messages/test")
51
52 assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
53 end
54 end
55
56 describe "GET /api/pleroma/admin/chats/:id/messages" do
57 test "it paginates", %{conn: conn} do
58 user = insert(:user)
59 recipient = insert(:user)
60
61 Enum.each(1..30, fn _ ->
62 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
63 end)
64
65 chat = Chat.get(user.id, recipient.ap_id)
66
67 result =
68 conn
69 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
70 |> json_response_and_validate_schema(200)
71
72 assert length(result) == 20
73
74 result =
75 conn
76 |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
77 |> json_response_and_validate_schema(200)
78
79 assert length(result) == 10
80 end
81
82 test "it returns the messages for a given chat", %{conn: conn} do
83 user = insert(:user)
84 other_user = insert(:user)
85 third_user = insert(:user)
86
87 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
88 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
89 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
90 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
91
92 chat = Chat.get(user.id, other_user.ap_id)
93
94 result =
95 conn
96 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
97 |> json_response_and_validate_schema(200)
98
99 result
100 |> Enum.each(fn message ->
101 assert message["chat_id"] == chat.id |> to_string()
102 end)
103
104 assert length(result) == 3
105 end
106 end
107 end