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