Skip to content
Snippets Groups Projects
Commit 8d70d920 authored by Camille Simiand's avatar Camille Simiand
Browse files

Add tests for login after resetting password

parent b993b5f8
No related branches found
No related tags found
1 merge request!23Tuleap 47 reset my password
......@@ -2,7 +2,7 @@
<p>To reset your password, please visit the following link</p>
<a href="{{ url('app_reset_password', {token: resetToken.token}) }}">{{ url('app_reset_password', {token: resetToken.token}) }}</a>
<a href="{{ url('app_reset_password', {token: resetToken.token}) }}">Reset my password</a>
<p>This link will expire in {{ resetToken.expirationMessageKey|trans(resetToken.expirationMessageData, 'ResetPasswordBundle') }}.</p>
......
......@@ -4,33 +4,25 @@ namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Form;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\Mime\RawMessage;
class ResetPasswordControllerTest extends WebTestCase
{
private KernelBrowser $client;
private Form $form;
private const VERIFIED_USER_EMAIL = 'defaultUser@localhost.com';
private const UNVERIFIED_USER_EMAIL = 'notRegistered@localhost.com';
private const NEW_PASSWORD = "newpassword";
protected function setUp(): void
{
$this->client = static::createClient();
$crawler = $this->client->request('GET', '/reset-password');
$this->assertResponseIsSuccessful();
$this->client->enableProfiler();
$submit_button = $crawler->selectButton('Reset password');
$this->form = $submit_button->form();
}
public function testVerifiedUserShouldReceiveAnEmailLinkToResetHisPassword(): void
{
$this->form['reset_password_request_form[email]'] = self::VERIFIED_USER_EMAIL;
$this->client->submit($this->form);
$emailMessage = $this->getMailerMessage(0);
$emailMessage = $this->getEmailMessageWithResetPasswordLinkForVerifiedUser();
$this->assertEmailCount(1, null);
$this->assertEmailTextBodyContains($emailMessage, 'To reset your password, please visit the following link');
......@@ -44,9 +36,86 @@ class ResetPasswordControllerTest extends WebTestCase
public function testUnverifiedUserShouldNotReceiveAnEmailLinkToResetHisPassword(): void
{
$this->form['reset_password_request_form[email]'] = self::UNVERIFIED_USER_EMAIL;
$this->client->submit($this->form);
$this->submitResetPasswordFormWithAnUnverifiedEmailAddress();
$this->assertEmailCount(0);
}
public function testUserShouldBeAbleToLoginWithHisNewPasswordAfterResettingHisPassword(): void
{
$this->userShouldBeRedirectedToTheResetPasswordPageWhenClickingOnEmailLink();
$crawler = $this->client->request('GET', '/reset-password/reset');
$reset_password_submit_button = $crawler->selectButton('Reset password');
$reset_password_form = $reset_password_submit_button->form();
$reset_password_form['change_password_form[plainPassword][first]'] = self::NEW_PASSWORD;
$reset_password_form['change_password_form[plainPassword][second]'] = self::NEW_PASSWORD;
$this->client->submit($reset_password_form);
$this->assertResponseRedirects('/login');
$this->client->followRedirect();
$this->assertResponseIsSuccessful("/login");
$this->testRegisteredUserWithEnabledAccountIsRedirectedToCapsulesPageWhenSubmittingLoginForm();
}
private function getEmailMessageWithResetPasswordLinkForVerifiedUser(): ?RawMessage
{
$crawler = $this->client->request('GET', '/reset-password');
$this->assertResponseIsSuccessful();
$submit_button = $crawler->selectButton('Reset password');
$form = $submit_button->form();
$form['reset_password_request_form[email]'] = self::VERIFIED_USER_EMAIL;
$this->client->submit($form);
return $this->getMailerMessage(0);
}
private function submitResetPasswordFormWithAnUnverifiedEmailAddress(): void
{
$crawler = $this->client->request('GET', '/reset-password');
$this->assertResponseIsSuccessful();
$submit_button = $crawler->selectButton('Reset password');
$form = $submit_button->form();
$form['reset_password_request_form[email]'] = self::UNVERIFIED_USER_EMAIL;
$this->client->submit($form);
}
private function testRegisteredUserWithEnabledAccountIsRedirectedToCapsulesPageWhenSubmittingLoginForm(): void
{
$crawler = $this->client->request('GET', '/login');
$login_submit_button = $crawler->selectButton('Log in');
$login_form = $login_submit_button->form();
$login_form['email'] = self::VERIFIED_USER_EMAIL;
$login_form['password'] = self::NEW_PASSWORD;
$this->client->submit($login_form);
$this->assertResponseRedirects(
'/my_capsules',
302,
'Once the user is logged in, he should be redirected to its capsules lists'
);
$this->client->followRedirect();
$this->assertResponseIsSuccessful('/my_capsules');
}
private function userShouldBeRedirectedToTheResetPasswordPageWhenClickingOnEmailLink(): void
{
$emailMessage = $this->getEmailMessageWithResetPasswordLinkForVerifiedUser();
$crawler = new Crawler($emailMessage->getHtmlBody());
$crawlerLink = $crawler->selectLink('Reset my password')->link();
$this->client->click($crawlerLink);
$this->assertResponseRedirects("/reset-password/reset");
$this->client->followRedirect();
$this->assertResponseIsSuccessful("/reset-password/reset");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment