Fixed password reminder
This commit is contained in:
parent
a89eb97c9d
commit
3cd07a38ca
3 changed files with 129 additions and 3 deletions
|
@ -17,7 +17,7 @@ class PasswordResetJob extends AbstractJob
|
||||||
if (empty($user->getConfirmedEmail()))
|
if (empty($user->getConfirmedEmail()))
|
||||||
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
|
throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed');
|
||||||
|
|
||||||
UserModel::sendPasswordResetEmail($user);
|
self::sendEmail($user);
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ class PasswordResetJob extends AbstractJob
|
||||||
|
|
||||||
$user = $token->getUser();
|
$user = $token->getUser();
|
||||||
$user->setPassword($newPassword);
|
$user->setPassword($newPassword);
|
||||||
$token->used = true;
|
$token->setUsed(true);
|
||||||
TokenModel::save($token);
|
TokenModel::save($token);
|
||||||
UserModel::save($user);
|
UserModel::save($user);
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,6 @@ class ActivateUserEmailJobTest extends AbstractTest
|
||||||
$this->assert->areEqual('godzilla@whitestar.gov', $user->getConfirmedEmail());
|
$this->assert->areEqual('godzilla@whitestar.gov', $user->getConfirmedEmail());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testUsingTokenTwice()
|
public function testUsingTokenTwice()
|
||||||
{
|
{
|
||||||
getConfig()->registration->needEmailForRegistering = true;
|
getConfig()->registration->needEmailForRegistering = true;
|
||||||
|
|
127
tests/JobTests/PasswordResetJobTest.php
Normal file
127
tests/JobTests/PasswordResetJobTest.php
Normal file
|
@ -0,0 +1,127 @@
|
||||||
|
<?php
|
||||||
|
class PasswordResetJobTest extends AbstractTest
|
||||||
|
{
|
||||||
|
public function testDontSendIfUnconfirmedMail()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = true;
|
||||||
|
Mailer::mockSending();
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
$user->setUnconfirmedEmail('godzilla@whitestar.gov');
|
||||||
|
UserModel::save($user);
|
||||||
|
|
||||||
|
$this->assert->throws(function() use ($user)
|
||||||
|
{
|
||||||
|
Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
||||||
|
]);
|
||||||
|
}, 'no e-mail confirmed');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSending()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = true;
|
||||||
|
Mailer::mockSending();
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
$user->setConfirmedEmail('godzilla@whitestar.gov');
|
||||||
|
UserModel::save($user);
|
||||||
|
|
||||||
|
$this->assert->areEqual(0, Mailer::getMailCounter());
|
||||||
|
|
||||||
|
$this->assert->doesNotThrow(function() use ($user)
|
||||||
|
{
|
||||||
|
Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->assert->areEqual(1, Mailer::getMailCounter());
|
||||||
|
|
||||||
|
$tokens = Mailer::getMailsSent()[0]->tokens;
|
||||||
|
$tokenText = $tokens['token'];
|
||||||
|
$token = TokenModel::getByToken($tokenText);
|
||||||
|
|
||||||
|
$this->assert->areEqual($user->getId(), $token->getUser()->getId());
|
||||||
|
$this->assert->isTrue(strpos($tokens['link'], $tokenText) !== false);
|
||||||
|
|
||||||
|
return $tokenText;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testObtainingNewPassword()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = true;
|
||||||
|
Mailer::mockSending();
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
$user->setConfirmedEmail('godzilla@whitestar.gov');
|
||||||
|
UserModel::save($user);
|
||||||
|
|
||||||
|
$this->assert->doesNotThrow(function() use ($user)
|
||||||
|
{
|
||||||
|
Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$tokenText = Mailer::getMailsSent()[0]->tokens['token'];
|
||||||
|
|
||||||
|
$ret = $this->assert->doesNotThrow(function() use ($tokenText)
|
||||||
|
{
|
||||||
|
return Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_TOKEN => $tokenText,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
$user = $ret->user;
|
||||||
|
$newPassword = $ret->newPassword;
|
||||||
|
$newPasswordHash = UserModel::hashPassword($newPassword, $user->getPasswordSalt());
|
||||||
|
|
||||||
|
$this->assert->areEqual($newPasswordHash, $user->getPasswordHash());
|
||||||
|
$this->assert->doesNotThrow(function() use ($user, $newPassword)
|
||||||
|
{
|
||||||
|
Auth::login($user->getName(), $newPassword, false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUsingTokenTwice()
|
||||||
|
{
|
||||||
|
getConfig()->registration->needEmailForRegistering = true;
|
||||||
|
Mailer::mockSending();
|
||||||
|
|
||||||
|
$user = $this->mockUser();
|
||||||
|
$user->setConfirmedEmail('godzilla@whitestar.gov');
|
||||||
|
UserModel::save($user);
|
||||||
|
|
||||||
|
Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_USER_NAME => $user->getName(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tokenText = Mailer::getMailsSent()[0]->tokens['token'];
|
||||||
|
|
||||||
|
Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_TOKEN => $tokenText,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assert->throws(function() use ($tokenText)
|
||||||
|
{
|
||||||
|
Api::run(
|
||||||
|
new PasswordResetJob(),
|
||||||
|
[
|
||||||
|
JobArgs::ARG_TOKEN => $tokenText,
|
||||||
|
]);
|
||||||
|
}, 'This token was already used');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue