Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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/read" do
13 setup do: oauth_access(["write:statuses"])
14
15 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
16 other_user = insert(:user)
17
18 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
19
20 assert chat.unread == 1
21
22 result =
23 conn
24 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
25 |> json_response_and_validate_schema(200)
26
27 assert result["unread"] == 0
28
29 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
30
31 assert chat.unread == 0
32 end
33 end
34
35 describe "POST /api/v1/pleroma/chats/:id/messages" do
36 setup do: oauth_access(["write:statuses"])
37
38 test "it posts a message to the chat", %{conn: conn, user: user} do
39 other_user = insert(:user)
40
41 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
42
43 result =
44 conn
45 |> put_req_header("content-type", "application/json")
46 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
47 |> json_response_and_validate_schema(200)
48
49 assert result["content"] == "Hallo!!"
50 assert result["chat_id"] == chat.id |> to_string()
51 end
52 end
53
54 describe "GET /api/v1/pleroma/chats/:id/messages" do
55 setup do: oauth_access(["read:statuses"])
56
57 test "it paginates", %{conn: conn, user: user} do
58 recipient = insert(:user)
59
60 Enum.each(1..30, fn _ ->
61 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
62 end)
63
64 chat = Chat.get(user.id, recipient.ap_id)
65
66 result =
67 conn
68 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
69 |> json_response_and_validate_schema(200)
70
71 assert length(result) == 20
72
73 result =
74 conn
75 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
76 |> json_response_and_validate_schema(200)
77
78 assert length(result) == 10
79 end
80
81 test "it returns the messages for a given chat", %{conn: conn, user: user} do
82 other_user = insert(:user)
83 third_user = insert(:user)
84
85 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
86 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
87 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
88 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
89
90 chat = Chat.get(user.id, other_user.ap_id)
91
92 result =
93 conn
94 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
95 |> json_response_and_validate_schema(200)
96
97 result
98 |> Enum.each(fn message ->
99 assert message["chat_id"] == chat.id |> to_string()
100 end)
101
102 assert length(result) == 3
103
104 # Trying to get the chat of a different user
105 result =
106 conn
107 |> assign(:user, other_user)
108 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
109
110 assert result |> json_response(404)
111 end
112 end
113
114 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
115 setup do: oauth_access(["write:statuses"])
116
117 test "it creates or returns a chat", %{conn: conn} do
118 other_user = insert(:user)
119
120 result =
121 conn
122 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
123 |> json_response_and_validate_schema(200)
124
125 assert result["id"]
126 end
127 end
128
129 describe "GET /api/v1/pleroma/chats" do
130 setup do: oauth_access(["read:statuses"])
131
132 test "it paginates", %{conn: conn, user: user} do
133 Enum.each(1..30, fn _ ->
134 recipient = insert(:user)
135 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
136 end)
137
138 result =
139 conn
140 |> get("/api/v1/pleroma/chats")
141 |> json_response_and_validate_schema(200)
142
143 assert length(result) == 20
144
145 result =
146 conn
147 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
148 |> json_response_and_validate_schema(200)
149
150 assert length(result) == 10
151 end
152
153 test "it return a list of chats the current user is participating in, in descending order of updates",
154 %{conn: conn, user: user} do
155 har = insert(:user)
156 jafnhar = insert(:user)
157 tridi = insert(:user)
158
159 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
160 :timer.sleep(1000)
161 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
162 :timer.sleep(1000)
163 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
164 :timer.sleep(1000)
165
166 # bump the second one
167 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
168
169 result =
170 conn
171 |> get("/api/v1/pleroma/chats")
172 |> json_response_and_validate_schema(200)
173
174 ids = Enum.map(result, & &1["id"])
175
176 assert ids == [
177 chat_2.id |> to_string(),
178 chat_3.id |> to_string(),
179 chat_1.id |> to_string()
180 ]
181 end
182 end
183 end