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)
162 assert %Pleroma.User.NotificationSetting{
167 privacy_option: "name_and_message"
168 } == user.notification_settings
171 test "it update notificatin privacy option", %{conn: conn} do
175 |> assign(:user, user)
176 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "name_only"})
177 |> json_response(:ok)
179 user = refresh_record(user)
181 assert %Pleroma.User.NotificationSetting{
186 privacy_option: "name_only"
187 } == user.notification_settings
191 describe "GET /api/statusnet/config" do
192 test "it returns config in xml format", %{conn: conn} do
193 instance = Pleroma.Config.get(:instance)
197 |> put_req_header("accept", "application/xml")
198 |> get("/api/statusnet/config")
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"
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"})
219 |> put_req_header("accept", "application/json")
220 |> get("/api/statusnet/config")
221 |> json_response(:ok)
225 "accountActivationRequired" => "0",
227 "description" => Keyword.get(instance, :description),
228 "invitesEnabled" => "1",
229 "name" => Keyword.get(instance, :name),
230 "pleromafe" => %{"theme" => "asuka-hospital"},
232 "safeDMMentionsEnabled" => "0",
233 "server" => Pleroma.Web.base_url(),
234 "textlimit" => to_string(Keyword.get(instance, :limit)),
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))
241 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
245 assert response == expected_data
248 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
249 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
253 |> get("/api/statusnet/config.json")
254 |> json_response(:ok)
256 assert response["site"]["safeDMMentionsEnabled"] == "1"
258 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
262 |> get("/api/statusnet/config.json")
263 |> json_response(:ok)
265 assert response["site"]["safeDMMentionsEnabled"] == "0"
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"})
274 |> get("/api/statusnet/config.json")
275 |> json_response(:ok)
277 refute response["site"]["pleromafe"]
279 Pleroma.Config.put([:instance, :managed_config], true)
283 |> get("/api/statusnet/config.json")
284 |> json_response(:ok)
286 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
290 describe "GET /api/pleroma/frontend_configurations" do
291 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
302 Pleroma.Config.put(:frontend_configurations, config)
306 |> get("/api/pleroma/frontend_configurations")
307 |> json_response(:ok)
309 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
313 describe "/api/pleroma/emoji" do
314 test "returns json with custom emoji with tags", %{conn: conn} do
317 |> get("/api/pleroma/emoji")
318 |> json_response(200)
320 assert Enum.all?(emoji, fn
326 is_binary(url) and is_list(tags)
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
336 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
339 assert redirected_to(conn) =~ "/notice/"
342 test "show follow account page if the `acct` is a account link", %{conn: conn} do
346 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
349 assert html_response(response, 200) =~ "Log in to follow"
352 test "show follow page if the `acct` is a account link", %{conn: conn} do
357 |> assign(:user, user)
358 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
360 assert html_response(response, 200) =~ "Remote follow"
363 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
366 assert capture_log(fn ->
369 |> assign(:user, user)
370 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
372 assert html_response(response, 200) =~ "Error fetching user"
373 end) =~ "Object has been deleted"
377 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
378 test "follows user", %{conn: conn} do
380 user2 = insert(:user)
384 |> assign(:user, user)
385 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
388 assert response =~ "Account followed!"
389 assert user2.follower_address in User.following(user)
392 test "returns error when user is deactivated", %{conn: conn} do
393 user = insert(:user, deactivated: true)
394 user2 = insert(:user)
398 |> assign(:user, user)
399 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
402 assert response =~ "Error following account"
405 test "returns error when user is blocked", %{conn: conn} do
406 Pleroma.Config.put([:user, :deny_follow_blocked], true)
408 user2 = insert(:user)
410 {:ok, _user} = Pleroma.User.block(user2, user)
414 |> assign(:user, user)
415 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
418 assert response =~ "Error following account"
421 test "returns error when followee not found", %{conn: conn} do
426 |> assign(:user, user)
427 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
430 assert response =~ "Error following account"
433 test "returns success result when user already in followers", %{conn: conn} do
435 user2 = insert(:user)
436 {:ok, _, _, _} = CommonAPI.follow(user, user2)
440 |> assign(:user, refresh_record(user))
441 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
444 assert response =~ "Account followed!"
448 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
449 test "follows", %{conn: conn} do
451 user2 = insert(:user)
455 |> post("/ostatus_subscribe", %{
456 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
460 assert response =~ "Account followed!"
461 assert user2.follower_address in User.following(user)
464 test "returns error when followee not found", %{conn: conn} do
469 |> post("/ostatus_subscribe", %{
470 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
474 assert response =~ "Error following account"
477 test "returns error when login invalid", %{conn: conn} do
482 |> post("/ostatus_subscribe", %{
483 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
487 assert response =~ "Wrong username or password"
490 test "returns error when password invalid", %{conn: conn} do
492 user2 = insert(:user)
496 |> post("/ostatus_subscribe", %{
497 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
501 assert response =~ "Wrong username or password"
504 test "returns error when user is blocked", %{conn: conn} do
505 Pleroma.Config.put([:user, :deny_follow_blocked], true)
507 user2 = insert(:user)
508 {:ok, _user} = Pleroma.User.block(user2, user)
512 |> post("/ostatus_subscribe", %{
513 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
517 assert response =~ "Error following account"
521 describe "GET /api/pleroma/healthcheck" do
522 clear_config([:instance, :healthcheck])
524 test "returns 503 when healthcheck disabled", %{conn: conn} do
525 Pleroma.Config.put([:instance, :healthcheck], false)
529 |> get("/api/pleroma/healthcheck")
530 |> json_response(503)
532 assert response == %{}
535 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
536 Pleroma.Config.put([:instance, :healthcheck], true)
538 with_mock Pleroma.Healthcheck,
539 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
542 |> get("/api/pleroma/healthcheck")
543 |> json_response(200)
555 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
556 Pleroma.Config.put([:instance, :healthcheck], true)
558 with_mock Pleroma.Healthcheck,
559 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
562 |> get("/api/pleroma/healthcheck")
563 |> json_response(503)
576 describe "POST /api/pleroma/disable_account" do
577 test "it returns HTTP 200", %{conn: conn} do
582 |> assign(:user, user)
583 |> post("/api/pleroma/disable_account", %{"password" => "test"})
584 |> json_response(:ok)
586 assert response == %{"status" => "success"}
587 ObanHelpers.perform_all()
589 user = User.get_cached_by_id(user.id)
591 assert user.deactivated == true
594 test "it returns returns when password invalid", %{conn: conn} do
599 |> assign(:user, user)
600 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
601 |> json_response(:ok)
603 assert response == %{"error" => "Invalid password."}
604 user = User.get_cached_by_id(user.id)
606 refute user.deactivated
610 describe "GET /api/statusnet/version" do
611 test "it returns version in xml format", %{conn: conn} do
614 |> put_req_header("accept", "application/xml")
615 |> get("/api/statusnet/version")
618 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
621 test "it returns version in json format", %{conn: conn} do
624 |> put_req_header("accept", "application/json")
625 |> get("/api/statusnet/version")
626 |> json_response(:ok)
628 assert response == "#{Pleroma.Application.named_version()}"
632 describe "POST /main/ostatus - remote_subscribe/2" do
633 test "renders subscribe form", %{conn: conn} do
638 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
641 refute response =~ "Could not find user"
642 assert response =~ "Remotely follow #{user.nickname}"
645 test "renders subscribe form with error when user not found", %{conn: conn} do
648 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
651 assert response =~ "Could not find user"
652 refute response =~ "Remotely follow"
655 test "it redirect to webfinger url", %{conn: conn} do
657 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
661 |> post("/main/ostatus", %{
662 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
665 assert redirected_to(conn) ==
666 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
669 test "it renders form with error when use not found", %{conn: conn} do
670 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
674 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
677 assert response =~ "Something went wrong."
681 test "it returns new captcha", %{conn: conn} do
682 with_mock Pleroma.Captcha,
683 new: fn -> "test_captcha" end do
686 |> get("/api/pleroma/captcha")
689 assert resp == "\"test_captcha\""
690 assert called(Pleroma.Captcha.new())
694 defp with_credentials(conn, username, password) do
695 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
696 put_req_header(conn, "authorization", header_content)
699 defp valid_user(_context) do
704 describe "POST /api/pleroma/change_email" do
707 test "without credentials", %{conn: conn} do
708 conn = post(conn, "/api/pleroma/change_email")
709 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
712 test "with credentials and invalid password", %{conn: conn, user: current_user} do
715 |> with_credentials(current_user.nickname, "test")
716 |> post("/api/pleroma/change_email", %{
718 "email" => "test@test.com"
721 assert json_response(conn, 200) == %{"error" => "Invalid password."}
724 test "with credentials, valid password and invalid email", %{
730 |> with_credentials(current_user.nickname, "test")
731 |> post("/api/pleroma/change_email", %{
732 "password" => "test",
736 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
739 test "with credentials, valid password and no email", %{
745 |> with_credentials(current_user.nickname, "test")
746 |> post("/api/pleroma/change_email", %{
750 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
753 test "with credentials, valid password and blank email", %{
759 |> with_credentials(current_user.nickname, "test")
760 |> post("/api/pleroma/change_email", %{
761 "password" => "test",
765 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
768 test "with credentials, valid password and non unique email", %{
776 |> with_credentials(current_user.nickname, "test")
777 |> post("/api/pleroma/change_email", %{
778 "password" => "test",
779 "email" => user.email
782 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
785 test "with credentials, valid password and valid email", %{
791 |> with_credentials(current_user.nickname, "test")
792 |> post("/api/pleroma/change_email", %{
793 "password" => "test",
794 "email" => "cofe@foobar.com"
797 assert json_response(conn, 200) == %{"status" => "success"}
801 describe "POST /api/pleroma/change_password" do
804 test "without credentials", %{conn: conn} do
805 conn = post(conn, "/api/pleroma/change_password")
806 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
809 test "with credentials and invalid password", %{conn: conn, user: current_user} do
812 |> with_credentials(current_user.nickname, "test")
813 |> post("/api/pleroma/change_password", %{
815 "new_password" => "newpass",
816 "new_password_confirmation" => "newpass"
819 assert json_response(conn, 200) == %{"error" => "Invalid password."}
822 test "with credentials, valid password and new password and confirmation not matching", %{
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"
835 assert json_response(conn, 200) == %{
836 "error" => "New password does not match confirmation."
840 test "with credentials, valid password and invalid new password", %{
846 |> with_credentials(current_user.nickname, "test")
847 |> post("/api/pleroma/change_password", %{
848 "password" => "test",
849 "new_password" => "",
850 "new_password_confirmation" => ""
853 assert json_response(conn, 200) == %{
854 "error" => "New password can't be blank."
858 test "with credentials, valid password and matching new password and confirmation", %{
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"
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
877 describe "POST /api/pleroma/delete_account" do
880 test "without credentials", %{conn: conn} do
881 conn = post(conn, "/api/pleroma/delete_account")
882 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
885 test "with credentials and invalid password", %{conn: conn, user: current_user} do
888 |> with_credentials(current_user.nickname, "test")
889 |> post("/api/pleroma/delete_account", %{"password" => "hi"})
891 assert json_response(conn, 200) == %{"error" => "Invalid password."}
894 test "with credentials and valid password", %{conn: conn, user: current_user} do
897 |> with_credentials(current_user.nickname, "test")
898 |> post("/api/pleroma/delete_account", %{"password" => "test"})
900 assert json_response(conn, 200) == %{"status" => "success"}
901 # Wait a second for the started task to end