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
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
15 import OpenApiSpex.TestAssertions
16 import Pleroma.Factory
18 describe "POST /api/v1/pleroma/chats/:id/messages" do
19 setup do: oauth_access(["write:statuses"])
21 test "it posts a message to the chat", %{conn: conn, user: user} do
22 other_user = insert(:user)
24 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
28 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
31 assert result["content"] == "Hallo!!"
32 assert result["chat_id"] == chat.id |> to_string()
33 assert_schema(result, "ChatMessageResponse", ApiSpec.spec())
37 describe "GET /api/v1/pleroma/chats/:id/messages" do
38 setup do: oauth_access(["read:statuses"])
40 test "it paginates", %{conn: conn, user: user} do
41 recipient = insert(:user)
43 Enum.each(1..30, fn _ ->
44 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
47 chat = Chat.get(user.id, recipient.ap_id)
51 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
54 assert length(result) == 20
55 assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
59 |> get("/api/v1/pleroma/chats/#{chat.id}/messages", %{"max_id" => List.last(result)["id"]})
62 assert length(result) == 10
63 assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
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)
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?")
75 chat = Chat.get(user.id, other_user.ap_id)
79 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
83 |> Enum.each(fn message ->
84 assert message["chat_id"] == chat.id |> to_string()
87 assert length(result) == 3
88 assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
90 # Trying to get the chat of a different user
93 |> assign(:user, other_user)
94 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
96 assert result |> json_response(404)
100 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
101 setup do: oauth_access(["write:statuses"])
103 test "it creates or returns a chat", %{conn: conn} do
104 other_user = insert(:user)
108 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
109 |> json_response(200)
112 assert_schema(result, "ChatResponse", ApiSpec.spec())
116 describe "GET /api/v1/pleroma/chats" do
117 setup do: oauth_access(["read:statuses"])
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)
127 |> get("/api/v1/pleroma/chats")
128 |> json_response(200)
130 assert length(result) == 20
131 assert_schema(result, "ChatsResponse", ApiSpec.spec())
135 |> get("/api/v1/pleroma/chats", %{max_id: List.last(result)["id"]})
136 |> json_response(200)
138 assert length(result) == 10
140 assert_schema(result, "ChatsResponse", ApiSpec.spec())
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
146 jafnhar = insert(:user)
147 tridi = insert(:user)
149 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
151 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
153 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
156 # bump the second one
157 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
161 |> get("/api/v1/pleroma/chats")
162 |> json_response(200)
164 ids = Enum.map(result, & &1["id"])
167 chat_2.id |> to_string(),
168 chat_3.id |> to_string(),
169 chat_1.id |> to_string()
172 assert_schema(result, "ChatsResponse", ApiSpec.spec())
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)
183 test "Chats example matches schema" do
184 api_spec = ApiSpec.spec()
185 schema = ChatsResponse.schema()
186 assert_schema(schema.example, "ChatsResponse", api_spec)
189 test "ChatMessage example matches schema" do
190 api_spec = ApiSpec.spec()
191 schema = ChatMessageResponse.schema()
192 assert_schema(schema.example, "ChatMessageResponse", api_spec)
195 test "ChatsMessage example matches schema" do
196 api_spec = ApiSpec.spec()
197 schema = ChatMessagesResponse.schema()
198 assert_schema(schema.example, "ChatMessagesResponse", api_spec)