Files
coolify/tests/Feature/Auth/TwoFactorChallengeAccessTest.php
T
Andras BacsaiandClaude Opus 5.5 a79ee909c6 chore: remove unused components and markup-only tests
- Delete Blade components, component classes, Livewire components and
  views that nothing uses after the redesign.
- Delete tests that only compare Blade/CSS source text; keep the source
  checks for authorization gates, stable wire:key values and escaping.
- Update stale tests to the current UI and behaviour.
- Update DESIGN.md to the current navigation and layout.
- Add a lesson: test behaviour, not source markup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 00:18:16 +02:00

103 lines
3.9 KiB
PHP

<?php
use App\Models\InstanceSettings;
use App\Models\Team;
use App\Models\User;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Facades\Route;
uses(RefreshDatabase::class);
beforeEach(function () {
InstanceSettings::forceCreate(['id' => 0]);
$this->user = User::factory()->create();
$this->team = Team::factory()->personal()->create();
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
session(['currentTeam' => $this->team]);
});
it('allows unauthenticated access to two-factor-challenge page', function () {
$response = $this->get('/two-factor-challenge');
// Fortify returns a redirect to /login if there's no login.id in session,
// but the important thing is it does NOT return a 419 or 500
expect($response->status())->toBeIn([200, 302]);
});
it('includes two-factor-challenge in allowed paths for unsubscribed accounts', function () {
$paths = allowedPathsForUnsubscribedAccounts();
expect($paths)->toContain('two-factor-challenge');
});
it('includes two-factor-challenge in allowed paths for invalid accounts', function () {
$paths = allowedPathsForInvalidAccounts();
expect($paths)->toContain('two-factor-challenge');
});
it('includes two-factor-challenge in allowed paths for boarding accounts', function () {
$paths = allowedPathsForBoardingAccounts();
expect($paths)->toContain('two-factor-challenge');
});
it('does not redirect authenticated user with force_password_reset from two-factor-challenge', function () {
$this->user->update(['force_password_reset' => true]);
$response = $this->actingAs($this->user)->get('/two-factor-challenge');
// Should NOT redirect to force-password-reset page
if ($response->isRedirect()) {
expect($response->headers->get('Location'))->not->toContain('force-password-reset');
}
});
it('renders 419 error page with login link instead of previous url', function () {
$response = $this->get('/two-factor-challenge', [
'X-CSRF-TOKEN' => 'invalid-token',
]);
// The 419 page should exist and contain a link to /login
$view = view('errors.419')->render();
expect($view)->toContain('/login');
expect($view)->toContain('Back to login');
expect($view)->toContain('This page is definitely old, not like you!');
expect($view)->toContain('error-shell');
expect($view)->not->toContain('url()->previous()');
});
it('redirects an authenticated stale two-factor submission home instead of showing 419', function () {
$request = Request::create('/two-factor-challenge', 'POST');
$request->setRouteResolver(fn () => Route::getRoutes()->match($request));
$request->setUserResolver(fn () => $this->user);
$response = app(ExceptionHandler::class)->render($request, new TokenMismatchException('CSRF token mismatch.'));
expect($response->getStatusCode())->toBe(302)
->and($response->headers->get('Location'))->toBe(url('/'));
});
it('still returns 419 for a stale two-factor submission without an authenticated session', function () {
$request = Request::create('/two-factor-challenge', 'POST');
$request->setRouteResolver(fn () => Route::getRoutes()->match($request));
$response = app(ExceptionHandler::class)->render($request, new TokenMismatchException('CSRF token mismatch.'));
expect($response->getStatusCode())->toBe(419);
});
it('keeps the 419 for stale tokens on routes other than login and the two-factor challenge', function () {
$request = Request::create('/two-factor-challenge', 'GET');
$request->setRouteResolver(fn () => Route::getRoutes()->match($request));
$request->setUserResolver(fn () => $this->user);
$response = app(ExceptionHandler::class)->render($request, new TokenMismatchException('CSRF token mismatch.'));
expect($response->getStatusCode())->toBe(419);
});