Merge pull request #1436 from wallabag/v2-register

Public registration & oAuth2 \o/
This commit is contained in:
Jeremy Benoist
2015-10-06 09:19:06 +02:00
82 changed files with 1148 additions and 1350 deletions

View File

@ -258,7 +258,8 @@ class ConfigControllerTest extends WallabagCoreTestCase
array(
array(
'new_user[username]' => '',
'new_user[password]' => '',
'new_user[plainPassword][first]' => '',
'new_user[plainPassword][second]' => '',
'new_user[email]' => '',
),
'Please enter a username',
@ -266,7 +267,8 @@ class ConfigControllerTest extends WallabagCoreTestCase
array(
array(
'new_user[username]' => 'a',
'new_user[password]' => 'mypassword',
'new_user[plainPassword][first]' => 'mypassword',
'new_user[plainPassword][second]' => 'mypassword',
'new_user[email]' => '',
),
'The username is too short',
@ -274,7 +276,8 @@ class ConfigControllerTest extends WallabagCoreTestCase
array(
array(
'new_user[username]' => 'wallace',
'new_user[password]' => 'mypassword',
'new_user[plainPassword][first]' => 'mypassword',
'new_user[plainPassword][second]' => 'mypassword',
'new_user[email]' => 'test',
),
'The email is not valid',
@ -282,11 +285,21 @@ class ConfigControllerTest extends WallabagCoreTestCase
array(
array(
'new_user[username]' => 'admin',
'new_user[password]' => 'wallacewallace',
'new_user[plainPassword][first]' => 'wallacewallace',
'new_user[plainPassword][second]' => 'wallacewallace',
'new_user[email]' => 'wallace@wallace.me',
),
'The username is already used',
),
array(
array(
'new_user[username]' => 'wallace',
'new_user[plainPassword][first]' => 'mypassword1',
'new_user[plainPassword][second]' => 'mypassword2',
'new_user[email]' => 'wallace@wallace.me',
),
'This value is not valid',
),
);
}
@ -325,7 +338,8 @@ class ConfigControllerTest extends WallabagCoreTestCase
$data = array(
'new_user[username]' => 'wallace',
'new_user[password]' => 'wallace1',
'new_user[plainPassword][first]' => 'wallace1',
'new_user[plainPassword][second]' => 'wallace1',
'new_user[email]' => 'wallace@wallace.me',
);
@ -340,7 +354,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $em
->getRepository('WallabagCoreBundle:User')
->getRepository('WallabagUserBundle:User')
->findOneByUsername('wallace');
$this->assertTrue(false !== $user);
@ -355,7 +369,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
// reset the token
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $em
->getRepository('WallabagCoreBundle:User')
->getRepository('WallabagUserBundle:User')
->findOneByUsername('admin');
if (!$user) {

View File

@ -64,7 +64,7 @@ class RssControllerTest extends WallabagCoreTestCase
$client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $em
->getRepository('WallabagCoreBundle:User')
->getRepository('WallabagUserBundle:User')
->findOneByUsername('admin');
$config = $user->getConfig();
@ -85,7 +85,7 @@ class RssControllerTest extends WallabagCoreTestCase
$client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $em
->getRepository('WallabagCoreBundle:User')
->getRepository('WallabagUserBundle:User')
->findOneByUsername('admin');
$config = $user->getConfig();
@ -107,7 +107,7 @@ class RssControllerTest extends WallabagCoreTestCase
$client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$user = $em
->getRepository('WallabagCoreBundle:User')
->getRepository('WallabagUserBundle:User')
->findOneByUsername('admin');
$config = $user->getConfig();

View File

@ -1,201 +0,0 @@
<?php
namespace Wallabag\CoreBundle\Tests\Controller;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Wallabag\CoreBundle\Tests\WallabagCoreTestCase;
class SecurityControllerTest extends WallabagCoreTestCase
{
public function testLogin()
{
$client = $this->getClient();
$crawler = $client->request('GET', '/new');
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertContains('login', $client->getResponse()->headers->get('location'));
}
public function testLoginFail()
{
$client = $this->getClient();
$crawler = $client->request('GET', '/login');
$form = $crawler->filter('button[type=submit]')->form();
$data = array(
'_username' => 'admin',
'_password' => 'admin',
);
$client->submit($form, $data);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertContains('login', $client->getResponse()->headers->get('location'));
$crawler = $client->followRedirect();
$this->assertContains('Bad credentials', $client->getResponse()->getContent());
}
public function testRedirectionAfterLogin()
{
$client = $this->getClient();
$client->followRedirects();
$crawler = $client->request('GET', '/config');
$form = $crawler->filter('button[type=submit]')->form();
$data = array(
'_username' => 'admin',
'_password' => 'mypassword',
);
$client->submit($form, $data);
$this->assertContains('RSS', $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]' => 'material',
);
$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()
);
}
}
public function testReset()
{
$client = $this->getClient();
$user = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:User')
->findOneByEmail('bobby@wallabag.org');
$crawler = $client->request('GET', '/forgot-password/'.$user->getConfirmationToken());
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertCount(2, $crawler->filter('input[type=password]'));
$this->assertCount(1, $form = $crawler->filter('button[type=submit]'));
$this->assertCount(1, $form);
$data = array(
'change_passwd[new_password][first]' => 'mypassword',
'change_passwd[new_password][second]' => 'mypassword',
);
$client->submit($form->form(), $data);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertContains('login', $client->getResponse()->headers->get('location'));
}
public function testResetBadToken()
{
$client = $this->getClient();
$client->request('GET', '/forgot-password/UIZOAU29UE902IEPZO');
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
public function testCheckEmailWithoutEmail()
{
$client = $this->getClient();
$client->request('GET', '/forgot-password/check-email');
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertContains('forgot-password', $client->getResponse()->headers->get('location'));
}
}

View File

@ -0,0 +1,92 @@
<?php
namespace Wallabag\CoreBundle\Tests\EventListener;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\UserBundle\Entity\User;
class RegistrationConfirmedListenerTest extends KernelTestCase
{
private $em;
private $listener;
private $dispatcher;
private $request;
private $response;
protected function setUp()
{
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->listener = new RegistrationConfirmedListener(
$this->em,
'baggy',
20,
50,
'fr'
);
$this->dispatcher = new EventDispatcher();
$this->dispatcher->addSubscriber($this->listener);
$this->request = Request::create('/');
$this->response = Response::create();
}
public function testWithInvalidUser()
{
$user = new User();
$user->setEnabled(false);
$event = new FilterUserResponseEvent(
$user,
$this->request,
$this->response
);
$this->em->expects($this->never())->method('persist');
$this->em->expects($this->never())->method('flush');
$this->dispatcher->dispatch(
FOSUserEvents::REGISTRATION_CONFIRMED,
$event
);
}
public function testWithValidUser()
{
$user = new User();
$user->setEnabled(true);
$event = new FilterUserResponseEvent(
$user,
$this->request,
$this->response
);
$config = new Config($user);
$config->setTheme('baggy');
$config->setItemsPerPage(20);
$config->setRssLimit(50);
$config->setLanguage('fr');
$this->em->expects($this->once())
->method('persist')
->will($this->returnValue($config));
$this->em->expects($this->once())
->method('flush');
$this->dispatcher->dispatch(
FOSUserEvents::REGISTRATION_CONFIRMED,
$event
);
}
}

View File

@ -4,7 +4,7 @@ namespace Wallabag\CoreBundle\Tests\Helper;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\UserBundle\Entity\User;
use Wallabag\CoreBundle\Helper\ContentProxy;
class ContentProxyTest extends KernelTestCase

View File

@ -6,7 +6,7 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\UserBundle\Entity\User;
class UsernameRssTokenConverterTest extends KernelTestCase
{
@ -96,7 +96,7 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$meta->expects($this->once())
->method('getName')
->will($this->returnValue('Wallabag\CoreBundle\Entity\User'));
->will($this->returnValue('Wallabag\UserBundle\Entity\User'));
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
@ -104,7 +104,7 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$em->expects($this->once())
->method('getClassMetadata')
->with('WallabagCoreBundle:User')
->with('WallabagUserBundle:User')
->will($this->returnValue($meta));
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
@ -117,10 +117,10 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$registry->expects($this->once())
->method('getManagerForClass')
->with('WallabagCoreBundle:User')
->with('WallabagUserBundle:User')
->will($this->returnValue($em));
$params = new ParamConverter(array('class' => 'WallabagCoreBundle:User'));
$params = new ParamConverter(array('class' => 'WallabagUserBundle:User'));
$converter = new UsernameRssTokenConverter($registry);
$this->assertTrue($converter->supports($params));
@ -144,7 +144,7 @@ class UsernameRssTokenConverterTest extends KernelTestCase
*/
public function testApplyUserNotFound()
{
$repo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\UserRepository')
$repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
->disableOriginalConstructor()
->getMock();
@ -159,7 +159,7 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$em->expects($this->once())
->method('getRepository')
->with('WallabagCoreBundle:User')
->with('WallabagUserBundle:User')
->will($this->returnValue($repo));
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
@ -168,10 +168,10 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$registry->expects($this->once())
->method('getManagerForClass')
->with('WallabagCoreBundle:User')
->with('WallabagUserBundle:User')
->will($this->returnValue($em));
$params = new ParamConverter(array('class' => 'WallabagCoreBundle:User'));
$params = new ParamConverter(array('class' => 'WallabagUserBundle:User'));
$converter = new UsernameRssTokenConverter($registry);
$request = new Request(array(), array(), array('username' => 'test', 'token' => 'test'));
@ -182,7 +182,7 @@ class UsernameRssTokenConverterTest extends KernelTestCase
{
$user = new User();
$repo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\UserRepository')
$repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
->disableOriginalConstructor()
->getMock();
@ -197,7 +197,7 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$em->expects($this->once())
->method('getRepository')
->with('WallabagCoreBundle:User')
->with('WallabagUserBundle:User')
->will($this->returnValue($repo));
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
@ -206,10 +206,10 @@ class UsernameRssTokenConverterTest extends KernelTestCase
$registry->expects($this->once())
->method('getManagerForClass')
->with('WallabagCoreBundle:User')
->with('WallabagUserBundle:User')
->will($this->returnValue($em));
$params = new ParamConverter(array('class' => 'WallabagCoreBundle:User', 'name' => 'user'));
$params = new ParamConverter(array('class' => 'WallabagUserBundle:User', 'name' => 'user'));
$converter = new UsernameRssTokenConverter($registry);
$request = new Request(array(), array(), array('username' => 'test', 'token' => 'test'));