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.ActivityPub.ActivityPub
9 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
13 describe "POST /api/v1/pleroma/chats/:id/read" do
14 setup do: oauth_access(["write:statuses"])
16 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
17 other_user = insert(:user)
19 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
21 assert chat.unread == 1
25 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
26 |> json_response_and_validate_schema(200)
28 assert result["unread"] == 0
30 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
32 assert chat.unread == 0
36 describe "POST /api/v1/pleroma/chats/:id/messages" do
37 setup do: oauth_access(["write:statuses"])
39 test "it posts a message to the chat", %{conn: conn, user: user} do
40 other_user = insert(:user)
42 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
46 |> put_req_header("content-type", "application/json")
47 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
48 |> json_response_and_validate_schema(200)
50 assert result["content"] == "Hallo!!"
51 assert result["chat_id"] == chat.id |> to_string()
54 test "it works with an attachment", %{conn: conn, user: user} do
56 content_type: "image/jpg",
57 path: Path.absname("test/fixtures/image.jpg"),
58 filename: "an_image.jpg"
61 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
63 other_user = insert(:user)
65 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
69 |> put_req_header("content-type", "application/json")
70 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
71 "content" => "Hallo!!",
72 "media_id" => to_string(upload.id)
74 |> json_response_and_validate_schema(200)
76 assert result["content"] == "Hallo!!"
77 assert result["chat_id"] == chat.id |> to_string()
81 describe "GET /api/v1/pleroma/chats/:id/messages" do
82 setup do: oauth_access(["read:statuses"])
84 test "it paginates", %{conn: conn, user: user} do
85 recipient = insert(:user)
87 Enum.each(1..30, fn _ ->
88 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
91 chat = Chat.get(user.id, recipient.ap_id)
95 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
96 |> json_response_and_validate_schema(200)
98 assert length(result) == 20
102 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
103 |> json_response_and_validate_schema(200)
105 assert length(result) == 10
108 test "it returns the messages for a given chat", %{conn: conn, user: user} do
109 other_user = insert(:user)
110 third_user = insert(:user)
112 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
113 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
114 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
115 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
117 chat = Chat.get(user.id, other_user.ap_id)
121 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
122 |> json_response_and_validate_schema(200)
125 |> Enum.each(fn message ->
126 assert message["chat_id"] == chat.id |> to_string()
129 assert length(result) == 3
131 # Trying to get the chat of a different user
134 |> assign(:user, other_user)
135 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
137 assert result |> json_response(404)
141 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
142 setup do: oauth_access(["write:statuses"])
144 test "it creates or returns a chat", %{conn: conn} do
145 other_user = insert(:user)
149 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
150 |> json_response_and_validate_schema(200)
156 describe "GET /api/v1/pleroma/chats" do
157 setup do: oauth_access(["read:statuses"])
159 test "it paginates", %{conn: conn, user: user} do
160 Enum.each(1..30, fn _ ->
161 recipient = insert(:user)
162 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
167 |> get("/api/v1/pleroma/chats")
168 |> json_response_and_validate_schema(200)
170 assert length(result) == 20
174 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
175 |> json_response_and_validate_schema(200)
177 assert length(result) == 10
180 test "it return a list of chats the current user is participating in, in descending order of updates",
181 %{conn: conn, user: user} do
183 jafnhar = insert(:user)
184 tridi = insert(:user)
186 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
188 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
190 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
193 # bump the second one
194 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
198 |> get("/api/v1/pleroma/chats")
199 |> json_response_and_validate_schema(200)
201 ids = Enum.map(result, & &1["id"])
204 chat_2.id |> to_string(),
205 chat_3.id |> to_string(),
206 chat_1.id |> to_string()