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", %{
67 "pleroma:chat_mention" => true,
68 "pleroma:emoji_reaction" => true,
72 "subscription" => @sub
74 |> json_response_and_validate_schema(200)
76 [subscription] = Pleroma.Repo.all(Subscription)
84 "pleroma:chat_mention" => true,
85 "pleroma:emoji_reaction" => true
87 "endpoint" => subscription.endpoint,
88 "id" => to_string(subscription.id),
89 "server_key" => @server_key
94 describe "gets a user subscription" do
95 test "returns error when push disabled ", %{conn: conn} do
96 assert_error_when_disable_push do
98 |> get("/api/v1/push/subscription", %{})
99 |> json_response_and_validate_schema(403)
103 test "returns error when user hasn't subscription", %{conn: conn} do
106 |> get("/api/v1/push/subscription", %{})
107 |> json_response_and_validate_schema(404)
109 assert %{"error" => "Record not found"} == res
112 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
114 insert(:push_subscription,
117 data: %{"alerts" => %{"mention" => true}}
122 |> get("/api/v1/push/subscription", %{})
123 |> json_response_and_validate_schema(200)
126 "alerts" => %{"mention" => true},
127 "endpoint" => "https://example.com/example/1234",
128 "id" => to_string(subscription.id),
129 "server_key" => @server_key
136 describe "updates a user subsciption" do
137 setup %{conn: conn, user: user, token: token} do
139 insert(:push_subscription,
148 "pleroma:chat_mention" => true,
149 "pleroma:emoji_reaction" => true
154 %{conn: conn, user: user, token: token, subscription: subscription}
157 test "returns error when push disabled ", %{conn: conn} do
158 assert_error_when_disable_push do
160 |> put("/api/v1/push/subscription", %{
163 "favourite" => false,
166 "pleroma:chat_mention" => false,
167 "pleroma:emoji_reaction" => false
170 |> json_response_and_validate_schema(403)
174 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
177 |> put("/api/v1/push/subscription", %{
181 "favourite" => false,
184 "pleroma:chat_mention" => false,
185 "pleroma:emoji_reaction" => false
189 |> json_response_and_validate_schema(200)
194 "favourite" => false,
197 "pleroma:chat_mention" => false,
198 "pleroma:emoji_reaction" => false
200 "endpoint" => "https://example.com/example/1234",
201 "id" => to_string(subscription.id),
202 "server_key" => @server_key
209 describe "deletes the user subscription" do
210 test "returns error when push disabled ", %{conn: conn} do
211 assert_error_when_disable_push do
213 |> delete("/api/v1/push/subscription", %{})
214 |> json_response_and_validate_schema(403)
218 test "returns error when user hasn't subscription", %{conn: conn} do
221 |> delete("/api/v1/push/subscription", %{})
222 |> json_response_and_validate_schema(404)
224 assert %{"error" => "Record not found"} == res
227 test "returns empty result and delete user subsciption", %{
233 insert(:push_subscription,
236 data: %{"alerts" => %{"mention" => true}}
241 |> delete("/api/v1/push/subscription", %{})
242 |> json_response_and_validate_schema(200)
245 refute Pleroma.Repo.get(Subscription, subscription.id)