Merge branch 'translations' into develop
[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
71 assert NaiveDateTime.diff(
72 NaiveDateTime.from_iso8601!(response["expires_at"]),
73 expires_at
74 ) < 5
75
76 filter = Filter.get(response["id"], user)
77
78 id = filter.id
79
80 assert_enqueued(
81 worker: PurgeExpiredFilter,
82 args: %{filter_id: filter.id}
83 )
84
85 assert {:ok, %{id: ^id}} =
86 perform_job(PurgeExpiredFilter, %{
87 filter_id: filter.id
88 })
89
90 assert Repo.aggregate(Filter, :count, :id) == 0
91 end
92 end
93
94 test "fetching a list of filters" do
95 %{user: user, conn: conn} = oauth_access(["read:filters"])
96
97 %{filter_id: id1} = insert(:filter, user: user)
98 %{filter_id: id2} = insert(:filter, user: user)
99
100 id1 = to_string(id1)
101 id2 = to_string(id2)
102
103 assert [%{"id" => ^id2}, %{"id" => ^id1}] =
104 conn
105 |> get("/api/v1/filters")
106 |> json_response_and_validate_schema(200)
107 end
108
109 test "fetching a list of filters without token", %{conn: conn} do
110 insert(:filter)
111
112 response =
113 conn
114 |> get("/api/v1/filters")
115 |> json_response(403)
116
117 assert response["error"] == "Invalid credentials."
118 end
119
120 test "get a filter" do
121 %{user: user, conn: conn} = oauth_access(["read:filters"])
122
123 # check whole_word false
124 filter = insert(:filter, user: user, whole_word: false)
125
126 resp1 =
127 conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(200)
128
129 assert resp1["whole_word"] == false
130
131 # check whole_word true
132 filter = insert(:filter, user: user, whole_word: true)
133
134 resp2 =
135 conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(200)
136
137 assert resp2["whole_word"] == true
138 end
139
140 test "get a filter not_found error" do
141 filter = insert(:filter)
142 %{conn: conn} = oauth_access(["read:filters"])
143
144 response =
145 conn |> get("/api/v1/filters/#{filter.filter_id}") |> json_response_and_validate_schema(404)
146
147 assert response["error"] == "Record not found"
148 end
149
150 describe "updating a filter" do
151 setup do: oauth_access(["write:filters"])
152
153 test "common" do
154 %{conn: conn, user: user} = oauth_access(["write:filters"])
155
156 filter =
157 insert(:filter,
158 user: user,
159 hide: true,
160 whole_word: true
161 )
162
163 params = %{
164 phrase: "nii",
165 context: ["public"],
166 irreversible: false
167 }
168
169 response =
170 conn
171 |> put_req_header("content-type", "application/json")
172 |> put("/api/v1/filters/#{filter.filter_id}", params)
173 |> json_response_and_validate_schema(200)
174
175 assert response["phrase"] == params.phrase
176 assert response["context"] == params.context
177 assert response["irreversible"] == false
178 assert response["whole_word"] == true
179 end
180
181 test "with adding expires_at", %{conn: conn, user: user} do
182 filter = insert(:filter, user: user)
183 in_seconds = 600
184
185 response =
186 conn
187 |> put_req_header("content-type", "application/json")
188 |> put("/api/v1/filters/#{filter.filter_id}", %{
189 phrase: "nii",
190 context: ["public"],
191 expires_in: in_seconds,
192 irreversible: true
193 })
194 |> json_response_and_validate_schema(200)
195
196 assert response["irreversible"] == true
197
198 expected_time =
199 NaiveDateTime.utc_now()
200 |> NaiveDateTime.add(in_seconds)
201
202 assert NaiveDateTime.diff(
203 NaiveDateTime.from_iso8601!(response["expires_at"]),
204 expected_time
205 ) < 5
206
207 filter = Filter.get(response["id"], user)
208
209 id = filter.id
210
211 assert_enqueued(
212 worker: PurgeExpiredFilter,
213 args: %{filter_id: id}
214 )
215
216 assert {:ok, %{id: ^id}} =
217 perform_job(PurgeExpiredFilter, %{
218 filter_id: id
219 })
220
221 assert Repo.aggregate(Filter, :count, :id) == 0
222 end
223
224 test "with removing expires_at", %{conn: conn, user: user} do
225 response =
226 conn
227 |> put_req_header("content-type", "application/json")
228 |> post("/api/v1/filters", %{
229 phrase: "cofe",
230 context: ["home"],
231 expires_in: 600
232 })
233 |> json_response_and_validate_schema(200)
234
235 filter = Filter.get(response["id"], user)
236
237 assert_enqueued(
238 worker: PurgeExpiredFilter,
239 args: %{filter_id: filter.id}
240 )
241
242 response =
243 conn
244 |> put_req_header("content-type", "application/json")
245 |> put("/api/v1/filters/#{filter.filter_id}", %{
246 phrase: "nii",
247 context: ["public"],
248 expires_in: nil,
249 whole_word: true
250 })
251 |> json_response_and_validate_schema(200)
252
253 refute_enqueued(
254 worker: PurgeExpiredFilter,
255 args: %{filter_id: filter.id}
256 )
257
258 assert response["irreversible"] == false
259 assert response["whole_word"] == true
260 assert response["expires_at"] == nil
261 end
262
263 test "expires_at is the same in create and update so job is in db", %{conn: conn, user: user} do
264 resp1 =
265 conn
266 |> put_req_header("content-type", "application/json")
267 |> post("/api/v1/filters", %{
268 phrase: "cofe",
269 context: ["home"],
270 expires_in: 600
271 })
272 |> json_response_and_validate_schema(200)
273
274 filter = Filter.get(resp1["id"], user)
275
276 assert_enqueued(
277 worker: PurgeExpiredFilter,
278 args: %{filter_id: filter.id}
279 )
280
281 job = PurgeExpiredFilter.get_expiration(filter.id)
282
283 resp2 =
284 conn
285 |> put_req_header("content-type", "application/json")
286 |> put("/api/v1/filters/#{filter.filter_id}", %{
287 phrase: "nii",
288 context: ["public"]
289 })
290 |> json_response_and_validate_schema(200)
291
292 updated_filter = Filter.get(resp2["id"], user)
293
294 assert_enqueued(
295 worker: PurgeExpiredFilter,
296 args: %{filter_id: updated_filter.id}
297 )
298
299 after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
300
301 assert resp1["expires_at"] == resp2["expires_at"]
302
303 assert job.scheduled_at == after_update.scheduled_at
304 end
305
306 test "updating expires_at updates oban job too", %{conn: conn, user: user} do
307 resp1 =
308 conn
309 |> put_req_header("content-type", "application/json")
310 |> post("/api/v1/filters", %{
311 phrase: "cofe",
312 context: ["home"],
313 expires_in: 600
314 })
315 |> json_response_and_validate_schema(200)
316
317 filter = Filter.get(resp1["id"], user)
318
319 assert_enqueued(
320 worker: PurgeExpiredFilter,
321 args: %{filter_id: filter.id}
322 )
323
324 job = PurgeExpiredFilter.get_expiration(filter.id)
325
326 resp2 =
327 conn
328 |> put_req_header("content-type", "application/json")
329 |> put("/api/v1/filters/#{filter.filter_id}", %{
330 phrase: "nii",
331 context: ["public"],
332 expires_in: 300
333 })
334 |> json_response_and_validate_schema(200)
335
336 updated_filter = Filter.get(resp2["id"], user)
337
338 assert_enqueued(
339 worker: PurgeExpiredFilter,
340 args: %{filter_id: updated_filter.id}
341 )
342
343 after_update = PurgeExpiredFilter.get_expiration(updated_filter.id)
344
345 refute resp1["expires_at"] == resp2["expires_at"]
346
347 refute job.scheduled_at == after_update.scheduled_at
348 end
349 end
350
351 test "update filter without token", %{conn: conn} do
352 filter = insert(:filter)
353
354 response =
355 conn
356 |> put_req_header("content-type", "application/json")
357 |> put("/api/v1/filters/#{filter.filter_id}", %{
358 phrase: "nii",
359 context: ["public"]
360 })
361 |> json_response(403)
362
363 assert response["error"] == "Invalid credentials."
364 end
365
366 describe "delete a filter" do
367 setup do: oauth_access(["write:filters"])
368
369 test "common", %{conn: conn, user: user} do
370 filter = insert(:filter, user: user)
371
372 assert conn
373 |> delete("/api/v1/filters/#{filter.filter_id}")
374 |> json_response_and_validate_schema(200) == %{}
375
376 assert Repo.all(Filter) == []
377 end
378
379 test "with expires_at", %{conn: conn, user: user} do
380 response =
381 conn
382 |> put_req_header("content-type", "application/json")
383 |> post("/api/v1/filters", %{
384 phrase: "cofe",
385 context: ["home"],
386 expires_in: 600
387 })
388 |> json_response_and_validate_schema(200)
389
390 filter = Filter.get(response["id"], user)
391
392 assert_enqueued(
393 worker: PurgeExpiredFilter,
394 args: %{filter_id: filter.id}
395 )
396
397 assert conn
398 |> delete("/api/v1/filters/#{filter.filter_id}")
399 |> json_response_and_validate_schema(200) == %{}
400
401 refute_enqueued(
402 worker: PurgeExpiredFilter,
403 args: %{filter_id: filter.id}
404 )
405
406 assert Repo.all(Filter) == []
407 assert Repo.all(Oban.Job) == []
408 end
409 end
410
411 test "delete a filter without token", %{conn: conn} do
412 filter = insert(:filter)
413
414 response =
415 conn
416 |> delete("/api/v1/filters/#{filter.filter_id}")
417 |> json_response(403)
418
419 assert response["error"] == "Invalid credentials."
420 end
421 end