1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 clear_config([:instance])
21 clear_config([:frontend_configurations, :pleroma_fe])
23 describe "POST /api/pleroma/follow_import" do
24 setup do: oauth_access(["follow"])
26 test "it returns HTTP 200", %{conn: conn} do
31 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
34 assert response == "job started"
37 test "it imports follow lists from file", %{user: user1, conn: conn} do
42 read!: fn "follow_list.txt" ->
43 "Account address,Show boosts\n#{user2.ap_id},true"
48 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
51 assert response == "job started"
53 assert ObanHelpers.member?(
55 "op" => "follow_import",
56 "follower_id" => user1.id,
57 "followed_identifiers" => [user2.ap_id]
59 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
64 test "it imports new-style mastodon follow lists", %{conn: conn} do
69 |> post("/api/pleroma/follow_import", %{
70 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
74 assert response == "job started"
77 test "requires 'follow' or 'write:follows' permissions" do
78 token1 = insert(:oauth_token, scopes: ["read", "write"])
79 token2 = insert(:oauth_token, scopes: ["follow"])
80 token3 = insert(:oauth_token, scopes: ["something"])
81 another_user = insert(:user)
83 for token <- [token1, token2, token3] do
86 |> put_req_header("authorization", "Bearer #{token.token}")
87 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
90 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
91 json_response(conn, 403)
93 assert json_response(conn, 200)
98 test "it imports follows with different nickname variations", %{conn: conn} do
99 [user2, user3, user4, user5, user6] = insert_list(5, :user)
106 "@" <> user4.nickname,
107 user5.nickname <> "@localhost",
108 "@" <> user6.nickname <> "@localhost"
114 |> post("/api/pleroma/follow_import", %{"list" => identifiers})
115 |> json_response(:ok)
117 assert response == "job started"
118 assert [{:ok, job_result}] = ObanHelpers.perform_all()
119 assert job_result == [user2, user3, user4, user5, user6]
123 describe "POST /api/pleroma/blocks_import" do
124 # Note: "follow" or "write:blocks" permission is required
125 setup do: oauth_access(["write:blocks"])
127 test "it returns HTTP 200", %{conn: conn} do
128 user2 = insert(:user)
132 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
133 |> json_response(:ok)
135 assert response == "job started"
138 test "it imports blocks users from file", %{user: user1, conn: conn} do
139 user2 = insert(:user)
140 user3 = insert(:user)
143 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
147 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
148 |> json_response(:ok)
150 assert response == "job started"
152 assert ObanHelpers.member?(
154 "op" => "blocks_import",
155 "blocker_id" => user1.id,
156 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
158 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
163 test "it imports blocks with different nickname variations", %{conn: conn} do
164 [user2, user3, user4, user5, user6] = insert_list(5, :user)
170 "@" <> user4.nickname,
171 user5.nickname <> "@localhost",
172 "@" <> user6.nickname <> "@localhost"
178 |> post("/api/pleroma/blocks_import", %{"list" => identifiers})
179 |> json_response(:ok)
181 assert response == "job started"
182 assert [{:ok, job_result}] = ObanHelpers.perform_all()
183 assert job_result == [user2, user3, user4, user5, user6]
187 describe "PUT /api/pleroma/notification_settings" do
188 setup do: oauth_access(["write:accounts"])
190 test "it updates notification settings", %{user: user, conn: conn} do
192 |> put("/api/pleroma/notification_settings", %{
193 "followers" => false,
196 |> json_response(:ok)
198 user = refresh_record(user)
200 assert %Pleroma.User.NotificationSetting{
205 privacy_option: false
206 } == user.notification_settings
209 test "it updates notification privacy option", %{user: user, conn: conn} do
211 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
212 |> json_response(:ok)
214 user = refresh_record(user)
216 assert %Pleroma.User.NotificationSetting{
222 } == user.notification_settings
226 describe "GET /api/statusnet/config" do
227 test "it returns config in xml format", %{conn: conn} do
228 instance = Pleroma.Config.get(:instance)
232 |> put_req_header("accept", "application/xml")
233 |> get("/api/statusnet/config")
237 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
238 Pleroma.Web.base_url()
239 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
240 !Keyword.get(instance, :registrations_open)
241 }</closed>\n</site>\n</config>\n"
244 test "it returns config in json format", %{conn: conn} do
245 instance = Pleroma.Config.get(:instance)
246 Pleroma.Config.put([:instance, :managed_config], true)
247 Pleroma.Config.put([:instance, :registrations_open], false)
248 Pleroma.Config.put([:instance, :invites_enabled], true)
249 Pleroma.Config.put([:instance, :public], false)
250 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
254 |> put_req_header("accept", "application/json")
255 |> get("/api/statusnet/config")
256 |> json_response(:ok)
260 "accountActivationRequired" => "0",
262 "description" => Keyword.get(instance, :description),
263 "invitesEnabled" => "1",
264 "name" => Keyword.get(instance, :name),
265 "pleromafe" => %{"theme" => "asuka-hospital"},
267 "safeDMMentionsEnabled" => "0",
268 "server" => Pleroma.Web.base_url(),
269 "textlimit" => to_string(Keyword.get(instance, :limit)),
271 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
272 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
273 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
274 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
276 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
280 assert response == expected_data
283 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
284 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
288 |> get("/api/statusnet/config.json")
289 |> json_response(:ok)
291 assert response["site"]["safeDMMentionsEnabled"] == "1"
293 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
297 |> get("/api/statusnet/config.json")
298 |> json_response(:ok)
300 assert response["site"]["safeDMMentionsEnabled"] == "0"
303 test "it returns the managed config", %{conn: conn} do
304 Pleroma.Config.put([:instance, :managed_config], false)
305 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
309 |> get("/api/statusnet/config.json")
310 |> json_response(:ok)
312 refute response["site"]["pleromafe"]
314 Pleroma.Config.put([:instance, :managed_config], true)
318 |> get("/api/statusnet/config.json")
319 |> json_response(:ok)
321 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
325 describe "GET /api/pleroma/frontend_configurations" do
326 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
337 Pleroma.Config.put(:frontend_configurations, config)
341 |> get("/api/pleroma/frontend_configurations")
342 |> json_response(:ok)
344 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
348 describe "/api/pleroma/emoji" do
349 test "returns json with custom emoji with tags", %{conn: conn} do
352 |> get("/api/pleroma/emoji")
353 |> json_response(200)
355 assert Enum.all?(emoji, fn
361 is_binary(url) and is_list(tags)
366 describe "GET /api/pleroma/healthcheck" do
367 clear_config([:instance, :healthcheck])
369 test "returns 503 when healthcheck disabled", %{conn: conn} do
370 Pleroma.Config.put([:instance, :healthcheck], false)
374 |> get("/api/pleroma/healthcheck")
375 |> json_response(503)
377 assert response == %{}
380 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
381 Pleroma.Config.put([:instance, :healthcheck], true)
383 with_mock Pleroma.Healthcheck,
384 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
387 |> get("/api/pleroma/healthcheck")
388 |> json_response(200)
400 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
401 Pleroma.Config.put([:instance, :healthcheck], true)
403 with_mock Pleroma.Healthcheck,
404 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
407 |> get("/api/pleroma/healthcheck")
408 |> json_response(503)
421 describe "POST /api/pleroma/disable_account" do
422 setup do: oauth_access(["write:accounts"])
424 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
427 |> post("/api/pleroma/disable_account", %{"password" => "test"})
428 |> json_response(:ok)
430 assert response == %{"status" => "success"}
431 ObanHelpers.perform_all()
433 user = User.get_cached_by_id(user.id)
435 assert user.deactivated == true
438 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
443 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
444 |> json_response(:ok)
446 assert response == %{"error" => "Invalid password."}
447 user = User.get_cached_by_id(user.id)
449 refute user.deactivated
453 describe "GET /api/statusnet/version" do
454 test "it returns version in xml format", %{conn: conn} do
457 |> put_req_header("accept", "application/xml")
458 |> get("/api/statusnet/version")
461 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
464 test "it returns version in json format", %{conn: conn} do
467 |> put_req_header("accept", "application/json")
468 |> get("/api/statusnet/version")
469 |> json_response(:ok)
471 assert response == "#{Pleroma.Application.named_version()}"
475 describe "POST /main/ostatus - remote_subscribe/2" do
476 test "renders subscribe form", %{conn: conn} do
481 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
484 refute response =~ "Could not find user"
485 assert response =~ "Remotely follow #{user.nickname}"
488 test "renders subscribe form with error when user not found", %{conn: conn} do
491 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
494 assert response =~ "Could not find user"
495 refute response =~ "Remotely follow"
498 test "it redirect to webfinger url", %{conn: conn} do
500 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
504 |> post("/main/ostatus", %{
505 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
508 assert redirected_to(conn) ==
509 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
512 test "it renders form with error when user not found", %{conn: conn} do
513 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
517 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
520 assert response =~ "Something went wrong."
524 test "it returns new captcha", %{conn: conn} do
525 with_mock Pleroma.Captcha,
526 new: fn -> "test_captcha" end do
529 |> get("/api/pleroma/captcha")
532 assert resp == "\"test_captcha\""
533 assert called(Pleroma.Captcha.new())
537 describe "POST /api/pleroma/change_email" do
538 setup do: oauth_access(["write:accounts"])
540 test "without permissions", %{conn: conn} do
543 |> assign(:token, nil)
544 |> post("/api/pleroma/change_email")
546 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
549 test "with proper permissions and invalid password", %{conn: conn} do
551 post(conn, "/api/pleroma/change_email", %{
553 "email" => "test@test.com"
556 assert json_response(conn, 200) == %{"error" => "Invalid password."}
559 test "with proper permissions, valid password and invalid email", %{
563 post(conn, "/api/pleroma/change_email", %{
564 "password" => "test",
568 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
571 test "with proper permissions, valid password and no email", %{
575 post(conn, "/api/pleroma/change_email", %{
579 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
582 test "with proper permissions, valid password and blank email", %{
586 post(conn, "/api/pleroma/change_email", %{
587 "password" => "test",
591 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
594 test "with proper permissions, valid password and non unique email", %{
600 post(conn, "/api/pleroma/change_email", %{
601 "password" => "test",
602 "email" => user.email
605 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
608 test "with proper permissions, valid password and valid email", %{
612 post(conn, "/api/pleroma/change_email", %{
613 "password" => "test",
614 "email" => "cofe@foobar.com"
617 assert json_response(conn, 200) == %{"status" => "success"}
621 describe "POST /api/pleroma/change_password" do
622 setup do: oauth_access(["write:accounts"])
624 test "without permissions", %{conn: conn} do
627 |> assign(:token, nil)
628 |> post("/api/pleroma/change_password")
630 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
633 test "with proper permissions and invalid password", %{conn: conn} do
635 post(conn, "/api/pleroma/change_password", %{
637 "new_password" => "newpass",
638 "new_password_confirmation" => "newpass"
641 assert json_response(conn, 200) == %{"error" => "Invalid password."}
644 test "with proper permissions, valid password and new password and confirmation not matching",
649 post(conn, "/api/pleroma/change_password", %{
650 "password" => "test",
651 "new_password" => "newpass",
652 "new_password_confirmation" => "notnewpass"
655 assert json_response(conn, 200) == %{
656 "error" => "New password does not match confirmation."
660 test "with proper permissions, valid password and invalid new password", %{
664 post(conn, "/api/pleroma/change_password", %{
665 "password" => "test",
666 "new_password" => "",
667 "new_password_confirmation" => ""
670 assert json_response(conn, 200) == %{
671 "error" => "New password can't be blank."
675 test "with proper permissions, valid password and matching new password and confirmation", %{
680 post(conn, "/api/pleroma/change_password", %{
681 "password" => "test",
682 "new_password" => "newpass",
683 "new_password_confirmation" => "newpass"
686 assert json_response(conn, 200) == %{"status" => "success"}
687 fetched_user = User.get_cached_by_id(user.id)
688 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
692 describe "POST /api/pleroma/delete_account" do
693 setup do: oauth_access(["write:accounts"])
695 test "without permissions", %{conn: conn} do
698 |> assign(:token, nil)
699 |> post("/api/pleroma/delete_account")
701 assert json_response(conn, 403) ==
702 %{"error" => "Insufficient permissions: write:accounts."}
705 test "with proper permissions and wrong or missing password", %{conn: conn} do
706 for params <- [%{"password" => "hi"}, %{}] do
707 ret_conn = post(conn, "/api/pleroma/delete_account", params)
709 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
713 test "with proper permissions and valid password", %{conn: conn} do
714 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
716 assert json_response(conn, 200) == %{"status" => "success"}