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.Subscription
13 "endpoint" => "https://example.com/example/1234",
15 "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
17 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
20 @server_key Keyword.get(Push.vapid_config(), :public_key)
24 token = insert(:oauth_token, user: user, scopes: ["push"])
28 |> assign(:user, user)
29 |> assign(:token, token)
31 %{conn: conn, user: user, token: token}
34 defmacro assert_error_when_disable_push(do: yield) do
36 vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
37 Application.put_env(:web_push_encryption, :vapid_details, [])
39 assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
42 Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
46 describe "creates push subscription" do
47 test "returns error when push disabled ", %{conn: conn} do
48 assert_error_when_disable_push do
50 |> post("/api/v1/push/subscription", %{})
55 test "successful creation", %{conn: conn} do
58 |> post("/api/v1/push/subscription", %{
59 "data" => %{"alerts" => %{"mention" => true, "test" => true}},
60 "subscription" => @sub
64 [subscription] = Pleroma.Repo.all(Subscription)
67 "alerts" => %{"mention" => true},
68 "endpoint" => subscription.endpoint,
69 "id" => to_string(subscription.id),
70 "server_key" => @server_key
75 describe "gets a user subscription" do
76 test "returns error when push disabled ", %{conn: conn} do
77 assert_error_when_disable_push do
79 |> get("/api/v1/push/subscription", %{})
84 test "returns error when user hasn't subscription", %{conn: conn} do
87 |> get("/api/v1/push/subscription", %{})
90 assert "Not found" == res
93 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
95 insert(:push_subscription,
98 data: %{"alerts" => %{"mention" => true}}
103 |> get("/api/v1/push/subscription", %{})
104 |> json_response(200)
107 "alerts" => %{"mention" => true},
108 "endpoint" => "https://example.com/example/1234",
109 "id" => to_string(subscription.id),
110 "server_key" => @server_key
117 describe "updates a user subsciption" do
118 setup %{conn: conn, user: user, token: token} do
120 insert(:push_subscription,
123 data: %{"alerts" => %{"mention" => true}}
126 %{conn: conn, user: user, token: token, subscription: subscription}
129 test "returns error when push disabled ", %{conn: conn} do
130 assert_error_when_disable_push do
132 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
133 |> json_response(403)
137 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
140 |> put("/api/v1/push/subscription", %{
141 data: %{"alerts" => %{"mention" => false, "follow" => true}}
143 |> json_response(200)
146 "alerts" => %{"follow" => true, "mention" => false},
147 "endpoint" => "https://example.com/example/1234",
148 "id" => to_string(subscription.id),
149 "server_key" => @server_key
156 describe "deletes the user subscription" do
157 test "returns error when push disabled ", %{conn: conn} do
158 assert_error_when_disable_push do
160 |> delete("/api/v1/push/subscription", %{})
161 |> json_response(403)
165 test "returns error when user hasn't subscription", %{conn: conn} do
168 |> delete("/api/v1/push/subscription", %{})
169 |> json_response(404)
171 assert "Not found" == res
174 test "returns empty result and delete user subsciption", %{
180 insert(:push_subscription,
183 data: %{"alerts" => %{"mention" => true}}
188 |> delete("/api/v1/push/subscription", %{})
189 |> json_response(200)
192 refute Pleroma.Repo.get(Subscription, subscription.id)