1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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: true
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:emoji_reaction" => true
119 "subscription" => @sub
121 |> json_response_and_validate_schema(200)
123 [subscription] = Pleroma.Repo.all(Subscription)
131 "pleroma:emoji_reaction" => true
133 "endpoint" => subscription.endpoint,
134 "id" => to_string(subscription.id),
135 "server_key" => @server_key
140 describe "gets a user subscription" do
141 test "returns error when user hasn't subscription", %{conn: conn} do
144 |> get("/api/v1/push/subscription", %{})
145 |> json_response_and_validate_schema(404)
147 assert %{"error" => "Record not found"} == res
150 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
152 insert(:push_subscription,
155 data: %{"alerts" => %{"mention" => true}}
160 |> get("/api/v1/push/subscription", %{})
161 |> json_response_and_validate_schema(200)
164 "alerts" => %{"mention" => true},
165 "endpoint" => "https://example.com/example/1234",
166 "id" => to_string(subscription.id),
167 "server_key" => @server_key
174 describe "updates a user subsciption" do
175 setup %{conn: conn, user: user, token: token} do
177 insert(:push_subscription,
186 "pleroma:emoji_reaction" => true
191 %{conn: conn, user: user, token: token, subscription: subscription}
194 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
197 |> put("/api/v1/push/subscription", %{
201 "favourite" => false,
204 "pleroma:emoji_reaction" => false
208 |> json_response_and_validate_schema(200)
213 "favourite" => false,
216 "pleroma:emoji_reaction" => false
218 "endpoint" => "https://example.com/example/1234",
219 "id" => to_string(subscription.id),
220 "server_key" => @server_key
227 describe "deletes the user subscription" do
228 test "returns error when user hasn't subscription", %{conn: conn} do
231 |> delete("/api/v1/push/subscription", %{})
232 |> json_response_and_validate_schema(404)
234 assert %{"error" => "Record not found"} == res
237 test "returns empty result and delete user subsciption", %{
243 insert(:push_subscription,
246 data: %{"alerts" => %{"mention" => true}}
251 |> delete("/api/v1/push/subscription", %{})
252 |> json_response_and_validate_schema(200)
255 refute Pleroma.Repo.get(Subscription, subscription.id)