Skip to content

Firefly III: a personal finances manager

Some more fixes and tests.

James Cole614c556c969c
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 @@
+<?php
+
+return [
+ 'verify_mail' => 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,29 +6,28 @@ class UserControllerTest extends TestCase
public function setUp()
{
parent::setUp();
-
- $this->mock = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
- }
-
- public function mock($class)
- {
- $mock = Mockery::mock($class);
-
- $this->app->instance($class, $mock);
-
- return $mock;
}
public function testLogin()
{
+ // mock:
View::shouldReceive('make')->with('user.login');
+
+ // call
$this->call('GET', '/login');
+
+ // test
}
public function testPostLogin()
{
- $data = ['email' => '[email protected]', 'password' => 'xxxx','remember_me' => '1'];
+ // data:
+ $data = ['email' => '[email protected]', 'password' => 'xxxx', 'remember_me' => '1'];
+
+ // mock
Auth::shouldReceive('attempt')->once()->andReturn(true);
+
+ // test
$this->call('POST', '/login', $data);
$this->assertSessionHas('success');
$this->assertRedirectedToRoute('index');
@@ -36,13 +35,121 @@ class UserControllerTest extends TestCase
public function testPostFalseLogin()
{
- $data = ['email' => '[email protected]', 'password' => 'xxxx','remember_me' => '1'];
+ // data
+ $data = ['email' => '[email protected]', '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!")'));
+
+ }
+
+ public function mock($class)
+ {
+ $mock = Mockery::mock($class);
+
+ $this->app->instance($class, $mock);
+
+ return $mock;
+ }
+
+ /**
+ * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
+ */
+ public function testPostRegisterNotAllowed()
+ {
+ // no mock for config! :(
+ Config::set('auth.verify_mail', true);
+ Config::set('auth.verify_reset', true);
+ Config::set('auth.allow_register', false);
+
+ // mock repository:
+ $data = [
+ 'email' => 'bla@bla'
+ ];
+
+ // test
+ $this->call('POST', '/register', $data);
+ $this->assertResponseStatus(404);
+
+ }
+
public function tearDown()
{
Mockery::close();