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 /api/pleroma/healthcheck" do
323 clear_config([:instance, :healthcheck])
325 test "returns 503 when healthcheck disabled", %{conn: conn} do
326 Pleroma.Config.put([:instance, :healthcheck], false)
330 |> get("/api/pleroma/healthcheck")
331 |> json_response(503)
333 assert response == %{}
336 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
337 Pleroma.Config.put([:instance, :healthcheck], true)
339 with_mock Pleroma.Healthcheck,
340 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
343 |> get("/api/pleroma/healthcheck")
344 |> json_response(200)
356 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
357 Pleroma.Config.put([:instance, :healthcheck], true)
359 with_mock Pleroma.Healthcheck,
360 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
363 |> get("/api/pleroma/healthcheck")
364 |> json_response(503)
377 describe "POST /api/pleroma/disable_account" do
378 setup do: oauth_access(["write:accounts"])
380 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
383 |> post("/api/pleroma/disable_account", %{"password" => "test"})
384 |> json_response(:ok)
386 assert response == %{"status" => "success"}
387 ObanHelpers.perform_all()
389 user = User.get_cached_by_id(user.id)
391 assert user.deactivated == true
394 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
399 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
400 |> json_response(:ok)
402 assert response == %{"error" => "Invalid password."}
403 user = User.get_cached_by_id(user.id)
405 refute user.deactivated
409 describe "GET /api/statusnet/version" do
410 test "it returns version in xml format", %{conn: conn} do
413 |> put_req_header("accept", "application/xml")
414 |> get("/api/statusnet/version")
417 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
420 test "it returns version in json format", %{conn: conn} do
423 |> put_req_header("accept", "application/json")
424 |> get("/api/statusnet/version")
425 |> json_response(:ok)
427 assert response == "#{Pleroma.Application.named_version()}"
431 describe "POST /main/ostatus - remote_subscribe/2" do
432 test "renders subscribe form", %{conn: conn} do
437 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
440 refute response =~ "Could not find user"
441 assert response =~ "Remotely follow #{user.nickname}"
444 test "renders subscribe form with error when user not found", %{conn: conn} do
447 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
450 assert response =~ "Could not find user"
451 refute response =~ "Remotely follow"
454 test "it redirect to webfinger url", %{conn: conn} do
456 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
460 |> post("/main/ostatus", %{
461 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
464 assert redirected_to(conn) ==
465 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
468 test "it renders form with error when user not found", %{conn: conn} do
469 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
473 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
476 assert response =~ "Something went wrong."
480 test "it returns new captcha", %{conn: conn} do
481 with_mock Pleroma.Captcha,
482 new: fn -> "test_captcha" end do
485 |> get("/api/pleroma/captcha")
488 assert resp == "\"test_captcha\""
489 assert called(Pleroma.Captcha.new())
493 describe "POST /api/pleroma/change_email" do
494 setup do: oauth_access(["write:accounts"])
496 test "without permissions", %{conn: conn} do
499 |> assign(:token, nil)
500 |> post("/api/pleroma/change_email")
502 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
505 test "with proper permissions and invalid password", %{conn: conn} do
507 post(conn, "/api/pleroma/change_email", %{
509 "email" => "test@test.com"
512 assert json_response(conn, 200) == %{"error" => "Invalid password."}
515 test "with proper permissions, valid password and invalid email", %{
519 post(conn, "/api/pleroma/change_email", %{
520 "password" => "test",
524 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
527 test "with proper permissions, valid password and no email", %{
531 post(conn, "/api/pleroma/change_email", %{
535 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
538 test "with proper permissions, valid password and blank email", %{
542 post(conn, "/api/pleroma/change_email", %{
543 "password" => "test",
547 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
550 test "with proper permissions, valid password and non unique email", %{
556 post(conn, "/api/pleroma/change_email", %{
557 "password" => "test",
558 "email" => user.email
561 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
564 test "with proper permissions, valid password and valid email", %{
568 post(conn, "/api/pleroma/change_email", %{
569 "password" => "test",
570 "email" => "cofe@foobar.com"
573 assert json_response(conn, 200) == %{"status" => "success"}
577 describe "POST /api/pleroma/change_password" do
578 setup do: oauth_access(["write:accounts"])
580 test "without permissions", %{conn: conn} do
583 |> assign(:token, nil)
584 |> post("/api/pleroma/change_password")
586 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
589 test "with proper permissions and invalid password", %{conn: conn} do
591 post(conn, "/api/pleroma/change_password", %{
593 "new_password" => "newpass",
594 "new_password_confirmation" => "newpass"
597 assert json_response(conn, 200) == %{"error" => "Invalid password."}
600 test "with proper permissions, valid password and new password and confirmation not matching",
605 post(conn, "/api/pleroma/change_password", %{
606 "password" => "test",
607 "new_password" => "newpass",
608 "new_password_confirmation" => "notnewpass"
611 assert json_response(conn, 200) == %{
612 "error" => "New password does not match confirmation."
616 test "with proper permissions, valid password and invalid new password", %{
620 post(conn, "/api/pleroma/change_password", %{
621 "password" => "test",
622 "new_password" => "",
623 "new_password_confirmation" => ""
626 assert json_response(conn, 200) == %{
627 "error" => "New password can't be blank."
631 test "with proper permissions, valid password and matching new password and confirmation", %{
636 post(conn, "/api/pleroma/change_password", %{
637 "password" => "test",
638 "new_password" => "newpass",
639 "new_password_confirmation" => "newpass"
642 assert json_response(conn, 200) == %{"status" => "success"}
643 fetched_user = User.get_cached_by_id(user.id)
644 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
648 describe "POST /api/pleroma/delete_account" do
649 setup do: oauth_access(["write:accounts"])
651 test "without permissions", %{conn: conn} do
654 |> assign(:token, nil)
655 |> post("/api/pleroma/delete_account")
657 assert json_response(conn, 403) ==
658 %{"error" => "Insufficient permissions: write:accounts."}
661 test "with proper permissions and wrong or missing password", %{conn: conn} do
662 for params <- [%{"password" => "hi"}, %{}] do
663 ret_conn = post(conn, "/api/pleroma/delete_account", params)
665 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
669 test "with proper permissions and valid password", %{conn: conn} do
670 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
672 assert json_response(conn, 200) == %{"status" => "success"}