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
10 alias Pleroma.Tests.ObanHelpers
13 import Pleroma.Factory
17 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
21 clear_config([:instance])
22 clear_config([:frontend_configurations, :pleroma_fe])
24 describe "POST /api/pleroma/follow_import" do
25 setup do: oauth_access(["follow"])
27 test "it returns HTTP 200", %{conn: conn} do
32 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
35 assert response == "job started"
38 test "it imports follow lists from file", %{user: user1, conn: conn} do
43 read!: fn "follow_list.txt" ->
44 "Account address,Show boosts\n#{user2.ap_id},true"
49 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
52 assert response == "job started"
54 assert ObanHelpers.member?(
56 "op" => "follow_import",
57 "follower_id" => user1.id,
58 "followed_identifiers" => [user2.ap_id]
60 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
65 test "it imports new-style mastodon follow lists", %{conn: conn} do
70 |> post("/api/pleroma/follow_import", %{
71 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
75 assert response == "job started"
78 test "requires 'follow' or 'write:follows' permissions" do
79 token1 = insert(:oauth_token, scopes: ["read", "write"])
80 token2 = insert(:oauth_token, scopes: ["follow"])
81 token3 = insert(:oauth_token, scopes: ["something"])
82 another_user = insert(:user)
84 for token <- [token1, token2, token3] do
87 |> put_req_header("authorization", "Bearer #{token.token}")
88 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
91 assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
92 json_response(conn, 403)
94 assert json_response(conn, 200)
100 describe "POST /api/pleroma/blocks_import" do
101 # Note: "follow" or "write:blocks" permission is required
102 setup do: oauth_access(["write:blocks"])
104 test "it returns HTTP 200", %{conn: conn} do
105 user2 = insert(:user)
109 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
110 |> json_response(:ok)
112 assert response == "job started"
115 test "it imports blocks users from file", %{user: user1, conn: conn} do
116 user2 = insert(:user)
117 user3 = insert(:user)
120 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
124 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
125 |> json_response(:ok)
127 assert response == "job started"
129 assert ObanHelpers.member?(
131 "op" => "blocks_import",
132 "blocker_id" => user1.id,
133 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
135 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
141 describe "PUT /api/pleroma/notification_settings" do
142 setup do: oauth_access(["write:accounts"])
144 test "it updates notification settings", %{user: user, conn: conn} do
146 |> put("/api/pleroma/notification_settings", %{
147 "followers" => false,
150 |> json_response(:ok)
152 user = refresh_record(user)
154 assert %Pleroma.User.NotificationSetting{
159 privacy_option: false
160 } == user.notification_settings
163 test "it updates notification privacy option", %{user: user, conn: conn} do
165 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
166 |> json_response(:ok)
168 user = refresh_record(user)
170 assert %Pleroma.User.NotificationSetting{
176 } == user.notification_settings
180 describe "GET /api/statusnet/config" do
181 test "it returns config in xml format", %{conn: conn} do
182 instance = Config.get(:instance)
186 |> put_req_header("accept", "application/xml")
187 |> get("/api/statusnet/config")
191 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
192 Pleroma.Web.base_url()
193 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
194 !Keyword.get(instance, :registrations_open)
195 }</closed>\n</site>\n</config>\n"
198 test "it returns config in json format", %{conn: conn} do
199 instance = Config.get(:instance)
200 Config.put([:instance, :managed_config], true)
201 Config.put([:instance, :registrations_open], false)
202 Config.put([:instance, :invites_enabled], true)
203 Config.put([:instance, :public], false)
204 Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
208 |> put_req_header("accept", "application/json")
209 |> get("/api/statusnet/config")
210 |> json_response(:ok)
214 "accountActivationRequired" => "0",
216 "description" => Keyword.get(instance, :description),
217 "invitesEnabled" => "1",
218 "name" => Keyword.get(instance, :name),
219 "pleromafe" => %{"theme" => "asuka-hospital"},
221 "safeDMMentionsEnabled" => "0",
222 "server" => Pleroma.Web.base_url(),
223 "textlimit" => to_string(Keyword.get(instance, :limit)),
225 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
226 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
227 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
228 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
230 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
234 assert response == expected_data
237 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
238 Config.put([:instance, :safe_dm_mentions], true)
242 |> get("/api/statusnet/config.json")
243 |> json_response(:ok)
245 assert response["site"]["safeDMMentionsEnabled"] == "1"
247 Config.put([:instance, :safe_dm_mentions], false)
251 |> get("/api/statusnet/config.json")
252 |> json_response(:ok)
254 assert response["site"]["safeDMMentionsEnabled"] == "0"
257 test "it returns the managed config", %{conn: conn} do
258 Config.put([:instance, :managed_config], false)
259 Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
263 |> get("/api/statusnet/config.json")
264 |> json_response(:ok)
266 refute response["site"]["pleromafe"]
268 Config.put([:instance, :managed_config], true)
272 |> get("/api/statusnet/config.json")
273 |> json_response(:ok)
275 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
279 describe "GET /api/pleroma/frontend_configurations" do
280 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
291 Config.put(:frontend_configurations, config)
295 |> get("/api/pleroma/frontend_configurations")
296 |> json_response(:ok)
298 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
302 describe "/api/pleroma/emoji" do
303 test "returns json with custom emoji with tags", %{conn: conn} do
306 |> get("/api/pleroma/emoji")
307 |> json_response(200)
309 assert Enum.all?(emoji, fn
315 is_binary(url) and is_list(tags)
320 describe "GET /api/pleroma/healthcheck" do
321 clear_config([:instance, :healthcheck])
323 test "returns 503 when healthcheck disabled", %{conn: conn} do
324 Config.put([:instance, :healthcheck], false)
328 |> get("/api/pleroma/healthcheck")
329 |> json_response(503)
331 assert response == %{}
334 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
335 Config.put([:instance, :healthcheck], true)
337 with_mock Pleroma.Healthcheck,
338 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
341 |> get("/api/pleroma/healthcheck")
342 |> json_response(200)
354 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
355 Config.put([:instance, :healthcheck], true)
357 with_mock Pleroma.Healthcheck,
358 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
361 |> get("/api/pleroma/healthcheck")
362 |> json_response(503)
375 describe "POST /api/pleroma/disable_account" do
376 setup do: oauth_access(["write:accounts"])
378 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
381 |> post("/api/pleroma/disable_account", %{"password" => "test"})
382 |> json_response(:ok)
384 assert response == %{"status" => "success"}
385 ObanHelpers.perform_all()
387 user = User.get_cached_by_id(user.id)
389 assert user.deactivated == true
392 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
397 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
398 |> json_response(:ok)
400 assert response == %{"error" => "Invalid password."}
401 user = User.get_cached_by_id(user.id)
403 refute user.deactivated
407 describe "GET /api/statusnet/version" do
408 test "it returns version in xml format", %{conn: conn} do
411 |> put_req_header("accept", "application/xml")
412 |> get("/api/statusnet/version")
415 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
418 test "it returns version in json format", %{conn: conn} do
421 |> put_req_header("accept", "application/json")
422 |> get("/api/statusnet/version")
423 |> json_response(:ok)
425 assert response == "#{Pleroma.Application.named_version()}"
429 describe "POST /main/ostatus - remote_subscribe/2" do
430 clear_config([:instance, :federating]) do
431 Config.put([:instance, :federating], true)
434 test "renders subscribe form", %{conn: conn} do
439 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
442 refute response =~ "Could not find user"
443 assert response =~ "Remotely follow #{user.nickname}"
446 test "renders subscribe form with error when user not found", %{conn: conn} do
449 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
452 assert response =~ "Could not find user"
453 refute response =~ "Remotely follow"
456 test "it redirect to webfinger url", %{conn: conn} do
458 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
462 |> post("/main/ostatus", %{
463 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
466 assert redirected_to(conn) ==
467 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
470 test "it renders form with error when user not found", %{conn: conn} do
471 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
475 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
478 assert response =~ "Something went wrong."
482 test "it returns new captcha", %{conn: conn} do
483 with_mock Pleroma.Captcha,
484 new: fn -> "test_captcha" end do
487 |> get("/api/pleroma/captcha")
490 assert resp == "\"test_captcha\""
491 assert called(Pleroma.Captcha.new())
495 describe "POST /api/pleroma/change_email" do
496 setup do: oauth_access(["write:accounts"])
498 test "without permissions", %{conn: conn} do
501 |> assign(:token, nil)
502 |> post("/api/pleroma/change_email")
504 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
507 test "with proper permissions and invalid password", %{conn: conn} do
509 post(conn, "/api/pleroma/change_email", %{
511 "email" => "test@test.com"
514 assert json_response(conn, 200) == %{"error" => "Invalid password."}
517 test "with proper permissions, valid password and invalid email", %{
521 post(conn, "/api/pleroma/change_email", %{
522 "password" => "test",
526 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
529 test "with proper permissions, valid password and no email", %{
533 post(conn, "/api/pleroma/change_email", %{
537 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
540 test "with proper permissions, valid password and blank email", %{
544 post(conn, "/api/pleroma/change_email", %{
545 "password" => "test",
549 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
552 test "with proper permissions, valid password and non unique email", %{
558 post(conn, "/api/pleroma/change_email", %{
559 "password" => "test",
560 "email" => user.email
563 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
566 test "with proper permissions, valid password and valid email", %{
570 post(conn, "/api/pleroma/change_email", %{
571 "password" => "test",
572 "email" => "cofe@foobar.com"
575 assert json_response(conn, 200) == %{"status" => "success"}
579 describe "POST /api/pleroma/change_password" do
580 setup do: oauth_access(["write:accounts"])
582 test "without permissions", %{conn: conn} do
585 |> assign(:token, nil)
586 |> post("/api/pleroma/change_password")
588 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
591 test "with proper permissions and invalid password", %{conn: conn} do
593 post(conn, "/api/pleroma/change_password", %{
595 "new_password" => "newpass",
596 "new_password_confirmation" => "newpass"
599 assert json_response(conn, 200) == %{"error" => "Invalid password."}
602 test "with proper permissions, valid password and new password and confirmation not matching",
607 post(conn, "/api/pleroma/change_password", %{
608 "password" => "test",
609 "new_password" => "newpass",
610 "new_password_confirmation" => "notnewpass"
613 assert json_response(conn, 200) == %{
614 "error" => "New password does not match confirmation."
618 test "with proper permissions, valid password and invalid new password", %{
622 post(conn, "/api/pleroma/change_password", %{
623 "password" => "test",
624 "new_password" => "",
625 "new_password_confirmation" => ""
628 assert json_response(conn, 200) == %{
629 "error" => "New password can't be blank."
633 test "with proper permissions, valid password and matching new password and confirmation", %{
638 post(conn, "/api/pleroma/change_password", %{
639 "password" => "test",
640 "new_password" => "newpass",
641 "new_password_confirmation" => "newpass"
644 assert json_response(conn, 200) == %{"status" => "success"}
645 fetched_user = User.get_cached_by_id(user.id)
646 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
650 describe "POST /api/pleroma/delete_account" do
651 setup do: oauth_access(["write:accounts"])
653 test "without permissions", %{conn: conn} do
656 |> assign(:token, nil)
657 |> post("/api/pleroma/delete_account")
659 assert json_response(conn, 403) ==
660 %{"error" => "Insufficient permissions: write:accounts."}
663 test "with proper permissions and wrong or missing password", %{conn: conn} do
664 for params <- [%{"password" => "hi"}, %{}] do
665 ret_conn = post(conn, "/api/pleroma/delete_account", params)
667 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
671 test "with proper permissions and valid password", %{conn: conn} do
672 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
674 assert json_response(conn, 200) == %{"status" => "success"}