Ensure all supported push notification subscription alert types are tested
[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 "successful creation", %{conn: conn} do
58 result =
59 conn
60 |> post("/api/v1/push/subscription", %{
61 "data" => %{
62 "alerts" => %{
63 "mention" => true,
64 "favourite" => true,
65 "follow" => true,
66 "reblog" => true,
67 "pleroma:chat_mention" => true,
68 "pleroma:emoji_reaction" => true,
69 "test" => true
70 }
71 },
72 "subscription" => @sub
73 })
74 |> json_response_and_validate_schema(200)
75
76 [subscription] = Pleroma.Repo.all(Subscription)
77
78 assert %{
79 "alerts" => %{
80 "mention" => true,
81 "favourite" => true,
82 "follow" => true,
83 "reblog" => true,
84 "pleroma:chat_mention" => true,
85 "pleroma:emoji_reaction" => true
86 },
87 "endpoint" => subscription.endpoint,
88 "id" => to_string(subscription.id),
89 "server_key" => @server_key
90 } == result
91 end
92 end
93
94 describe "gets a user subscription" do
95 test "returns error when push disabled ", %{conn: conn} do
96 assert_error_when_disable_push do
97 conn
98 |> get("/api/v1/push/subscription", %{})
99 |> json_response_and_validate_schema(403)
100 end
101 end
102
103 test "returns error when user hasn't subscription", %{conn: conn} do
104 res =
105 conn
106 |> get("/api/v1/push/subscription", %{})
107 |> json_response_and_validate_schema(404)
108
109 assert %{"error" => "Record not found"} == res
110 end
111
112 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
113 subscription =
114 insert(:push_subscription,
115 user: user,
116 token: token,
117 data: %{"alerts" => %{"mention" => true}}
118 )
119
120 res =
121 conn
122 |> get("/api/v1/push/subscription", %{})
123 |> json_response_and_validate_schema(200)
124
125 expect = %{
126 "alerts" => %{"mention" => true},
127 "endpoint" => "https://example.com/example/1234",
128 "id" => to_string(subscription.id),
129 "server_key" => @server_key
130 }
131
132 assert expect == res
133 end
134 end
135
136 describe "updates a user subsciption" do
137 setup %{conn: conn, user: user, token: token} do
138 subscription =
139 insert(:push_subscription,
140 user: user,
141 token: token,
142 data: %{
143 "alerts" => %{
144 "mention" => true,
145 "favourite" => true,
146 "follow" => true,
147 "reblog" => true,
148 "pleroma:chat_mention" => true,
149 "pleroma:emoji_reaction" => true
150 }
151 }
152 )
153
154 %{conn: conn, user: user, token: token, subscription: subscription}
155 end
156
157 test "returns error when push disabled ", %{conn: conn} do
158 assert_error_when_disable_push do
159 conn
160 |> put("/api/v1/push/subscription", %{
161 data: %{
162 "mention" => false,
163 "favourite" => false,
164 "follow" => false,
165 "reblog" => false,
166 "pleroma:chat_mention" => false,
167 "pleroma:emoji_reaction" => false
168 }
169 })
170 |> json_response_and_validate_schema(403)
171 end
172 end
173
174 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
175 res =
176 conn
177 |> put("/api/v1/push/subscription", %{
178 data: %{
179 "alerts" => %{
180 "mention" => false,
181 "favourite" => false,
182 "follow" => false,
183 "reblog" => false,
184 "pleroma:chat_mention" => false,
185 "pleroma:emoji_reaction" => false
186 }
187 }
188 })
189 |> json_response_and_validate_schema(200)
190
191 expect = %{
192 "alerts" => %{
193 "mention" => false,
194 "favourite" => false,
195 "follow" => false,
196 "reblog" => false,
197 "pleroma:chat_mention" => false,
198 "pleroma:emoji_reaction" => false
199 },
200 "endpoint" => "https://example.com/example/1234",
201 "id" => to_string(subscription.id),
202 "server_key" => @server_key
203 }
204
205 assert expect == res
206 end
207 end
208
209 describe "deletes the user subscription" do
210 test "returns error when push disabled ", %{conn: conn} do
211 assert_error_when_disable_push do
212 conn
213 |> delete("/api/v1/push/subscription", %{})
214 |> json_response_and_validate_schema(403)
215 end
216 end
217
218 test "returns error when user hasn't subscription", %{conn: conn} do
219 res =
220 conn
221 |> delete("/api/v1/push/subscription", %{})
222 |> json_response_and_validate_schema(404)
223
224 assert %{"error" => "Record not found"} == res
225 end
226
227 test "returns empty result and delete user subsciption", %{
228 conn: conn,
229 user: user,
230 token: token
231 } do
232 subscription =
233 insert(:push_subscription,
234 user: user,
235 token: token,
236 data: %{"alerts" => %{"mention" => true}}
237 )
238
239 res =
240 conn
241 |> delete("/api/v1/push/subscription", %{})
242 |> json_response_and_validate_schema(200)
243
244 assert %{} == res
245 refute Pleroma.Repo.get(Subscription, subscription.id)
246 end
247 end
248 end