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