update tests
[akkoma] / test / web / twitter_api / util_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Repo
10 alias Pleroma.Tests.ObanHelpers
11 alias Pleroma.User
12 alias Pleroma.Web.CommonAPI
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
15 import Mock
16
17 setup do
18 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
19 :ok
20 end
21
22 clear_config([:instance])
23 clear_config([:frontend_configurations, :pleroma_fe])
24 clear_config([:user, :deny_follow_blocked])
25
26 describe "POST /api/pleroma/follow_import" do
27 test "it returns HTTP 200", %{conn: conn} do
28 user1 = insert(:user)
29 user2 = insert(:user)
30
31 response =
32 conn
33 |> assign(:user, user1)
34 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
35 |> json_response(:ok)
36
37 assert response == "job started"
38 end
39
40 test "it imports follow lists from file", %{conn: conn} do
41 user1 = insert(:user)
42 user2 = insert(:user)
43
44 with_mocks([
45 {File, [],
46 read!: fn "follow_list.txt" ->
47 "Account address,Show boosts\n#{user2.ap_id},true"
48 end}
49 ]) do
50 response =
51 conn
52 |> assign(:user, user1)
53 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
54 |> json_response(:ok)
55
56 assert response == "job started"
57
58 assert ObanHelpers.member?(
59 %{
60 "op" => "follow_import",
61 "follower_id" => user1.id,
62 "followed_identifiers" => [user2.ap_id]
63 },
64 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
65 )
66 end
67 end
68
69 test "it imports new-style mastodon follow lists", %{conn: conn} do
70 user1 = insert(:user)
71 user2 = insert(:user)
72
73 response =
74 conn
75 |> assign(:user, user1)
76 |> post("/api/pleroma/follow_import", %{
77 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
78 })
79 |> json_response(:ok)
80
81 assert response == "job started"
82 end
83
84 test "requires 'follow' or 'write:follows' permissions", %{conn: conn} do
85 token1 = insert(:oauth_token, scopes: ["read", "write"])
86 token2 = insert(:oauth_token, scopes: ["follow"])
87 token3 = insert(:oauth_token, scopes: ["something"])
88 another_user = insert(:user)
89
90 for token <- [token1, token2, token3] do
91 conn =
92 conn
93 |> put_req_header("authorization", "Bearer #{token.token}")
94 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
95
96 if token == token3 do
97 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
98 json_response(conn, 403)
99 else
100 assert json_response(conn, 200)
101 end
102 end
103 end
104 end
105
106 describe "POST /api/pleroma/blocks_import" do
107 test "it returns HTTP 200", %{conn: conn} do
108 user1 = insert(:user)
109 user2 = insert(:user)
110
111 response =
112 conn
113 |> assign(:user, user1)
114 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
115 |> json_response(:ok)
116
117 assert response == "job started"
118 end
119
120 test "it imports blocks users from file", %{conn: conn} do
121 user1 = insert(:user)
122 user2 = insert(:user)
123 user3 = insert(:user)
124
125 with_mocks([
126 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
127 ]) do
128 response =
129 conn
130 |> assign(:user, user1)
131 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
132 |> json_response(:ok)
133
134 assert response == "job started"
135
136 assert ObanHelpers.member?(
137 %{
138 "op" => "blocks_import",
139 "blocker_id" => user1.id,
140 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
141 },
142 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
143 )
144 end
145 end
146 end
147
148 describe "PUT /api/pleroma/notification_settings" do
149 test "it updates notification settings", %{conn: conn} do
150 user = insert(:user)
151
152 conn
153 |> assign(:user, user)
154 |> put("/api/pleroma/notification_settings", %{
155 "followers" => false,
156 "bar" => 1
157 })
158 |> json_response(:ok)
159
160 user = Repo.get(User, user.id)
161
162 assert %Pleroma.User.NotificationSetting{
163 followers: false,
164 follows: true,
165 non_follows: true,
166 non_followers: true,
167 privacy_option: false
168 } == user.notification_settings
169 end
170
171 test "it update notificatin privacy option", %{conn: conn} do
172 user = insert(:user)
173
174 conn
175 |> assign(:user, user)
176 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
177 |> json_response(:ok)
178
179 user = refresh_record(user)
180
181 assert %Pleroma.User.NotificationSetting{
182 followers: true,
183 follows: true,
184 non_follows: true,
185 non_followers: true,
186 privacy_option: true
187 } == user.notification_settings
188 end
189 end
190
191 describe "GET /api/statusnet/config" do
192 test "it returns config in xml format", %{conn: conn} do
193 instance = Pleroma.Config.get(:instance)
194
195 response =
196 conn
197 |> put_req_header("accept", "application/xml")
198 |> get("/api/statusnet/config")
199 |> response(:ok)
200
201 assert response ==
202 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
203 Pleroma.Web.base_url()
204 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
205 !Keyword.get(instance, :registrations_open)
206 }</closed>\n</site>\n</config>\n"
207 end
208
209 test "it returns config in json format", %{conn: conn} do
210 instance = Pleroma.Config.get(:instance)
211 Pleroma.Config.put([:instance, :managed_config], true)
212 Pleroma.Config.put([:instance, :registrations_open], false)
213 Pleroma.Config.put([:instance, :invites_enabled], true)
214 Pleroma.Config.put([:instance, :public], false)
215 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
216
217 response =
218 conn
219 |> put_req_header("accept", "application/json")
220 |> get("/api/statusnet/config")
221 |> json_response(:ok)
222
223 expected_data = %{
224 "site" => %{
225 "accountActivationRequired" => "0",
226 "closed" => "1",
227 "description" => Keyword.get(instance, :description),
228 "invitesEnabled" => "1",
229 "name" => Keyword.get(instance, :name),
230 "pleromafe" => %{"theme" => "asuka-hospital"},
231 "private" => "1",
232 "safeDMMentionsEnabled" => "0",
233 "server" => Pleroma.Web.base_url(),
234 "textlimit" => to_string(Keyword.get(instance, :limit)),
235 "uploadlimit" => %{
236 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
237 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
238 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
239 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
240 },
241 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
242 }
243 }
244
245 assert response == expected_data
246 end
247
248 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
249 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
250
251 response =
252 conn
253 |> get("/api/statusnet/config.json")
254 |> json_response(:ok)
255
256 assert response["site"]["safeDMMentionsEnabled"] == "1"
257
258 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
259
260 response =
261 conn
262 |> get("/api/statusnet/config.json")
263 |> json_response(:ok)
264
265 assert response["site"]["safeDMMentionsEnabled"] == "0"
266 end
267
268 test "it returns the managed config", %{conn: conn} do
269 Pleroma.Config.put([:instance, :managed_config], false)
270 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
271
272 response =
273 conn
274 |> get("/api/statusnet/config.json")
275 |> json_response(:ok)
276
277 refute response["site"]["pleromafe"]
278
279 Pleroma.Config.put([:instance, :managed_config], true)
280
281 response =
282 conn
283 |> get("/api/statusnet/config.json")
284 |> json_response(:ok)
285
286 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
287 end
288 end
289
290 describe "GET /api/pleroma/frontend_configurations" do
291 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
292 config = [
293 frontend_a: %{
294 x: 1,
295 y: 2
296 },
297 frontend_b: %{
298 z: 3
299 }
300 ]
301
302 Pleroma.Config.put(:frontend_configurations, config)
303
304 response =
305 conn
306 |> get("/api/pleroma/frontend_configurations")
307 |> json_response(:ok)
308
309 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
310 end
311 end
312
313 describe "/api/pleroma/emoji" do
314 test "returns json with custom emoji with tags", %{conn: conn} do
315 emoji =
316 conn
317 |> get("/api/pleroma/emoji")
318 |> json_response(200)
319
320 assert Enum.all?(emoji, fn
321 {_key,
322 %{
323 "image_url" => url,
324 "tags" => tags
325 }} ->
326 is_binary(url) and is_list(tags)
327 end)
328 end
329 end
330
331 describe "GET /ostatus_subscribe - remote_follow/2" do
332 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
333 conn =
334 get(
335 conn,
336 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
337 )
338
339 assert redirected_to(conn) =~ "/notice/"
340 end
341
342 test "show follow account page if the `acct` is a account link", %{conn: conn} do
343 response =
344 get(
345 conn,
346 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
347 )
348
349 assert html_response(response, 200) =~ "Log in to follow"
350 end
351
352 test "show follow page if the `acct` is a account link", %{conn: conn} do
353 user = insert(:user)
354
355 response =
356 conn
357 |> assign(:user, user)
358 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
359
360 assert html_response(response, 200) =~ "Remote follow"
361 end
362
363 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
364 user = insert(:user)
365
366 assert capture_log(fn ->
367 response =
368 conn
369 |> assign(:user, user)
370 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
371
372 assert html_response(response, 200) =~ "Error fetching user"
373 end) =~ "Object has been deleted"
374 end
375 end
376
377 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
378 test "follows user", %{conn: conn} do
379 user = insert(:user)
380 user2 = insert(:user)
381
382 response =
383 conn
384 |> assign(:user, user)
385 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
386 |> response(200)
387
388 assert response =~ "Account followed!"
389 assert user2.follower_address in User.following(user)
390 end
391
392 test "returns error when user is deactivated", %{conn: conn} do
393 user = insert(:user, deactivated: true)
394 user2 = insert(:user)
395
396 response =
397 conn
398 |> assign(:user, user)
399 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
400 |> response(200)
401
402 assert response =~ "Error following account"
403 end
404
405 test "returns error when user is blocked", %{conn: conn} do
406 Pleroma.Config.put([:user, :deny_follow_blocked], true)
407 user = insert(:user)
408 user2 = insert(:user)
409
410 {:ok, _user_block} = Pleroma.User.block(user2, user)
411
412 response =
413 conn
414 |> assign(:user, user)
415 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
416 |> response(200)
417
418 assert response =~ "Error following account"
419 end
420
421 test "returns error when followee not found", %{conn: conn} do
422 user = insert(:user)
423
424 response =
425 conn
426 |> assign(:user, user)
427 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
428 |> response(200)
429
430 assert response =~ "Error following account"
431 end
432
433 test "returns success result when user already in followers", %{conn: conn} do
434 user = insert(:user)
435 user2 = insert(:user)
436 {:ok, _, _, _} = CommonAPI.follow(user, user2)
437
438 response =
439 conn
440 |> assign(:user, refresh_record(user))
441 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
442 |> response(200)
443
444 assert response =~ "Account followed!"
445 end
446 end
447
448 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
449 test "follows", %{conn: conn} do
450 user = insert(:user)
451 user2 = insert(:user)
452
453 response =
454 conn
455 |> post("/ostatus_subscribe", %{
456 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
457 })
458 |> response(200)
459
460 assert response =~ "Account followed!"
461 assert user2.follower_address in User.following(user)
462 end
463
464 test "returns error when followee not found", %{conn: conn} do
465 user = insert(:user)
466
467 response =
468 conn
469 |> post("/ostatus_subscribe", %{
470 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
471 })
472 |> response(200)
473
474 assert response =~ "Error following account"
475 end
476
477 test "returns error when login invalid", %{conn: conn} do
478 user = insert(:user)
479
480 response =
481 conn
482 |> post("/ostatus_subscribe", %{
483 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
484 })
485 |> response(200)
486
487 assert response =~ "Wrong username or password"
488 end
489
490 test "returns error when password invalid", %{conn: conn} do
491 user = insert(:user)
492 user2 = insert(:user)
493
494 response =
495 conn
496 |> post("/ostatus_subscribe", %{
497 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
498 })
499 |> response(200)
500
501 assert response =~ "Wrong username or password"
502 end
503
504 test "returns error when user is blocked", %{conn: conn} do
505 Pleroma.Config.put([:user, :deny_follow_blocked], true)
506 user = insert(:user)
507 user2 = insert(:user)
508 {:ok, _user_block} = Pleroma.User.block(user2, user)
509
510 response =
511 conn
512 |> post("/ostatus_subscribe", %{
513 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
514 })
515 |> response(200)
516
517 assert response =~ "Error following account"
518 end
519 end
520
521 describe "GET /api/pleroma/healthcheck" do
522 clear_config([:instance, :healthcheck])
523
524 test "returns 503 when healthcheck disabled", %{conn: conn} do
525 Pleroma.Config.put([:instance, :healthcheck], false)
526
527 response =
528 conn
529 |> get("/api/pleroma/healthcheck")
530 |> json_response(503)
531
532 assert response == %{}
533 end
534
535 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
536 Pleroma.Config.put([:instance, :healthcheck], true)
537
538 with_mock Pleroma.Healthcheck,
539 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
540 response =
541 conn
542 |> get("/api/pleroma/healthcheck")
543 |> json_response(200)
544
545 assert %{
546 "active" => _,
547 "healthy" => true,
548 "idle" => _,
549 "memory_used" => _,
550 "pool_size" => _
551 } = response
552 end
553 end
554
555 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
556 Pleroma.Config.put([:instance, :healthcheck], true)
557
558 with_mock Pleroma.Healthcheck,
559 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
560 response =
561 conn
562 |> get("/api/pleroma/healthcheck")
563 |> json_response(503)
564
565 assert %{
566 "active" => _,
567 "healthy" => false,
568 "idle" => _,
569 "memory_used" => _,
570 "pool_size" => _
571 } = response
572 end
573 end
574 end
575
576 describe "POST /api/pleroma/disable_account" do
577 test "it returns HTTP 200", %{conn: conn} do
578 user = insert(:user)
579
580 response =
581 conn
582 |> assign(:user, user)
583 |> post("/api/pleroma/disable_account", %{"password" => "test"})
584 |> json_response(:ok)
585
586 assert response == %{"status" => "success"}
587 ObanHelpers.perform_all()
588
589 user = User.get_cached_by_id(user.id)
590
591 assert user.deactivated == true
592 end
593
594 test "it returns returns when password invalid", %{conn: conn} do
595 user = insert(:user)
596
597 response =
598 conn
599 |> assign(:user, user)
600 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
601 |> json_response(:ok)
602
603 assert response == %{"error" => "Invalid password."}
604 user = User.get_cached_by_id(user.id)
605
606 refute user.deactivated
607 end
608 end
609
610 describe "GET /api/statusnet/version" do
611 test "it returns version in xml format", %{conn: conn} do
612 response =
613 conn
614 |> put_req_header("accept", "application/xml")
615 |> get("/api/statusnet/version")
616 |> response(:ok)
617
618 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
619 end
620
621 test "it returns version in json format", %{conn: conn} do
622 response =
623 conn
624 |> put_req_header("accept", "application/json")
625 |> get("/api/statusnet/version")
626 |> json_response(:ok)
627
628 assert response == "#{Pleroma.Application.named_version()}"
629 end
630 end
631
632 describe "POST /main/ostatus - remote_subscribe/2" do
633 test "renders subscribe form", %{conn: conn} do
634 user = insert(:user)
635
636 response =
637 conn
638 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
639 |> response(:ok)
640
641 refute response =~ "Could not find user"
642 assert response =~ "Remotely follow #{user.nickname}"
643 end
644
645 test "renders subscribe form with error when user not found", %{conn: conn} do
646 response =
647 conn
648 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
649 |> response(:ok)
650
651 assert response =~ "Could not find user"
652 refute response =~ "Remotely follow"
653 end
654
655 test "it redirect to webfinger url", %{conn: conn} do
656 user = insert(:user)
657 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
658
659 conn =
660 conn
661 |> post("/main/ostatus", %{
662 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
663 })
664
665 assert redirected_to(conn) ==
666 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
667 end
668
669 test "it renders form with error when use not found", %{conn: conn} do
670 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
671
672 response =
673 conn
674 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
675 |> response(:ok)
676
677 assert response =~ "Something went wrong."
678 end
679 end
680
681 test "it returns new captcha", %{conn: conn} do
682 with_mock Pleroma.Captcha,
683 new: fn -> "test_captcha" end do
684 resp =
685 conn
686 |> get("/api/pleroma/captcha")
687 |> response(200)
688
689 assert resp == "\"test_captcha\""
690 assert called(Pleroma.Captcha.new())
691 end
692 end
693
694 defp with_credentials(conn, username, password) do
695 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
696 put_req_header(conn, "authorization", header_content)
697 end
698
699 defp valid_user(_context) do
700 user = insert(:user)
701 [user: user]
702 end
703
704 describe "POST /api/pleroma/change_email" do
705 setup [:valid_user]
706
707 test "without credentials", %{conn: conn} do
708 conn = post(conn, "/api/pleroma/change_email")
709 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
710 end
711
712 test "with credentials and invalid password", %{conn: conn, user: current_user} do
713 conn =
714 conn
715 |> with_credentials(current_user.nickname, "test")
716 |> post("/api/pleroma/change_email", %{
717 "password" => "hi",
718 "email" => "test@test.com"
719 })
720
721 assert json_response(conn, 200) == %{"error" => "Invalid password."}
722 end
723
724 test "with credentials, valid password and invalid email", %{
725 conn: conn,
726 user: current_user
727 } do
728 conn =
729 conn
730 |> with_credentials(current_user.nickname, "test")
731 |> post("/api/pleroma/change_email", %{
732 "password" => "test",
733 "email" => "foobar"
734 })
735
736 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
737 end
738
739 test "with credentials, valid password and no email", %{
740 conn: conn,
741 user: current_user
742 } do
743 conn =
744 conn
745 |> with_credentials(current_user.nickname, "test")
746 |> post("/api/pleroma/change_email", %{
747 "password" => "test"
748 })
749
750 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
751 end
752
753 test "with credentials, valid password and blank email", %{
754 conn: conn,
755 user: current_user
756 } do
757 conn =
758 conn
759 |> with_credentials(current_user.nickname, "test")
760 |> post("/api/pleroma/change_email", %{
761 "password" => "test",
762 "email" => ""
763 })
764
765 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
766 end
767
768 test "with credentials, valid password and non unique email", %{
769 conn: conn,
770 user: current_user
771 } do
772 user = insert(:user)
773
774 conn =
775 conn
776 |> with_credentials(current_user.nickname, "test")
777 |> post("/api/pleroma/change_email", %{
778 "password" => "test",
779 "email" => user.email
780 })
781
782 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
783 end
784
785 test "with credentials, valid password and valid email", %{
786 conn: conn,
787 user: current_user
788 } do
789 conn =
790 conn
791 |> with_credentials(current_user.nickname, "test")
792 |> post("/api/pleroma/change_email", %{
793 "password" => "test",
794 "email" => "cofe@foobar.com"
795 })
796
797 assert json_response(conn, 200) == %{"status" => "success"}
798 end
799 end
800
801 describe "POST /api/pleroma/change_password" do
802 setup [:valid_user]
803
804 test "without credentials", %{conn: conn} do
805 conn = post(conn, "/api/pleroma/change_password")
806 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
807 end
808
809 test "with credentials and invalid password", %{conn: conn, user: current_user} do
810 conn =
811 conn
812 |> with_credentials(current_user.nickname, "test")
813 |> post("/api/pleroma/change_password", %{
814 "password" => "hi",
815 "new_password" => "newpass",
816 "new_password_confirmation" => "newpass"
817 })
818
819 assert json_response(conn, 200) == %{"error" => "Invalid password."}
820 end
821
822 test "with credentials, valid password and new password and confirmation not matching", %{
823 conn: conn,
824 user: current_user
825 } do
826 conn =
827 conn
828 |> with_credentials(current_user.nickname, "test")
829 |> post("/api/pleroma/change_password", %{
830 "password" => "test",
831 "new_password" => "newpass",
832 "new_password_confirmation" => "notnewpass"
833 })
834
835 assert json_response(conn, 200) == %{
836 "error" => "New password does not match confirmation."
837 }
838 end
839
840 test "with credentials, valid password and invalid new password", %{
841 conn: conn,
842 user: current_user
843 } do
844 conn =
845 conn
846 |> with_credentials(current_user.nickname, "test")
847 |> post("/api/pleroma/change_password", %{
848 "password" => "test",
849 "new_password" => "",
850 "new_password_confirmation" => ""
851 })
852
853 assert json_response(conn, 200) == %{
854 "error" => "New password can't be blank."
855 }
856 end
857
858 test "with credentials, valid password and matching new password and confirmation", %{
859 conn: conn,
860 user: current_user
861 } do
862 conn =
863 conn
864 |> with_credentials(current_user.nickname, "test")
865 |> post("/api/pleroma/change_password", %{
866 "password" => "test",
867 "new_password" => "newpass",
868 "new_password_confirmation" => "newpass"
869 })
870
871 assert json_response(conn, 200) == %{"status" => "success"}
872 fetched_user = User.get_cached_by_id(current_user.id)
873 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
874 end
875 end
876
877 describe "POST /api/pleroma/delete_account" do
878 setup [:valid_user]
879
880 test "without credentials", %{conn: conn} do
881 conn = post(conn, "/api/pleroma/delete_account")
882 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
883 end
884
885 test "with credentials and invalid password", %{conn: conn, user: current_user} do
886 conn =
887 conn
888 |> with_credentials(current_user.nickname, "test")
889 |> post("/api/pleroma/delete_account", %{"password" => "hi"})
890
891 assert json_response(conn, 200) == %{"error" => "Invalid password."}
892 end
893
894 test "with credentials and valid password", %{conn: conn, user: current_user} do
895 conn =
896 conn
897 |> with_credentials(current_user.nickname, "test")
898 |> post("/api/pleroma/delete_account", %{"password" => "test"})
899
900 assert json_response(conn, 200) == %{"status" => "success"}
901 end
902 end
903 end