Skip to content

Firefly III: a personal finances manager

Also covered ProfileController.

James Cole924ec29eafc2
diff --git a/app/controllers/ProfileController.php b/app/controllers/ProfileController.php
index 4d5fea493b..7fcb539843 100644
--- a/app/controllers/ProfileController.php
+++ b/app/controllers/ProfileController.php
@@ -1,8 +1,14 @@
<?php
+use Firefly\Storage\User\UserRepositoryInterface as URI;
+
class ProfileController extends BaseController
{
+ public function __construct(URI $user) {
+ $this->user = $user;
+ }
+
public function index()
{
return View::make('profile.index');
@@ -37,11 +43,8 @@ class ProfileController extends BaseController
}
// update the user with the new password.
- $password = Hash::make(Input::get('new1'));
- /** @noinspection PhpUndefinedFieldInspection */
- Auth::user()->password = $password;
- /** @noinspection PhpUndefinedMethodInspection */
- Auth::user()->save();
+ $this->user->updatePassword(Auth::user(),Input::get('new1'));
+
Session::flash('success', 'Password changed!');
return Redirect::route('profile');
}
diff --git a/app/lib/Firefly/Storage/User/EloquentUserRepository.php b/app/lib/Firefly/Storage/User/EloquentUserRepository.php
index abd43ab2ac..855f5415d5 100644
--- a/app/lib/Firefly/Storage/User/EloquentUserRepository.php
+++ b/app/lib/Firefly/Storage/User/EloquentUserRepository.php
@@ -51,4 +51,14 @@ class EloquentUserRepository implements UserRepositoryInterface
return \User::where('email', $email)->first();
}
+ public function updatePassword(\User $user, $password)
+ {
+ $password = \Hash::make($password);
+ /** @noinspection PhpUndefinedFieldInspection */
+ $user->password = $password;
+ /** @noinspection PhpUndefinedMethodInspection */
+ $user->save();
+ return true;
+ }
+
}
\ No newline at end of file
diff --git a/app/lib/Firefly/Storage/User/UserRepositoryInterface.php b/app/lib/Firefly/Storage/User/UserRepositoryInterface.php
index 4490249d99..2aef50b4b3 100644
--- a/app/lib/Firefly/Storage/User/UserRepositoryInterface.php
+++ b/app/lib/Firefly/Storage/User/UserRepositoryInterface.php
@@ -15,5 +15,7 @@ interface UserRepositoryInterface
public function findByEmail($email);
+ public function updatePassword(\User $user,$password);
+
}
\ No newline at end of file
diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php
index d367fe53d4..58a560c9cd 100644
--- a/app/tests/TestCase.php
+++ b/app/tests/TestCase.php
@@ -1,19 +1,29 @@
<?php
-class TestCase extends Illuminate\Foundation\Testing\TestCase {
+class TestCase extends Illuminate\Foundation\Testing\TestCase
+{
- /**
- * Creates the application.
- *
- * @return \Symfony\Component\HttpKernel\HttpKernelInterface
- */
- public function createApplication()
- {
- $unitTesting = true;
+ /**
+ * Creates the application.
+ *
+ * @return \Symfony\Component\HttpKernel\HttpKernelInterface
+ */
+ public function createApplication()
+ {
+ $unitTesting = true;
- $testEnvironment = 'testing';
+ $testEnvironment = 'testing';
- return require __DIR__.'/../../bootstrap/start.php';
- }
+ return require __DIR__ . '/../../bootstrap/start.php';
+ }
+
+ public function mock($class)
+ {
+ $mock = Mockery::mock($class);
+
+ $this->app->instance($class, $mock);
+
+ return $mock;
+ }
}
diff --git a/app/tests/controllers/ProfileControllerTest.php b/app/tests/controllers/ProfileControllerTest.php
new file mode 100644
index 0000000000..e2ce08f22d
--- /dev/null
+++ b/app/tests/controllers/ProfileControllerTest.php
@@ -0,0 +1,141 @@
+<?php
+
+class ProfileControllerTest extends TestCase
+{
+
+ public function testIndex()
+ {
+ // mock:
+ View::shouldReceive('make')->with('profile.index');
+
+ // call
+ $this->call('GET', '/profile');
+
+ // test
+ $this->assertResponseOk();
+ }
+
+ public function testChangePassword()
+ {
+ // mock:
+ View::shouldReceive('make')->with('profile.change-password');
+
+ // call
+ $this->call('GET', '/profile/change-password');
+
+ // test
+ $this->assertResponseOk();
+ }
+
+ public function testOldNoMatch()
+ {
+ Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->andReturn(new User);
+ Hash::shouldReceive('check')->andReturn(false);
+
+ $data = [
+ 'old' => 'lala',
+ 'new1' => 'a',
+ 'new2' => 'a',
+ ];
+
+
+ // call
+ $this->call('POST', '/profile/change-password', $data);
+
+ // test
+ $this->assertResponseOk();
+ $this->assertSessionHas('error', 'Invalid current password!');
+ }
+
+ public function testNewEmpty()
+ {
+ Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->andReturn(new User);
+ Hash::shouldReceive('check')->andReturn(true);
+
+ $data = [
+ 'old' => 'lala',
+ 'new1' => '',
+ 'new2' => '',
+ ];
+
+
+ // call
+ $this->call('POST', '/profile/change-password', $data);
+
+ // test
+ $this->assertResponseOk();
+ $this->assertSessionHas('error', 'Do fill in a password!');
+ }
+
+ public function testOldSame()
+ {
+ Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->andReturn(new User);
+ Hash::shouldReceive('check')->andReturn(true);
+ Hash::shouldReceive('make')->andReturn('blala');
+
+ $data = [
+ 'old' => 'a',
+ 'new1' => 'a',
+ 'new2' => 'a',
+ ];
+
+
+ // call
+ $this->call('POST', '/profile/change-password', $data);
+
+ // test
+ $this->assertResponseOk();
+ $this->assertSessionHas('error', 'The idea is to change your password.');
+ }
+
+ public function testNewNoMatch()
+ {
+ Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->andReturn(new User);
+ Hash::shouldReceive('check')->andReturn(true);
+ Hash::shouldReceive('make')->andReturn('blala');
+
+ $data = [
+ 'old' => 'b',
+ 'new1' => 'c',
+ 'new2' => 'd',
+ ];
+
+
+ // call
+ $this->call('POST', '/profile/change-password', $data);
+
+ // test
+ $this->assertResponseOk();
+ $this->assertSessionHas('error', 'New passwords do not match!');
+ }
+
+ public function testPostChangePassword()
+ {
+ Auth::shouldReceive('check')->andReturn(true);
+ Auth::shouldReceive('user')->andReturn(new User);
+ Hash::shouldReceive('check')->andReturn(true);
+ Hash::shouldReceive('make')->andReturn('blala');
+
+ $repository = $this->mock('Firefly\Storage\User\UserRepositoryInterface');
+ $repository->shouldReceive('updatePassword')->once()->andReturn(true);
+
+ $data = [
+ 'old' => 'b',
+ 'new1' => 'c',
+ 'new2' => 'c',
+ ];
+
+
+ // call
+ $this->call('POST', '/profile/change-password', $data);
+
+ // test
+ $this->assertRedirectedToRoute('profile');
+ $this->assertSessionHas('success', 'Password changed!');
+ }
+
+}
\ No newline at end of file
diff --git a/app/tests/controllers/UserControllerTest.php b/app/tests/controllers/UserControllerTest.php
index b07585323c..a709e8709b 100644
--- a/app/tests/controllers/UserControllerTest.php
+++ b/app/tests/controllers/UserControllerTest.php
@@ -17,6 +17,7 @@ class UserControllerTest extends TestCase
$this->call('GET', '/login');
// test
+ $this->assertResponseOk();
}
public function testPostLogin()
@@ -94,14 +95,7 @@ class UserControllerTest extends TestCase
}
- public function mock($class)
- {
- $mock = Mockery::mock($class);
-
- $this->app->instance($class, $mock);
- return $mock;
- }
/**
* Register and verify FAILED: