2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-06-24 13:01:56 -06:00
|
|
|
defmodule Pleroma.Web.TwitterAPI.PasswordControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.PasswordResetToken
|
2019-09-22 07:08:07 -06:00
|
|
|
alias Pleroma.User
|
2019-06-24 13:01:56 -06:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
describe "GET /api/pleroma/password_reset/token" do
|
|
|
|
test "it returns error when token invalid", %{conn: conn} do
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> get("/api/pleroma/password_reset/token")
|
|
|
|
|> html_response(:ok)
|
|
|
|
|
|
|
|
assert response =~ "<h2>Invalid Token</h2>"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it shows password reset form", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, token} = PasswordResetToken.create_token(user)
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> get("/api/pleroma/password_reset/#{token.token}")
|
|
|
|
|> html_response(:ok)
|
|
|
|
|
|
|
|
assert response =~ "<h2>Password Reset for #{user.nickname}</h2>"
|
|
|
|
end
|
2020-11-19 04:27:06 -07:00
|
|
|
|
|
|
|
test "it returns an error when the token has expired", %{conn: conn} do
|
|
|
|
clear_config([:instance, :password_reset_token_validity], 0)
|
|
|
|
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, token} = PasswordResetToken.create_token(user)
|
|
|
|
|
|
|
|
:timer.sleep(2000)
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> get("/api/pleroma/password_reset/#{token.token}")
|
|
|
|
|> html_response(:ok)
|
|
|
|
|
|
|
|
assert response =~ "<h2>Invalid Token</h2>"
|
|
|
|
end
|
2019-06-24 13:01:56 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /api/pleroma/password_reset" do
|
2020-11-19 04:27:06 -07:00
|
|
|
test "it fails for an expired token", %{conn: conn} do
|
|
|
|
clear_config([:instance, :password_reset_token_validity], 0)
|
|
|
|
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, token} = PasswordResetToken.create_token(user)
|
|
|
|
:timer.sleep(2000)
|
|
|
|
{:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
|
|
|
|
|
|
|
|
params = %{
|
|
|
|
"password" => "test",
|
|
|
|
password_confirmation: "test",
|
|
|
|
token: token.token
|
|
|
|
}
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> post("/api/pleroma/password_reset", %{data: params})
|
|
|
|
|> html_response(:ok)
|
|
|
|
|
|
|
|
refute response =~ "<h2>Password changed!</h2>"
|
|
|
|
end
|
|
|
|
|
2019-06-24 13:01:56 -06:00
|
|
|
test "it returns HTTP 200", %{conn: conn} do
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, token} = PasswordResetToken.create_token(user)
|
2020-08-25 10:17:51 -06:00
|
|
|
{:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
|
2019-06-24 13:01:56 -06:00
|
|
|
|
|
|
|
params = %{
|
|
|
|
"password" => "test",
|
|
|
|
password_confirmation: "test",
|
|
|
|
token: token.token
|
|
|
|
}
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> post("/api/pleroma/password_reset", %{data: params})
|
|
|
|
|> html_response(:ok)
|
|
|
|
|
|
|
|
assert response =~ "<h2>Password changed!</h2>"
|
|
|
|
|
|
|
|
user = refresh_record(user)
|
2020-05-12 15:42:24 -06:00
|
|
|
assert Pbkdf2.verify_pass("test", user.password_hash)
|
2020-01-20 03:53:14 -07:00
|
|
|
assert Enum.empty?(Token.get_user_tokens(user))
|
2019-06-24 13:01:56 -06:00
|
|
|
end
|
2019-09-22 07:08:07 -06:00
|
|
|
|
|
|
|
test "it sets password_reset_pending to false", %{conn: conn} do
|
2019-10-16 12:59:21 -06:00
|
|
|
user = insert(:user, password_reset_pending: true)
|
2019-09-22 07:08:07 -06:00
|
|
|
|
|
|
|
{:ok, token} = PasswordResetToken.create_token(user)
|
2020-08-25 10:17:51 -06:00
|
|
|
{:ok, _access_token} = Token.create(insert(:oauth_app), user, %{})
|
2019-09-22 07:08:07 -06:00
|
|
|
|
|
|
|
params = %{
|
|
|
|
"password" => "test",
|
|
|
|
password_confirmation: "test",
|
|
|
|
token: token.token
|
|
|
|
}
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> post("/api/pleroma/password_reset", %{data: params})
|
|
|
|
|> html_response(:ok)
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert User.get_by_id(user.id).password_reset_pending == false
|
2019-09-22 07:08:07 -06:00
|
|
|
end
|
2019-06-24 13:01:56 -06:00
|
|
|
end
|
|
|
|
end
|