ChatController: Basic message posting.
[akkoma] / test / web / pleroma_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 defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
5 use Pleroma.Web.ConnCase, async: true
6
7 alias Pleroma.Chat
8 alias Pleroma.Web.CommonAPI
9
10 import Pleroma.Factory
11
12 describe "POST /api/v1/pleroma/chats/:id/messages" do
13 test "it posts a message to the chat", %{conn: conn} do
14 user = insert(:user)
15 other_user = insert(:user)
16
17 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
18
19 result =
20 conn
21 |> assign(:user, user)
22 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
23 |> json_response(200)
24
25 assert result["content"] == "Hallo!!"
26 end
27 end
28
29 describe "GET /api/v1/pleroma/chats/:id/messages" do
30 # TODO
31 # - Test that statuses don't show
32 # - Test the case where it's not the user's chat
33 # - Test the returned data
34 test "it returns the messages for a given chat", %{conn: conn} do
35 user = insert(:user)
36 other_user = insert(:user)
37 third_user = insert(:user)
38
39 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
40 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
41 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
42 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
43
44 chat = Chat.get(user.id, other_user.ap_id)
45
46 result =
47 conn
48 |> assign(:user, user)
49 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
50 |> json_response(200)
51
52 assert length(result) == 3
53 end
54 end
55
56 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
57 test "it creates or returns a chat", %{conn: conn} do
58 user = insert(:user)
59 other_user = insert(:user)
60
61 result =
62 conn
63 |> assign(:user, user)
64 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
65 |> json_response(200)
66
67 assert result["id"]
68 end
69 end
70
71 describe "GET /api/v1/pleroma/chats" do
72 test "it return a list of chats the current user is participating in, in descending order of updates",
73 %{conn: conn} do
74 user = insert(:user)
75 har = insert(:user)
76 jafnhar = insert(:user)
77 tridi = insert(:user)
78
79 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
80 :timer.sleep(1000)
81 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
82 :timer.sleep(1000)
83 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
84 :timer.sleep(1000)
85
86 # bump the second one
87 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
88
89 result =
90 conn
91 |> assign(:user, user)
92 |> get("/api/v1/pleroma/chats")
93 |> json_response(200)
94
95 ids = Enum.map(result, & &1["id"])
96
97 assert ids == [chat_2.id, chat_3.id, chat_1.id]
98 end
99 end
100 end