From 614c556c969c167a5271d8cdb386b1aad4d00690 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 3 Jul 2014 08:30:12 +0200 Subject: [PATCH] Some more fixes and tests. --- app/config/testing/auth.php | 8 + .../Storage/User/EloquentUserRepository.php | 2 +- app/models/User.php | 4 +- app/tests/controllers/UserControllerTest.php | 147 +++++++++++++++--- 4 files changed, 138 insertions(+), 23 deletions(-) create mode 100644 app/config/testing/auth.php diff --git a/app/config/testing/auth.php b/app/config/testing/auth.php new file mode 100644 index 0000000000..427141b2cd --- /dev/null +++ b/app/config/testing/auth.php @@ -0,0 +1,8 @@ + true, + 'verify_reset' => true, + 'allow_register' => true + +]; diff --git a/app/lib/Firefly/Storage/User/EloquentUserRepository.php b/app/lib/Firefly/Storage/User/EloquentUserRepository.php index b5d3b4ed0a..abd43ab2ac 100644 --- a/app/lib/Firefly/Storage/User/EloquentUserRepository.php +++ b/app/lib/Firefly/Storage/User/EloquentUserRepository.php @@ -12,7 +12,7 @@ class EloquentUserRepository implements UserRepositoryInterface public function register($array) { $user = new \User; - $user->email = $array['emai']; + $user->email = isset($array['email']) ? $array['email'] : null; $user->migrated = 0; $user->verification = \Str::random(32); $user->password = \Hash::make(\Str::random(12)); diff --git a/app/models/User.php b/app/models/User.php index aab02454ea..1dd21b1fa9 100644 --- a/app/models/User.php +++ b/app/models/User.php @@ -20,8 +20,8 @@ class User extends Elegant implements UserInterface, RemindableInterface public static $rules = [ - 'email' => 'email|unique:users,email', - 'migrated' => 'numeric|between:0,1', + 'email' => 'required|email|unique:users,email', + 'migrated' => 'required|numeric|between:0,1', 'password' => 'between:60,60', 'verification' => 'between:32,32', ]; diff --git a/app/tests/controllers/UserControllerTest.php b/app/tests/controllers/UserControllerTest.php index 3b3023eae6..da9b128e2a 100644 --- a/app/tests/controllers/UserControllerTest.php +++ b/app/tests/controllers/UserControllerTest.php @@ -6,8 +6,118 @@ class UserControllerTest extends TestCase public function setUp() { parent::setUp(); + } + + public function testLogin() + { + // mock: + View::shouldReceive('make')->with('user.login'); + + // call + $this->call('GET', '/login'); + + // test + } + + public function testPostLogin() + { + // data: + $data = ['email' => 'bla@bla.nl', 'password' => 'xxxx', 'remember_me' => '1']; + + // mock + Auth::shouldReceive('attempt')->once()->andReturn(true); + + // test + $this->call('POST', '/login', $data); + $this->assertSessionHas('success'); + $this->assertRedirectedToRoute('index'); + } + + public function testPostFalseLogin() + { + // data + $data = ['email' => 'bla@bla.nl', 'password' => 'xxxx', 'remember_me' => '1']; + + // mock + Auth::shouldReceive('attempt')->once()->andReturn(false); + View::shouldReceive('make')->with('user.login')->once(); + + // test + $this->call('POST', '/login', $data); + $this->assertSessionHas('error'); + } + + public function testRegister() + { + // no mock for config! :( + Config::set('auth.allow_register', true); + // test + $this->call('GET', '/register'); + $this->assertResponseOk(); + } + + /** + * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException + */ + public function testRegisterNotAllowed() + { + // no mock for config! :( + Config::set('auth.allow_register', false); + // test + $this->call('GET', '/register'); + $this->assertResponseStatus(404); + } + + /** + * Register and verify: + */ + public function testPostRegisterAllowed() + { + // no mock for config! :( + Config::set('auth.verify_mail', true); + Config::set('auth.allow_register', true); + + // mock repository: + $repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface'); + $email = $this->mock('Firefly\Helper\Email\EmailHelper'); + $repository->shouldReceive('register')->once()->andReturn(new User); + $email->shouldReceive('sendVerificationMail')->once()->andReturn(true); + // data: + $data = [ + 'email' => 'bla@bla' + ]; + + // test + $crawler = $this->client->request('POST', '/register', $data); + $this->assertTrue($this->client->getResponse()->isOk()); + $this->assertCount(1, $crawler->filter('h1:contains("Verification pending")')); + + } + + /** + * Register and NO verify: + */ + public function testPostRegisterNoVerifyAllowed() + { + // no mock for config! :( + Config::set('auth.verify_mail', false); + Config::set('auth.allow_register', true); + + // mock repository: + $repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface'); + $email = $this->mock('Firefly\Helper\Email\EmailHelper'); + $repository->shouldReceive('register')->once()->andReturn(new User); + $email->shouldReceive('sendPasswordMail')->once()->andReturn(true); + // data: + $data = [ + 'email' => 'bla@bla' + ]; + + // test + $crawler = $this->client->request('POST', '/register', $data); + $this->assertTrue($this->client->getResponse()->isOk()); + $this->assertCount(1, $crawler->filter('h1:contains("Registered!")')); - $this->mock = $this->mock('Firefly\Storage\User\UserRepositoryInterface'); } public function mock($class) @@ -19,28 +129,25 @@ class UserControllerTest extends TestCase return $mock; } - public function testLogin() + /** + * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException + */ + public function testPostRegisterNotAllowed() { - View::shouldReceive('make')->with('user.login'); - $this->call('GET', '/login'); - } + // no mock for config! :( + Config::set('auth.verify_mail', true); + Config::set('auth.verify_reset', true); + Config::set('auth.allow_register', false); - public function testPostLogin() - { - $data = ['email' => 'bla@bla.nl', 'password' => 'xxxx','remember_me' => '1']; - Auth::shouldReceive('attempt')->once()->andReturn(true); - $this->call('POST', '/login', $data); - $this->assertSessionHas('success'); - $this->assertRedirectedToRoute('index'); - } + // mock repository: + $data = [ + 'email' => 'bla@bla' + ]; + + // test + $this->call('POST', '/register', $data); + $this->assertResponseStatus(404); - public function testPostFalseLogin() - { - $data = ['email' => 'bla@bla.nl', 'password' => 'xxxx','remember_me' => '1']; - Auth::shouldReceive('attempt')->once()->andReturn(false); - View::shouldReceive('make')->with('user.login')->once(); - $this->call('POST', '/login', $data); - $this->assertSessionHas('error'); } public function tearDown()