Merge branch 'develop' into issue/1276-2
[akkoma] / test / web / mastodon_api / controllers / subscription_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
6 use Pleroma.Web.ConnCase
7
8 import Pleroma.Factory
9 alias Pleroma.Web.Push
10 alias Pleroma.Web.Push.Subscription
11
12 @sub %{
13 "endpoint" => "https://example.com/example/1234",
14 "keys" => %{
15 "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
16 "p256dh" =>
17 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
18 }
19 }
20 @server_key Keyword.get(Push.vapid_config(), :public_key)
21
22 setup do
23 user = insert(:user)
24 token = insert(:oauth_token, user: user, scopes: ["push"])
25
26 conn =
27 build_conn()
28 |> assign(:user, user)
29 |> assign(:token, token)
30
31 %{conn: conn, user: user, token: token}
32 end
33
34 defmacro assert_error_when_disable_push(do: yield) do
35 quote do
36 vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
37 Application.put_env(:web_push_encryption, :vapid_details, [])
38
39 assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
40 unquote(yield)
41
42 Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
43 end
44 end
45
46 describe "creates push subscription" do
47 test "returns error when push disabled ", %{conn: conn} do
48 assert_error_when_disable_push do
49 conn
50 |> post("/api/v1/push/subscription", %{})
51 |> json_response(403)
52 end
53 end
54
55 test "successful creation", %{conn: conn} do
56 result =
57 conn
58 |> post("/api/v1/push/subscription", %{
59 "data" => %{"alerts" => %{"mention" => true, "test" => true}},
60 "subscription" => @sub
61 })
62 |> json_response(200)
63
64 [subscription] = Pleroma.Repo.all(Subscription)
65
66 assert %{
67 "alerts" => %{"mention" => true},
68 "endpoint" => subscription.endpoint,
69 "id" => to_string(subscription.id),
70 "server_key" => @server_key
71 } == result
72 end
73 end
74
75 describe "gets a user subscription" do
76 test "returns error when push disabled ", %{conn: conn} do
77 assert_error_when_disable_push do
78 conn
79 |> get("/api/v1/push/subscription", %{})
80 |> json_response(403)
81 end
82 end
83
84 test "returns error when user hasn't subscription", %{conn: conn} do
85 res =
86 conn
87 |> get("/api/v1/push/subscription", %{})
88 |> json_response(404)
89
90 assert "Not found" == res
91 end
92
93 test "returns a user subsciption", %{conn: conn, user: user, token: token} do
94 subscription =
95 insert(:push_subscription,
96 user: user,
97 token: token,
98 data: %{"alerts" => %{"mention" => true}}
99 )
100
101 res =
102 conn
103 |> get("/api/v1/push/subscription", %{})
104 |> json_response(200)
105
106 expect = %{
107 "alerts" => %{"mention" => true},
108 "endpoint" => "https://example.com/example/1234",
109 "id" => to_string(subscription.id),
110 "server_key" => @server_key
111 }
112
113 assert expect == res
114 end
115 end
116
117 describe "updates a user subsciption" do
118 setup %{conn: conn, user: user, token: token} do
119 subscription =
120 insert(:push_subscription,
121 user: user,
122 token: token,
123 data: %{"alerts" => %{"mention" => true}}
124 )
125
126 %{conn: conn, user: user, token: token, subscription: subscription}
127 end
128
129 test "returns error when push disabled ", %{conn: conn} do
130 assert_error_when_disable_push do
131 conn
132 |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
133 |> json_response(403)
134 end
135 end
136
137 test "returns updated subsciption", %{conn: conn, subscription: subscription} do
138 res =
139 conn
140 |> put("/api/v1/push/subscription", %{
141 data: %{"alerts" => %{"mention" => false, "follow" => true}}
142 })
143 |> json_response(200)
144
145 expect = %{
146 "alerts" => %{"follow" => true, "mention" => false},
147 "endpoint" => "https://example.com/example/1234",
148 "id" => to_string(subscription.id),
149 "server_key" => @server_key
150 }
151
152 assert expect == res
153 end
154 end
155
156 describe "deletes the user subscription" do
157 test "returns error when push disabled ", %{conn: conn} do
158 assert_error_when_disable_push do
159 conn
160 |> delete("/api/v1/push/subscription", %{})
161 |> json_response(403)
162 end
163 end
164
165 test "returns error when user hasn't subscription", %{conn: conn} do
166 res =
167 conn
168 |> delete("/api/v1/push/subscription", %{})
169 |> json_response(404)
170
171 assert "Not found" == res
172 end
173
174 test "returns empty result and delete user subsciption", %{
175 conn: conn,
176 user: user,
177 token: token
178 } do
179 subscription =
180 insert(:push_subscription,
181 user: user,
182 token: token,
183 data: %{"alerts" => %{"mention" => true}}
184 )
185
186 res =
187 conn
188 |> delete("/api/v1/push/subscription", %{})
189 |> json_response(200)
190
191 assert %{} == res
192 refute Pleroma.Repo.get(Subscription, subscription.id)
193 end
194 end
195 end