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", %{
61 "data" => %{"alerts" => %{"mention" => true, "test" => true}},
62 "subscription" => @sub
64 |> json_response_and_validate_schema(200)
66 [subscription] = Pleroma.Repo.all(Subscription)
69 "alerts" => %{"mention" => true},
70 "endpoint" => subscription.endpoint,
71 "id" => to_string(subscription.id),
72 "server_key" => @server_key
77 describe "gets a user subscription" do
78 test "returns error when push disabled ", %{conn: conn} do
79 assert_error_when_disable_push do
81 |> get("/api/v1/push/subscription", %{})
82 |> json_response_and_validate_schema(403)
86 test "returns error when user hasn't subscription", %{conn: conn} do
89 |> get("/api/v1/push/subscription", %{})
90 |> json_response_and_validate_schema(404)
92 assert %{"error" => "Record not found"} == res
95 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
97 insert(:push_subscription,
100 data: %{"alerts" => %{"mention" => true}}
105 |> get("/api/v1/push/subscription", %{})
106 |> json_response_and_validate_schema(200)
109 "alerts" => %{"mention" => true},
110 "endpoint" => "https://example.com/example/1234",
111 "id" => to_string(subscription.id),
112 "server_key" => @server_key
119 describe "updates a user subsciption" do
120 setup %{conn: conn, user: user, token: token} do
122 insert(:push_subscription,
125 data: %{"alerts" => %{"mention" => true}}
128 %{conn: conn, user: user, token: token, subscription: subscription}
131 test "returns error when push disabled ", %{conn: conn} do
132 assert_error_when_disable_push do
134 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
135 |> json_response_and_validate_schema(403)
139 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
142 |> put("/api/v1/push/subscription", %{
143 data: %{"alerts" => %{"mention" => false, "follow" => true}}
145 |> json_response_and_validate_schema(200)
148 "alerts" => %{"follow" => true, "mention" => false},
149 "endpoint" => "https://example.com/example/1234",
150 "id" => to_string(subscription.id),
151 "server_key" => @server_key
158 describe "deletes the user subscription" do
159 test "returns error when push disabled ", %{conn: conn} do
160 assert_error_when_disable_push do
162 |> delete("/api/v1/push/subscription", %{})
163 |> json_response_and_validate_schema(403)
167 test "returns error when user hasn't subscription", %{conn: conn} do
170 |> delete("/api/v1/push/subscription", %{})
171 |> json_response_and_validate_schema(404)
173 assert %{"error" => "Record not found"} == res
176 test "returns empty result and delete user subsciption", %{
182 insert(:push_subscription,
185 data: %{"alerts" => %{"mention" => true}}
190 |> delete("/api/v1/push/subscription", %{})
191 |> json_response_and_validate_schema(200)
194 refute Pleroma.Repo.get(Subscription, subscription.id)