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.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
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 |> put_req_header("content-type", "application/json")
29 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
32 assert result["content"] == "Hallo!!"
33 assert result["chat_id"] == chat.id |> to_string()
34 assert_schema(result, "ChatMessageResponse", ApiSpec.spec())
38 describe "GET /api/v1/pleroma/chats/:id/messages" do
39 setup do: oauth_access(["read:statuses"])
41 test "it paginates", %{conn: conn, user: user} do
42 recipient = insert(:user)
44 Enum.each(1..30, fn _ ->
45 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
48 chat = Chat.get(user.id, recipient.ap_id)
52 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
55 assert length(result) == 20
56 assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
60 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
63 assert length(result) == 10
64 assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
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)
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?")
76 chat = Chat.get(user.id, other_user.ap_id)
80 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
84 |> Enum.each(fn message ->
85 assert message["chat_id"] == chat.id |> to_string()
88 assert length(result) == 3
89 assert_schema(result, "ChatMessagesResponse", ApiSpec.spec())
91 # Trying to get the chat of a different user
94 |> assign(:user, other_user)
95 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
97 assert result |> json_response(404)
101 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
102 setup do: oauth_access(["write:statuses"])
104 test "it creates or returns a chat", %{conn: conn} do
105 other_user = insert(:user)
109 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
110 |> json_response(200)
113 assert_schema(result, "ChatResponse", ApiSpec.spec())
117 describe "GET /api/v1/pleroma/chats" do
118 setup do: oauth_access(["read:statuses"])
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)
128 |> get("/api/v1/pleroma/chats")
129 |> json_response(200)
131 assert length(result) == 20
132 assert_schema(result, "ChatsResponse", ApiSpec.spec())
136 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
137 |> json_response(200)
139 assert length(result) == 10
141 assert_schema(result, "ChatsResponse", ApiSpec.spec())
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
147 jafnhar = insert(:user)
148 tridi = insert(:user)
150 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
152 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
154 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
157 # bump the second one
158 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
162 |> get("/api/v1/pleroma/chats")
163 |> json_response(200)
165 ids = Enum.map(result, & &1["id"])
168 chat_2.id |> to_string(),
169 chat_3.id |> to_string(),
170 chat_1.id |> to_string()
173 assert_schema(result, "ChatsResponse", ApiSpec.spec())
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)
184 test "Chats example matches schema" do
185 api_spec = ApiSpec.spec()
186 schema = ChatsResponse.schema()
187 assert_schema(schema.example, "ChatsResponse", api_spec)
190 test "ChatMessage example matches schema" do
191 api_spec = ApiSpec.spec()
192 schema = ChatMessageResponse.schema()
193 assert_schema(schema.example, "ChatMessageResponse", api_spec)
196 test "ChatsMessage example matches schema" do
197 api_spec = ApiSpec.spec()
198 schema = ChatMessagesResponse.schema()
199 assert_schema(schema.example, "ChatMessagesResponse", api_spec)