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
10 alias Pleroma.Tests.ObanHelpers
12 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 test "it returns HTTP 200", %{conn: conn} do
33 |> assign(:user, user1)
34 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
37 assert response == "job started"
40 test "it imports follow lists from file", %{conn: conn} do
46 read!: fn "follow_list.txt" ->
47 "Account address,Show boosts\n#{user2.ap_id},true"
52 |> assign(:user, user1)
53 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
56 assert response == "job started"
58 assert ObanHelpers.member?(
60 "op" => "follow_import",
61 "follower_id" => user1.id,
62 "followed_identifiers" => [user2.ap_id]
64 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
69 test "it imports new-style mastodon follow lists", %{conn: conn} do
75 |> assign(:user, user1)
76 |> post("/api/pleroma/follow_import", %{
77 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
81 assert response == "job started"
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)
90 for token <- [token1, token2, token3] do
93 |> put_req_header("authorization", "Bearer #{token.token}")
94 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
97 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
98 json_response(conn, 403)
100 assert json_response(conn, 200)
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)
113 |> assign(:user, user1)
114 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
115 |> json_response(:ok)
117 assert response == "job started"
120 test "it imports blocks users from file", %{conn: conn} do
121 user1 = insert(:user)
122 user2 = insert(:user)
123 user3 = insert(:user)
126 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
130 |> assign(:user, user1)
131 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
132 |> json_response(:ok)
134 assert response == "job started"
136 assert ObanHelpers.member?(
138 "op" => "blocks_import",
139 "blocker_id" => user1.id,
140 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
142 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
148 describe "PUT /api/pleroma/notification_settings" do
149 test "it updates notification settings", %{conn: conn} do
153 |> assign(:user, user)
154 |> put("/api/pleroma/notification_settings", %{
155 "followers" => false,
158 |> json_response(:ok)
160 user = Repo.get(User, user.id)
163 "followers" => false,
165 "non_follows" => true,
166 "non_followers" => true
167 } == user.info.notification_settings
171 describe "GET /api/statusnet/config" do
172 test "it returns config in xml format", %{conn: conn} do
173 instance = Pleroma.Config.get(:instance)
177 |> put_req_header("accept", "application/xml")
178 |> get("/api/statusnet/config")
182 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
183 Pleroma.Web.base_url()
184 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
185 !Keyword.get(instance, :registrations_open)
186 }</closed>\n</site>\n</config>\n"
189 test "it returns config in json format", %{conn: conn} do
190 instance = Pleroma.Config.get(:instance)
191 Pleroma.Config.put([:instance, :managed_config], true)
192 Pleroma.Config.put([:instance, :registrations_open], false)
193 Pleroma.Config.put([:instance, :invites_enabled], true)
194 Pleroma.Config.put([:instance, :public], false)
195 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
199 |> put_req_header("accept", "application/json")
200 |> get("/api/statusnet/config")
201 |> json_response(:ok)
205 "accountActivationRequired" => "0",
207 "description" => Keyword.get(instance, :description),
208 "invitesEnabled" => "1",
209 "name" => Keyword.get(instance, :name),
210 "pleromafe" => %{"theme" => "asuka-hospital"},
212 "safeDMMentionsEnabled" => "0",
213 "server" => Pleroma.Web.base_url(),
214 "textlimit" => to_string(Keyword.get(instance, :limit)),
216 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
217 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
218 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
219 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
221 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
225 assert response == expected_data
228 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
229 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
233 |> get("/api/statusnet/config.json")
234 |> json_response(:ok)
236 assert response["site"]["safeDMMentionsEnabled"] == "1"
238 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
242 |> get("/api/statusnet/config.json")
243 |> json_response(:ok)
245 assert response["site"]["safeDMMentionsEnabled"] == "0"
248 test "it returns the managed config", %{conn: conn} do
249 Pleroma.Config.put([:instance, :managed_config], false)
250 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
254 |> get("/api/statusnet/config.json")
255 |> json_response(:ok)
257 refute response["site"]["pleromafe"]
259 Pleroma.Config.put([:instance, :managed_config], true)
263 |> get("/api/statusnet/config.json")
264 |> json_response(:ok)
266 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
270 describe "GET /api/pleroma/frontend_configurations" do
271 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
282 Pleroma.Config.put(:frontend_configurations, config)
286 |> get("/api/pleroma/frontend_configurations")
287 |> json_response(:ok)
289 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
293 describe "/api/pleroma/emoji" do
294 test "returns json with custom emoji with tags", %{conn: conn} do
297 |> get("/api/pleroma/emoji")
298 |> json_response(200)
300 assert Enum.all?(emoji, fn
306 is_binary(url) and is_list(tags)
311 describe "GET /ostatus_subscribe - remote_follow/2" do
312 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
316 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
319 assert redirected_to(conn) =~ "/notice/"
322 test "show follow account page if the `acct` is a account link", %{conn: conn} do
326 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
329 assert html_response(response, 200) =~ "Log in to follow"
332 test "show follow page if the `acct` is a account link", %{conn: conn} do
337 |> assign(:user, user)
338 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
340 assert html_response(response, 200) =~ "Remote follow"
343 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
346 assert capture_log(fn ->
349 |> assign(:user, user)
350 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
352 assert html_response(response, 200) =~ "Error fetching user"
353 end) =~ "Object has been deleted"
357 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
358 test "follows user", %{conn: conn} do
360 user2 = insert(:user)
364 |> assign(:user, user)
365 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
368 assert response =~ "Account followed!"
369 assert user2.follower_address in refresh_record(user).following
372 test "returns error when user is deactivated", %{conn: conn} do
373 user = insert(:user, info: %{deactivated: true})
374 user2 = insert(:user)
378 |> assign(:user, user)
379 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
382 assert response =~ "Error following account"
385 test "returns error when user is blocked", %{conn: conn} do
386 Pleroma.Config.put([:user, :deny_follow_blocked], true)
388 user2 = insert(:user)
390 {:ok, _user} = Pleroma.User.block(user2, user)
394 |> assign(:user, user)
395 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
398 assert response =~ "Error following account"
401 test "returns error when followee not found", %{conn: conn} do
406 |> assign(:user, user)
407 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
410 assert response =~ "Error following account"
413 test "returns success result when user already in followers", %{conn: conn} do
415 user2 = insert(:user)
416 {:ok, _, _, _} = CommonAPI.follow(user, user2)
420 |> assign(:user, refresh_record(user))
421 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
424 assert response =~ "Account followed!"
428 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
429 test "follows", %{conn: conn} do
431 user2 = insert(:user)
435 |> post("/ostatus_subscribe", %{
436 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
440 assert response =~ "Account followed!"
441 assert user2.follower_address in refresh_record(user).following
444 test "returns error when followee not found", %{conn: conn} do
449 |> post("/ostatus_subscribe", %{
450 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
454 assert response =~ "Error following account"
457 test "returns error when login invalid", %{conn: conn} do
462 |> post("/ostatus_subscribe", %{
463 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
467 assert response =~ "Wrong username or password"
470 test "returns error when password invalid", %{conn: conn} do
472 user2 = insert(:user)
476 |> post("/ostatus_subscribe", %{
477 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
481 assert response =~ "Wrong username or password"
484 test "returns error when user is blocked", %{conn: conn} do
485 Pleroma.Config.put([:user, :deny_follow_blocked], true)
487 user2 = insert(:user)
488 {:ok, _user} = Pleroma.User.block(user2, user)
492 |> post("/ostatus_subscribe", %{
493 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
497 assert response =~ "Error following account"
501 describe "GET /api/pleroma/healthcheck" do
502 clear_config([:instance, :healthcheck])
504 test "returns 503 when healthcheck disabled", %{conn: conn} do
505 Pleroma.Config.put([:instance, :healthcheck], false)
509 |> get("/api/pleroma/healthcheck")
510 |> json_response(503)
512 assert response == %{}
515 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
516 Pleroma.Config.put([:instance, :healthcheck], true)
518 with_mock Pleroma.Healthcheck,
519 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
522 |> get("/api/pleroma/healthcheck")
523 |> json_response(200)
535 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
536 Pleroma.Config.put([:instance, :healthcheck], true)
538 with_mock Pleroma.Healthcheck,
539 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
542 |> get("/api/pleroma/healthcheck")
543 |> json_response(503)
556 describe "POST /api/pleroma/disable_account" do
557 test "it returns HTTP 200", %{conn: conn} do
562 |> assign(:user, user)
563 |> post("/api/pleroma/disable_account", %{"password" => "test"})
564 |> json_response(:ok)
566 assert response == %{"status" => "success"}
567 ObanHelpers.perform_all()
569 user = User.get_cached_by_id(user.id)
571 assert user.info.deactivated == true
574 test "it returns returns when password invalid", %{conn: conn} do
579 |> assign(:user, user)
580 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
581 |> json_response(:ok)
583 assert response == %{"error" => "Invalid password."}
584 user = User.get_cached_by_id(user.id)
586 refute user.info.deactivated
590 describe "GET /api/statusnet/version" do
591 test "it returns version in xml format", %{conn: conn} do
594 |> put_req_header("accept", "application/xml")
595 |> get("/api/statusnet/version")
598 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
601 test "it returns version in json format", %{conn: conn} do
604 |> put_req_header("accept", "application/json")
605 |> get("/api/statusnet/version")
606 |> json_response(:ok)
608 assert response == "#{Pleroma.Application.named_version()}"
612 describe "POST /main/ostatus - remote_subscribe/2" do
613 test "renders subscribe form", %{conn: conn} do
618 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
621 refute response =~ "Could not find user"
622 assert response =~ "Remotely follow #{user.nickname}"
625 test "renders subscribe form with error when user not found", %{conn: conn} do
628 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
631 assert response =~ "Could not find user"
632 refute response =~ "Remotely follow"
635 test "it redirect to webfinger url", %{conn: conn} do
637 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
641 |> post("/main/ostatus", %{
642 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
645 assert redirected_to(conn) ==
646 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
649 test "it renders form with error when use not found", %{conn: conn} do
650 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
654 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
657 assert response =~ "Something went wrong."
661 test "it returns new captcha", %{conn: conn} do
662 with_mock Pleroma.Captcha,
663 new: fn -> "test_captcha" end do
666 |> get("/api/pleroma/captcha")
669 assert resp == "\"test_captcha\""
670 assert called(Pleroma.Captcha.new())
674 defp with_credentials(conn, username, password) do
675 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
676 put_req_header(conn, "authorization", header_content)
679 defp valid_user(_context) do
684 describe "POST /api/pleroma/change_email" do
687 test "without credentials", %{conn: conn} do
688 conn = post(conn, "/api/pleroma/change_email")
689 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
692 test "with credentials and invalid password", %{conn: conn, user: current_user} do
695 |> with_credentials(current_user.nickname, "test")
696 |> post("/api/pleroma/change_email", %{
698 "email" => "test@test.com"
701 assert json_response(conn, 200) == %{"error" => "Invalid password."}
704 test "with credentials, valid password and invalid email", %{
710 |> with_credentials(current_user.nickname, "test")
711 |> post("/api/pleroma/change_email", %{
712 "password" => "test",
716 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
719 test "with credentials, valid password and no email", %{
725 |> with_credentials(current_user.nickname, "test")
726 |> post("/api/pleroma/change_email", %{
730 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
733 test "with credentials, valid password and blank email", %{
739 |> with_credentials(current_user.nickname, "test")
740 |> post("/api/pleroma/change_email", %{
741 "password" => "test",
745 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
748 test "with credentials, valid password and non unique email", %{
756 |> with_credentials(current_user.nickname, "test")
757 |> post("/api/pleroma/change_email", %{
758 "password" => "test",
759 "email" => user.email
762 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
765 test "with credentials, valid password and valid email", %{
771 |> with_credentials(current_user.nickname, "test")
772 |> post("/api/pleroma/change_email", %{
773 "password" => "test",
774 "email" => "cofe@foobar.com"
777 assert json_response(conn, 200) == %{"status" => "success"}
781 describe "POST /api/pleroma/change_password" do
784 test "without credentials", %{conn: conn} do
785 conn = post(conn, "/api/pleroma/change_password")
786 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
789 test "with credentials and invalid password", %{conn: conn, user: current_user} do
792 |> with_credentials(current_user.nickname, "test")
793 |> post("/api/pleroma/change_password", %{
795 "new_password" => "newpass",
796 "new_password_confirmation" => "newpass"
799 assert json_response(conn, 200) == %{"error" => "Invalid password."}
802 test "with credentials, valid password and new password and confirmation not matching", %{
808 |> with_credentials(current_user.nickname, "test")
809 |> post("/api/pleroma/change_password", %{
810 "password" => "test",
811 "new_password" => "newpass",
812 "new_password_confirmation" => "notnewpass"
815 assert json_response(conn, 200) == %{
816 "error" => "New password does not match confirmation."
820 test "with credentials, valid password and invalid new password", %{
826 |> with_credentials(current_user.nickname, "test")
827 |> post("/api/pleroma/change_password", %{
828 "password" => "test",
829 "new_password" => "",
830 "new_password_confirmation" => ""
833 assert json_response(conn, 200) == %{
834 "error" => "New password can't be blank."
838 test "with credentials, valid password and matching new password and confirmation", %{
844 |> with_credentials(current_user.nickname, "test")
845 |> post("/api/pleroma/change_password", %{
846 "password" => "test",
847 "new_password" => "newpass",
848 "new_password_confirmation" => "newpass"
851 assert json_response(conn, 200) == %{"status" => "success"}
852 fetched_user = User.get_cached_by_id(current_user.id)
853 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
857 describe "POST /api/pleroma/delete_account" do
860 test "without credentials", %{conn: conn} do
861 conn = post(conn, "/api/pleroma/delete_account")
862 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
865 test "with credentials and invalid password", %{conn: conn, user: current_user} do
868 |> with_credentials(current_user.nickname, "test")
869 |> post("/api/pleroma/delete_account", %{"password" => "hi"})
871 assert json_response(conn, 200) == %{"error" => "Invalid password."}
874 test "with credentials and valid password", %{conn: conn, user: current_user} do
877 |> with_credentials(current_user.nickname, "test")
878 |> post("/api/pleroma/delete_account", %{"password" => "test"})
880 assert json_response(conn, 200) == %{"status" => "success"}
881 # Wait a second for the started task to end