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 "PUT /api/pleroma/notification_settings" do
25 setup do: oauth_access(["write:accounts"])
27 test "it updates notification settings", %{user: user, conn: conn} do
29 |> put("/api/pleroma/notification_settings", %{
30 "block_from_strangers" => true,
35 user = refresh_record(user)
37 assert %Pleroma.User.NotificationSetting{
38 block_from_strangers: true,
39 hide_notification_contents: false
40 } == user.notification_settings
43 test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
45 |> put("/api/pleroma/notification_settings", %{"hide_notification_contents" => "1"})
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 Config.put(:frontend_configurations, config)
73 |> get("/api/pleroma/frontend_configurations")
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")
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 Config.put([:instance, :healthcheck], false)
106 |> get("/api/pleroma/healthcheck")
107 |> json_response(503)
109 assert response == %{}
112 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
113 Config.put([: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(200)
132 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
133 Config.put([: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(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(:ok)
162 assert response == %{"status" => "success"}
163 ObanHelpers.perform_all()
165 user = User.get_cached_by_id(user.id)
167 assert user.deactivated == true
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(:ok)
178 assert response == %{"error" => "Invalid password."}
179 user = User.get_cached_by_id(user.id)
181 refute user.deactivated
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 |> post("/api/pleroma/change_email")
258 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
261 test "with proper permissions and invalid password", %{conn: conn} do
263 post(conn, "/api/pleroma/change_email", %{
265 "email" => "test@test.com"
268 assert json_response(conn, 200) == %{"error" => "Invalid password."}
271 test "with proper permissions, valid password and invalid email", %{
275 post(conn, "/api/pleroma/change_email", %{
276 "password" => "test",
280 assert json_response(conn, 200) == %{"error" => "Email has invalid format."}
283 test "with proper permissions, valid password and no email", %{
287 post(conn, "/api/pleroma/change_email", %{
291 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
294 test "with proper permissions, valid password and blank email", %{
298 post(conn, "/api/pleroma/change_email", %{
299 "password" => "test",
303 assert json_response(conn, 200) == %{"error" => "Email can't be blank."}
306 test "with proper permissions, valid password and non unique email", %{
312 post(conn, "/api/pleroma/change_email", %{
313 "password" => "test",
314 "email" => user.email
317 assert json_response(conn, 200) == %{"error" => "Email has already been taken."}
320 test "with proper permissions, valid password and valid email", %{
324 post(conn, "/api/pleroma/change_email", %{
325 "password" => "test",
326 "email" => "cofe@foobar.com"
329 assert json_response(conn, 200) == %{"status" => "success"}
333 describe "POST /api/pleroma/change_password" do
334 setup do: oauth_access(["write:accounts"])
336 test "without permissions", %{conn: conn} do
339 |> assign(:token, nil)
340 |> post("/api/pleroma/change_password")
342 assert json_response(conn, 403) == %{"error" => "Insufficient permissions: write:accounts."}
345 test "with proper permissions and invalid password", %{conn: conn} do
347 post(conn, "/api/pleroma/change_password", %{
349 "new_password" => "newpass",
350 "new_password_confirmation" => "newpass"
353 assert json_response(conn, 200) == %{"error" => "Invalid password."}
356 test "with proper permissions, valid password and new password and confirmation not matching",
361 post(conn, "/api/pleroma/change_password", %{
362 "password" => "test",
363 "new_password" => "newpass",
364 "new_password_confirmation" => "notnewpass"
367 assert json_response(conn, 200) == %{
368 "error" => "New password does not match confirmation."
372 test "with proper permissions, valid password and invalid new password", %{
376 post(conn, "/api/pleroma/change_password", %{
377 "password" => "test",
378 "new_password" => "",
379 "new_password_confirmation" => ""
382 assert json_response(conn, 200) == %{
383 "error" => "New password can't be blank."
387 test "with proper permissions, valid password and matching new password and confirmation", %{
392 post(conn, "/api/pleroma/change_password", %{
393 "password" => "test",
394 "new_password" => "newpass",
395 "new_password_confirmation" => "newpass"
398 assert json_response(conn, 200) == %{"status" => "success"}
399 fetched_user = User.get_cached_by_id(user.id)
400 assert Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
404 describe "POST /api/pleroma/delete_account" do
405 setup do: oauth_access(["write:accounts"])
407 test "without permissions", %{conn: conn} do
410 |> assign(:token, nil)
411 |> post("/api/pleroma/delete_account")
413 assert json_response(conn, 403) ==
414 %{"error" => "Insufficient permissions: write:accounts."}
417 test "with proper permissions and wrong or missing password", %{conn: conn} do
418 for params <- [%{"password" => "hi"}, %{}] do
419 ret_conn = post(conn, "/api/pleroma/delete_account", params)
421 assert json_response(ret_conn, 200) == %{"error" => "Invalid password."}
425 test "with proper permissions and valid password", %{conn: conn, user: user} do
426 conn = post(conn, "/api/pleroma/delete_account", %{"password" => "test"})
427 ObanHelpers.perform_all()
428 assert json_response(conn, 200) == %{"status" => "success"}
430 user = User.get_by_id(user.id)
431 assert user.deactivated == true
432 assert user.name == nil
433 assert user.bio == ""
434 assert user.password_hash == nil