From 6894d48e03c397096bb64420373afa60c397fe97 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sat, 7 Mar 2015 23:25:36 +0100 Subject: Handle forgot password --- .../Tests/Controller/SecurityControllerTest.php | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'src/Wallabag/CoreBundle/Tests') diff --git a/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php b/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php index 54cf5073..e02c4d05 100644 --- a/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php +++ b/src/Wallabag/CoreBundle/Tests/Controller/SecurityControllerTest.php @@ -3,6 +3,8 @@ namespace Wallabag\CoreBundle\Tests\Controller; use Wallabag\CoreBundle\Tests\WallabagTestCase; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; class SecurityControllerTest extends WallabagTestCase { @@ -37,4 +39,99 @@ class SecurityControllerTest extends WallabagTestCase $this->assertContains('Bad credentials', $client->getResponse()->getContent()); } + + public function testForgotPassword() + { + $client = $this->getClient(); + + $crawler = $client->request('GET', '/forgot-password'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $this->assertContains('Forgot password', $client->getResponse()->getContent()); + + $form = $crawler->filter('button[type=submit]'); + + $this->assertCount(1, $form); + + return array( + 'form' => $form->form(), + 'client' => $client, + ); + } + + /** + * @depends testForgotPassword + */ + public function testSubmitForgotPasswordFail($parameters) + { + $form = $parameters['form']; + $client = $parameters['client']; + + $data = array( + 'forgot_password[email]' => 'baggy', + ); + + $client->submit($form, $data); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertContains('No user found with this email', $client->getResponse()->getContent()); + } + + /** + * @depends testForgotPassword + * + * Instead of using collector which slow down the test suite + * http://symfony.com/doc/current/cookbook/email/testing.html + * + * Use a different way where Swift store email as file + */ + public function testSubmitForgotPassword($parameters) + { + $form = $parameters['form']; + $client = $parameters['client']; + + $spoolDir = $client->getKernel()->getContainer()->getParameter('swiftmailer.spool.default.file.path'); + + // cleanup pool dir + $filesystem = new Filesystem(); + $filesystem->remove($spoolDir); + + // to use `getCollector` since `collect: false` in config_test.yml + $client->enableProfiler(); + + $data = array( + 'forgot_password[email]' => 'bobby@wallabag.org', + ); + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('An email has been sent to', $client->getResponse()->getContent()); + + // find every files (ie: emails) inside the spool dir except hidden files + $finder = new Finder(); + $finder + ->in($spoolDir) + ->ignoreDotFiles(true) + ->files(); + + $this->assertCount(1, $finder, 'Only one email has been sent'); + + foreach ($finder as $file) { + $message = unserialize(file_get_contents($file)); + + $this->assertInstanceOf('Swift_Message', $message); + $this->assertEquals('Reset Password', $message->getSubject()); + $this->assertEquals('no-reply@wallabag.org', key($message->getFrom())); + $this->assertEquals('bobby@wallabag.org', key($message->getTo())); + $this->assertContains( + 'To reset your password - please visit', + $message->getBody() + ); + } + } } -- cgit v1.2.3