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/messages" do
13 setup do: oauth_access(["write:statuses"])
15 test "it posts a message to the chat", %{conn: conn, user: user} do
16 other_user = insert(:user)
18 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
22 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
25 assert result["content"] == "Hallo!!"
26 assert result["chat_id"] == chat.id |> to_string()
30 describe "GET /api/v1/pleroma/chats/:id/messages" do
31 setup do: oauth_access(["read:statuses"])
33 test "it paginates", %{conn: conn, user: user} do
34 recipient = insert(:user)
36 Enum.each(1..30, fn _ ->
37 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
40 chat = Chat.get(user.id, recipient.ap_id)
44 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
47 assert length(result) == 20
51 |> get("/api/v1/pleroma/chats/#{chat.id}/messages", %{"max_id" => List.last(result)["id"]})
54 assert length(result) == 10
57 test "it returns the messages for a given chat", %{conn: conn, user: user} do
58 other_user = insert(:user)
59 third_user = insert(:user)
61 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
62 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
63 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
64 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
66 chat = Chat.get(user.id, other_user.ap_id)
70 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
74 |> Enum.each(fn message ->
75 assert message["chat_id"] == chat.id |> to_string()
78 assert length(result) == 3
80 # Trying to get the chat of a different user
83 |> assign(:user, other_user)
84 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
86 assert result |> json_response(404)
90 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
91 setup do: oauth_access(["write:statuses"])
93 test "it creates or returns a chat", %{conn: conn} do
94 other_user = insert(:user)
98 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
105 describe "GET /api/v1/pleroma/chats" do
106 setup do: oauth_access(["read:statuses"])
108 test "it paginates", %{conn: conn, user: user} do
109 Enum.each(1..30, fn _ ->
110 recipient = insert(:user)
111 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
116 |> get("/api/v1/pleroma/chats")
117 |> json_response(200)
119 assert length(result) == 20
123 |> get("/api/v1/pleroma/chats", %{max_id: List.last(result)["id"]})
124 |> json_response(200)
126 assert length(result) == 10
129 test "it return a list of chats the current user is participating in, in descending order of updates",
130 %{conn: conn, user: user} do
132 jafnhar = insert(:user)
133 tridi = insert(:user)
135 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
137 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
139 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
142 # bump the second one
143 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
147 |> get("/api/v1/pleroma/chats")
148 |> json_response(200)
150 ids = Enum.map(result, & &1["id"])
153 chat_2.id |> to_string(),
154 chat_3.id |> to_string(),
155 chat_1.id |> to_string()