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
28 |> put("/api/pleroma/notification_settings", %{
29 "block_from_strangers" => true,
34 user = refresh_record(user)
36 assert %Pleroma.User.NotificationSetting{
37 block_from_strangers: true,
38 hide_notification_contents: false
39 } == user.notification_settings
42 test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
44 |> put("/api/pleroma/notification_settings", %{"hide_notification_contents" => "1"})
47 user = refresh_record(user)
49 assert %Pleroma.User.NotificationSetting{
50 block_from_strangers: false,
51 hide_notification_contents: true
52 } == user.notification_settings
56 describe "GET /api/pleroma/frontend_configurations" do
57 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
68 clear_config(:frontend_configurations, config)
72 |> get("/api/pleroma/frontend_configurations")
75 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
79 describe "/api/pleroma/emoji" do
80 test "returns json with custom emoji with tags", %{conn: conn} do
83 |> get("/api/pleroma/emoji")
86 assert Enum.all?(emoji, fn
92 is_binary(url) and is_list(tags)
97 describe "GET /api/pleroma/healthcheck" do
98 setup do: clear_config([:instance, :healthcheck])
100 test "returns 503 when healthcheck disabled", %{conn: conn} do
101 clear_config([:instance, :healthcheck], false)
105 |> get("/api/pleroma/healthcheck")
106 |> json_response(503)
108 assert response == %{}
111 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
112 clear_config([:instance, :healthcheck], true)
114 with_mock Pleroma.Healthcheck,
115 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
118 |> get("/api/pleroma/healthcheck")
119 |> json_response(200)
131 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
132 clear_config([:instance, :healthcheck], true)
134 with_mock Pleroma.Healthcheck,
135 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
138 |> get("/api/pleroma/healthcheck")
139 |> json_response(503)
152 describe "POST /api/pleroma/disable_account" do
153 setup do: oauth_access(["write:accounts"])
155 test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
158 |> post("/api/pleroma/disable_account", %{"password" => "test"})
159 |> json_response(:ok)
161 assert response == %{"status" => "success"}
162 ObanHelpers.perform_all()
164 user = User.get_cached_by_id(user.id)
166 refute user.is_active
169 test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
174 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
175 |> json_response(:ok)
177 assert response == %{"error" => "Invalid password."}
178 user = User.get_cached_by_id(user.id)
180 assert user.is_active
184 describe "POST /main/ostatus - remote_subscribe/2" do
185 setup do: clear_config([:instance, :federating], true)
187 test "renders subscribe form", %{conn: conn} do
192 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
195 refute response =~ "Could not find user"
196 assert response =~ "Remotely follow #{user.nickname}"
199 test "renders subscribe form with error when user not found", %{conn: conn} do
202 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
205 assert response =~ "Could not find user"
206 refute response =~ "Remotely follow"
209 test "it redirect to webfinger url", %{conn: conn} do
211 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
215 |> post("/main/ostatus", %{
216 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
219 assert redirected_to(conn) ==
220 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
223 test "it renders form with error when user not found", %{conn: conn} do
224 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
228 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
231 assert response =~ "Something went wrong."
235 test "it returns new captcha", %{conn: conn} do
236 with_mock Pleroma.Captcha,
237 new: fn -> "test_captcha" end do
240 |> get("/api/pleroma/captcha")
243 assert resp == "\"test_captcha\""
244 assert called(Pleroma.Captcha.new())
248 describe "POST /api/pleroma/change_email" do
249 setup do: oauth_access(["write:accounts"])
251 test "without permissions", %{conn: conn} do
254 |> assign(:token, nil)
255 |> post("/api/pleroma/change_email")
257 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
260 test "with proper permissions and invalid password", %{conn: conn} do
262 post(conn, "/api/pleroma/change_email", %{
264 "email" => "test@test.com"
267 assert json_response(conn, 200) == %{"error" => "Invalid password."}
270 test "with proper permissions, valid password and invalid email", %{
274 post(conn, "/api/pleroma/change_email", %{
275 "password" => "test",
279 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
282 test "with proper permissions, valid password and no email", %{
286 post(conn, "/api/pleroma/change_email", %{
290 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
293 test "with proper permissions, valid password and blank email", %{
297 post(conn, "/api/pleroma/change_email", %{
298 "password" => "test",
302 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
305 test "with proper permissions, valid password and non unique email", %{
311 post(conn, "/api/pleroma/change_email", %{
312 "password" => "test",
313 "email" => user.email
316 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
319 test "with proper permissions, valid password and valid email", %{
323 post(conn, "/api/pleroma/change_email", %{
324 "password" => "test",
325 "email" => "cofe@foobar.com"
328 assert json_response(conn, 200) == %{"status" => "success"}
332 describe "POST /api/pleroma/change_password" do
333 setup do: oauth_access(["write:accounts"])
335 test "without permissions", %{conn: conn} do
338 |> assign(:token, nil)
339 |> post("/api/pleroma/change_password")
341 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
344 test "with proper permissions and invalid password", %{conn: conn} do
346 post(conn, "/api/pleroma/change_password", %{
348 "new_password" => "newpass",
349 "new_password_confirmation" => "newpass"
352 assert json_response(conn, 200) == %{"error" => "Invalid password."}
355 test "with proper permissions, valid password and new password and confirmation not matching",
360 post(conn, "/api/pleroma/change_password", %{
361 "password" => "test",
362 "new_password" => "newpass",
363 "new_password_confirmation" => "notnewpass"
366 assert json_response(conn, 200) == %{
367 "error" => "New password does not match confirmation."
371 test "with proper permissions, valid password and invalid new password", %{
375 post(conn, "/api/pleroma/change_password", %{
376 "password" => "test",
377 "new_password" => "",
378 "new_password_confirmation" => ""
381 assert json_response(conn, 200) == %{
382 "error" => "New password can't be blank."
386 test "with proper permissions, valid password and matching new password and confirmation", %{
391 post(conn, "/api/pleroma/change_password", %{
392 "password" => "test",
393 "new_password" => "newpass",
394 "new_password_confirmation" => "newpass"
397 assert json_response(conn, 200) == %{"status" => "success"}
398 fetched_user = User.get_cached_by_id(user.id)
399 assert Pleroma.Password.Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
403 describe "POST /api/pleroma/delete_account" do
404 setup do: oauth_access(["write:accounts"])
406 test "without permissions", %{conn: conn} do
409 |> assign(:token, nil)
410 |> post("/api/pleroma/delete_account")
412 assert json_response(conn, 403) ==
413 %{"error" => "Insufficient permissions: write:accounts."}
416 test "with proper permissions and wrong or missing password", %{conn: conn} do
417 for params <- [%{"password" => "hi"}, %{}] do
418 ret_conn = post(conn, "/api/pleroma/delete_account", params)
420 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
424 test "with proper permissions and valid password", %{conn: conn, user: user} do
425 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
426 ObanHelpers.perform_all()
427 assert json_response(conn, 200) == %{"status" => "success"}
429 user = User.get_by_id(user.id)
430 refute user.is_active
431 assert user.name == nil
432 assert user.bio == ""
433 assert user.password_hash == nil