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 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
23 |> json_response(200)
24
25 assert result["content"] == "Hallo!!"
26 assert result["chat_id"] == chat.id |> to_string()
27 end
28 end
29
30 describe "GET /api/v1/pleroma/chats/:id/messages" do
31 setup do: oauth_access(["read:statuses"])
32
33 test "it paginates", %{conn: conn, user: user} do
34 recipient = insert(:user)
35
36 Enum.each(1..30, fn _ ->
37 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
38 end)
39
40 chat = Chat.get(user.id, recipient.ap_id)
41
42 result =
43 conn
44 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
45 |> json_response(200)
46
47 assert length(result) == 20
48
49 result =
50 conn
51 |> get("/api/v1/pleroma/chats/#{chat.id}/messages", %{"max_id" => List.last(result)["id"]})
52 |> json_response(200)
53
54 assert length(result) == 10
55 end
56
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)
60
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?")
65
66 chat = Chat.get(user.id, other_user.ap_id)
67
68 result =
69 conn
70 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
71 |> json_response(200)
72
73 result
74 |> Enum.each(fn message ->
75 assert message["chat_id"] == chat.id |> to_string()
76 end)
77
78 assert length(result) == 3
79
80 # Trying to get the chat of a different user
81 result =
82 conn
83 |> assign(:user, other_user)
84 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
85
86 assert result |> json_response(404)
87 end
88 end
89
90 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
91 setup do: oauth_access(["write:statuses"])
92
93 test "it creates or returns a chat", %{conn: conn} do
94 other_user = insert(:user)
95
96 result =
97 conn
98 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
99 |> json_response(200)
100
101 assert result["id"]
102 end
103 end
104
105 describe "GET /api/v1/pleroma/chats" do
106 setup do: oauth_access(["read:statuses"])
107
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)
112 end)
113
114 result =
115 conn
116 |> get("/api/v1/pleroma/chats")
117 |> json_response(200)
118
119 assert length(result) == 20
120
121 result =
122 conn
123 |> get("/api/v1/pleroma/chats", %{max_id: List.last(result)["id"]})
124 |> json_response(200)
125
126 assert length(result) == 10
127 end
128
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
131 har = insert(:user)
132 jafnhar = insert(:user)
133 tridi = insert(:user)
134
135 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
136 :timer.sleep(1000)
137 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
138 :timer.sleep(1000)
139 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
140 :timer.sleep(1000)
141
142 # bump the second one
143 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
144
145 result =
146 conn
147 |> get("/api/v1/pleroma/chats")
148 |> json_response(200)
149
150 ids = Enum.map(result, & &1["id"])
151
152 assert ids == [
153 chat_2.id |> to_string(),
154 chat_3.id |> to_string(),
155 chat_1.id |> to_string()
156 ]
157 end
158 end
159 end