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