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 "creates push subscription" do
49 test "returns error when push disabled ", %{conn: conn} do
50 assert_error_when_disable_push do
52 |> post("/api/v1/push/subscription", %{subscription: @sub})
53 |> json_response_and_validate_schema(403)
57 test "successful creation", %{conn: conn} do
60 |> post("/api/v1/push/subscription", %{
62 "alerts" => %{"mention" => true, "test" => true, "pleroma:chat_mention" => true}
64 "subscription" => @sub
66 |> json_response_and_validate_schema(200)
68 [subscription] = Pleroma.Repo.all(Subscription)
71 "alerts" => %{"mention" => true, "pleroma:chat_mention" => true},
72 "endpoint" => subscription.endpoint,
73 "id" => to_string(subscription.id),
74 "server_key" => @server_key
79 describe "gets a user subscription" do
80 test "returns error when push disabled ", %{conn: conn} do
81 assert_error_when_disable_push do
83 |> get("/api/v1/push/subscription", %{})
84 |> json_response_and_validate_schema(403)
88 test "returns error when user hasn't subscription", %{conn: conn} do
91 |> get("/api/v1/push/subscription", %{})
92 |> json_response_and_validate_schema(404)
94 assert %{"error" => "Record not found"} == res
97 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
99 insert(:push_subscription,
102 data: %{"alerts" => %{"mention" => true}}
107 |> get("/api/v1/push/subscription", %{})
108 |> json_response_and_validate_schema(200)
111 "alerts" => %{"mention" => true},
112 "endpoint" => "https://example.com/example/1234",
113 "id" => to_string(subscription.id),
114 "server_key" => @server_key
121 describe "updates a user subsciption" do
122 setup %{conn: conn, user: user, token: token} do
124 insert(:push_subscription,
127 data: %{"alerts" => %{"mention" => true}}
130 %{conn: conn, user: user, token: token, subscription: subscription}
133 test "returns error when push disabled ", %{conn: conn} do
134 assert_error_when_disable_push do
136 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
137 |> json_response_and_validate_schema(403)
141 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
144 |> put("/api/v1/push/subscription", %{
145 data: %{"alerts" => %{"mention" => false, "follow" => true}}
147 |> json_response_and_validate_schema(200)
150 "alerts" => %{"follow" => true, "mention" => false},
151 "endpoint" => "https://example.com/example/1234",
152 "id" => to_string(subscription.id),
153 "server_key" => @server_key
160 describe "deletes the user subscription" do
161 test "returns error when push disabled ", %{conn: conn} do
162 assert_error_when_disable_push do
164 |> delete("/api/v1/push/subscription", %{})
165 |> json_response_and_validate_schema(403)
169 test "returns error when user hasn't subscription", %{conn: conn} do
172 |> delete("/api/v1/push/subscription", %{})
173 |> json_response_and_validate_schema(404)
175 assert %{"error" => "Record not found"} == res
178 test "returns empty result and delete user subsciption", %{
184 insert(:push_subscription,
187 data: %{"alerts" => %{"mention" => true}}
192 |> delete("/api/v1/push/subscription", %{})
193 |> json_response_and_validate_schema(200)
196 refute Pleroma.Repo.get(Subscription, subscription.id)