1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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, [])
38 assert "Something went wrong" == unquote(yield)
39 Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
43 describe "creates push subscription" do
44 test "returns error when push disabled ", %{conn: conn} do
45 assert_error_when_disable_push do
47 |> post("/api/v1/push/subscription", %{})
52 test "successful creation", %{conn: conn} do
55 |> post("/api/v1/push/subscription", %{
56 "data" => %{"alerts" => %{"mention" => true, "test" => true}},
57 "subscription" => @sub
61 [subscription] = Pleroma.Repo.all(Subscription)
64 "alerts" => %{"mention" => true},
65 "endpoint" => subscription.endpoint,
66 "id" => to_string(subscription.id),
67 "server_key" => @server_key
72 describe "gets a user subscription" do
73 test "returns error when push disabled ", %{conn: conn} do
74 assert_error_when_disable_push do
76 |> get("/api/v1/push/subscription", %{})
81 test "returns error when user hasn't subscription", %{conn: conn} do
84 |> get("/api/v1/push/subscription", %{})
87 assert "Not found" == res
90 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
92 insert(:push_subscription,
95 data: %{"alerts" => %{"mention" => true}}
100 |> get("/api/v1/push/subscription", %{})
101 |> json_response(200)
104 "alerts" => %{"mention" => true},
105 "endpoint" => "https://example.com/example/1234",
106 "id" => to_string(subscription.id),
107 "server_key" => @server_key
114 describe "updates a user subsciption" do
115 setup %{conn: conn, user: user, token: token} do
117 insert(:push_subscription,
120 data: %{"alerts" => %{"mention" => true}}
123 %{conn: conn, user: user, token: token, subscription: subscription}
126 test "returns error when push disabled ", %{conn: conn} do
127 assert_error_when_disable_push do
129 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
130 |> json_response(500)
134 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
137 |> put("/api/v1/push/subscription", %{
138 data: %{"alerts" => %{"mention" => false, "follow" => true}}
140 |> json_response(200)
143 "alerts" => %{"follow" => true, "mention" => false},
144 "endpoint" => "https://example.com/example/1234",
145 "id" => to_string(subscription.id),
146 "server_key" => @server_key
153 describe "deletes the user subscription" do
154 test "returns error when push disabled ", %{conn: conn} do
155 assert_error_when_disable_push do
157 |> delete("/api/v1/push/subscription", %{})
158 |> json_response(500)
162 test "returns error when user hasn't subscription", %{conn: conn} do
165 |> delete("/api/v1/push/subscription", %{})
166 |> json_response(404)
168 assert "Not found" == res
171 test "returns empty result and delete user subsciption", %{
177 insert(:push_subscription,
180 data: %{"alerts" => %{"mention" => true}}
185 |> delete("/api/v1/push/subscription", %{})
186 |> json_response(200)
189 refute Pleroma.Repo.get(Subscription, subscription.id)