|
17 | 17 | use CodeIgniter\I18n\Time; |
18 | 18 | use CodeIgniter\Shield\Authentication\Actions\Email2FA; |
19 | 19 | use CodeIgniter\Shield\Config\Auth; |
| 20 | +use CodeIgniter\Shield\Models\UserIdentityModel; |
20 | 21 | use CodeIgniter\Test\FeatureTestTrait; |
| 22 | +use CodeIgniter\Test\TestResponse; |
21 | 23 | use Config\Services; |
22 | 24 | use Config\Validation; |
| 25 | +use Tests\Support\AdminEmail2FA; |
23 | 26 | use Tests\Support\DatabaseTestCase; |
24 | 27 | use Tests\Support\FakeUser; |
25 | 28 |
|
@@ -256,4 +259,90 @@ public function testLoginRedirectsToActionIfDefined(): void |
256 | 259 | $result->assertSessionMissing('errors'); |
257 | 260 | $this->assertSame(site_url('auth/a/show'), $result->getRedirectUrl()); |
258 | 261 | } |
| 262 | + |
| 263 | + public function testLoginRedirectsToConditionalActionWhenItApplies(): void |
| 264 | + { |
| 265 | + $this->enableAdminEmail2FA(); |
| 266 | + |
| 267 | + $this->user->addGroup('admin'); |
| 268 | + $this->createUserEmailIdentity(); |
| 269 | + |
| 270 | + $result = $this->loginUser(); |
| 271 | + |
| 272 | + $result->assertStatus(302); |
| 273 | + $result->assertRedirect(); |
| 274 | + $this->assertSame(site_url('auth/a/show'), $result->getRedirectUrl()); |
| 275 | + } |
| 276 | + |
| 277 | + public function testLoginSkipsConditionalActionWhenItDoesNotApply(): void |
| 278 | + { |
| 279 | + $this->enableAdminEmail2FA(); |
| 280 | + $this->createUserEmailIdentity(); |
| 281 | + |
| 282 | + $result = $this->loginUser(); |
| 283 | + |
| 284 | + $result->assertStatus(302); |
| 285 | + $result->assertRedirect(); |
| 286 | + $this->assertSame(site_url(), $result->getRedirectUrl()); |
| 287 | + } |
| 288 | + |
| 289 | + public function testLoginIgnoresStoredConditionalActionIdentityWhenItDoesNotApply(): void |
| 290 | + { |
| 291 | + $this->enableAdminEmail2FA(); |
| 292 | + $this->createUserEmailIdentity(); |
| 293 | + |
| 294 | + model(UserIdentityModel::class)->insert([ |
| 295 | + 'user_id' => $this->user->id, |
| 296 | + 'type' => 'email_2fa', |
| 297 | + 'name' => 'login', |
| 298 | + 'secret' => '123456', |
| 299 | + 'extra' => lang('Auth.need2FA'), |
| 300 | + ]); |
| 301 | + |
| 302 | + $result = $this->loginUser(); |
| 303 | + |
| 304 | + $result->assertStatus(302); |
| 305 | + $result->assertRedirect(); |
| 306 | + $this->assertSame(site_url(), $result->getRedirectUrl()); |
| 307 | + } |
| 308 | + |
| 309 | + public function testLoginKeepsExistingPendingConditionalActionInSession(): void |
| 310 | + { |
| 311 | + $this->enableAdminEmail2FA(); |
| 312 | + $this->createUserEmailIdentity(); |
| 313 | + |
| 314 | + $result = $this->withSession([ |
| 315 | + 'user' => [ |
| 316 | + 'id' => $this->user->id, |
| 317 | + 'auth_action' => AdminEmail2FA::class, |
| 318 | + ], |
| 319 | + ])->get('/login'); |
| 320 | + |
| 321 | + $result->assertStatus(302); |
| 322 | + $result->assertRedirect(); |
| 323 | + $this->assertSame(site_url('auth/a/show'), $result->getRedirectUrl()); |
| 324 | + } |
| 325 | + |
| 326 | + private function enableAdminEmail2FA(): void |
| 327 | + { |
| 328 | + $config = config('Auth'); |
| 329 | + $config->actions['login'] = AdminEmail2FA::class; |
| 330 | + Factories::injectMock('config', 'Auth', $config); |
| 331 | + } |
| 332 | + |
| 333 | + private function createUserEmailIdentity(): void |
| 334 | + { |
| 335 | + $this->user->createEmailIdentity([ |
| 336 | + 'email' => 'foo@example.com', |
| 337 | + 'password' => 'secret123', |
| 338 | + ]); |
| 339 | + } |
| 340 | + |
| 341 | + private function loginUser(): TestResponse |
| 342 | + { |
| 343 | + return $this->post('/login', [ |
| 344 | + 'email' => 'foo@example.com', |
| 345 | + 'password' => 'secret123', |
| 346 | + ]); |
| 347 | + } |
259 | 348 | } |
0 commit comments