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 setup do: clear_config([:instance])
22 setup do: 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)
99 test "it imports with different nickname variations", %{conn: conn} do
100 [user2, user3, user4, user5, user6] = insert_list(5, :user)
107 "@" <> user4.nickname,
108 user5.nickname <> "@localhost",
109 "@" <> user6.nickname <> "@localhost"
115 |> post("/api/pleroma/follow_import", %{"list" => identifiers})
116 |> json_response(:ok)
118 assert response == "job started"
119 assert [job_result] = ObanHelpers.perform_all()
120 assert job_result == [user2, user3, user4, user5, user6]
124 describe "POST /api/pleroma/blocks_import" do
125 # Note: "follow" or "write:blocks" permission is required
126 setup do: oauth_access(["write:blocks"])
128 test "it returns HTTP 200", %{conn: conn} do
129 user2 = insert(:user)
133 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
134 |> json_response(:ok)
136 assert response == "job started"
139 test "it imports blocks users from file", %{user: user1, conn: conn} do
140 user2 = insert(:user)
141 user3 = insert(:user)
144 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
148 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
149 |> json_response(:ok)
151 assert response == "job started"
153 assert ObanHelpers.member?(
155 "op" => "blocks_import",
156 "blocker_id" => user1.id,
157 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
159 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
165 describe "PUT /api/pleroma/notification_settings" do
166 setup do: oauth_access(["write:accounts"])
168 test "it updates notification settings", %{user: user, conn: conn} do
170 |> put("/api/pleroma/notification_settings", %{
171 "followers" => false,
174 |> json_response(:ok)
176 user = refresh_record(user)
178 assert %Pleroma.User.NotificationSetting{
183 privacy_option: false
184 } == user.notification_settings
187 test "it updates notification privacy option", %{user: user, conn: conn} do
189 |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
190 |> json_response(:ok)
192 user = refresh_record(user)
194 assert %Pleroma.User.NotificationSetting{
200 } == user.notification_settings
204 describe "GET /api/statusnet/config" do
205 test "it returns config in xml format", %{conn: conn} do
206 instance = Config.get(:instance)
210 |> put_req_header("accept", "application/xml")
211 |> get("/api/statusnet/config")
215 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
216 Pleroma.Web.base_url()
217 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
218 !Keyword.get(instance, :registrations_open)
219 }</closed>\n</site>\n</config>\n"
222 test "it returns config in json format", %{conn: conn} do
223 instance = Config.get(:instance)
224 Config.put([:instance, :managed_config], true)
225 Config.put([:instance, :registrations_open], false)
226 Config.put([:instance, :invites_enabled], true)
227 Config.put([:instance, :public], false)
228 Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
232 |> put_req_header("accept", "application/json")
233 |> get("/api/statusnet/config")
234 |> json_response(:ok)
238 "accountActivationRequired" => "0",
240 "description" => Keyword.get(instance, :description),
241 "invitesEnabled" => "1",
242 "name" => Keyword.get(instance, :name),
243 "pleromafe" => %{"theme" => "asuka-hospital"},
245 "safeDMMentionsEnabled" => "0",
246 "server" => Pleroma.Web.base_url(),
247 "textlimit" => to_string(Keyword.get(instance, :limit)),
249 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
250 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
251 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
252 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
254 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
258 assert response == expected_data
261 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
262 Config.put([:instance, :safe_dm_mentions], true)
266 |> get("/api/statusnet/config.json")
267 |> json_response(:ok)
269 assert response["site"]["safeDMMentionsEnabled"] == "1"
271 Config.put([:instance, :safe_dm_mentions], false)
275 |> get("/api/statusnet/config.json")
276 |> json_response(:ok)
278 assert response["site"]["safeDMMentionsEnabled"] == "0"
281 test "it returns the managed config", %{conn: conn} do
282 Config.put([:instance, :managed_config], false)
283 Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
287 |> get("/api/statusnet/config.json")
288 |> json_response(:ok)
290 refute response["site"]["pleromafe"]
292 Config.put([:instance, :managed_config], true)
296 |> get("/api/statusnet/config.json")
297 |> json_response(:ok)
299 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
303 describe "GET /api/pleroma/frontend_configurations" do
304 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
315 Config.put(:frontend_configurations, config)
319 |> get("/api/pleroma/frontend_configurations")
320 |> json_response(:ok)
322 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
326 describe "/api/pleroma/emoji" do
327 test "returns json with custom emoji with tags", %{conn: conn} do
330 |> get("/api/pleroma/emoji")
331 |> json_response(200)
333 assert Enum.all?(emoji, fn
339 is_binary(url) and is_list(tags)
344 describe "GET /api/pleroma/healthcheck" do
345 setup do: clear_config([:instance, :healthcheck])
347 test "returns 503 when healthcheck disabled", %{conn: conn} do
348 Config.put([:instance, :healthcheck], false)
352 |> get("/api/pleroma/healthcheck")
353 |> json_response(503)
355 assert response == %{}
358 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
359 Config.put([:instance, :healthcheck], true)
361 with_mock Pleroma.Healthcheck,
362 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
365 |> get("/api/pleroma/healthcheck")
366 |> json_response(200)
378 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
379 Config.put([:instance, :healthcheck], true)
381 with_mock Pleroma.Healthcheck,
382 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
385 |> get("/api/pleroma/healthcheck")
386 |> json_response(503)
399 describe "POST /api/pleroma/disable_account" do
400 setup do: oauth_access(["write:accounts"])
402 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
405 |> post("/api/pleroma/disable_account", %{"password" => "test"})
406 |> json_response(:ok)
408 assert response == %{"status" => "success"}
409 ObanHelpers.perform_all()
411 user = User.get_cached_by_id(user.id)
413 assert user.deactivated == true
416 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
421 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
422 |> json_response(:ok)
424 assert response == %{"error" => "Invalid password."}
425 user = User.get_cached_by_id(user.id)
427 refute user.deactivated
431 describe "GET /api/statusnet/version" do
432 test "it returns version in xml format", %{conn: conn} do
435 |> put_req_header("accept", "application/xml")
436 |> get("/api/statusnet/version")
439 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
442 test "it returns version in json format", %{conn: conn} do
445 |> put_req_header("accept", "application/json")
446 |> get("/api/statusnet/version")
447 |> json_response(:ok)
449 assert response == "#{Pleroma.Application.named_version()}"
453 describe "POST /main/ostatus - remote_subscribe/2" do
454 setup do: clear_config([:instance, :federating], true)
456 test "renders subscribe form", %{conn: conn} do
461 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
464 refute response =~ "Could not find user"
465 assert response =~ "Remotely follow #{user.nickname}"
468 test "renders subscribe form with error when user not found", %{conn: conn} do
471 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
474 assert response =~ "Could not find user"
475 refute response =~ "Remotely follow"
478 test "it redirect to webfinger url", %{conn: conn} do
480 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
484 |> post("/main/ostatus", %{
485 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
488 assert redirected_to(conn) ==
489 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
492 test "it renders form with error when user not found", %{conn: conn} do
493 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
497 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
500 assert response =~ "Something went wrong."
504 test "it returns new captcha", %{conn: conn} do
505 with_mock Pleroma.Captcha,
506 new: fn -> "test_captcha" end do
509 |> get("/api/pleroma/captcha")
512 assert resp == "\"test_captcha\""
513 assert called(Pleroma.Captcha.new())
517 describe "POST /api/pleroma/change_email" do
518 setup do: oauth_access(["write:accounts"])
520 test "without permissions", %{conn: conn} do
523 |> assign(:token, nil)
524 |> post("/api/pleroma/change_email")
526 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
529 test "with proper permissions and invalid password", %{conn: conn} do
531 post(conn, "/api/pleroma/change_email", %{
533 "email" => "test@test.com"
536 assert json_response(conn, 200) == %{"error" => "Invalid password."}
539 test "with proper permissions, valid password and invalid email", %{
543 post(conn, "/api/pleroma/change_email", %{
544 "password" => "test",
548 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
551 test "with proper permissions, valid password and no email", %{
555 post(conn, "/api/pleroma/change_email", %{
559 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
562 test "with proper permissions, valid password and blank email", %{
566 post(conn, "/api/pleroma/change_email", %{
567 "password" => "test",
571 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
574 test "with proper permissions, valid password and non unique email", %{
580 post(conn, "/api/pleroma/change_email", %{
581 "password" => "test",
582 "email" => user.email
585 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
588 test "with proper permissions, valid password and valid email", %{
592 post(conn, "/api/pleroma/change_email", %{
593 "password" => "test",
594 "email" => "cofe@foobar.com"
597 assert json_response(conn, 200) == %{"status" => "success"}
601 describe "POST /api/pleroma/change_password" do
602 setup do: oauth_access(["write:accounts"])
604 test "without permissions", %{conn: conn} do
607 |> assign(:token, nil)
608 |> post("/api/pleroma/change_password")
610 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
613 test "with proper permissions and invalid password", %{conn: conn} do
615 post(conn, "/api/pleroma/change_password", %{
617 "new_password" => "newpass",
618 "new_password_confirmation" => "newpass"
621 assert json_response(conn, 200) == %{"error" => "Invalid password."}
624 test "with proper permissions, valid password and new password and confirmation not matching",
629 post(conn, "/api/pleroma/change_password", %{
630 "password" => "test",
631 "new_password" => "newpass",
632 "new_password_confirmation" => "notnewpass"
635 assert json_response(conn, 200) == %{
636 "error" => "New password does not match confirmation."
640 test "with proper permissions, valid password and invalid new password", %{
644 post(conn, "/api/pleroma/change_password", %{
645 "password" => "test",
646 "new_password" => "",
647 "new_password_confirmation" => ""
650 assert json_response(conn, 200) == %{
651 "error" => "New password can't be blank."
655 test "with proper permissions, valid password and matching new password and confirmation", %{
660 post(conn, "/api/pleroma/change_password", %{
661 "password" => "test",
662 "new_password" => "newpass",
663 "new_password_confirmation" => "newpass"
666 assert json_response(conn, 200) == %{"status" => "success"}
667 fetched_user = User.get_cached_by_id(user.id)
668 assert Comeonin.Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
672 describe "POST /api/pleroma/delete_account" do
673 setup do: oauth_access(["write:accounts"])
675 test "without permissions", %{conn: conn} do
678 |> assign(:token, nil)
679 |> post("/api/pleroma/delete_account")
681 assert json_response(conn, 403) ==
682 %{"error" => "Insufficient permissions: write:accounts."}
685 test "with proper permissions and wrong or missing password", %{conn: conn} do
686 for params <- [%{"password" => "hi"}, %{}] do
687 ret_conn = post(conn, "/api/pleroma/delete_account", params)
689 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
693 test "with proper permissions and valid password", %{conn: conn} do
694 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
696 assert json_response(conn, 200) == %{"status" => "success"}