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 follows 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 [{:ok, 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)
164 test "it imports blocks with different nickname variations", %{conn: conn} do
165 [user2, user3, user4, user5, user6] = insert_list(5, :user)
171 "@" <> user4.nickname,
172 user5.nickname <> "@localhost",
173 "@" <> user6.nickname <> "@localhost"
179 |> post("/api/pleroma/blocks_import", %{"list" => identifiers})
180 |> json_response(:ok)
182 assert response == "job started"
183 assert [{:ok, job_result}] = ObanHelpers.perform_all()
184 assert job_result == [user2, user3, user4, user5, user6]
188 describe "PUT /api/pleroma/notification_settings" do
189 setup do: oauth_access(["write:accounts"])
191 test "it updates notification settings", %{user: user, conn: conn} do
193 |> put("/api/pleroma/notification_settings", %{
194 "block_from_strangers" => true,
197 |> json_response(:ok)
199 user = refresh_record(user)
201 assert %Pleroma.User.NotificationSetting{
202 block_from_strangers: true,
203 hide_notification_contents: false
204 } == user.notification_settings
207 test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
209 |> put("/api/pleroma/notification_settings", %{"hide_notification_contents" => "1"})
210 |> json_response(:ok)
212 user = refresh_record(user)
214 assert %Pleroma.User.NotificationSetting{
215 block_from_strangers: false,
216 hide_notification_contents: true
217 } == user.notification_settings
221 describe "GET /api/pleroma/frontend_configurations" do
222 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
233 Config.put(:frontend_configurations, config)
237 |> get("/api/pleroma/frontend_configurations")
238 |> json_response(:ok)
240 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
244 describe "/api/pleroma/emoji" do
245 test "returns json with custom emoji with tags", %{conn: conn} do
248 |> get("/api/pleroma/emoji")
249 |> json_response(200)
251 assert Enum.all?(emoji, fn
257 is_binary(url) and is_list(tags)
262 describe "GET /api/pleroma/healthcheck" do
263 setup do: clear_config([:instance, :healthcheck])
265 test "returns 503 when healthcheck disabled", %{conn: conn} do
266 Config.put([:instance, :healthcheck], false)
270 |> get("/api/pleroma/healthcheck")
271 |> json_response(503)
273 assert response == %{}
276 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
277 Config.put([:instance, :healthcheck], true)
279 with_mock Pleroma.Healthcheck,
280 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
283 |> get("/api/pleroma/healthcheck")
284 |> json_response(200)
296 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
297 Config.put([:instance, :healthcheck], true)
299 with_mock Pleroma.Healthcheck,
300 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
303 |> get("/api/pleroma/healthcheck")
304 |> json_response(503)
317 describe "POST /api/pleroma/disable_account" do
318 setup do: oauth_access(["write:accounts"])
320 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
323 |> post("/api/pleroma/disable_account", %{"password" => "test"})
324 |> json_response(:ok)
326 assert response == %{"status" => "success"}
327 ObanHelpers.perform_all()
329 user = User.get_cached_by_id(user.id)
331 assert user.deactivated == true
334 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
339 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
340 |> json_response(:ok)
342 assert response == %{"error" => "Invalid password."}
343 user = User.get_cached_by_id(user.id)
345 refute user.deactivated
349 describe "POST /main/ostatus - remote_subscribe/2" do
350 setup do: clear_config([:instance, :federating], true)
352 test "renders subscribe form", %{conn: conn} do
357 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
360 refute response =~ "Could not find user"
361 assert response =~ "Remotely follow #{user.nickname}"
364 test "renders subscribe form with error when user not found", %{conn: conn} do
367 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
370 assert response =~ "Could not find user"
371 refute response =~ "Remotely follow"
374 test "it redirect to webfinger url", %{conn: conn} do
376 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
380 |> post("/main/ostatus", %{
381 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
384 assert redirected_to(conn) ==
385 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
388 test "it renders form with error when user not found", %{conn: conn} do
389 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
393 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
396 assert response =~ "Something went wrong."
400 test "it returns new captcha", %{conn: conn} do
401 with_mock Pleroma.Captcha,
402 new: fn -> "test_captcha" end do
405 |> get("/api/pleroma/captcha")
408 assert resp == "\"test_captcha\""
409 assert called(Pleroma.Captcha.new())
413 describe "POST /api/pleroma/change_email" do
414 setup do: oauth_access(["write:accounts"])
416 test "without permissions", %{conn: conn} do
419 |> assign(:token, nil)
420 |> post("/api/pleroma/change_email")
422 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
425 test "with proper permissions and invalid password", %{conn: conn} do
427 post(conn, "/api/pleroma/change_email", %{
429 "email" => "test@test.com"
432 assert json_response(conn, 200) == %{"error" => "Invalid password."}
435 test "with proper permissions, valid password and invalid email", %{
439 post(conn, "/api/pleroma/change_email", %{
440 "password" => "test",
444 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
447 test "with proper permissions, valid password and no email", %{
451 post(conn, "/api/pleroma/change_email", %{
455 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
458 test "with proper permissions, valid password and blank email", %{
462 post(conn, "/api/pleroma/change_email", %{
463 "password" => "test",
467 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
470 test "with proper permissions, valid password and non unique email", %{
476 post(conn, "/api/pleroma/change_email", %{
477 "password" => "test",
478 "email" => user.email
481 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
484 test "with proper permissions, valid password and valid email", %{
488 post(conn, "/api/pleroma/change_email", %{
489 "password" => "test",
490 "email" => "cofe@foobar.com"
493 assert json_response(conn, 200) == %{"status" => "success"}
497 describe "POST /api/pleroma/change_password" do
498 setup do: oauth_access(["write:accounts"])
500 test "without permissions", %{conn: conn} do
503 |> assign(:token, nil)
504 |> post("/api/pleroma/change_password")
506 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
509 test "with proper permissions and invalid password", %{conn: conn} do
511 post(conn, "/api/pleroma/change_password", %{
513 "new_password" => "newpass",
514 "new_password_confirmation" => "newpass"
517 assert json_response(conn, 200) == %{"error" => "Invalid password."}
520 test "with proper permissions, valid password and new password and confirmation not matching",
525 post(conn, "/api/pleroma/change_password", %{
526 "password" => "test",
527 "new_password" => "newpass",
528 "new_password_confirmation" => "notnewpass"
531 assert json_response(conn, 200) == %{
532 "error" => "New password does not match confirmation."
536 test "with proper permissions, valid password and invalid new password", %{
540 post(conn, "/api/pleroma/change_password", %{
541 "password" => "test",
542 "new_password" => "",
543 "new_password_confirmation" => ""
546 assert json_response(conn, 200) == %{
547 "error" => "New password can't be blank."
551 test "with proper permissions, valid password and matching new password and confirmation", %{
556 post(conn, "/api/pleroma/change_password", %{
557 "password" => "test",
558 "new_password" => "newpass",
559 "new_password_confirmation" => "newpass"
562 assert json_response(conn, 200) == %{"status" => "success"}
563 fetched_user = User.get_cached_by_id(user.id)
564 assert Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
568 describe "POST /api/pleroma/delete_account" do
569 setup do: oauth_access(["write:accounts"])
571 test "without permissions", %{conn: conn} do
574 |> assign(:token, nil)
575 |> post("/api/pleroma/delete_account")
577 assert json_response(conn, 403) ==
578 %{"error" => "Insufficient permissions: write:accounts."}
581 test "with proper permissions and wrong or missing password", %{conn: conn} do
582 for params <- [%{"password" => "hi"}, %{}] do
583 ret_conn = post(conn, "/api/pleroma/delete_account", params)
585 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
589 test "with proper permissions and valid password", %{conn: conn, user: user} do
590 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
591 ObanHelpers.perform_all()
592 assert json_response(conn, 200) == %{"status" => "success"}
594 user = User.get_by_id(user.id)
595 assert user.deactivated == true
596 assert user.name == nil
597 assert user.bio == ""
598 assert user.password_hash == nil