1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
9 alias Pleroma.Tests.ObanHelpers
12 import Pleroma.Factory
16 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 setup do: clear_config([:instance])
21 setup do: clear_config([:frontend_configurations, :pleroma_fe])
23 describe "PUT /api/pleroma/notification_settings" do
24 setup do: oauth_access(["write:accounts"])
26 test "it updates notification settings", %{user: user, conn: conn} do
29 "/api/pleroma/notification_settings?#{URI.encode_query(%{block_from_strangers: true})}"
31 |> json_response_and_validate_schema(:ok)
33 user = refresh_record(user)
35 assert %Pleroma.User.NotificationSetting{
36 block_from_strangers: true,
37 hide_notification_contents: false
38 } == user.notification_settings
41 test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
44 "/api/pleroma/notification_settings?#{URI.encode_query(%{hide_notification_contents: 1})}"
46 |> json_response_and_validate_schema(:ok)
48 user = refresh_record(user)
50 assert %Pleroma.User.NotificationSetting{
51 block_from_strangers: false,
52 hide_notification_contents: true
53 } == user.notification_settings
57 describe "GET /api/pleroma/frontend_configurations" do
58 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
69 clear_config(:frontend_configurations, config)
73 |> get("/api/pleroma/frontend_configurations")
74 |> json_response_and_validate_schema(:ok)
76 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
80 describe "/api/pleroma/emoji" do
81 test "returns json with custom emoji with tags", %{conn: conn} do
84 |> get("/api/pleroma/emoji")
85 |> json_response_and_validate_schema(200)
87 assert Enum.all?(emoji, fn
93 is_binary(url) and is_list(tags)
98 describe "GET /api/pleroma/healthcheck" do
99 setup do: clear_config([:instance, :healthcheck])
101 test "returns 503 when healthcheck disabled", %{conn: conn} do
102 clear_config([:instance, :healthcheck], false)
106 |> get("/api/pleroma/healthcheck")
107 |> json_response_and_validate_schema(503)
109 assert response == %{}
112 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
113 clear_config([:instance, :healthcheck], true)
115 with_mock Pleroma.Healthcheck,
116 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
119 |> get("/api/pleroma/healthcheck")
120 |> json_response_and_validate_schema(200)
132 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
133 clear_config([:instance, :healthcheck], true)
135 with_mock Pleroma.Healthcheck,
136 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
139 |> get("/api/pleroma/healthcheck")
140 |> json_response_and_validate_schema(503)
153 describe "POST /api/pleroma/disable_account" do
154 setup do: oauth_access(["write:accounts"])
156 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
159 |> post("/api/pleroma/disable_account?password=test")
160 |> json_response_and_validate_schema(:ok)
162 assert response == %{"status" => "success"}
163 ObanHelpers.perform_all()
165 user = User.get_cached_by_id(user.id)
167 refute user.is_active
170 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
175 |> post("/api/pleroma/disable_account?password=test1")
176 |> json_response_and_validate_schema(:ok)
178 assert response == %{"error" => "Invalid password."}
179 user = User.get_cached_by_id(user.id)
181 assert user.is_active
185 describe "POST /main/ostatus - remote_subscribe/2" do
186 setup do: clear_config([:instance, :federating], true)
188 test "renders subscribe form", %{conn: conn} do
193 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
196 refute response =~ "Could not find user"
197 assert response =~ "Remotely follow #{user.nickname}"
200 test "renders subscribe form with error when user not found", %{conn: conn} do
203 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
206 assert response =~ "Could not find user"
207 refute response =~ "Remotely follow"
210 test "it redirect to webfinger url", %{conn: conn} do
212 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
216 |> post("/main/ostatus", %{
217 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
220 assert redirected_to(conn) ==
221 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
224 test "it renders form with error when user not found", %{conn: conn} do
225 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
229 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
232 assert response =~ "Something went wrong."
236 test "it returns new captcha", %{conn: conn} do
237 with_mock Pleroma.Captcha,
238 new: fn -> "test_captcha" end do
241 |> get("/api/pleroma/captcha")
244 assert resp == "\"test_captcha\""
245 assert called(Pleroma.Captcha.new())
249 describe "POST /api/pleroma/change_email" do
250 setup do: oauth_access(["write:accounts"])
252 test "without permissions", %{conn: conn} do
255 |> assign(:token, nil)
256 |> put_req_header("content-type", "multipart/form-data")
257 |> post("/api/pleroma/change_email", %{password: "hi", email: "test@test.com"})
259 assert json_response_and_validate_schema(conn, 403) == %{
260 "error" => "Insufficient permissions: write:accounts."
264 test "with proper permissions and invalid password", %{conn: conn} do
267 |> put_req_header("content-type", "multipart/form-data")
268 |> post("/api/pleroma/change_email", %{password: "hi", email: "test@test.com"})
270 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
273 test "with proper permissions, valid password and invalid email", %{
278 |> put_req_header("content-type", "multipart/form-data")
279 |> post("/api/pleroma/change_email", %{password: "test", email: "foobar"})
281 assert json_response_and_validate_schema(conn, 200) == %{
282 "error" => "Email has invalid format."
286 test "with proper permissions, valid password and no email", %{
291 |> put_req_header("content-type", "multipart/form-data")
292 |> post("/api/pleroma/change_email", %{password: "test"})
294 assert %{"error" => "Missing field: email."} = json_response_and_validate_schema(conn, 400)
297 test "with proper permissions, valid password and blank email, when instance requires user email",
301 orig_account_activation_required =
302 Pleroma.Config.get([:instance, :account_activation_required])
304 Pleroma.Config.put([:instance, :account_activation_required], true)
308 [:instance, :account_activation_required],
309 orig_account_activation_required
315 |> put_req_header("content-type", "multipart/form-data")
316 |> post("/api/pleroma/change_email", %{password: "test", email: ""})
318 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Email can't be blank."}
321 test "with proper permissions, valid password and blank email, when instance does not require user email",
325 orig_account_activation_required =
326 Pleroma.Config.get([:instance, :account_activation_required])
328 Pleroma.Config.put([:instance, :account_activation_required], false)
332 [:instance, :account_activation_required],
333 orig_account_activation_required
339 |> put_req_header("content-type", "multipart/form-data")
340 |> post("/api/pleroma/change_email", %{password: "test", email: ""})
342 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
345 test "with proper permissions, valid password and non unique email", %{
352 |> put_req_header("content-type", "multipart/form-data")
353 |> post("/api/pleroma/change_email", %{password: "test", email: user.email})
355 assert json_response_and_validate_schema(conn, 200) == %{
356 "error" => "Email has already been taken."
360 test "with proper permissions, valid password and valid email", %{
365 |> put_req_header("content-type", "multipart/form-data")
366 |> post("/api/pleroma/change_email", %{password: "test", email: "cofe@foobar.com"})
368 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
372 describe "POST /api/pleroma/change_password" do
373 setup do: oauth_access(["write:accounts"])
375 test "without permissions", %{conn: conn} do
378 |> assign(:token, nil)
379 |> put_req_header("content-type", "multipart/form-data")
380 |> post("/api/pleroma/change_password", %{
382 "new_password" => "newpass",
383 "new_password_confirmation" => "newpass"
386 assert json_response_and_validate_schema(conn, 403) == %{
387 "error" => "Insufficient permissions: write:accounts."
391 test "with proper permissions and invalid password", %{conn: conn} do
394 |> put_req_header("content-type", "multipart/form-data")
395 |> post("/api/pleroma/change_password", %{
397 "new_password" => "newpass",
398 "new_password_confirmation" => "newpass"
401 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
404 test "with proper permissions, valid password and new password and confirmation not matching",
410 |> put_req_header("content-type", "multipart/form-data")
411 |> post("/api/pleroma/change_password", %{
412 "password" => "test",
413 "new_password" => "newpass",
414 "new_password_confirmation" => "notnewpass"
417 assert json_response_and_validate_schema(conn, 200) == %{
418 "error" => "New password does not match confirmation."
422 test "with proper permissions, valid password and invalid new password", %{
427 |> put_req_header("content-type", "multipart/form-data")
428 |> post("/api/pleroma/change_password", %{
431 new_password_confirmation: ""
434 assert json_response_and_validate_schema(conn, 200) == %{
435 "error" => "New password can't be blank."
439 test "with proper permissions, valid password and matching new password and confirmation", %{
445 |> put_req_header("content-type", "multipart/form-data")
447 "/api/pleroma/change_password",
450 new_password: "newpass",
451 new_password_confirmation: "newpass"
455 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
456 fetched_user = User.get_cached_by_id(user.id)
457 assert Pleroma.Password.Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
461 describe "POST /api/pleroma/delete_account" do
462 setup do: oauth_access(["write:accounts"])
464 test "without permissions", %{conn: conn} do
467 |> assign(:token, nil)
468 |> post("/api/pleroma/delete_account")
470 assert json_response_and_validate_schema(conn, 403) ==
471 %{"error" => "Insufficient permissions: write:accounts."}
474 test "with proper permissions and wrong or missing password", %{conn: conn} do
475 for params <- [%{"password" => "hi"}, %{}] do
478 |> put_req_header("content-type", "application/json")
479 |> post("/api/pleroma/delete_account", params)
481 assert json_response_and_validate_schema(ret_conn, 200) == %{
482 "error" => "Invalid password."
487 test "with proper permissions and valid password (URL query)", %{conn: conn, user: user} do
490 |> put_req_header("content-type", "application/json")
491 |> post("/api/pleroma/delete_account?password=test")
493 ObanHelpers.perform_all()
494 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
496 user = User.get_by_id(user.id)
497 refute user.is_active
498 assert user.name == nil
499 assert user.bio == ""
500 assert user.password_hash == nil
503 test "with proper permissions and valid password (JSON body)", %{conn: conn, user: user} do
506 |> put_req_header("content-type", "application/json")
507 |> post("/api/pleroma/delete_account", %{password: "test"})
509 ObanHelpers.perform_all()
510 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
512 user = User.get_by_id(user.id)
513 refute user.is_active
514 assert user.name == nil
515 assert user.bio == ""
516 assert user.password_hash == nil
520 describe "POST /api/pleroma/move_account" do
521 setup do: oauth_access(["write:accounts"])
523 test "without permissions", %{conn: conn} do
524 target_user = insert(:user)
525 target_nick = target_user |> User.full_nickname()
529 |> assign(:token, nil)
530 |> put_req_header("content-type", "multipart/form-data")
531 |> post("/api/pleroma/move_account", %{
533 "target_account" => target_nick
536 assert json_response_and_validate_schema(conn, 403) == %{
537 "error" => "Insufficient permissions: write:accounts."
541 test "with proper permissions and invalid password", %{conn: conn} do
542 target_user = insert(:user)
543 target_nick = target_user |> User.full_nickname()
547 |> put_req_header("content-type", "multipart/form-data")
548 |> post("/api/pleroma/move_account", %{
550 "target_account" => target_nick
553 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
556 test "with proper permissions, valid password and target account does not alias this",
560 target_user = insert(:user)
561 target_nick = target_user |> User.full_nickname()
565 |> put_req_header("content-type", "multipart/form-data")
566 |> post("/api/pleroma/move_account", %{
567 "password" => "test",
568 "target_account" => target_nick
571 assert json_response_and_validate_schema(conn, 200) == %{
572 "error" => "Target account must have the origin in `alsoKnownAs`"
576 test "with proper permissions, valid password and target account does not exist",
580 target_nick = "not_found@mastodon.social"
584 |> put_req_header("content-type", "multipart/form-data")
585 |> post("/api/pleroma/move_account", %{
586 "password" => "test",
587 "target_account" => target_nick
590 assert json_response_and_validate_schema(conn, 404) == %{
591 "error" => "Target account not found."
595 test "with proper permissions, valid password, remote target account aliases this and local cache does not exist",
597 user = insert(:user, ap_id: "https://lm.kazv.moe/users/testuser")
598 %{user: _user, conn: conn} = oauth_access(["write:accounts"], user: user)
600 target_nick = "mewmew@lm.kazv.moe"
604 |> put_req_header("content-type", "multipart/form-data")
605 |> post("/api/pleroma/move_account", %{
606 "password" => "test",
607 "target_account" => target_nick
610 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
613 test "with proper permissions, valid password, remote target account aliases this and local cache does not alias this",
615 user = insert(:user, ap_id: "https://lm.kazv.moe/users/testuser")
616 %{user: _user, conn: conn} = oauth_access(["write:accounts"], user: user)
621 ap_id: "https://lm.kazv.moe/users/mewmew",
622 nickname: "mewmew@lm.kazv.moe",
626 target_nick = target_user |> User.full_nickname()
630 |> put_req_header("content-type", "multipart/form-data")
631 |> post("/api/pleroma/move_account", %{
632 "password" => "test",
633 "target_account" => target_nick
636 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
639 test "with proper permissions, valid password, remote target account does not alias this and local cache aliases this",
647 ap_id: "https://lm.kazv.moe/users/mewmew",
648 nickname: "mewmew@lm.kazv.moe",
650 also_known_as: [user.ap_id]
653 target_nick = target_user |> User.full_nickname()
657 |> put_req_header("content-type", "multipart/form-data")
658 |> post("/api/pleroma/move_account", %{
659 "password" => "test",
660 "target_account" => target_nick
663 assert json_response_and_validate_schema(conn, 200) == %{
664 "error" => "Target account must have the origin in `alsoKnownAs`"
668 test "with proper permissions, valid password and target account aliases this", %{
672 target_user = insert(:user, also_known_as: [user.ap_id])
673 target_nick = target_user |> User.full_nickname()
674 follower = insert(:user)
676 User.follow(follower, user)
678 assert User.following?(follower, user)
682 |> put_req_header("content-type", "multipart/form-data")
684 "/api/pleroma/move_account",
687 target_account: target_nick
691 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
694 "op" => "move_following",
695 "origin_id" => user.id,
696 "target_id" => target_user.id
699 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
701 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
703 refute User.following?(follower, user)
704 assert User.following?(follower, target_user)
707 test "prefix nickname by @ should work", %{
711 target_user = insert(:user, also_known_as: [user.ap_id])
712 target_nick = target_user |> User.full_nickname()
713 follower = insert(:user)
715 User.follow(follower, user)
717 assert User.following?(follower, user)
721 |> put_req_header("content-type", "multipart/form-data")
723 "/api/pleroma/move_account",
726 target_account: "@" <> target_nick
730 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
733 "op" => "move_following",
734 "origin_id" => user.id,
735 "target_id" => target_user.id
738 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
740 Pleroma.Workers.BackgroundWorker.perform(%Oban.Job{args: params})
742 refute User.following?(follower, user)
743 assert User.following?(follower, target_user)
747 describe "GET /api/pleroma/aliases" do
748 setup do: oauth_access(["read:accounts"])
750 test "without permissions", %{conn: conn} do
753 |> assign(:token, nil)
754 |> get("/api/pleroma/aliases")
756 assert json_response_and_validate_schema(conn, 403) == %{
757 "error" => "Insufficient permissions: read:accounts."
761 test "with permissions", %{
764 assert %{"aliases" => []} =
766 |> get("/api/pleroma/aliases")
767 |> json_response_and_validate_schema(200)
770 test "with permissions and aliases", %{} do
772 user2 = insert(:user)
774 assert {:ok, user} = user |> User.add_alias(user2)
776 %{user: _user, conn: conn} = oauth_access(["read:accounts"], user: user)
778 assert %{"aliases" => aliases} =
780 |> get("/api/pleroma/aliases")
781 |> json_response_and_validate_schema(200)
783 assert aliases == [user2 |> User.full_nickname()]
787 describe "PUT /api/pleroma/aliases" do
788 setup do: oauth_access(["write:accounts"])
790 test "without permissions", %{conn: conn} do
793 |> assign(:token, nil)
794 |> put_req_header("content-type", "application/json")
795 |> put("/api/pleroma/aliases", %{alias: "none"})
797 assert json_response_and_validate_schema(conn, 403) == %{
798 "error" => "Insufficient permissions: write:accounts."
802 test "with permissions, no alias param", %{
807 |> put_req_header("content-type", "application/json")
808 |> put("/api/pleroma/aliases", %{})
810 assert %{"error" => "Missing field: alias."} = json_response_and_validate_schema(conn, 400)
813 test "with permissions, with alias param", %{
816 user2 = insert(:user)
820 |> put_req_header("content-type", "application/json")
821 |> put("/api/pleroma/aliases", %{alias: user2 |> User.full_nickname()})
823 assert json_response_and_validate_schema(conn, 200) == %{
824 "status" => "success"
829 describe "DELETE /api/pleroma/aliases" do
831 alias_user = insert(:user)
832 non_alias_user = insert(:user)
833 user = insert(:user, also_known_as: [alias_user.ap_id])
835 oauth_access(["write:accounts"], user: user)
836 |> Map.put(:alias_user, alias_user)
837 |> Map.put(:non_alias_user, non_alias_user)
840 test "without permissions", %{conn: conn} do
843 |> assign(:token, nil)
844 |> put_req_header("content-type", "application/json")
845 |> delete("/api/pleroma/aliases", %{alias: "none"})
847 assert json_response_and_validate_schema(conn, 403) == %{
848 "error" => "Insufficient permissions: write:accounts."
852 test "with permissions, no alias param", %{conn: conn} do
855 |> put_req_header("content-type", "application/json")
856 |> delete("/api/pleroma/aliases", %{})
858 assert %{"error" => "Missing field: alias."} = json_response_and_validate_schema(conn, 400)
861 test "with permissions, account does not have such alias", %{
863 non_alias_user: non_alias_user
867 |> put_req_header("content-type", "application/json")
868 |> delete("/api/pleroma/aliases", %{alias: non_alias_user |> User.full_nickname()})
870 assert %{"error" => "Account has no such alias."} =
871 json_response_and_validate_schema(conn, 404)
874 test "with permissions, account does have such alias", %{
876 alias_user: alias_user
880 |> put_req_header("content-type", "application/json")
881 |> delete("/api/pleroma/aliases", %{alias: alias_user |> User.full_nickname()})
883 assert %{"status" => "success"} = json_response_and_validate_schema(conn, 200)