98ab9e71768d658166ef44b1b84f656a52923437
[akkoma] / test / pleroma / web / mastodon_api / controllers / filter_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.FilterControllerTest do
6 use Pleroma.Web.ConnCase, async: true
7 use Oban.Testing, repo: Pleroma.Repo
8
9 import Pleroma.Factory
10
11 alias Pleroma.Filter
12 alias Pleroma.Repo
13 alias Pleroma.Workers.PurgeExpiredFilter
14
15 test "non authenticated creation request", %{conn: conn} do
16 response =
17 conn
18 |> put_req_header("content-type", "application/json")
19 |> post("/api/v1/filters", %{"phrase" => "knights", context: ["home"]})
20 |> json_response(403)
21
22 assert response["error"] == "Invalid credentials."
23 end
24
25 describe "creating" do
26 setup do: oauth_access(["write:filters"])
27
28 test "a common filter", %{conn: conn, user: user} do
29 params = %{
30 phrase: "knights",
31 context: ["home"],
32 irreversible: true
33 }
34
35 response =
36 conn
37 |> put_req_header("content-type", "application/json")
38 |> post("/api/v1/filters", params)
39 |> json_response_and_validate_schema(200)
40
41 assert response["phrase"] == params.phrase
42 assert response["context"] == params.context
43 assert response["irreversible"] == true
44 assert response["id"] != nil
45 assert response["id"] != ""
46 assert response["expires_at"] == nil
47
48 filter = Filter.get(response["id"], user)
49 assert filter.hide == true
50 end
51
52 test "a filter with expires_in", %{conn: conn, user: user} do
53 in_seconds = 600
54
55 response =
56 conn
57 |> put_req_header("content-type", "application/json")
58 |> post("/api/v1/filters", %{
59 "phrase" => "knights",
60 context: ["home"],
61 expires_in: in_seconds
62 })
63 |> json_response_and_validate_schema(200)
64
65 assert response["irreversible"] == false
66
67 expires_at =
68 NaiveDateTime.utc_now()
69 |> NaiveDateTime.add(in_seconds)
70 |> Pleroma.Web.CommonAPI.Utils.to_masto_date()
71
72 assert response["expires_at"] == expires_at
73
74 filter = Filter.get(response["id"], user)
75
76 id = filter.id
77
78 assert_enqueued(
79 worker: PurgeExpiredFilter,
80 args: %{filter_id: filter.id}
81 )
82
83 assert {:ok, %{id: ^id}} =
84 perform_job(PurgeExpiredFilter, %{
85 filter_id: filter.id
86 })
87
88 assert Repo.aggregate(Filter, :count, :id) == 0
89 end
90 end
91
92 test "fetching a list of filters" do
93 %{user: user, conn: conn} = oauth_access(["read:filters"])
94
95 %{filter_id: id1} = insert(:filter, user: user)
96 %{filter_id: id2} = insert(:filter, user: user)
97
98 id1 = to_string(id1)
99 id2 = to_string(id2)
100
101 assert [%{"id" => ^id2}, %{"id" => ^id1}] =
102 conn
103 |> get("/api/v1/filters")
104 |> json_response_and_validate_schema(200)
105 end
106
107 test "fetching a list of filters without token", %{conn: conn} do
108 insert(:filter)
109
110 response =
111 conn
112 |> get("/api/v1/filters")
113 |> json_response(403)
114
115 assert response["error"] == "Invalid credentials."
116 end
117
118 test "get a filter" do
119 %{user: user, conn: conn} = oauth_access(["read:filters"])
120
121 # check whole_word false
122 filter = insert(:filter, user: user, whole_word: false)
123
124 resp1 =
125 conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(200)
126
127 assert resp1["whole_word"] == false
128
129 # check whole_word true
130 filter = insert(:filter, user: user, whole_word: true)
131
132 resp2 =
133 conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(200)
134
135 assert resp2["whole_word"] == true
136 end
137
138 test "get a filter not_found error" do
139 filter = insert(:filter)
140 %{conn: conn} = oauth_access(["read:filters"])
141
142 response =
143 conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(404)
144
145 assert response["error"] == "Record not found"
146 end
147
148 describe "updating a filter" do
149 setup do: oauth_access(["write:filters"])
150
151 test "common" do
152 %{conn: conn, user: user} = oauth_access(["write:filters"])
153
154 filter =
155 insert(:filter,
156 user: user,
157 hide: true,
158 whole_word: true
159 )
160
161 params = %{
162 phrase: "nii",
163 context: ["public"],
164 irreversible: false
165 }
166
167 response =
168 conn
169 |> put_req_header("content-type", "application/json")
170 |> put("/api/v1/filters/#{filter.filter_id}", params)
171 |> json_response_and_validate_schema(200)
172
173 assert response["phrase"] == params.phrase
174 assert response["context"] == params.context
175 assert response["irreversible"] == false
176 assert response["whole_word"] == true
177 end
178
179 test "with adding expires_at", %{conn: conn, user: user} do
180 filter = insert(:filter, user: user)
181 in_seconds = 600
182
183 response =
184 conn
185 |> put_req_header("content-type", "application/json")
186 |> put("/api/v1/filters/#{filter.filter_id}", %{
187 phrase: "nii",
188 context: ["public"],
189 expires_in: in_seconds,
190 irreversible: true
191 })
192 |> json_response_and_validate_schema(200)
193
194 assert response["irreversible"] == true
195
196 assert response["expires_at"] ==
197 NaiveDateTime.utc_now()
198 |> NaiveDateTime.add(in_seconds)
199 |> Pleroma.Web.CommonAPI.Utils.to_masto_date()
200
201 filter = Filter.get(response["id"], user)
202
203 id = filter.id
204
205 assert_enqueued(
206 worker: PurgeExpiredFilter,
207 args: %{filter_id: id}
208 )
209
210 assert {:ok, %{id: ^id}} =
211 perform_job(PurgeExpiredFilter, %{
212 filter_id: id
213 })
214
215 assert Repo.aggregate(Filter, :count, :id) == 0
216 end
217
218 test "with removing expires_at", %{conn: conn, user: user} do
219 response =
220 conn
221 |> put_req_header("content-type", "application/json")
222 |> post("/api/v1/filters", %{
223 phrase: "cofe",
224 context: ["home"],
225 expires_in: 600
226 })
227 |> json_response_and_validate_schema(200)
228
229 filter = Filter.get(response["id"], user)
230
231 assert_enqueued(
232 worker: PurgeExpiredFilter,
233 args: %{filter_id: filter.id}
234 )
235
236 response =
237 conn
238 |> put_req_header("content-type", "application/json")
239 |> put("/api/v1/filters/#{filter.filter_id}", %{
240 phrase: "nii",
241 context: ["public"],
242 expires_in: nil,
243 whole_word: true
244 })
245 |> json_response_and_validate_schema(200)
246
247 refute_enqueued(
248 worker: PurgeExpiredFilter,
249 args: %{filter_id: filter.id}
250 )
251
252 assert response["irreversible"] == false
253 assert response["whole_word"] == true
254 assert response["expires_at"] == nil
255 end
256
257 test "expires_at is the same in create and update so job is in db", %{conn: conn, user: user} do
258 resp1 =
259 conn
260 |> put_req_header("content-type", "application/json")
261 |> post("/api/v1/filters", %{
262 phrase: "cofe",
263 context: ["home"],
264 expires_in: 600
265 })
266 |> json_response_and_validate_schema(200)
267
268 filter = Filter.get(resp1["id"], user)
269
270 assert_enqueued(
271 worker: PurgeExpiredFilter,
272 args: %{filter_id: filter.id}
273 )
274
275 job = PurgeExpiredFilter.get_expiration(filter.id)
276
277 resp2 =
278 conn
279 |> put_req_header("content-type", "application/json")
280 |> put("/api/v1/filters/#{filter.filter_id}", %{
281 phrase: "nii",
282 context: ["public"]
283 })
284 |> json_response_and_validate_schema(200)
285
286 updated_filter = Filter.get(resp2["id"], user)
287
288 assert_enqueued(
289 worker: PurgeExpiredFilter,
290 args: %{filter_id: updated_filter.id}
291 )
292
293 after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
294
295 assert resp1["expires_at"] == resp2["expires_at"]
296
297 assert job.scheduled_at == after_update.scheduled_at
298 end
299
300 test "updating expires_at updates oban job too", %{conn: conn, user: user} do
301 resp1 =
302 conn
303 |> put_req_header("content-type", "application/json")
304 |> post("/api/v1/filters", %{
305 phrase: "cofe",
306 context: ["home"],
307 expires_in: 600
308 })
309 |> json_response_and_validate_schema(200)
310
311 filter = Filter.get(resp1["id"], user)
312
313 assert_enqueued(
314 worker: PurgeExpiredFilter,
315 args: %{filter_id: filter.id}
316 )
317
318 job = PurgeExpiredFilter.get_expiration(filter.id)
319
320 resp2 =
321 conn
322 |> put_req_header("content-type", "application/json")
323 |> put("/api/v1/filters/#{filter.filter_id}", %{
324 phrase: "nii",
325 context: ["public"],
326 expires_in: 300
327 })
328 |> json_response_and_validate_schema(200)
329
330 updated_filter = Filter.get(resp2["id"], user)
331
332 assert_enqueued(
333 worker: PurgeExpiredFilter,
334 args: %{filter_id: updated_filter.id}
335 )
336
337 after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
338
339 refute resp1["expires_at"] == resp2["expires_at"]
340
341 refute job.scheduled_at == after_update.scheduled_at
342 end
343 end
344
345 test "update filter without token", %{conn: conn} do
346 filter = insert(:filter)
347
348 response =
349 conn
350 |> put_req_header("content-type", "application/json")
351 |> put("/api/v1/filters/#{filter.filter_id}", %{
352 phrase: "nii",
353 context: ["public"]
354 })
355 |> json_response(403)
356
357 assert response["error"] == "Invalid credentials."
358 end
359
360 describe "delete a filter" do
361 setup do: oauth_access(["write:filters"])
362
363 test "common", %{conn: conn, user: user} do
364 filter = insert(:filter, user: user)
365
366 assert conn
367 |> delete("/api/v1/filters/#{filter.filter_id}")
368 |> json_response_and_validate_schema(200) == %{}
369
370 assert Repo.all(Filter) == []
371 end
372
373 test "with expires_at", %{conn: conn, user: user} do
374 response =
375 conn
376 |> put_req_header("content-type", "application/json")
377 |> post("/api/v1/filters", %{
378 phrase: "cofe",
379 context: ["home"],
380 expires_in: 600
381 })
382 |> json_response_and_validate_schema(200)
383
384 filter = Filter.get(response["id"], user)
385
386 assert_enqueued(
387 worker: PurgeExpiredFilter,
388 args: %{filter_id: filter.id}
389 )
390
391 assert conn
392 |> delete("/api/v1/filters/#{filter.filter_id}")
393 |> json_response_and_validate_schema(200) == %{}
394
395 refute_enqueued(
396 worker: PurgeExpiredFilter,
397 args: %{filter_id: filter.id}
398 )
399
400 assert Repo.all(Filter) == []
401 assert Repo.all(Oban.Job) == []
402 end
403 end
404
405 test "delete a filter without token", %{conn: conn} do
406 filter = insert(:filter)
407
408 response =
409 conn
410 |> delete("/api/v1/filters/#{filter.filter_id}")
411 |> json_response(403)
412
413 assert response["error"] == "Invalid credentials."
414 end
415 end