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