Skip to content

Commit b8952b6

Browse files
jasonvargaclaude
andcommitted
Add coverage for configured 2FA setup URL and login redirect stash
Adds middleware tests for the configured two_factor_setup_url (redirect target and loop bypass) and login-form tests covering when login.redirect is or isn't stashed in the session. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 62ffcd8 commit b8952b6

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

tests/Feature/Users/TwoFactorRoutesTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ protected function resolveApplicationConfiguration($app)
2323
Route::get('/test-frontend-route', function () {
2424
return 'ok';
2525
})->middleware('statamic.web');
26+
27+
Route::get('/custom-setup', function () {
28+
return 'setup page';
29+
})->middleware('statamic.web');
2630
});
2731
}
2832

@@ -87,6 +91,39 @@ public function frontend_two_factor_setup_middleware_redirects_when_two_factor_i
8791
->assertRedirect(route('statamic.two-factor-setup', ['referer' => url('/test-frontend-route')]));
8892
}
8993

94+
#[Test]
95+
public function frontend_two_factor_setup_middleware_redirects_to_configured_url()
96+
{
97+
config([
98+
'statamic.users.two_factor_enforced_roles' => ['*'],
99+
'statamic.users.two_factor_setup_url' => '/custom-setup',
100+
]);
101+
102+
$user = tap(User::make()->makeSuper()->email('admin@domain.com'))->save();
103+
104+
$this
105+
->actingAs($user)
106+
->get('/test-frontend-route')
107+
->assertRedirect('/custom-setup');
108+
}
109+
110+
#[Test]
111+
public function frontend_two_factor_setup_middleware_allows_configured_url_through()
112+
{
113+
config([
114+
'statamic.users.two_factor_enforced_roles' => ['*'],
115+
'statamic.users.two_factor_setup_url' => '/custom-setup',
116+
]);
117+
118+
$user = tap(User::make()->makeSuper()->email('admin@domain.com'))->save();
119+
120+
$this
121+
->actingAs($user)
122+
->get('/custom-setup')
123+
->assertOk()
124+
->assertSee('setup page');
125+
}
126+
90127
#[Test]
91128
#[DefineEnvironment('disableTwoFactor')]
92129
public function frontend_two_factor_setup_middleware_does_not_redirect_when_two_factor_is_disabled()

tests/Tags/User/LoginFormTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,47 @@ public function it_stores_redirect_in_session_for_two_factor_challenge()
387387
->assertSessionHas('login.redirect', '/dashboard');
388388
}
389389

390+
#[Test]
391+
public function it_does_not_stash_login_redirect_when_two_factor_is_not_enforced()
392+
{
393+
User::make()
394+
->id(1)
395+
->email('san@holo.com')
396+
->password('chewy')
397+
->save();
398+
399+
$this
400+
->post('/!/auth/login', [
401+
'token' => 'test-token',
402+
'email' => 'san@holo.com',
403+
'password' => 'chewy',
404+
'_redirect' => '/dashboard',
405+
])
406+
->assertRedirect('/dashboard')
407+
->assertSessionMissing('login.redirect');
408+
}
409+
410+
#[Test]
411+
public function it_stashes_login_redirect_when_two_factor_setup_is_required()
412+
{
413+
config()->set('statamic.users.two_factor_enforced_roles', ['*']);
414+
415+
User::make()
416+
->id(1)
417+
->email('san@holo.com')
418+
->password('chewy')
419+
->save();
420+
421+
$this
422+
->post('/!/auth/login', [
423+
'token' => 'test-token',
424+
'email' => 'san@holo.com',
425+
'password' => 'chewy',
426+
'_redirect' => '/dashboard',
427+
])
428+
->assertSessionHas('login.redirect', '/dashboard');
429+
}
430+
390431
#[Test]
391432
#[DefineEnvironment('disableTwoFactor')]
392433
public function it_skips_two_factor_challenge_when_two_factor_is_disabled()

0 commit comments

Comments
 (0)