Merge branch 'develop' into 'remove-twitter-api'
[akkoma] / test / web / twitter_api / util_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.TwitterAPI.UtilControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
8
9 alias Pleroma.Config
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12
13 import Pleroma.Factory
14 import Mock
15
16 setup do
17 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 :ok
19 end
20
21 setup do: clear_config([:instance])
22 setup do: clear_config([:frontend_configurations, :pleroma_fe])
23
24 describe "POST /api/pleroma/follow_import" do
25 setup do: oauth_access(["follow"])
26
27 test "it returns HTTP 200", %{conn: conn} do
28 user2 = insert(:user)
29
30 response =
31 conn
32 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
33 |> json_response(:ok)
34
35 assert response == "job started"
36 end
37
38 test "it imports follow lists from file", %{user: user1, conn: conn} do
39 user2 = insert(:user)
40
41 with_mocks([
42 {File, [],
43 read!: fn "follow_list.txt" ->
44 "Account address,Show boosts\n#{user2.ap_id},true"
45 end}
46 ]) do
47 response =
48 conn
49 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
50 |> json_response(:ok)
51
52 assert response == "job started"
53
54 assert ObanHelpers.member?(
55 %{
56 "op" => "follow_import",
57 "follower_id" => user1.id,
58 "followed_identifiers" => [user2.ap_id]
59 },
60 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
61 )
62 end
63 end
64
65 test "it imports new-style mastodon follow lists", %{conn: conn} do
66 user2 = insert(:user)
67
68 response =
69 conn
70 |> post("/api/pleroma/follow_import", %{
71 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
72 })
73 |> json_response(:ok)
74
75 assert response == "job started"
76 end
77
78 test "requires 'follow' or 'write:follows' permissions" do
79 token1 = insert(:oauth_token, scopes: ["read", "write"])
80 token2 = insert(:oauth_token, scopes: ["follow"])
81 token3 = insert(:oauth_token, scopes: ["something"])
82 another_user = insert(:user)
83
84 for token <- [token1, token2, token3] do
85 conn =
86 build_conn()
87 |> put_req_header("authorization", "Bearer #{token.token}")
88 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
89
90 if token == token3 do
91 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
92 json_response(conn, 403)
93 else
94 assert json_response(conn, 200)
95 end
96 end
97 end
98
99 test "it imports follows with different nickname variations", %{conn: conn} do
100 [user2, user3, user4, user5, user6] = insert_list(5, :user)
101
102 identifiers =
103 [
104 user2.ap_id,
105 user3.nickname,
106 " ",
107 "@" <> user4.nickname,
108 user5.nickname <> "@localhost",
109 "@" <> user6.nickname <> "@localhost"
110 ]
111 |> Enum.join("\n")
112
113 response =
114 conn
115 |> post("/api/pleroma/follow_import", %{"list" => identifiers})
116 |> json_response(:ok)
117
118 assert response == "job started"
119 assert [{:ok, job_result}] = ObanHelpers.perform_all()
120 assert job_result == [user2, user3, user4, user5, user6]
121 end
122 end
123
124 describe "POST /api/pleroma/blocks_import" do
125 # Note: "follow" or "write:blocks" permission is required
126 setup do: oauth_access(["write:blocks"])
127
128 test "it returns HTTP 200", %{conn: conn} do
129 user2 = insert(:user)
130
131 response =
132 conn
133 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
134 |> json_response(:ok)
135
136 assert response == "job started"
137 end
138
139 test "it imports blocks users from file", %{user: user1, conn: conn} do
140 user2 = insert(:user)
141 user3 = insert(:user)
142
143 with_mocks([
144 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
145 ]) do
146 response =
147 conn
148 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
149 |> json_response(:ok)
150
151 assert response == "job started"
152
153 assert ObanHelpers.member?(
154 %{
155 "op" => "blocks_import",
156 "blocker_id" => user1.id,
157 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
158 },
159 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
160 )
161 end
162 end
163
164 test "it imports blocks with different nickname variations", %{conn: conn} do
165 [user2, user3, user4, user5, user6] = insert_list(5, :user)
166
167 identifiers =
168 [
169 user2.ap_id,
170 user3.nickname,
171 "@" <> user4.nickname,
172 user5.nickname <> "@localhost",
173 "@" <> user6.nickname <> "@localhost"
174 ]
175 |> Enum.join(" ")
176
177 response =
178 conn
179 |> post("/api/pleroma/blocks_import", %{"list" => identifiers})
180 |> json_response(:ok)
181
182 assert response == "job started"
183 assert [{:ok, job_result}] = ObanHelpers.perform_all()
184 assert job_result == [user2, user3, user4, user5, user6]
185 end
186 end
187
188 describe "PUT /api/pleroma/notification_settings" do
189 setup do: oauth_access(["write:accounts"])
190
191 test "it updates notification settings", %{user: user, conn: conn} do
192 conn
193 |> put("/api/pleroma/notification_settings", %{
194 "followers" => false,
195 "bar" => 1
196 })
197 |> json_response(:ok)
198
199 user = refresh_record(user)
200
201 assert %Pleroma.User.NotificationSetting{
202 followers: false,
203 follows: true,
204 non_follows: true,
205 non_followers: true,
206 privacy_option: false
207 } == user.notification_settings
208 end
209
210 test "it updates notification privacy option", %{user: user, conn: conn} do
211 conn
212 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
213 |> json_response(:ok)
214
215 user = refresh_record(user)
216
217 assert %Pleroma.User.NotificationSetting{
218 followers: true,
219 follows: true,
220 non_follows: true,
221 non_followers: true,
222 privacy_option: true
223 } == user.notification_settings
224 end
225 end
226
227 describe "GET /api/pleroma/frontend_configurations" do
228 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
229 config = [
230 frontend_a: %{
231 x: 1,
232 y: 2
233 },
234 frontend_b: %{
235 z: 3
236 }
237 ]
238
239 Config.put(:frontend_configurations, config)
240
241 response =
242 conn
243 |> get("/api/pleroma/frontend_configurations")
244 |> json_response(:ok)
245
246 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
247 end
248 end
249
250 describe "/api/pleroma/emoji" do
251 test "returns json with custom emoji with tags", %{conn: conn} do
252 emoji =
253 conn
254 |> get("/api/pleroma/emoji")
255 |> json_response(200)
256
257 assert Enum.all?(emoji, fn
258 {_key,
259 %{
260 "image_url" => url,
261 "tags" => tags
262 }} ->
263 is_binary(url) and is_list(tags)
264 end)
265 end
266 end
267
268 describe "GET /api/pleroma/healthcheck" do
269 setup do: clear_config([:instance, :healthcheck])
270
271 test "returns 503 when healthcheck disabled", %{conn: conn} do
272 Config.put([:instance, :healthcheck], false)
273
274 response =
275 conn
276 |> get("/api/pleroma/healthcheck")
277 |> json_response(503)
278
279 assert response == %{}
280 end
281
282 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
283 Config.put([:instance, :healthcheck], true)
284
285 with_mock Pleroma.Healthcheck,
286 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
287 response =
288 conn
289 |> get("/api/pleroma/healthcheck")
290 |> json_response(200)
291
292 assert %{
293 "active" => _,
294 "healthy" => true,
295 "idle" => _,
296 "memory_used" => _,
297 "pool_size" => _
298 } = response
299 end
300 end
301
302 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
303 Config.put([:instance, :healthcheck], true)
304
305 with_mock Pleroma.Healthcheck,
306 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
307 response =
308 conn
309 |> get("/api/pleroma/healthcheck")
310 |> json_response(503)
311
312 assert %{
313 "active" => _,
314 "healthy" => false,
315 "idle" => _,
316 "memory_used" => _,
317 "pool_size" => _
318 } = response
319 end
320 end
321 end
322
323 describe "POST /api/pleroma/disable_account" do
324 setup do: oauth_access(["write:accounts"])
325
326 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
327 response =
328 conn
329 |> post("/api/pleroma/disable_account", %{"password" => "test"})
330 |> json_response(:ok)
331
332 assert response == %{"status" => "success"}
333 ObanHelpers.perform_all()
334
335 user = User.get_cached_by_id(user.id)
336
337 assert user.deactivated == true
338 end
339
340 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
341 user = insert(:user)
342
343 response =
344 conn
345 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
346 |> json_response(:ok)
347
348 assert response == %{"error" => "Invalid password."}
349 user = User.get_cached_by_id(user.id)
350
351 refute user.deactivated
352 end
353 end
354
355 describe "POST /main/ostatus - remote_subscribe/2" do
356 setup do: clear_config([:instance, :federating], true)
357
358 test "renders subscribe form", %{conn: conn} do
359 user = insert(:user)
360
361 response =
362 conn
363 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
364 |> response(:ok)
365
366 refute response =~ "Could not find user"
367 assert response =~ "Remotely follow #{user.nickname}"
368 end
369
370 test "renders subscribe form with error when user not found", %{conn: conn} do
371 response =
372 conn
373 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
374 |> response(:ok)
375
376 assert response =~ "Could not find user"
377 refute response =~ "Remotely follow"
378 end
379
380 test "it redirect to webfinger url", %{conn: conn} do
381 user = insert(:user)
382 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
383
384 conn =
385 conn
386 |> post("/main/ostatus", %{
387 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
388 })
389
390 assert redirected_to(conn) ==
391 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
392 end
393
394 test "it renders form with error when user not found", %{conn: conn} do
395 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
396
397 response =
398 conn
399 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
400 |> response(:ok)
401
402 assert response =~ "Something went wrong."
403 end
404 end
405
406 test "it returns new captcha", %{conn: conn} do
407 with_mock Pleroma.Captcha,
408 new: fn -> "test_captcha" end do
409 resp =
410 conn
411 |> get("/api/pleroma/captcha")
412 |> response(200)
413
414 assert resp == "\"test_captcha\""
415 assert called(Pleroma.Captcha.new())
416 end
417 end
418
419 describe "POST /api/pleroma/change_email" do
420 setup do: oauth_access(["write:accounts"])
421
422 test "without permissions", %{conn: conn} do
423 conn =
424 conn
425 |> assign(:token, nil)
426 |> post("/api/pleroma/change_email")
427
428 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
429 end
430
431 test "with proper permissions and invalid password", %{conn: conn} do
432 conn =
433 post(conn, "/api/pleroma/change_email", %{
434 "password" => "hi",
435 "email" => "test@test.com"
436 })
437
438 assert json_response(conn, 200) == %{"error" => "Invalid password."}
439 end
440
441 test "with proper permissions, valid password and invalid email", %{
442 conn: conn
443 } do
444 conn =
445 post(conn, "/api/pleroma/change_email", %{
446 "password" => "test",
447 "email" => "foobar"
448 })
449
450 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
451 end
452
453 test "with proper permissions, valid password and no email", %{
454 conn: conn
455 } do
456 conn =
457 post(conn, "/api/pleroma/change_email", %{
458 "password" => "test"
459 })
460
461 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
462 end
463
464 test "with proper permissions, valid password and blank email", %{
465 conn: conn
466 } do
467 conn =
468 post(conn, "/api/pleroma/change_email", %{
469 "password" => "test",
470 "email" => ""
471 })
472
473 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
474 end
475
476 test "with proper permissions, valid password and non unique email", %{
477 conn: conn
478 } do
479 user = insert(:user)
480
481 conn =
482 post(conn, "/api/pleroma/change_email", %{
483 "password" => "test",
484 "email" => user.email
485 })
486
487 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
488 end
489
490 test "with proper permissions, valid password and valid email", %{
491 conn: conn
492 } do
493 conn =
494 post(conn, "/api/pleroma/change_email", %{
495 "password" => "test",
496 "email" => "cofe@foobar.com"
497 })
498
499 assert json_response(conn, 200) == %{"status" => "success"}
500 end
501 end
502
503 describe "POST /api/pleroma/change_password" do
504 setup do: oauth_access(["write:accounts"])
505
506 test "without permissions", %{conn: conn} do
507 conn =
508 conn
509 |> assign(:token, nil)
510 |> post("/api/pleroma/change_password")
511
512 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
513 end
514
515 test "with proper permissions and invalid password", %{conn: conn} do
516 conn =
517 post(conn, "/api/pleroma/change_password", %{
518 "password" => "hi",
519 "new_password" => "newpass",
520 "new_password_confirmation" => "newpass"
521 })
522
523 assert json_response(conn, 200) == %{"error" => "Invalid password."}
524 end
525
526 test "with proper permissions, valid password and new password and confirmation not matching",
527 %{
528 conn: conn
529 } do
530 conn =
531 post(conn, "/api/pleroma/change_password", %{
532 "password" => "test",
533 "new_password" => "newpass",
534 "new_password_confirmation" => "notnewpass"
535 })
536
537 assert json_response(conn, 200) == %{
538 "error" => "New password does not match confirmation."
539 }
540 end
541
542 test "with proper permissions, valid password and invalid new password", %{
543 conn: conn
544 } do
545 conn =
546 post(conn, "/api/pleroma/change_password", %{
547 "password" => "test",
548 "new_password" => "",
549 "new_password_confirmation" => ""
550 })
551
552 assert json_response(conn, 200) == %{
553 "error" => "New password can't be blank."
554 }
555 end
556
557 test "with proper permissions, valid password and matching new password and confirmation", %{
558 conn: conn,
559 user: user
560 } do
561 conn =
562 post(conn, "/api/pleroma/change_password", %{
563 "password" => "test",
564 "new_password" => "newpass",
565 "new_password_confirmation" => "newpass"
566 })
567
568 assert json_response(conn, 200) == %{"status" => "success"}
569 fetched_user = User.get_cached_by_id(user.id)
570 assert Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
571 end
572 end
573
574 describe "POST /api/pleroma/delete_account" do
575 setup do: oauth_access(["write:accounts"])
576
577 test "without permissions", %{conn: conn} do
578 conn =
579 conn
580 |> assign(:token, nil)
581 |> post("/api/pleroma/delete_account")
582
583 assert json_response(conn, 403) ==
584 %{"error" => "Insufficient permissions: write:accounts."}
585 end
586
587 test "with proper permissions and wrong or missing password", %{conn: conn} do
588 for params <- [%{"password" => "hi"}, %{}] do
589 ret_conn = post(conn, "/api/pleroma/delete_account", params)
590
591 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
592 end
593 end
594
595 test "with proper permissions and valid password", %{conn: conn} do
596 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
597
598 assert json_response(conn, 200) == %{"status" => "success"}
599 end
600 end
601 end