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.PasswordControllerTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.PasswordResetToken
10 alias Pleroma.Web.OAuth.Token
11 import Pleroma.Factory
13 describe "GET /api/pleroma/password_reset/token" do
14 test "it returns error when token invalid", %{conn: conn} do
17 |> get("/api/pleroma/password_reset/token")
20 assert response =~ "<h2>Invalid Token</h2>"
23 test "it shows password reset form", %{conn: conn} do
25 {:ok, token} = PasswordResetToken.create_token(user)
29 |> get("/api/pleroma/password_reset/#{token.token}")
32 assert response =~ "<h2>Password Reset for #{user.nickname}</h2>"
35 test "it returns an error when the token has expired", %{conn: conn} do
36 clear_config([:instance, :password_reset_token_validity], 0)
39 {:ok, token} = PasswordResetToken.create_token(user)
40 {:ok, token} = time_travel(token, -2)
44 |> get("/api/pleroma/password_reset/#{token.token}")
47 assert response =~ "<h2>Invalid Token</h2>"
51 describe "POST /api/pleroma/password_reset" do
52 test "it fails for an expired token", %{conn: conn} do
53 clear_config([:instance, :password_reset_token_validity], 0)
56 {:ok, token} = PasswordResetToken.create_token(user)
57 {:ok, token} = time_travel(token, -2)
58 {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
62 password_confirmation: "test",
68 |> assign(:user, user)
69 |> post("/api/pleroma/password_reset", %{data: params})
72 refute response =~ "<h2>Password changed!</h2>"
75 test "it returns HTTP 200", %{conn: conn} do
77 {:ok, token} = PasswordResetToken.create_token(user)
78 {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
82 password_confirmation: "test",
88 |> assign(:user, user)
89 |> post("/api/pleroma/password_reset", %{data: params})
92 assert response =~ "<h2>Password changed!</h2>"
94 user = refresh_record(user)
95 assert Pleroma.Password.Pbkdf2.verify_pass("test", user.password_hash)
96 assert Enum.empty?(Token.get_user_tokens(user))
99 test "it sets password_reset_pending to false", %{conn: conn} do
100 user = insert(:user, password_reset_pending: true)
102 {:ok, token} = PasswordResetToken.create_token(user)
103 {:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
106 "password" => "test",
107 password_confirmation: "test",
112 |> assign(:user, user)
113 |> post("/api/pleroma/password_reset", %{data: params})
114 |> html_response(:ok)
116 assert User.get_by_id(user.id).password_reset_pending == false