Skip to content

Commit ecd7cd3

Browse files
fix: some test cases
1 parent 80972f0 commit ecd7cd3

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

app/libs/Auth/AuthService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,12 @@ public function validateCredentials(string $username, string $password): User
187187
* @var User|null $user
188188
*/
189189
$user = $this->user_repository->getByEmailOrName($username);
190-
$valid = Auth::getProvider()->validateCredentials($user, ['username' => $username, 'password' => $password]);
191-
if (!$valid) {
190+
if (!$user) {
191+
throw new AuthenticationException();
192+
}
193+
194+
$isUserValid = Auth::getProvider()->validateCredentials($user, ['username' => $username, 'password' => $password]);
195+
if (!$isUserValid) {
192196
throw new AuthenticationException();
193197
}
194198

tests/unit/AuthServiceValidateCredentialsTest.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use App\libs\OAuth2\Repositories\IOAuth2OTPRepository;
1616
use Auth\AuthService;
17+
use Auth\CustomAuthProvider;
1718
use Auth\Exceptions\AuthenticationException;
1819
use Auth\Repositories\IUserRepository;
1920
use Mockery;
@@ -89,19 +90,19 @@ public function testValidCredentials_returnsUser_withoutEstablishingSession(): v
8990
$username = 'jane.doe';
9091
$password = 'Str0ng!Pass';
9192

93+
$resolved_user = Mockery::mock('Auth\User');
9294
$this->mock_user_repository
9395
->expects($this->once())
9496
->method('getByEmailOrName')
9597
->with($username)
96-
->willReturn(null);
98+
->willReturn($resolved_user);
9799

98-
$resolved_user = Mockery::mock('Auth\User');
99100
$resolved_user->shouldReceive('canLogin')->andReturn(true);
100101

101-
$provider_mock = Mockery::mock('Illuminate\Contracts\Auth\UserProvider');
102-
$provider_mock->shouldReceive('retrieveByCredentials')
102+
$provider_mock = Mockery::mock(CustomAuthProvider::class);
103+
$provider_mock->shouldReceive('validateCredentials')
103104
->once()
104-
->with(['username' => $username, 'password' => $password])
105+
->with($resolved_user, ['username' => $username, 'password' => $password])
105106
->andReturn($resolved_user);
106107

107108
$this->auth_mock->shouldReceive('getProvider')->once()->andReturn($provider_mock);
@@ -122,16 +123,17 @@ public function testInvalidCredentials_throwsAuthenticationException(): void
122123
$username = 'jane.doe';
123124
$password = 'wrong';
124125

126+
$resolved_user = Mockery::mock('Auth\User');
125127
$this->mock_user_repository
126128
->expects($this->once())
127129
->method('getByEmailOrName')
128130
->with($username)
129-
->willReturn(null);
131+
->willReturn($resolved_user);
130132

131-
$provider_mock = Mockery::mock('Illuminate\Contracts\Auth\UserProvider');
132-
$provider_mock->shouldReceive('retrieveByCredentials')
133+
$provider_mock = Mockery::mock(CustomAuthProvider::class);
134+
$provider_mock->shouldReceive('validateCredentials')
133135
->once()
134-
->with(['username' => $username, 'password' => $password])
136+
->with($resolved_user, ['username' => $username, 'password' => $password])
135137
->andReturn(null);
136138

137139
$this->auth_mock->shouldReceive('getProvider')->once()->andReturn($provider_mock);
@@ -154,21 +156,20 @@ public function testProviderReturnsUserThatCannotLogin_throwsAuthenticationExcep
154156
$password = 'Str0ng!Pass';
155157

156158
// Pre-check: user not found in repository, so the locked-account short-circuit is not taken.
159+
$resolved_user = Mockery::mock('Auth\User');
157160
$this->mock_user_repository
158161
->expects($this->once())
159162
->method('getByEmailOrName')
160163
->with($username)
161-
->willReturn(null);
164+
->willReturn($resolved_user);
162165

163166
// Provider returns a valid User instance, but canLogin() is false.
164-
$non_loginable_user = Mockery::mock('Auth\User');
165-
$non_loginable_user->shouldReceive('canLogin')->andReturn(false);
166167

167-
$provider_mock = Mockery::mock('Illuminate\Contracts\Auth\UserProvider');
168-
$provider_mock->shouldReceive('retrieveByCredentials')
168+
$provider_mock = Mockery::mock(CustomAuthProvider::class);
169+
$provider_mock->shouldReceive('validateCredentials')
169170
->once()
170-
->with(['username' => $username, 'password' => $password])
171-
->andReturn($non_loginable_user);
171+
->with($resolved_user, ['username' => $username, 'password' => $password])
172+
->andReturn(false);
172173

173174
$this->auth_mock->shouldReceive('getProvider')->once()->andReturn($provider_mock);
174175
$this->auth_mock->shouldNotReceive('login');
@@ -186,6 +187,7 @@ public function testProviderReturnsUserThatCannotLogin_throwsAuthenticationExcep
186187
public function testLockedAccount_throwsAuthenticationException_withLockedMessage(): void
187188
{
188189
$username = 'locked.user';
190+
$password = 'Str0ng!Pass';
189191

190192
$locked_user = Mockery::mock('Auth\User');
191193
$locked_user->shouldReceive('isActive')->andReturn(false);
@@ -197,7 +199,13 @@ public function testLockedAccount_throwsAuthenticationException_withLockedMessag
197199
->willReturn($locked_user);
198200

199201
// Provider must NOT be consulted when the user is locked.
200-
$this->auth_mock->shouldNotReceive('getProvider');
202+
$provider_mock = Mockery::mock(CustomAuthProvider::class);
203+
$provider_mock->shouldReceive('validateCredentials')
204+
->once()
205+
->with($locked_user, ['username' => $username, 'password' => $password])
206+
->andReturn(false);
207+
208+
$this->auth_mock->shouldReceive('getProvider')->once()->andReturn($provider_mock);
201209
$this->auth_mock->shouldNotReceive('login');
202210
$this->auth_mock->shouldNotReceive('attempt');
203211

0 commit comments

Comments
 (0)