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