1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
6 use Pleroma.Web.ConnCase, async: false
10 alias Pleroma.Web.Push
11 alias Pleroma.Web.Push.Subscription
14 "endpoint" => "https://example.com/example/1234",
16 "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
18 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
21 @server_key Keyword.get(Push.vapid_config(), :public_key)
25 token = insert(:oauth_token, user: user, scopes: ["push"])
29 |> assign(:user, user)
30 |> assign(:token, token)
31 |> put_req_header("content-type", "application/json")
33 %{conn: conn, user: user, token: token}
36 defmacro assert_error_when_disable_push(do: yield) do
38 assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
43 describe "when disabled" do
45 vapid_config = Application.get_env(:web_push_encryption, :vapid_details)
47 Application.put_env(:web_push_encryption, :vapid_details, [])
49 on_exit(fn -> Application.put_env(:web_push_encryption, :vapid_details, vapid_config) end)
52 test "POST returns error", %{conn: conn} do
53 assert_error_when_disable_push do
55 |> post("/api/v1/push/subscription", %{
56 "data" => %{"alerts" => %{"mention" => true}},
57 "subscription" => @sub
59 |> json_response_and_validate_schema(403)
63 test "GET returns error", %{conn: conn} do
64 assert_error_when_disable_push do
66 |> get("/api/v1/push/subscription", %{})
67 |> json_response_and_validate_schema(403)
71 test "PUT returns error", %{conn: conn} do
72 assert_error_when_disable_push do
74 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
75 |> json_response_and_validate_schema(403)
79 test "DELETE returns error", %{conn: conn} do
80 assert_error_when_disable_push do
82 |> delete("/api/v1/push/subscription", %{})
83 |> json_response_and_validate_schema(403)
88 describe "creates push subscription" do
89 test "ignores unsupported types", %{conn: conn} do
92 |> post("/api/v1/push/subscription", %{
95 "fake_unsupported_type" => true
98 "subscription" => @sub
100 |> json_response_and_validate_schema(200)
104 "fake_unsupported_type" => true
109 test "successful creation", %{conn: conn} do
112 |> post("/api/v1/push/subscription", %{
119 "pleroma:chat_mention" => true,
120 "pleroma:emoji_reaction" => true
123 "subscription" => @sub
125 |> json_response_and_validate_schema(200)
127 [subscription] = Pleroma.Repo.all(Subscription)
135 "pleroma:chat_mention" => true,
136 "pleroma:emoji_reaction" => true
138 "endpoint" => subscription.endpoint,
139 "id" => to_string(subscription.id),
140 "server_key" => @server_key
145 describe "gets a user subscription" do
146 test "returns error when user hasn't subscription", %{conn: conn} do
149 |> get("/api/v1/push/subscription", %{})
150 |> json_response_and_validate_schema(404)
152 assert %{"error" => "Record not found"} == res
155 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
157 insert(:push_subscription,
160 data: %{"alerts" => %{"mention" => true}}
165 |> get("/api/v1/push/subscription", %{})
166 |> json_response_and_validate_schema(200)
169 "alerts" => %{"mention" => true},
170 "endpoint" => "https://example.com/example/1234",
171 "id" => to_string(subscription.id),
172 "server_key" => @server_key
179 describe "updates a user subsciption" do
180 setup %{conn: conn, user: user, token: token} do
182 insert(:push_subscription,
191 "pleroma:chat_mention" => true,
192 "pleroma:emoji_reaction" => true
197 %{conn: conn, user: user, token: token, subscription: subscription}
200 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
203 |> put("/api/v1/push/subscription", %{
207 "favourite" => false,
210 "pleroma:chat_mention" => false,
211 "pleroma:emoji_reaction" => false
215 |> json_response_and_validate_schema(200)
220 "favourite" => false,
223 "pleroma:chat_mention" => false,
224 "pleroma:emoji_reaction" => false
226 "endpoint" => "https://example.com/example/1234",
227 "id" => to_string(subscription.id),
228 "server_key" => @server_key
235 describe "deletes the user subscription" do
236 test "returns error when user hasn't subscription", %{conn: conn} do
239 |> delete("/api/v1/push/subscription", %{})
240 |> json_response_and_validate_schema(404)
242 assert %{"error" => "Record not found"} == res
245 test "returns empty result and delete user subsciption", %{
251 insert(:push_subscription,
254 data: %{"alerts" => %{"mention" => true}}
259 |> delete("/api/v1/push/subscription", %{})
260 |> json_response_and_validate_schema(200)
263 refute Pleroma.Repo.get(Subscription, subscription.id)