1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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
11 alias Pleroma.Web.CommonAPI
13 import ExUnit.CaptureLog
14 import Pleroma.Factory
18 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 clear_config([:instance])
23 clear_config([:frontend_configurations, :pleroma_fe])
24 clear_config([:user, :deny_follow_blocked])
26 describe "POST /api/pleroma/follow_import" do
27 setup do: oauth_access(["follow"])
29 test "it returns HTTP 200", %{conn: conn} do
34 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
37 assert response == "job started"
40 test "it imports follow lists from file", %{user: user1, conn: conn} do
45 read!: fn "follow_list.txt" ->
46 "Account address,Show boosts\n#{user2.ap_id},true"
51 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
54 assert response == "job started"
56 assert ObanHelpers.member?(
58 "op" => "follow_import",
59 "follower_id" => user1.id,
60 "followed_identifiers" => [user2.ap_id]
62 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
67 test "it imports new-style mastodon follow lists", %{conn: conn} do
72 |> post("/api/pleroma/follow_import", %{
73 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
77 assert response == "job started"
80 test "requires 'follow' or 'write:follows' permissions" do
81 token1 = insert(:oauth_token, scopes: ["read", "write"])
82 token2 = insert(:oauth_token, scopes: ["follow"])
83 token3 = insert(:oauth_token, scopes: ["something"])
84 another_user = insert(:user)
86 for token <- [token1, token2, token3] do
89 |> put_req_header("authorization", "Bearer #{token.token}")
90 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
93 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
94 json_response(conn, 403)
96 assert json_response(conn, 200)
102 describe "POST /api/pleroma/blocks_import" do
103 # Note: "follow" or "write:blocks" permission is required
104 setup do: oauth_access(["write:blocks"])
106 test "it returns HTTP 200", %{conn: conn} do
107 user2 = insert(:user)
111 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
112 |> json_response(:ok)
114 assert response == "job started"
117 test "it imports blocks users from file", %{user: user1, conn: conn} do
118 user2 = insert(:user)
119 user3 = insert(:user)
122 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
126 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
127 |> json_response(:ok)
129 assert response == "job started"
131 assert ObanHelpers.member?(
133 "op" => "blocks_import",
134 "blocker_id" => user1.id,
135 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
137 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
143 describe "PUT /api/pleroma/notification_settings" do
144 setup do: oauth_access(["write:accounts"])
146 test "it updates notification settings", %{user: user, conn: conn} do
148 |> put("/api/pleroma/notification_settings", %{
149 "followers" => false,
152 |> json_response(:ok)
154 user = refresh_record(user)
156 assert %Pleroma.User.NotificationSetting{
161 privacy_option: false
162 } == user.notification_settings
165 test "it updates notification privacy option", %{user: user, conn: conn} do
167 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
168 |> json_response(:ok)
170 user = refresh_record(user)
172 assert %Pleroma.User.NotificationSetting{
178 } == user.notification_settings
182 describe "GET /api/statusnet/config" do
183 test "it returns config in xml format", %{conn: conn} do
184 instance = Pleroma.Config.get(:instance)
188 |> put_req_header("accept", "application/xml")
189 |> get("/api/statusnet/config")
193 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
194 Pleroma.Web.base_url()
195 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
196 !Keyword.get(instance, :registrations_open)
197 }</closed>\n</site>\n</config>\n"
200 test "it returns config in json format", %{conn: conn} do
201 instance = Pleroma.Config.get(:instance)
202 Pleroma.Config.put([:instance, :managed_config], true)
203 Pleroma.Config.put([:instance, :registrations_open], false)
204 Pleroma.Config.put([:instance, :invites_enabled], true)
205 Pleroma.Config.put([:instance, :public], false)
206 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
210 |> put_req_header("accept", "application/json")
211 |> get("/api/statusnet/config")
212 |> json_response(:ok)
216 "accountActivationRequired" => "0",
218 "description" => Keyword.get(instance, :description),
219 "invitesEnabled" => "1",
220 "name" => Keyword.get(instance, :name),
221 "pleromafe" => %{"theme" => "asuka-hospital"},
223 "safeDMMentionsEnabled" => "0",
224 "server" => Pleroma.Web.base_url(),
225 "textlimit" => to_string(Keyword.get(instance, :limit)),
227 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
228 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
229 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
230 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
232 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
236 assert response == expected_data
239 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
240 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
244 |> get("/api/statusnet/config.json")
245 |> json_response(:ok)
247 assert response["site"]["safeDMMentionsEnabled"] == "1"
249 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
253 |> get("/api/statusnet/config.json")
254 |> json_response(:ok)
256 assert response["site"]["safeDMMentionsEnabled"] == "0"
259 test "it returns the managed config", %{conn: conn} do
260 Pleroma.Config.put([:instance, :managed_config], false)
261 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
265 |> get("/api/statusnet/config.json")
266 |> json_response(:ok)
268 refute response["site"]["pleromafe"]
270 Pleroma.Config.put([:instance, :managed_config], true)
274 |> get("/api/statusnet/config.json")
275 |> json_response(:ok)
277 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
281 describe "GET /api/pleroma/frontend_configurations" do
282 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
293 Pleroma.Config.put(:frontend_configurations, config)
297 |> get("/api/pleroma/frontend_configurations")
298 |> json_response(:ok)
300 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
304 describe "/api/pleroma/emoji" do
305 test "returns json with custom emoji with tags", %{conn: conn} do
308 |> get("/api/pleroma/emoji")
309 |> json_response(200)
311 assert Enum.all?(emoji, fn
317 is_binary(url) and is_list(tags)
322 describe "GET /ostatus_subscribe - remote_follow/2" do
323 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
327 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
330 assert redirected_to(conn) =~ "/notice/"
333 test "show follow account page if the `acct` is a account link", %{conn: conn} do
337 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
340 assert html_response(response, 200) =~ "Log in to follow"
343 test "show follow page if the `acct` is a account link", %{conn: conn} do
348 |> assign(:user, user)
349 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
351 assert html_response(response, 200) =~ "Remote follow"
354 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
357 assert capture_log(fn ->
360 |> assign(:user, user)
361 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
363 assert html_response(response, 200) =~ "Error fetching user"
364 end) =~ "Object has been deleted"
368 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user" do
369 setup do: oauth_access(["follow"])
371 test "follows user", %{user: user, conn: conn} do
372 user2 = insert(:user)
376 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
379 assert response =~ "Account followed!"
380 assert user2.follower_address in User.following(user)
383 test "returns error when user is deactivated" do
384 user = insert(:user, deactivated: true)
385 user2 = insert(:user)
389 |> assign(:user, user)
390 |> assign(:token, insert(:oauth_token, user: user, scopes: ["follow"]))
391 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
394 assert response =~ "Error following account"
397 test "returns error when user is blocked", %{user: user, conn: conn} do
398 Pleroma.Config.put([:user, :deny_follow_blocked], true)
399 user2 = insert(:user)
401 {:ok, _user_block} = Pleroma.User.block(user2, user)
405 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
408 assert response =~ "Error following account"
411 test "returns error on insufficient permissions", %{user: user, conn: conn} do
412 user2 = insert(:user)
414 for token <- [nil, insert(:oauth_token, user: user, scopes: ["read"])] do
417 |> assign(:token, token)
418 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
421 assert response =~ "Error following account"
425 test "returns error when followee not found", %{conn: conn} do
428 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
431 assert response =~ "Error following account"
434 test "returns success result when user already in followers", %{user: user, conn: conn} do
435 user2 = insert(:user)
436 {:ok, _, _, _} = CommonAPI.follow(user, user2)
440 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
443 assert response =~ "Account followed!"
447 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user" do
448 test "follows", %{conn: conn} do
450 user2 = insert(:user)
454 |> post("/ostatus_subscribe", %{
455 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
459 assert response =~ "Account followed!"
460 assert user2.follower_address in User.following(user)
463 test "returns error when followee not found", %{conn: conn} do
468 |> post("/ostatus_subscribe", %{
469 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
473 assert response =~ "Error following account"
476 test "returns error when login invalid", %{conn: conn} do
481 |> post("/ostatus_subscribe", %{
482 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
486 assert response =~ "Wrong username or password"
489 test "returns error when password invalid", %{conn: conn} do
491 user2 = insert(:user)
495 |> post("/ostatus_subscribe", %{
496 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
500 assert response =~ "Wrong username or password"
503 test "returns error when user is blocked", %{conn: conn} do
504 Pleroma.Config.put([:user, :deny_follow_blocked], true)
506 user2 = insert(:user)
507 {:ok, _user_block} = Pleroma.User.block(user2, user)
511 |> post("/ostatus_subscribe", %{
512 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
516 assert response =~ "Error following account"
520 describe "GET /api/pleroma/healthcheck" do
521 clear_config([:instance, :healthcheck])
523 test "returns 503 when healthcheck disabled", %{conn: conn} do
524 Pleroma.Config.put([:instance, :healthcheck], false)
528 |> get("/api/pleroma/healthcheck")
529 |> json_response(503)
531 assert response == %{}
534 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
535 Pleroma.Config.put([:instance, :healthcheck], true)
537 with_mock Pleroma.Healthcheck,
538 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
541 |> get("/api/pleroma/healthcheck")
542 |> json_response(200)
554 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
555 Pleroma.Config.put([:instance, :healthcheck], true)
557 with_mock Pleroma.Healthcheck,
558 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
561 |> get("/api/pleroma/healthcheck")
562 |> json_response(503)
575 describe "POST /api/pleroma/disable_account" do
576 setup do: oauth_access(["write:accounts"])
578 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
581 |> post("/api/pleroma/disable_account", %{"password" => "test"})
582 |> json_response(:ok)
584 assert response == %{"status" => "success"}
585 ObanHelpers.perform_all()
587 user = User.get_cached_by_id(user.id)
589 assert user.deactivated == true
592 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
597 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
598 |> json_response(:ok)
600 assert response == %{"error" => "Invalid password."}
601 user = User.get_cached_by_id(user.id)
603 refute user.deactivated
607 describe "GET /api/statusnet/version" do
608 test "it returns version in xml format", %{conn: conn} do
611 |> put_req_header("accept", "application/xml")
612 |> get("/api/statusnet/version")
615 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
618 test "it returns version in json format", %{conn: conn} do
621 |> put_req_header("accept", "application/json")
622 |> get("/api/statusnet/version")
623 |> json_response(:ok)
625 assert response == "#{Pleroma.Application.named_version()}"
629 describe "POST /main/ostatus - remote_subscribe/2" do
630 test "renders subscribe form", %{conn: conn} do
635 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
638 refute response =~ "Could not find user"
639 assert response =~ "Remotely follow #{user.nickname}"
642 test "renders subscribe form with error when user not found", %{conn: conn} do
645 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
648 assert response =~ "Could not find user"
649 refute response =~ "Remotely follow"
652 test "it redirect to webfinger url", %{conn: conn} do
654 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
658 |> post("/main/ostatus", %{
659 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
662 assert redirected_to(conn) ==
663 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
666 test "it renders form with error when user not found", %{conn: conn} do
667 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
671 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
674 assert response =~ "Something went wrong."
678 test "it returns new captcha", %{conn: conn} do
679 with_mock Pleroma.Captcha,
680 new: fn -> "test_captcha" end do
683 |> get("/api/pleroma/captcha")
686 assert resp == "\"test_captcha\""
687 assert called(Pleroma.Captcha.new())
691 describe "POST /api/pleroma/change_email" do
692 setup do: oauth_access(["write:accounts"])
694 test "without permissions", %{conn: conn} do
697 |> assign(:token, nil)
698 |> post("/api/pleroma/change_email")
700 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
703 test "with proper permissions and invalid password", %{conn: conn} do
705 post(conn, "/api/pleroma/change_email", %{
707 "email" => "test@test.com"
710 assert json_response(conn, 200) == %{"error" => "Invalid password."}
713 test "with proper permissions, valid password and invalid email", %{
717 post(conn, "/api/pleroma/change_email", %{
718 "password" => "test",
722 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
725 test "with proper permissions, valid password and no email", %{
729 post(conn, "/api/pleroma/change_email", %{
733 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
736 test "with proper permissions, valid password and blank email", %{
740 post(conn, "/api/pleroma/change_email", %{
741 "password" => "test",
745 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
748 test "with proper permissions, valid password and non unique email", %{
754 post(conn, "/api/pleroma/change_email", %{
755 "password" => "test",
756 "email" => user.email
759 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
762 test "with proper permissions, valid password and valid email", %{
766 post(conn, "/api/pleroma/change_email", %{
767 "password" => "test",
768 "email" => "cofe@foobar.com"
771 assert json_response(conn, 200) == %{"status" => "success"}
775 describe "POST /api/pleroma/change_password" do
776 setup do: oauth_access(["write:accounts"])
778 test "without permissions", %{conn: conn} do
781 |> assign(:token, nil)
782 |> post("/api/pleroma/change_password")
784 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
787 test "with proper permissions and invalid password", %{conn: conn} do
789 post(conn, "/api/pleroma/change_password", %{
791 "new_password" => "newpass",
792 "new_password_confirmation" => "newpass"
795 assert json_response(conn, 200) == %{"error" => "Invalid password."}
798 test "with proper permissions, valid password and new password and confirmation not matching",
803 post(conn, "/api/pleroma/change_password", %{
804 "password" => "test",
805 "new_password" => "newpass",
806 "new_password_confirmation" => "notnewpass"
809 assert json_response(conn, 200) == %{
810 "error" => "New password does not match confirmation."
814 test "with proper permissions, valid password and invalid new password", %{
818 post(conn, "/api/pleroma/change_password", %{
819 "password" => "test",
820 "new_password" => "",
821 "new_password_confirmation" => ""
824 assert json_response(conn, 200) == %{
825 "error" => "New password can't be blank."
829 test "with proper permissions, valid password and matching new password and confirmation", %{
834 post(conn, "/api/pleroma/change_password", %{
835 "password" => "test",
836 "new_password" => "newpass",
837 "new_password_confirmation" => "newpass"
840 assert json_response(conn, 200) == %{"status" => "success"}
841 fetched_user = User.get_cached_by_id(user.id)
842 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
846 describe "POST /api/pleroma/delete_account" do
847 setup do: oauth_access(["write:accounts"])
849 test "without permissions", %{conn: conn} do
852 |> assign(:token, nil)
853 |> post("/api/pleroma/delete_account")
855 assert json_response(conn, 403) ==
856 %{"error" => "Insufficient permissions: write:accounts."}
859 test "with proper permissions and wrong or missing password", %{conn: conn} do
860 for params <- [%{"password" => "hi"}, %{}] do
861 ret_conn = post(conn, "/api/pleroma/delete_account", params)
863 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
867 test "with proper permissions and valid password", %{conn: conn} do
868 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
870 assert json_response(conn, 200) == %{"status" => "success"}