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