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.CommonAPI
10 import Pleroma.Factory
12 describe "POST /api/v1/pleroma/chats/:id/read" do
13 setup do: oauth_access(["write:statuses"])
15 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
16 other_user = insert(:user)
18 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
20 assert chat.unread == 1
24 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
25 |> json_response_and_validate_schema(200)
27 assert result["unread"] == 0
29 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
31 assert chat.unread == 0
35 describe "POST /api/v1/pleroma/chats/:id/messages" do
36 setup do: oauth_access(["write:statuses"])
38 test "it posts a message to the chat", %{conn: conn, user: user} do
39 other_user = insert(:user)
41 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
45 |> put_req_header("content-type", "application/json")
46 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
47 |> json_response_and_validate_schema(200)
49 assert result["content"] == "Hallo!!"
50 assert result["chat_id"] == chat.id |> to_string()
54 describe "GET /api/v1/pleroma/chats/:id/messages" do
55 setup do: oauth_access(["read:statuses"])
57 test "it paginates", %{conn: conn, user: user} do
58 recipient = insert(:user)
60 Enum.each(1..30, fn _ ->
61 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
64 chat = Chat.get(user.id, recipient.ap_id)
68 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
69 |> json_response_and_validate_schema(200)
71 assert length(result) == 20
75 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
76 |> json_response_and_validate_schema(200)
78 assert length(result) == 10
81 test "it returns the messages for a given chat", %{conn: conn, user: user} do
82 other_user = insert(:user)
83 third_user = insert(:user)
85 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
86 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
87 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
88 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
90 chat = Chat.get(user.id, other_user.ap_id)
94 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
95 |> json_response_and_validate_schema(200)
98 |> Enum.each(fn message ->
99 assert message["chat_id"] == chat.id |> to_string()
102 assert length(result) == 3
104 # Trying to get the chat of a different user
107 |> assign(:user, other_user)
108 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
110 assert result |> json_response(404)
114 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
115 setup do: oauth_access(["write:statuses"])
117 test "it creates or returns a chat", %{conn: conn} do
118 other_user = insert(:user)
122 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
123 |> json_response_and_validate_schema(200)
129 describe "GET /api/v1/pleroma/chats" do
130 setup do: oauth_access(["read:statuses"])
132 test "it paginates", %{conn: conn, user: user} do
133 Enum.each(1..30, fn _ ->
134 recipient = insert(:user)
135 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
140 |> get("/api/v1/pleroma/chats")
141 |> json_response_and_validate_schema(200)
143 assert length(result) == 20
147 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
148 |> json_response_and_validate_schema(200)
150 assert length(result) == 10
153 test "it return a list of chats the current user is participating in, in descending order of updates",
154 %{conn: conn, user: user} do
156 jafnhar = insert(:user)
157 tridi = insert(:user)
159 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
161 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
163 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
166 # bump the second one
167 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
171 |> get("/api/v1/pleroma/chats")
172 |> json_response_and_validate_schema(200)
174 ids = Enum.map(result, & &1["id"])
177 chat_2.id |> to_string(),
178 chat_3.id |> to_string(),
179 chat_1.id |> to_string()