Test that we ignore invalid subscription alert types separately.
[akkoma] / test / pleroma / web / mastodon_api / controllers / subscription_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
5 defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
6 use Pleroma.Web.ConnCase
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 "creates push subscription" do
49 test "returns error when push disabled ", %{conn: conn} do
50 assert_error_when_disable_push do
51 conn
52 |> post("/api/v1/push/subscription", %{subscription: @sub})
53 |> json_response_and_validate_schema(403)
54 end
55 end
56
57 test "ignores unsupported types", %{conn: conn} do
58 result =
59 conn
60 |> post("/api/v1/push/subscription", %{
61 "data" => %{
62 "alerts" => %{
63 "fake_unsupported_type" => true
64 }
65 },
66 "subscription" => @sub
67 })
68 |> json_response_and_validate_schema(200)
69
70 refute %{
71 "alerts" => %{
72 "fake_unsupported_type" => true
73 }
74 } == result
75 end
76
77 test "successful creation", %{conn: conn} do
78 result =
79 conn
80 |> post("/api/v1/push/subscription", %{
81 "data" => %{
82 "alerts" => %{
83 "mention" => true,
84 "favourite" => true,
85 "follow" => true,
86 "reblog" => true,
87 "pleroma:chat_mention" => true,
88 "pleroma:emoji_reaction" => true
89 }
90 },
91 "subscription" => @sub
92 })
93 |> json_response_and_validate_schema(200)
94
95 [subscription] = Pleroma.Repo.all(Subscription)
96
97 assert %{
98 "alerts" => %{
99 "mention" => true,
100 "favourite" => true,
101 "follow" => true,
102 "reblog" => true,
103 "pleroma:chat_mention" => true,
104 "pleroma:emoji_reaction" => true
105 },
106 "endpoint" => subscription.endpoint,
107 "id" => to_string(subscription.id),
108 "server_key" => @server_key
109 } == result
110 end
111 end
112
113 describe "gets a user subscription" do
114 test "returns error when push disabled ", %{conn: conn} do
115 assert_error_when_disable_push do
116 conn
117 |> get("/api/v1/push/subscription", %{})
118 |> json_response_and_validate_schema(403)
119 end
120 end
121
122 test "returns error when user hasn't subscription", %{conn: conn} do
123 res =
124 conn
125 |> get("/api/v1/push/subscription", %{})
126 |> json_response_and_validate_schema(404)
127
128 assert %{"error" => "Record not found"} == res
129 end
130
131 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
132 subscription =
133 insert(:push_subscription,
134 user: user,
135 token: token,
136 data: %{"alerts" => %{"mention" => true}}
137 )
138
139 res =
140 conn
141 |> get("/api/v1/push/subscription", %{})
142 |> json_response_and_validate_schema(200)
143
144 expect = %{
145 "alerts" => %{"mention" => true},
146 "endpoint" => "https://example.com/example/1234",
147 "id" => to_string(subscription.id),
148 "server_key" => @server_key
149 }
150
151 assert expect == res
152 end
153 end
154
155 describe "updates a user subsciption" do
156 setup %{conn: conn, user: user, token: token} do
157 subscription =
158 insert(:push_subscription,
159 user: user,
160 token: token,
161 data: %{
162 "alerts" => %{
163 "mention" => true,
164 "favourite" => true,
165 "follow" => true,
166 "reblog" => true,
167 "pleroma:chat_mention" => true,
168 "pleroma:emoji_reaction" => true
169 }
170 }
171 )
172
173 %{conn: conn, user: user, token: token, subscription: subscription}
174 end
175
176 test "returns error when push disabled ", %{conn: conn} do
177 assert_error_when_disable_push do
178 conn
179 |> put("/api/v1/push/subscription", %{
180 data: %{
181 "mention" => false,
182 "favourite" => false,
183 "follow" => false,
184 "reblog" => false,
185 "pleroma:chat_mention" => false,
186 "pleroma:emoji_reaction" => false
187 }
188 })
189 |> json_response_and_validate_schema(403)
190 end
191 end
192
193 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
194 res =
195 conn
196 |> put("/api/v1/push/subscription", %{
197 data: %{
198 "alerts" => %{
199 "mention" => false,
200 "favourite" => false,
201 "follow" => false,
202 "reblog" => false,
203 "pleroma:chat_mention" => 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:chat_mention" => false,
217 "pleroma:emoji_reaction" => false
218 },
219 "endpoint" => "https://example.com/example/1234",
220 "id" => to_string(subscription.id),
221 "server_key" => @server_key
222 }
223
224 assert expect == res
225 end
226 end
227
228 describe "deletes the user subscription" do
229 test "returns error when push disabled ", %{conn: conn} do
230 assert_error_when_disable_push do
231 conn
232 |> delete("/api/v1/push/subscription", %{})
233 |> json_response_and_validate_schema(403)
234 end
235 end
236
237 test "returns error when user hasn't subscription", %{conn: conn} do
238 res =
239 conn
240 |> delete("/api/v1/push/subscription", %{})
241 |> json_response_and_validate_schema(404)
242
243 assert %{"error" => "Record not found"} == res
244 end
245
246 test "returns empty result and delete user subsciption", %{
247 conn: conn,
248 user: user,
249 token: token
250 } do
251 subscription =
252 insert(:push_subscription,
253 user: user,
254 token: token,
255 data: %{"alerts" => %{"mention" => true}}
256 )
257
258 res =
259 conn
260 |> delete("/api/v1/push/subscription", %{})
261 |> json_response_and_validate_schema(200)
262
263 assert %{} == res
264 refute Pleroma.Repo.get(Subscription, subscription.id)
265 end
266 end
267 end