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
10 alias Pleroma.Activity
13 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
32 message = insert(:chat_message_activity, chat: chat)
33 %{chat: chat, message: message}
36 test "deletes chat message", %{conn: conn, chat: chat, message: message, admin: admin} do
38 |> delete("/api/pleroma/admin/chats/#{chat.id}/messages/#{message.id}")
39 |> json_response_and_validate_schema(:ok)
41 refute Activity.get_by_id(message.id)
43 log_entry = Repo.one(ModerationLog)
45 assert ModerationLog.get_log_entry_message(log_entry) ==
46 "@#{admin.nickname} deleted chat message ##{message.id}"
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")
52 assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
56 describe "GET /api/pleroma/admin/chats/:id/messages" do
57 test "it paginates", %{conn: conn} do
59 recipient = insert(:user)
61 Enum.each(1..30, fn _ ->
62 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
65 chat = Chat.get(user.id, recipient.ap_id)
69 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
70 |> json_response_and_validate_schema(200)
72 assert length(result) == 20
76 |> get("/api/pleroma/admin/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
77 |> json_response_and_validate_schema(200)
79 assert length(result) == 10
82 test "it returns the messages for a given chat", %{conn: conn} do
84 other_user = insert(:user)
85 third_user = insert(:user)
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?")
92 chat = Chat.get(user.id, other_user.ap_id)
96 |> get("/api/pleroma/admin/chats/#{chat.id}/messages")
97 |> json_response_and_validate_schema(200)
100 |> Enum.each(fn message ->
101 assert message["chat_id"] == chat.id |> to_string()
104 assert length(result) == 3