1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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 vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
39 Application.put_env(:web_push_encryption, :vapid_details, [])
41 assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
44 Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
48 describe "when disabled" do
49 test "POST returns error", %{conn: conn} do
50 assert_error_when_disable_push do
52 |> post("/api/v1/push/subscription", %{
53 "data" => %{"alerts" => %{"mention" => true}},
54 "subscription" => @sub
56 |> json_response_and_validate_schema(403)
60 test "GET returns error", %{conn: conn} do
61 assert_error_when_disable_push do
63 |> get("/api/v1/push/subscription", %{})
64 |> json_response_and_validate_schema(403)
68 test "PUT returns error", %{conn: conn} do
69 assert_error_when_disable_push do
71 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
72 |> json_response_and_validate_schema(403)
76 test "DELETE returns error", %{conn: conn} do
77 assert_error_when_disable_push do
79 |> delete("/api/v1/push/subscription", %{})
80 |> json_response_and_validate_schema(403)
85 describe "creates push subscription" do
86 test "ignores unsupported types", %{conn: conn} do
89 |> post("/api/v1/push/subscription", %{
92 "fake_unsupported_type" => true
95 "subscription" => @sub
97 |> json_response_and_validate_schema(200)
101 "fake_unsupported_type" => true
106 test "successful creation", %{conn: conn} do
109 |> post("/api/v1/push/subscription", %{
116 "pleroma:chat_mention" => true,
117 "pleroma:emoji_reaction" => true
120 "subscription" => @sub
122 |> json_response_and_validate_schema(200)
124 [subscription] = Pleroma.Repo.all(Subscription)
132 "pleroma:chat_mention" => true,
133 "pleroma:emoji_reaction" => true
135 "endpoint" => subscription.endpoint,
136 "id" => to_string(subscription.id),
137 "server_key" => @server_key
142 describe "gets a user subscription" do
143 test "returns error when user hasn't subscription", %{conn: conn} do
146 |> get("/api/v1/push/subscription", %{})
147 |> json_response_and_validate_schema(404)
149 assert %{"error" => "Record not found"} == res
152 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
154 insert(:push_subscription,
157 data: %{"alerts" => %{"mention" => true}}
162 |> get("/api/v1/push/subscription", %{})
163 |> json_response_and_validate_schema(200)
166 "alerts" => %{"mention" => true},
167 "endpoint" => "https://example.com/example/1234",
168 "id" => to_string(subscription.id),
169 "server_key" => @server_key
176 describe "updates a user subsciption" do
177 setup %{conn: conn, user: user, token: token} do
179 insert(:push_subscription,
188 "pleroma:chat_mention" => true,
189 "pleroma:emoji_reaction" => true
194 %{conn: conn, user: user, token: token, subscription: subscription}
197 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
200 |> put("/api/v1/push/subscription", %{
204 "favourite" => false,
207 "pleroma:chat_mention" => false,
208 "pleroma:emoji_reaction" => false
212 |> json_response_and_validate_schema(200)
217 "favourite" => false,
220 "pleroma:chat_mention" => false,
221 "pleroma:emoji_reaction" => false
223 "endpoint" => "https://example.com/example/1234",
224 "id" => to_string(subscription.id),
225 "server_key" => @server_key
232 describe "deletes the user subscription" do
233 test "returns error when user hasn't subscription", %{conn: conn} do
236 |> delete("/api/v1/push/subscription", %{})
237 |> json_response_and_validate_schema(404)
239 assert %{"error" => "Record not found"} == res
242 test "returns empty result and delete user subsciption", %{
248 insert(:push_subscription,
251 data: %{"alerts" => %{"mention" => true}}
256 |> delete("/api/v1/push/subscription", %{})
257 |> json_response_and_validate_schema(200)
260 refute Pleroma.Repo.get(Subscription, subscription.id)