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' permission", %{conn: conn} do
85 token1 = insert(:oauth_token, scopes: ["read", "write"])
86 token2 = insert(:oauth_token, scopes: ["follow"])
87 another_user = insert(:user)
89 for token <- [token1, token2] do
92 |> put_req_header("authorization", "Bearer #{token.token}")
93 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
96 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
98 assert json_response(conn, 200)
104 describe "POST /api/pleroma/blocks_import" do
105 test "it returns HTTP 200", %{conn: conn} do
106 user1 = insert(:user)
107 user2 = insert(:user)
111 |> assign(:user, user1)
112 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
113 |> json_response(:ok)
115 assert response == "job started"
118 test "it imports blocks users from file", %{conn: conn} do
119 user1 = insert(:user)
120 user2 = insert(:user)
121 user3 = insert(:user)
124 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
128 |> assign(:user, user1)
129 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
130 |> json_response(:ok)
132 assert response == "job started"
134 assert ObanHelpers.member?(
136 "op" => "blocks_import",
137 "blocker_id" => user1.id,
138 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
140 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
146 describe "PUT /api/pleroma/notification_settings" do
147 test "it updates notification settings", %{conn: conn} do
151 |> assign(:user, user)
152 |> put("/api/pleroma/notification_settings", %{
153 "followers" => false,
156 |> json_response(:ok)
158 user = Repo.get(User, user.id)
161 "followers" => false,
163 "non_follows" => true,
164 "non_followers" => true
165 } == user.info.notification_settings
169 describe "GET /api/statusnet/config" do
170 test "it returns config in xml format", %{conn: conn} do
171 instance = Pleroma.Config.get(:instance)
175 |> put_req_header("accept", "application/xml")
176 |> get("/api/statusnet/config")
180 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
181 Pleroma.Web.base_url()
182 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
183 !Keyword.get(instance, :registrations_open)
184 }</closed>\n</site>\n</config>\n"
187 test "it returns config in json format", %{conn: conn} do
188 instance = Pleroma.Config.get(:instance)
189 Pleroma.Config.put([:instance, :managed_config], true)
190 Pleroma.Config.put([:instance, :registrations_open], false)
191 Pleroma.Config.put([:instance, :invites_enabled], true)
192 Pleroma.Config.put([:instance, :public], false)
193 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
197 |> put_req_header("accept", "application/json")
198 |> get("/api/statusnet/config")
199 |> json_response(:ok)
203 "accountActivationRequired" => "0",
205 "description" => Keyword.get(instance, :description),
206 "invitesEnabled" => "1",
207 "name" => Keyword.get(instance, :name),
208 "pleromafe" => %{"theme" => "asuka-hospital"},
210 "safeDMMentionsEnabled" => "0",
211 "server" => Pleroma.Web.base_url(),
212 "textlimit" => to_string(Keyword.get(instance, :limit)),
214 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
215 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
216 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
217 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
219 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
223 assert response == expected_data
226 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
227 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
231 |> get("/api/statusnet/config.json")
232 |> json_response(:ok)
234 assert response["site"]["safeDMMentionsEnabled"] == "1"
236 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
240 |> get("/api/statusnet/config.json")
241 |> json_response(:ok)
243 assert response["site"]["safeDMMentionsEnabled"] == "0"
246 test "it returns the managed config", %{conn: conn} do
247 Pleroma.Config.put([:instance, :managed_config], false)
248 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
252 |> get("/api/statusnet/config.json")
253 |> json_response(:ok)
255 refute response["site"]["pleromafe"]
257 Pleroma.Config.put([:instance, :managed_config], true)
261 |> get("/api/statusnet/config.json")
262 |> json_response(:ok)
264 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
268 describe "GET /api/pleroma/frontend_configurations" do
269 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
280 Pleroma.Config.put(:frontend_configurations, config)
284 |> get("/api/pleroma/frontend_configurations")
285 |> json_response(:ok)
287 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
291 describe "/api/pleroma/emoji" do
292 test "returns json with custom emoji with tags", %{conn: conn} do
295 |> get("/api/pleroma/emoji")
296 |> json_response(200)
298 assert Enum.all?(emoji, fn
304 is_binary(url) and is_list(tags)
309 describe "GET /ostatus_subscribe - remote_follow/2" do
310 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
314 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
317 assert redirected_to(conn) =~ "/notice/"
320 test "show follow account page if the `acct` is a account link", %{conn: conn} do
324 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
327 assert html_response(response, 200) =~ "Log in to follow"
330 test "show follow page if the `acct` is a account link", %{conn: conn} do
335 |> assign(:user, user)
336 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
338 assert html_response(response, 200) =~ "Remote follow"
341 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
344 assert capture_log(fn ->
347 |> assign(:user, user)
348 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
350 assert html_response(response, 200) =~ "Error fetching user"
351 end) =~ "Object has been deleted"
355 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
356 test "follows user", %{conn: conn} do
358 user2 = insert(:user)
362 |> assign(:user, user)
363 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
366 assert response =~ "Account followed!"
367 assert user2.follower_address in refresh_record(user).following
370 test "returns error when user is deactivated", %{conn: conn} do
371 user = insert(:user, info: %{deactivated: true})
372 user2 = insert(:user)
376 |> assign(:user, user)
377 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
380 assert response =~ "Error following account"
383 test "returns error when user is blocked", %{conn: conn} do
384 Pleroma.Config.put([:user, :deny_follow_blocked], true)
386 user2 = insert(:user)
388 {:ok, _user} = Pleroma.User.block(user2, user)
392 |> assign(:user, user)
393 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
396 assert response =~ "Error following account"
399 test "returns error when followee not found", %{conn: conn} do
404 |> assign(:user, user)
405 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
408 assert response =~ "Error following account"
411 test "returns success result when user already in followers", %{conn: conn} do
413 user2 = insert(:user)
414 {:ok, _, _, _} = CommonAPI.follow(user, user2)
418 |> assign(:user, refresh_record(user))
419 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
422 assert response =~ "Account followed!"
426 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
427 test "follows", %{conn: conn} do
429 user2 = insert(:user)
433 |> post("/ostatus_subscribe", %{
434 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
438 assert response =~ "Account followed!"
439 assert user2.follower_address in refresh_record(user).following
442 test "returns error when followee not found", %{conn: conn} do
447 |> post("/ostatus_subscribe", %{
448 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
452 assert response =~ "Error following account"
455 test "returns error when login invalid", %{conn: conn} do
460 |> post("/ostatus_subscribe", %{
461 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
465 assert response =~ "Wrong username or password"
468 test "returns error when password invalid", %{conn: conn} do
470 user2 = insert(:user)
474 |> post("/ostatus_subscribe", %{
475 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
479 assert response =~ "Wrong username or password"
482 test "returns error when user is blocked", %{conn: conn} do
483 Pleroma.Config.put([:user, :deny_follow_blocked], true)
485 user2 = insert(:user)
486 {:ok, _user} = Pleroma.User.block(user2, user)
490 |> post("/ostatus_subscribe", %{
491 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
495 assert response =~ "Error following account"
499 describe "GET /api/pleroma/healthcheck" do
500 clear_config([:instance, :healthcheck])
502 test "returns 503 when healthcheck disabled", %{conn: conn} do
503 Pleroma.Config.put([:instance, :healthcheck], false)
507 |> get("/api/pleroma/healthcheck")
508 |> json_response(503)
510 assert response == %{}
513 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
514 Pleroma.Config.put([:instance, :healthcheck], true)
516 with_mock Pleroma.Healthcheck,
517 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
520 |> get("/api/pleroma/healthcheck")
521 |> json_response(200)
533 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
534 Pleroma.Config.put([:instance, :healthcheck], true)
536 with_mock Pleroma.Healthcheck,
537 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
540 |> get("/api/pleroma/healthcheck")
541 |> json_response(503)
554 describe "POST /api/pleroma/disable_account" do
555 test "it returns HTTP 200", %{conn: conn} do
560 |> assign(:user, user)
561 |> post("/api/pleroma/disable_account", %{"password" => "test"})
562 |> json_response(:ok)
564 assert response == %{"status" => "success"}
565 ObanHelpers.perform_all()
567 user = User.get_cached_by_id(user.id)
569 assert user.info.deactivated == true
572 test "it returns returns when password invalid", %{conn: conn} do
577 |> assign(:user, user)
578 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
579 |> json_response(:ok)
581 assert response == %{"error" => "Invalid password."}
582 user = User.get_cached_by_id(user.id)
584 refute user.info.deactivated
588 describe "GET /api/statusnet/version" do
589 test "it returns version in xml format", %{conn: conn} do
592 |> put_req_header("accept", "application/xml")
593 |> get("/api/statusnet/version")
596 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
599 test "it returns version in json format", %{conn: conn} do
602 |> put_req_header("accept", "application/json")
603 |> get("/api/statusnet/version")
604 |> json_response(:ok)
606 assert response == "#{Pleroma.Application.named_version()}"
610 describe "POST /main/ostatus - remote_subscribe/2" do
611 test "renders subscribe form", %{conn: conn} do
616 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
619 refute response =~ "Could not find user"
620 assert response =~ "Remotely follow #{user.nickname}"
623 test "renders subscribe form with error when user not found", %{conn: conn} do
626 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
629 assert response =~ "Could not find user"
630 refute response =~ "Remotely follow"
633 test "it redirect to webfinger url", %{conn: conn} do
635 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
639 |> post("/main/ostatus", %{
640 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
643 assert redirected_to(conn) ==
644 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
647 test "it renders form with error when use not found", %{conn: conn} do
648 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
652 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
655 assert response =~ "Something went wrong."
659 test "it returns new captcha", %{conn: conn} do
660 with_mock Pleroma.Captcha,
661 new: fn -> "test_captcha" end do
664 |> get("/api/pleroma/captcha")
667 assert resp == "\"test_captcha\""
668 assert called(Pleroma.Captcha.new())
672 defp with_credentials(conn, username, password) do
673 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
674 put_req_header(conn, "authorization", header_content)
677 defp valid_user(_context) do
682 describe "POST /api/pleroma/change_email" do
685 test "without credentials", %{conn: conn} do
686 conn = post(conn, "/api/pleroma/change_email")
687 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
690 test "with credentials and invalid password", %{conn: conn, user: current_user} do
693 |> with_credentials(current_user.nickname, "test")
694 |> post("/api/pleroma/change_email", %{
696 "email" => "test@test.com"
699 assert json_response(conn, 200) == %{"error" => "Invalid password."}
702 test "with credentials, valid password and invalid email", %{
708 |> with_credentials(current_user.nickname, "test")
709 |> post("/api/pleroma/change_email", %{
710 "password" => "test",
714 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
717 test "with credentials, valid password and no email", %{
723 |> with_credentials(current_user.nickname, "test")
724 |> post("/api/pleroma/change_email", %{
728 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
731 test "with credentials, valid password and blank email", %{
737 |> with_credentials(current_user.nickname, "test")
738 |> post("/api/pleroma/change_email", %{
739 "password" => "test",
743 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
746 test "with credentials, valid password and non unique email", %{
754 |> with_credentials(current_user.nickname, "test")
755 |> post("/api/pleroma/change_email", %{
756 "password" => "test",
757 "email" => user.email
760 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
763 test "with credentials, valid password and valid email", %{
769 |> with_credentials(current_user.nickname, "test")
770 |> post("/api/pleroma/change_email", %{
771 "password" => "test",
772 "email" => "cofe@foobar.com"
775 assert json_response(conn, 200) == %{"status" => "success"}