Skip to content

Commit e24c52a

Browse files
committed
Improvements
- make last_name field required when creating user and update its tests - added backend authentication event hooks (backend.auth.success and backend.auth.logout )
1 parent 044d8f4 commit e24c52a

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

src/Darryldecode/Backend/Components/Auth/Controllers/AuthController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function postLogin(Request $request, Throttle $throttle, User $user, Disp
5151
// if authentication is good
5252
if( $result->isSuccessful() )
5353
{
54-
$dispatcher->fire('auth.loginSuccess', array($result->getData()));
54+
$dispatcher->fire('backend.auth.success', array($result->getData()));
5555

5656
if( $request->get('ru') != '' )
5757
{
@@ -76,7 +76,7 @@ public function getLogout(Dispatcher $dispatcher)
7676
{
7777
Auth::logout();
7878

79-
$dispatcher->fire('auth.logout');
79+
$dispatcher->fire('backend.auth.logout');
8080

8181
return redirect(Helpers::getLoginRoute());
8282
}

src/Darryldecode/Backend/Components/User/Commands/CreateUserCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public function handle(User $user, Factory $validator, Dispatcher $dispatcher, G
8686
// validate data
8787
$validationResult = $validator->make(array(
8888
'first_name' => $this->firstName,
89+
'last_name' => $this->lastName,
8990
'email' => $this->email,
9091
'password' => $this->password,
9192
), $this->user->getValidationRules());

src/Darryldecode/Backend/Components/User/Models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
5050
*/
5151
public static $rules = array(
5252
'first_name' => 'required',
53+
'last_name' => 'required',
5354
'email' => 'required|email|unique:users',
5455
'password' => 'required|min:8',
5556
);

tests/User/functional/commands/CreateUserCommandTest.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,9 @@ public function testShouldDenyIfUserHasNoPermission()
7272
$this->assertEquals('Not enough permission.', $result->getMessage());
7373
}
7474

75-
public function testRequiredFieldsPermissions()
75+
public function testShouldRequireFirstNameField()
7676
{
77-
// create user and logged in
78-
$user = User::create(array(
79-
'first_name' => 'darryl',
80-
'email' => '[email protected]',
81-
'password' => 'pass$darryl',
82-
'permissions' => array(
83-
'superuser' => 1
84-
)
85-
));
86-
87-
$this->application['auth']->loginUsingId($user->id);
77+
$this->createUserAndLoggedIn(array('superuser' => 1));
8878

8979
// dummy request, required first name
9080
$request = Request::create('','GET',array(
@@ -105,6 +95,11 @@ public function testRequiredFieldsPermissions()
10595
$this->assertFalse($result->isSuccessful(), 'Transaction should not be successful.');
10696
$this->assertEquals(400, $result->getStatusCode(), 'Status code should be 400');
10797
$this->assertEquals('The first name field is required.', $result->getMessage());
98+
}
99+
100+
public function testShouldRequireLastName()
101+
{
102+
$this->createUserAndLoggedIn(array('superuser' => 1));
108103

109104
// dummy request, required email
110105
$request = Request::create('','GET',array(
@@ -124,13 +119,18 @@ public function testRequiredFieldsPermissions()
124119

125120
$this->assertFalse($result->isSuccessful(), 'Transaction should not be successful.');
126121
$this->assertEquals(400, $result->getStatusCode(), 'Status code should be 400');
127-
$this->assertEquals('The email field is required.', $result->getMessage());
122+
$this->assertEquals('The last name field is required.', $result->getMessage());
123+
}
124+
125+
public function testShouldRequireEmail()
126+
{
127+
$this->createUserAndLoggedIn(array('superuser' => 1));
128128

129-
// dummy request, required valid email
129+
// dummy request, required email
130130
$request = Request::create('','GET',array(
131131
'firstName' => 'John',
132-
'lastName' => '',
133-
'email' => 'some invalid email',
132+
'lastName' => 'Doe',
133+
'email' => '',
134134
'password' => '',
135135
'permissions' => '',
136136
'groups' => array(),
@@ -144,13 +144,18 @@ public function testRequiredFieldsPermissions()
144144

145145
$this->assertFalse($result->isSuccessful(), 'Transaction should not be successful.');
146146
$this->assertEquals(400, $result->getStatusCode(), 'Status code should be 400');
147-
$this->assertEquals('The email must be a valid email address.', $result->getMessage());
147+
$this->assertEquals('The email field is required.', $result->getMessage());
148+
}
149+
150+
public function testShouldRequireEmailToBeUnique()
151+
{
152+
$this->createUserAndLoggedIn(array('superuser' => 1));
148153

149-
// dummy request, required unique email
154+
// dummy request, required email
150155
$request = Request::create('','GET',array(
151156
'firstName' => 'John',
152-
'lastName' => '',
153-
'email' => '[email protected]', // this email is already used aboved
157+
'lastName' => 'Doe',
158+
'email' => '[email protected]', // existing email
154159
'password' => '',
155160
'permissions' => '',
156161
'groups' => array(),
@@ -280,4 +285,17 @@ public function testShouldAssociateTheUserToTheGroupIfThereIsAnyProvided()
280285
$this->assertTrue($createdUser->inGroup($artist), 'User should be in artist group');
281286
$this->assertFalse($createdUser->inGroup($moderator), 'User should not be in moderator group');
282287
}
288+
289+
protected function createUserAndLoggedIn($permissions)
290+
{
291+
// create user and logged in
292+
$user = User::create(array(
293+
'first_name' => 'darryl',
294+
'email' => '[email protected]',
295+
'password' => 'pass$darryl',
296+
'permissions' => $permissions
297+
));
298+
299+
$this->application['auth']->loginUsingId($user->id);
300+
}
283301
}

0 commit comments

Comments
 (0)