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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 describe "POST /api/v1/pleroma/chats/:id/read" do
16 setup do: oauth_access(["write:statuses"])
18 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
19 other_user = insert(:user)
21 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
23 assert chat.unread == 1
27 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
28 |> json_response_and_validate_schema(200)
30 assert result["unread"] == 0
32 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
34 assert chat.unread == 0
38 describe "POST /api/v1/pleroma/chats/:id/messages" do
39 setup do: oauth_access(["write:statuses"])
41 test "it posts a message to the chat", %{conn: conn, user: user} do
42 other_user = insert(:user)
44 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
48 |> put_req_header("content-type", "application/json")
49 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
50 |> json_response_and_validate_schema(200)
52 assert result["content"] == "Hallo!!"
53 assert result["chat_id"] == chat.id |> to_string()
56 test "it fails if there is no content", %{conn: conn, user: user} do
57 other_user = insert(:user)
59 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
63 |> put_req_header("content-type", "application/json")
64 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
65 |> json_response_and_validate_schema(400)
70 test "it works with an attachment", %{conn: conn, user: user} do
72 content_type: "image/jpg",
73 path: Path.absname("test/fixtures/image.jpg"),
74 filename: "an_image.jpg"
77 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
79 other_user = insert(:user)
81 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
85 |> put_req_header("content-type", "application/json")
86 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
87 "media_id" => to_string(upload.id)
89 |> json_response_and_validate_schema(200)
91 assert result["attachment"]
95 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
96 setup do: oauth_access(["write:statuses"])
98 test "it deletes a message for the author of the message", %{conn: conn, user: user} do
99 recipient = insert(:user)
102 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
104 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
106 object = Object.normalize(message, false)
108 chat = Chat.get(user.id, recipient.ap_id)
112 |> put_req_header("content-type", "application/json")
113 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
114 |> json_response_and_validate_schema(200)
116 assert result["id"] == to_string(object.id)
118 object = Object.normalize(other_message, false)
122 |> put_req_header("content-type", "application/json")
123 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
124 |> json_response(400)
126 assert result == %{"error" => "could_not_delete"}
130 describe "GET /api/v1/pleroma/chats/:id/messages" do
131 setup do: oauth_access(["read:statuses"])
133 test "it paginates", %{conn: conn, user: user} do
134 recipient = insert(:user)
136 Enum.each(1..30, fn _ ->
137 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
140 chat = Chat.get(user.id, recipient.ap_id)
144 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
145 |> json_response_and_validate_schema(200)
147 assert length(result) == 20
151 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
152 |> json_response_and_validate_schema(200)
154 assert length(result) == 10
157 test "it returns the messages for a given chat", %{conn: conn, user: user} do
158 other_user = insert(:user)
159 third_user = insert(:user)
161 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
162 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
163 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
164 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
166 chat = Chat.get(user.id, other_user.ap_id)
170 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
171 |> json_response_and_validate_schema(200)
174 |> Enum.each(fn message ->
175 assert message["chat_id"] == chat.id |> to_string()
178 assert length(result) == 3
180 # Trying to get the chat of a different user
183 |> assign(:user, other_user)
184 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
186 assert result |> json_response(404)
190 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
191 setup do: oauth_access(["write:statuses"])
193 test "it creates or returns a chat", %{conn: conn} do
194 other_user = insert(:user)
198 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
199 |> json_response_and_validate_schema(200)
205 describe "GET /api/v1/pleroma/chats/:id" do
206 setup do: oauth_access(["read:statuses"])
208 test "it returns a chat", %{conn: conn, user: user} do
209 other_user = insert(:user)
211 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
215 |> get("/api/v1/pleroma/chats/#{chat.id}")
216 |> json_response_and_validate_schema(200)
218 assert result["id"] == to_string(chat.id)
222 describe "GET /api/v1/pleroma/chats" do
223 setup do: oauth_access(["read:statuses"])
225 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
226 recipient = insert(:user)
228 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
232 |> get("/api/v1/pleroma/chats")
233 |> json_response_and_validate_schema(200)
235 assert length(result) == 1
237 User.block(user, recipient)
241 |> get("/api/v1/pleroma/chats")
242 |> json_response_and_validate_schema(200)
244 assert length(result) == 0
247 test "it paginates", %{conn: conn, user: user} do
248 Enum.each(1..30, fn _ ->
249 recipient = insert(:user)
250 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
255 |> get("/api/v1/pleroma/chats")
256 |> json_response_and_validate_schema(200)
258 assert length(result) == 20
262 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
263 |> json_response_and_validate_schema(200)
265 assert length(result) == 10
268 test "it return a list of chats the current user is participating in, in descending order of updates",
269 %{conn: conn, user: user} do
271 jafnhar = insert(:user)
272 tridi = insert(:user)
274 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
276 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
278 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
281 # bump the second one
282 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
286 |> get("/api/v1/pleroma/chats")
287 |> json_response_and_validate_schema(200)
289 ids = Enum.map(result, & &1["id"])
292 chat_2.id |> to_string(),
293 chat_3.id |> to_string(),
294 chat_1.id |> to_string()