Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
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
6
7 alias Pleroma.Chat
8 alias Pleroma.Web.CommonAPI
9
10 import Pleroma.Factory
11
12 describe "POST /api/v1/pleroma/chats/:id/messages" do
13 setup do: oauth_access(["write:statuses"])
14
15 test "it posts a message to the chat", %{conn: conn, user: user} do
16 other_user = insert(:user)
17
18 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
19
20 result =
21 conn
22 |> put_req_header("content-type", "application/json")
23 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
24 |> json_response_and_validate_schema(200)
25
26 assert result["content"] == "Hallo!!"
27 assert result["chat_id"] == chat.id |> to_string()
28 end
29 end
30
31 describe "GET /api/v1/pleroma/chats/:id/messages" do
32 setup do: oauth_access(["read:statuses"])
33
34 test "it paginates", %{conn: conn, user: user} do
35 recipient = insert(:user)
36
37 Enum.each(1..30, fn _ ->
38 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
39 end)
40
41 chat = Chat.get(user.id, recipient.ap_id)
42
43 result =
44 conn
45 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
46 |> json_response_and_validate_schema(200)
47
48 assert length(result) == 20
49
50 result =
51 conn
52 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
53 |> json_response_and_validate_schema(200)
54
55 assert length(result) == 10
56 end
57
58 test "it returns the messages for a given chat", %{conn: conn, user: user} do
59 other_user = insert(:user)
60 third_user = insert(:user)
61
62 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
63 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
64 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
65 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
66
67 chat = Chat.get(user.id, other_user.ap_id)
68
69 result =
70 conn
71 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
72 |> json_response_and_validate_schema(200)
73
74 result
75 |> Enum.each(fn message ->
76 assert message["chat_id"] == chat.id |> to_string()
77 end)
78
79 assert length(result) == 3
80
81 # Trying to get the chat of a different user
82 result =
83 conn
84 |> assign(:user, other_user)
85 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
86
87 assert result |> json_response(404)
88 end
89 end
90
91 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
92 setup do: oauth_access(["write:statuses"])
93
94 test "it creates or returns a chat", %{conn: conn} do
95 other_user = insert(:user)
96
97 result =
98 conn
99 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
100 |> json_response_and_validate_schema(200)
101
102 assert result["id"]
103 end
104 end
105
106 describe "GET /api/v1/pleroma/chats" do
107 setup do: oauth_access(["read:statuses"])
108
109 test "it paginates", %{conn: conn, user: user} do
110 Enum.each(1..30, fn _ ->
111 recipient = insert(:user)
112 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
113 end)
114
115 result =
116 conn
117 |> get("/api/v1/pleroma/chats")
118 |> json_response_and_validate_schema(200)
119
120 assert length(result) == 20
121
122 result =
123 conn
124 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
125 |> json_response_and_validate_schema(200)
126
127 assert length(result) == 10
128 end
129
130 test "it return a list of chats the current user is participating in, in descending order of updates",
131 %{conn: conn, user: user} do
132 har = insert(:user)
133 jafnhar = insert(:user)
134 tridi = insert(:user)
135
136 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
137 :timer.sleep(1000)
138 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
139 :timer.sleep(1000)
140 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
141 :timer.sleep(1000)
142
143 # bump the second one
144 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
145
146 result =
147 conn
148 |> get("/api/v1/pleroma/chats")
149 |> json_response_and_validate_schema(200)
150
151 ids = Enum.map(result, & &1["id"])
152
153 assert ids == [
154 chat_2.id |> to_string(),
155 chat_3.id |> to_string(),
156 chat_1.id |> to_string()
157 ]
158 end
159 end
160 end