purge chat and shout endpoints
[akkoma] / test / pleroma / web / mastodon_api / controllers / subscription_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7
8 import Pleroma.Factory
9
10 alias Pleroma.Web.Push
11 alias Pleroma.Web.Push.Subscription
12
13 @sub %{
14 "endpoint" => "https://example.com/example/1234",
15 "keys" => %{
16 "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
17 "p256dh" =>
18 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
19 }
20 }
21 @server_key Keyword.get(Push.vapid_config(), :public_key)
22
23 setup do
24 user = insert(:user)
25 token = insert(:oauth_token, user: user, scopes: ["push"])
26
27 conn =
28 build_conn()
29 |> assign(:user, user)
30 |> assign(:token, token)
31 |> put_req_header("content-type", "application/json")
32
33 %{conn: conn, user: user, token: token}
34 end
35
36 defmacro assert_error_when_disable_push(do: yield) do
37 quote do
38 vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
39 Application.put_env(:web_push_encryption, :vapid_details, [])
40
41 assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
42 unquote(yield)
43
44 Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
45 end
46 end
47
48 describe "when disabled" do
49 test "POST returns error", %{conn: conn} do
50 assert_error_when_disable_push do
51 conn
52 |> post("/api/v1/push/subscription", %{
53 "data" => %{"alerts" => %{"mention" => true}},
54 "subscription" => @sub
55 })
56 |> json_response_and_validate_schema(403)
57 end
58 end
59
60 test "GET returns error", %{conn: conn} do
61 assert_error_when_disable_push do
62 conn
63 |> get("/api/v1/push/subscription", %{})
64 |> json_response_and_validate_schema(403)
65 end
66 end
67
68 test "PUT returns error", %{conn: conn} do
69 assert_error_when_disable_push do
70 conn
71 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
72 |> json_response_and_validate_schema(403)
73 end
74 end
75
76 test "DELETE returns error", %{conn: conn} do
77 assert_error_when_disable_push do
78 conn
79 |> delete("/api/v1/push/subscription", %{})
80 |> json_response_and_validate_schema(403)
81 end
82 end
83 end
84
85 describe "creates push subscription" do
86 test "ignores unsupported types", %{conn: conn} do
87 result =
88 conn
89 |> post("/api/v1/push/subscription", %{
90 "data" => %{
91 "alerts" => %{
92 "fake_unsupported_type" => true
93 }
94 },
95 "subscription" => @sub
96 })
97 |> json_response_and_validate_schema(200)
98
99 refute %{
100 "alerts" => %{
101 "fake_unsupported_type" => true
102 }
103 } == result
104 end
105
106 test "successful creation", %{conn: conn} do
107 result =
108 conn
109 |> post("/api/v1/push/subscription", %{
110 "data" => %{
111 "alerts" => %{
112 "mention" => true,
113 "favourite" => true,
114 "follow" => true,
115 "reblog" => true,
116 "pleroma:emoji_reaction" => true
117 }
118 },
119 "subscription" => @sub
120 })
121 |> json_response_and_validate_schema(200)
122
123 [subscription] = Pleroma.Repo.all(Subscription)
124
125 assert %{
126 "alerts" => %{
127 "mention" => true,
128 "favourite" => true,
129 "follow" => true,
130 "reblog" => true,
131 "pleroma:emoji_reaction" => true
132 },
133 "endpoint" => subscription.endpoint,
134 "id" => to_string(subscription.id),
135 "server_key" => @server_key
136 } == result
137 end
138 end
139
140 describe "gets a user subscription" do
141 test "returns error when user hasn't subscription", %{conn: conn} do
142 res =
143 conn
144 |> get("/api/v1/push/subscription", %{})
145 |> json_response_and_validate_schema(404)
146
147 assert %{"error" => "Record not found"} == res
148 end
149
150 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
151 subscription =
152 insert(:push_subscription,
153 user: user,
154 token: token,
155 data: %{"alerts" => %{"mention" => true}}
156 )
157
158 res =
159 conn
160 |> get("/api/v1/push/subscription", %{})
161 |> json_response_and_validate_schema(200)
162
163 expect = %{
164 "alerts" => %{"mention" => true},
165 "endpoint" => "https://example.com/example/1234",
166 "id" => to_string(subscription.id),
167 "server_key" => @server_key
168 }
169
170 assert expect == res
171 end
172 end
173
174 describe "updates a user subsciption" do
175 setup %{conn: conn, user: user, token: token} do
176 subscription =
177 insert(:push_subscription,
178 user: user,
179 token: token,
180 data: %{
181 "alerts" => %{
182 "mention" => true,
183 "favourite" => true,
184 "follow" => true,
185 "reblog" => true,
186 "pleroma:emoji_reaction" => true
187 }
188 }
189 )
190
191 %{conn: conn, user: user, token: token, subscription: subscription}
192 end
193
194 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
195 res =
196 conn
197 |> put("/api/v1/push/subscription", %{
198 data: %{
199 "alerts" => %{
200 "mention" => false,
201 "favourite" => false,
202 "follow" => false,
203 "reblog" => false,
204 "pleroma:emoji_reaction" => false
205 }
206 }
207 })
208 |> json_response_and_validate_schema(200)
209
210 expect = %{
211 "alerts" => %{
212 "mention" => false,
213 "favourite" => false,
214 "follow" => false,
215 "reblog" => false,
216 "pleroma:emoji_reaction" => false
217 },
218 "endpoint" => "https://example.com/example/1234",
219 "id" => to_string(subscription.id),
220 "server_key" => @server_key
221 }
222
223 assert expect == res
224 end
225 end
226
227 describe "deletes the user subscription" do
228 test "returns error when user hasn't subscription", %{conn: conn} do
229 res =
230 conn
231 |> delete("/api/v1/push/subscription", %{})
232 |> json_response_and_validate_schema(404)
233
234 assert %{"error" => "Record not found"} == res
235 end
236
237 test "returns empty result and delete user subsciption", %{
238 conn: conn,
239 user: user,
240 token: token
241 } do
242 subscription =
243 insert(:push_subscription,
244 user: user,
245 token: token,
246 data: %{"alerts" => %{"mention" => true}}
247 )
248
249 res =
250 conn
251 |> delete("/api/v1/push/subscription", %{})
252 |> json_response_and_validate_schema(200)
253
254 assert %{} == res
255 refute Pleroma.Repo.get(Subscription, subscription.id)
256 end
257 end
258 end