Skip to content

Firefly III: a personal finances manager

More tests; profile controller.

James Colef18a5fd0a351
diff --git a/app/lib/Firefly/Storage/User/EloquentUserRepository.php b/app/lib/Firefly/Storage/User/EloquentUserRepository.php
index 5af4f54220..a677ae1f22 100644
--- a/app/lib/Firefly/Storage/User/EloquentUserRepository.php
+++ b/app/lib/Firefly/Storage/User/EloquentUserRepository.php
@@ -84,7 +84,6 @@ class EloquentUserRepository implements UserRepositoryInterface
*/
public function updatePassword(\User $user, $password)
{
- $password = \Hash::make($password);
/** @noinspection PhpUndefinedFieldInspection */
$user->password = $password;
/** @noinspection PhpUndefinedMethodInspection */
diff --git a/app/models/User.php b/app/models/User.php
index 49d3012a5a..020484d3c4 100644
--- a/app/models/User.php
+++ b/app/models/User.php
@@ -10,19 +10,19 @@ use LaravelBook\Ardent\Ardent;
/**
* User
*
- * @property integer $id
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * @property string $email
- * @property string $password
- * @property string $reset
- * @property string $remember_token
- * @property boolean $migrated
- * @property-read \Illuminate\Database\Eloquent\Collection|\Account[] $accounts
- * @property-read \Illuminate\Database\Eloquent\Collection|\Preference[] $preferences
- * @property-read \Illuminate\Database\Eloquent\Collection|\Component[] $components
- * @property-read \Illuminate\Database\Eloquent\Collection|\Budget[] $budgets
- * @property-read \Illuminate\Database\Eloquent\Collection|\Category[] $categories
+ * @property integer $id
+ * @property \Carbon\Carbon $created_at
+ * @property \Carbon\Carbon $updated_at
+ * @property string $email
+ * @property string $password
+ * @property string $reset
+ * @property string $remember_token
+ * @property boolean $migrated
+ * @property-read \Illuminate\Database\Eloquent\Collection|\Account[] $accounts
+ * @property-read \Illuminate\Database\Eloquent\Collection|\Preference[] $preferences
+ * @property-read \Illuminate\Database\Eloquent\Collection|\Component[] $components
+ * @property-read \Illuminate\Database\Eloquent\Collection|\Budget[] $budgets
+ * @property-read \Illuminate\Database\Eloquent\Collection|\Category[] $categories
* @method static \Illuminate\Database\Query\Builder|\User whereId($value)
* @method static \Illuminate\Database\Query\Builder|\User whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\User whereUpdatedAt($value)
@@ -31,8 +31,8 @@ use LaravelBook\Ardent\Ardent;
* @method static \Illuminate\Database\Query\Builder|\User whereReset($value)
* @method static \Illuminate\Database\Query\Builder|\User whereRememberToken($value)
* @method static \Illuminate\Database\Query\Builder|\User whereMigrated($value)
- * @property-read \Illuminate\Database\Eloquent\Collection|\TransactionJournal[] $transactionjournals
- * @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks
+ * @property-read \Illuminate\Database\Eloquent\Collection|\TransactionJournal[] $transactionjournals
+ * @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks
* @property-read \Illuminate\Database\Eloquent\Collection|\RecurringTransaction[] $recurringtransactions
*/
class User extends Ardent implements UserInterface, RemindableInterface
@@ -48,14 +48,6 @@ class User extends Ardent implements UserInterface, RemindableInterface
'password' => 'required|between:60,60',
'reset' => 'between:32,32',
];
-
- public static $factory
- = [
- 'email' => 'email',
- 'password' => 'string|60',
- 'migrated' => '0'
-
- ];
/**
* The database table used by the model.
*
@@ -69,14 +61,34 @@ class User extends Ardent implements UserInterface, RemindableInterface
*/
protected $hidden = ['remember_token'];
+ public static function factory()
+ {
+ return [
+ 'email' => 'email',
+ 'password' => 'sander',
+ 'migrated' => '0'
+
+ ];
+ }
+
public function accounts()
{
return $this->hasMany('Account');
}
- public function recurringtransactions()
+ public function budgets()
{
- return $this->hasMany('RecurringTransaction');
+ return $this->hasMany('Budget');
+ }
+
+ public function categories()
+ {
+ return $this->hasMany('Category');
+ }
+
+ public function components()
+ {
+ return $this->hasMany('Component');
}
public function piggybanks()
@@ -89,19 +101,14 @@ class User extends Ardent implements UserInterface, RemindableInterface
return $this->hasMany('Preference');
}
- public function components()
- {
- return $this->hasMany('Component');
- }
-
- public function budgets()
+ public function recurringtransactions()
{
- return $this->hasMany('Budget');
+ return $this->hasMany('RecurringTransaction');
}
- public function categories()
+ public function setPasswordAttribute($value)
{
- return $this->hasMany('Category');
+ $this->attributes['password'] = Hash::make($value);
}
public function transactionjournals()
diff --git a/app/tests/controllers/ProfileControllerTest.php b/app/tests/controllers/ProfileControllerTest.php
new file mode 100644
index 0000000000..2ac2fa526b
--- /dev/null
+++ b/app/tests/controllers/ProfileControllerTest.php
@@ -0,0 +1,129 @@
+<?php
+use Mockery as m;
+use Zizaco\FactoryMuff\Facade\FactoryMuff as f;
+
+/**
+ * Class ProfileControllerTest
+ */
+class ProfileControllerTest extends TestCase
+{
+ protected $_user;
+
+ public function setUp()
+ {
+ parent::setUp();
+ Artisan::call('migrate');
+ Artisan::call('db:seed');
+ $this->_user = m::mock('User', 'Eloquent');
+
+ }
+
+ public function tearDown()
+ {
+ m::close();
+ }
+
+
+ public function testChangePassword()
+ {
+ // for binding
+ Auth::shouldReceive('user')->andReturn($this->_user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($this->_user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
+
+ $this->action('GET', 'ProfileController@changePassword');
+ $this->assertResponseOk();
+ }
+
+ public function testIndex()
+ {
+ // for binding
+ Auth::shouldReceive('user')->andReturn($this->_user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($this->_user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
+
+ $this->action('GET', 'ProfileController@index');
+ $this->assertResponseOk();
+ }
+
+ public function testPostChangePasswordDifferentNew()
+ {
+ $user = f::create('User');
+ // for binding
+ Auth::shouldReceive('user')->andReturn($user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn($user->email);
+ $this->_user->shouldReceive('getAttribute')->with('password')->andReturn($user->password);
+
+ $this->action(
+ 'POST', 'ProfileController@postChangePassword',
+ ['old' => 'sander', 'new1' => 'sander1', 'new2' => 'sander2']
+ );
+ $this->assertResponseOk();
+ }
+
+ public function testPostChangePasswordOK()
+ {
+ $user = f::create('User');
+ // for binding
+ Auth::shouldReceive('user')->andReturn($user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn($user->email);
+ $this->_user->shouldReceive('getAttribute')->with('password')->andReturn($user->password);
+
+ $this->action(
+ 'POST', 'ProfileController@postChangePassword',
+ ['old' => 'sander', 'new1' => 'sander2', 'new2' => 'sander2']
+ );
+ $this->assertResponseStatus(302);
+ }
+
+
+ public function testPostChangePasswordNoCurrent()
+ {
+ // for binding
+ Auth::shouldReceive('user')->andReturn($this->_user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($this->_user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn('some@email');
+ $this->_user->shouldReceive('getAttribute')->with('password')->andReturn('Blablabla');
+
+ $this->action('POST', 'ProfileController@postChangePassword');
+ $this->assertResponseOk();
+ }
+
+ public function testPostChangePasswordNoMatchNew()
+ {
+ $user = f::create('User');
+ // for binding
+ Auth::shouldReceive('user')->andReturn($user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn($user->email);
+ $this->_user->shouldReceive('getAttribute')->with('password')->andReturn($user->password);
+
+ $this->action(
+ 'POST', 'ProfileController@postChangePassword', ['old' => 'sander', 'new1' => 'sander', 'new2' => 'sander']
+ );
+ $this->assertResponseOk();
+ }
+
+ public function testPostChangePasswordSame()
+ {
+ $user = f::create('User');
+ // for binding
+ Auth::shouldReceive('user')->andReturn($user);
+ Auth::shouldReceive('check')->andReturn(true);
+ $this->_user->shouldReceive('getAttribute')->with('id')->andReturn($user->id);
+ $this->_user->shouldReceive('getAttribute')->with('email')->andReturn($user->email);
+ $this->_user->shouldReceive('getAttribute')->with('password')->andReturn($user->password);
+
+ $this->action('POST', 'ProfileController@postChangePassword', ['old' => 'sander']);
+ $this->assertResponseOk();
+ }
+
+}
\ No newline at end of file