1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 setup do: clear_config([:instance])
21 setup do: clear_config([:frontend_configurations, :pleroma_fe])
23 describe "PUT /api/pleroma/notification_settings" do
24 setup do: oauth_access(["write:accounts"])
26 test "it updates notification settings", %{user: user, conn: conn} do
29 "/api/pleroma/notification_settings?#{URI.encode_query(%{block_from_strangers: true})}"
31 |> json_response_and_validate_schema(:ok)
33 user = refresh_record(user)
35 assert %Pleroma.User.NotificationSetting{
36 block_from_strangers: true,
37 hide_notification_contents: false
38 } == user.notification_settings
41 test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
44 "/api/pleroma/notification_settings?#{URI.encode_query(%{hide_notification_contents: 1})}"
46 |> json_response_and_validate_schema(:ok)
48 user = refresh_record(user)
50 assert %Pleroma.User.NotificationSetting{
51 block_from_strangers: false,
52 hide_notification_contents: true
53 } == user.notification_settings
57 describe "GET /api/pleroma/frontend_configurations" do
58 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
69 clear_config(:frontend_configurations, config)
73 |> get("/api/pleroma/frontend_configurations")
74 |> json_response_and_validate_schema(:ok)
76 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
80 describe "/api/pleroma/emoji" do
81 test "returns json with custom emoji with tags", %{conn: conn} do
84 |> get("/api/pleroma/emoji")
85 |> json_response_and_validate_schema(200)
87 assert Enum.all?(emoji, fn
93 is_binary(url) and is_list(tags)
98 describe "GET /api/pleroma/healthcheck" do
99 setup do: clear_config([:instance, :healthcheck])
101 test "returns 503 when healthcheck disabled", %{conn: conn} do
102 clear_config([:instance, :healthcheck], false)
106 |> get("/api/pleroma/healthcheck")
107 |> json_response_and_validate_schema(503)
109 assert response == %{}
112 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
113 clear_config([:instance, :healthcheck], true)
115 with_mock Pleroma.Healthcheck,
116 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
119 |> get("/api/pleroma/healthcheck")
120 |> json_response_and_validate_schema(200)
132 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
133 clear_config([:instance, :healthcheck], true)
135 with_mock Pleroma.Healthcheck,
136 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
139 |> get("/api/pleroma/healthcheck")
140 |> json_response_and_validate_schema(503)
153 describe "POST /api/pleroma/disable_account" do
154 setup do: oauth_access(["write:accounts"])
156 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
159 |> post("/api/pleroma/disable_account?password=test")
160 |> json_response_and_validate_schema(:ok)
162 assert response == %{"status" => "success"}
163 ObanHelpers.perform_all()
165 user = User.get_cached_by_id(user.id)
167 refute user.is_active
170 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
175 |> post("/api/pleroma/disable_account?password=test1")
176 |> json_response_and_validate_schema(:ok)
178 assert response == %{"error" => "Invalid password."}
179 user = User.get_cached_by_id(user.id)
181 assert user.is_active
185 describe "POST /main/ostatus - remote_subscribe/2" do
186 setup do: clear_config([:instance, :federating], true)
188 test "renders subscribe form", %{conn: conn} do
193 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
196 refute response =~ "Could not find user"
197 assert response =~ "Remotely follow #{user.nickname}"
200 test "renders subscribe form with error when user not found", %{conn: conn} do
203 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
206 assert response =~ "Could not find user"
207 refute response =~ "Remotely follow"
210 test "it redirect to webfinger url", %{conn: conn} do
212 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
216 |> post("/main/ostatus", %{
217 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
220 assert redirected_to(conn) ==
221 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
224 test "it renders form with error when user not found", %{conn: conn} do
225 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
229 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
232 assert response =~ "Something went wrong."
236 test "it returns new captcha", %{conn: conn} do
237 with_mock Pleroma.Captcha,
238 new: fn -> "test_captcha" end do
241 |> get("/api/pleroma/captcha")
244 assert resp == "\"test_captcha\""
245 assert called(Pleroma.Captcha.new())
249 describe "POST /api/pleroma/change_email" do
250 setup do: oauth_access(["write:accounts"])
252 test "without permissions", %{conn: conn} do
255 |> assign(:token, nil)
256 |> put_req_header("content-type", "multipart/form-data")
257 |> post("/api/pleroma/change_email", %{password: "hi", email: "test@test.com"})
259 assert json_response_and_validate_schema(conn, 403) == %{
260 "error" => "Insufficient permissions: write:accounts."
264 test "with proper permissions and invalid password", %{conn: conn} do
267 |> put_req_header("content-type", "multipart/form-data")
268 |> post("/api/pleroma/change_email", %{password: "hi", email: "test@test.com"})
270 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
273 test "with proper permissions, valid password and invalid email", %{
278 |> put_req_header("content-type", "multipart/form-data")
279 |> post("/api/pleroma/change_email", %{password: "test", email: "foobar"})
281 assert json_response_and_validate_schema(conn, 200) == %{
282 "error" => "Email has invalid format."
286 test "with proper permissions, valid password and no email", %{
291 |> put_req_header("content-type", "multipart/form-data")
292 |> post("/api/pleroma/change_email", %{password: "test"})
294 assert %{"error" => "Missing field: email."} = json_response_and_validate_schema(conn, 400)
297 test "with proper permissions, valid password and blank email, when instance requires user email",
301 orig_account_activation_required =
302 Pleroma.Config.get([:instance, :account_activation_required])
304 Pleroma.Config.put([:instance, :account_activation_required], true)
308 [:instance, :account_activation_required],
309 orig_account_activation_required
315 |> put_req_header("content-type", "multipart/form-data")
316 |> post("/api/pleroma/change_email", %{password: "test", email: ""})
318 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Email can't be blank."}
321 test "with proper permissions, valid password and blank email, when instance does not require user email",
325 orig_account_activation_required =
326 Pleroma.Config.get([:instance, :account_activation_required])
328 Pleroma.Config.put([:instance, :account_activation_required], false)
332 [:instance, :account_activation_required],
333 orig_account_activation_required
339 |> put_req_header("content-type", "multipart/form-data")
340 |> post("/api/pleroma/change_email", %{password: "test", email: ""})
342 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
345 test "with proper permissions, valid password and non unique email", %{
352 |> put_req_header("content-type", "multipart/form-data")
353 |> post("/api/pleroma/change_email", %{password: "test", email: user.email})
355 assert json_response_and_validate_schema(conn, 200) == %{
356 "error" => "Email has already been taken."
360 test "with proper permissions, valid password and valid email", %{
365 |> put_req_header("content-type", "multipart/form-data")
366 |> post("/api/pleroma/change_email", %{password: "test", email: "cofe@foobar.com"})
368 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
372 describe "POST /api/pleroma/change_password" do
373 setup do: oauth_access(["write:accounts"])
375 test "without permissions", %{conn: conn} do
378 |> assign(:token, nil)
379 |> put_req_header("content-type", "multipart/form-data")
380 |> post("/api/pleroma/change_password", %{
382 "new_password" => "newpass",
383 "new_password_confirmation" => "newpass"
386 assert json_response_and_validate_schema(conn, 403) == %{
387 "error" => "Insufficient permissions: write:accounts."
391 test "with proper permissions and invalid password", %{conn: conn} do
394 |> put_req_header("content-type", "multipart/form-data")
395 |> post("/api/pleroma/change_password", %{
397 "new_password" => "newpass",
398 "new_password_confirmation" => "newpass"
401 assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
404 test "with proper permissions, valid password and new password and confirmation not matching",
410 |> put_req_header("content-type", "multipart/form-data")
411 |> post("/api/pleroma/change_password", %{
412 "password" => "test",
413 "new_password" => "newpass",
414 "new_password_confirmation" => "notnewpass"
417 assert json_response_and_validate_schema(conn, 200) == %{
418 "error" => "New password does not match confirmation."
422 test "with proper permissions, valid password and invalid new password", %{
427 |> put_req_header("content-type", "multipart/form-data")
428 |> post("/api/pleroma/change_password", %{
431 new_password_confirmation: ""
434 assert json_response_and_validate_schema(conn, 200) == %{
435 "error" => "New password can't be blank."
439 test "with proper permissions, valid password and matching new password and confirmation", %{
445 |> put_req_header("content-type", "multipart/form-data")
447 "/api/pleroma/change_password",
450 new_password: "newpass",
451 new_password_confirmation: "newpass"
455 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
456 fetched_user = User.get_cached_by_id(user.id)
457 assert Pleroma.Password.Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
461 describe "POST /api/pleroma/delete_account" do
462 setup do: oauth_access(["write:accounts"])
464 test "without permissions", %{conn: conn} do
467 |> assign(:token, nil)
468 |> post("/api/pleroma/delete_account")
470 assert json_response_and_validate_schema(conn, 403) ==
471 %{"error" => "Insufficient permissions: write:accounts."}
474 test "with proper permissions and wrong or missing password", %{conn: conn} do
475 for params <- [%{"password" => "hi"}, %{}] do
478 |> put_req_header("content-type", "application/json")
479 |> post("/api/pleroma/delete_account", params)
481 assert json_response_and_validate_schema(ret_conn, 200) == %{
482 "error" => "Invalid password."
487 test "with proper permissions and valid password (URL query)", %{conn: conn, user: user} do
490 |> put_req_header("content-type", "application/json")
491 |> post("/api/pleroma/delete_account?password=test")
493 ObanHelpers.perform_all()
494 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
496 user = User.get_by_id(user.id)
497 refute user.is_active
498 assert user.name == nil
499 assert user.bio == ""
500 assert user.password_hash == nil
503 test "with proper permissions and valid password (JSON body)", %{conn: conn, user: user} do
506 |> put_req_header("content-type", "application/json")
507 |> post("/api/pleroma/delete_account", %{password: "test"})
509 ObanHelpers.perform_all()
510 assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
512 user = User.get_by_id(user.id)
513 refute user.is_active
514 assert user.name == nil
515 assert user.bio == ""
516 assert user.password_hash == nil