|
17 | 17 | use CodeIgniter\Shield\Authentication\Authentication; |
18 | 18 | use CodeIgniter\Shield\Authentication\Authenticators\AccessTokens; |
19 | 19 | use CodeIgniter\Shield\Config\Auth; |
| 20 | +use CodeIgniter\Shield\Config\AuthToken; |
20 | 21 | use CodeIgniter\Shield\Entities\AccessToken; |
21 | 22 | use CodeIgniter\Shield\Entities\User; |
22 | 23 | use CodeIgniter\Shield\Models\UserIdentityModel; |
@@ -222,6 +223,38 @@ public function testAttemptSuccess(): void |
222 | 223 | ]); |
223 | 224 | } |
224 | 225 |
|
| 226 | + public function testAttemptSuccessLog(): void |
| 227 | + { |
| 228 | + // Change $recordLoginAttempt in Config. |
| 229 | + /** @var AuthToken $config */ |
| 230 | + $config = config('AuthToken'); |
| 231 | + $config->recordLoginAttempt = Auth::RECORD_LOGIN_ATTEMPT_ALL; |
| 232 | + |
| 233 | + /** @var User $user */ |
| 234 | + $user = fake(UserModel::class); |
| 235 | + $token = $user->generateAccessToken('foo'); |
| 236 | + $this->setRequestHeader($token->raw_token); |
| 237 | + |
| 238 | + $result = $this->auth->attempt([ |
| 239 | + 'token' => $token->raw_token, |
| 240 | + ]); |
| 241 | + |
| 242 | + $this->assertInstanceOf(Result::class, $result); |
| 243 | + $this->assertTrue($result->isOK()); |
| 244 | + |
| 245 | + $foundUser = $result->extraInfo(); |
| 246 | + $this->assertInstanceOf(User::class, $foundUser); |
| 247 | + $this->assertSame($user->id, $foundUser->id); |
| 248 | + $this->assertInstanceOf(AccessToken::class, $foundUser->currentAccessToken()); |
| 249 | + $this->assertSame($token->token, $foundUser->currentAccessToken()->token); |
| 250 | + |
| 251 | + $this->seeInDatabase($this->tables['token_logins'], [ |
| 252 | + 'id_type' => AccessTokens::ID_TYPE_ACCESS_TOKEN, |
| 253 | + 'identifier' => 'foo', |
| 254 | + 'success' => 1, |
| 255 | + ]); |
| 256 | + } |
| 257 | + |
225 | 258 | protected function setRequestHeader(string $token): void |
226 | 259 | { |
227 | 260 | $request = service('request'); |
|
0 commit comments