Rename SubscriptionNotificationController list and get actions to index and show
[akkoma] / test / web / pleroma_api / subscription_notification_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Repo
9 alias Pleroma.SubscriptionNotification
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
13 import Tesla.Mock
14
15 setup do
16 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
20 clear_config([:instance, :public])
21 clear_config([:rich_media, :enabled])
22
23 describe "subscription_notifications" do
24 setup do
25 user = insert(:user)
26 subscriber = insert(:user)
27
28 User.subscribe(subscriber, user)
29
30 {:ok, %{user: user, subscriber: subscriber}}
31 end
32
33 test "list of notifications", %{conn: conn, user: user, subscriber: subscriber} do
34 status_text = "Hello"
35 {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text})
36 path = subscription_notification_path(conn, :index)
37
38 conn =
39 conn
40 |> assign(:user, subscriber)
41 |> get(path)
42
43 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
44 assert response == status_text
45 end
46
47 test "getting a single notification", %{conn: conn, user: user, subscriber: subscriber} do
48 status_text = "Hello"
49
50 {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text})
51 [notification] = Repo.all(SubscriptionNotification)
52
53 path = subscription_notification_path(conn, :show, notification)
54
55 conn =
56 conn
57 |> assign(:user, subscriber)
58 |> get(path)
59
60 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
61 assert response == status_text
62 end
63
64 test "dismissing a single notification also deletes it", %{
65 conn: conn,
66 user: user,
67 subscriber: subscriber
68 } do
69 status_text = "Hello"
70 {:ok, _activity} = CommonAPI.post(user, %{"status" => status_text})
71
72 [notification] = Repo.all(SubscriptionNotification)
73
74 conn =
75 conn
76 |> assign(:user, subscriber)
77 |> post(subscription_notification_path(conn, :dismiss), %{"id" => notification.id})
78
79 assert %{} = json_response(conn, 200)
80
81 assert Repo.all(SubscriptionNotification) == []
82 end
83
84 test "clearing all notifications also deletes them", %{
85 conn: conn,
86 user: user,
87 subscriber: subscriber
88 } do
89 status_text1 = "Hello"
90 status_text2 = "Hello again"
91 {:ok, _activity1} = CommonAPI.post(user, %{"status" => status_text1})
92 {:ok, _activity2} = CommonAPI.post(user, %{"status" => status_text2})
93
94 conn =
95 conn
96 |> assign(:user, subscriber)
97 |> post(subscription_notification_path(conn, :clear))
98
99 assert %{} = json_response(conn, 200)
100
101 conn =
102 build_conn()
103 |> assign(:user, subscriber)
104 |> get(subscription_notification_path(conn, :index))
105
106 assert json_response(conn, 200) == []
107
108 assert Repo.all(SubscriptionNotification) == []
109 end
110
111 test "paginates notifications using min_id, since_id, max_id, and limit", %{
112 conn: conn,
113 user: user,
114 subscriber: subscriber
115 } do
116 {:ok, activity1} = CommonAPI.post(user, %{"status" => "Hello 1"})
117 {:ok, activity2} = CommonAPI.post(user, %{"status" => "Hello 2"})
118 {:ok, activity3} = CommonAPI.post(user, %{"status" => "Hello 3"})
119 {:ok, activity4} = CommonAPI.post(user, %{"status" => "Hello 4"})
120
121 notification1_id =
122 Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string()
123
124 notification2_id =
125 Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string()
126
127 notification3_id =
128 Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string()
129
130 notification4_id =
131 Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string()
132
133 conn = assign(conn, :user, subscriber)
134
135 # min_id
136 conn_res =
137 get(
138 conn,
139 subscription_notification_path(conn, :index, %{
140 "limit" => 2,
141 "min_id" => notification1_id
142 })
143 )
144
145 result = json_response(conn_res, 200)
146 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
147
148 # since_id
149 conn_res =
150 get(
151 conn,
152 subscription_notification_path(conn, :index, %{
153 "limit" => 2,
154 "since_id" => notification1_id
155 })
156 )
157
158 result = json_response(conn_res, 200)
159 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
160
161 # max_id
162 conn_res =
163 get(
164 conn,
165 subscription_notification_path(conn, :index, %{
166 "limit" => 2,
167 "max_id" => notification4_id
168 })
169 )
170
171 result = json_response(conn_res, 200)
172 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
173 end
174
175 test "destroy multiple", %{conn: conn, user: user1, subscriber: user2} do
176 # mutual subscription
177 User.subscribe(user1, user2)
178
179 {:ok, activity1} = CommonAPI.post(user1, %{"status" => "Hello 1"})
180 {:ok, activity2} = CommonAPI.post(user1, %{"status" => "World 1"})
181 {:ok, activity3} = CommonAPI.post(user2, %{"status" => "Hello 2"})
182 {:ok, activity4} = CommonAPI.post(user2, %{"status" => "World 2"})
183
184 notification1_id =
185 Repo.get_by(SubscriptionNotification, activity_id: activity1.id).id |> to_string()
186
187 notification2_id =
188 Repo.get_by(SubscriptionNotification, activity_id: activity2.id).id |> to_string()
189
190 notification3_id =
191 Repo.get_by(SubscriptionNotification, activity_id: activity3.id).id |> to_string()
192
193 notification4_id =
194 Repo.get_by(SubscriptionNotification, activity_id: activity4.id).id |> to_string()
195
196 conn = assign(conn, :user, user1)
197
198 conn_res = get(conn, subscription_notification_path(conn, :index))
199
200 result = json_response(conn_res, 200)
201
202 Enum.each(result, fn %{"id" => id} ->
203 assert id in [notification3_id, notification4_id]
204 end)
205
206 conn2 = assign(conn, :user, user2)
207
208 conn_res = get(conn2, subscription_notification_path(conn, :index))
209
210 result = json_response(conn_res, 200)
211
212 Enum.each(result, fn %{"id" => id} ->
213 assert id in [notification1_id, notification2_id]
214 end)
215
216 conn_destroy =
217 delete(conn, subscription_notification_path(conn, :destroy_multiple), %{
218 "ids" => [notification3_id, notification4_id]
219 })
220
221 assert json_response(conn_destroy, 200) == %{}
222
223 conn_res = get(conn2, subscription_notification_path(conn, :index))
224
225 result = json_response(conn_res, 200)
226
227 Enum.each(result, fn %{"id" => id} ->
228 assert id in [notification1_id, notification2_id]
229 end)
230
231 assert length(Repo.all(SubscriptionNotification)) == 2
232 end
233 end
234 end