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