Files
ppeasy_vg12/tests/Browser/Auth/GatedPagesTest.php
T
2026-10-09 08:21:06 +02:00

92 lines
3.0 KiB
PHP

<?php
use Carbon\Carbon;
use Facades\Tests\Setup\UserFactory;
use PragmaRX\Google2FA\Google2FA;
use Tests\UpdatesSettings;
uses(UpdatesSettings::class);
beforeEach(function () {
$this->artisan('db:seed');
});
it('sends an unverified user to the verification notice', function () {
$user = UserFactory::user()->unverified()->create(['password' => 'password']);
visit('/login')
->fill('#username', $user->email)
->fill('#password', 'password')
->click('Log In')
->assertSee('Verify Your Email Address')
->assertSee('Before proceeding, please check your email for a verification link.')
->assertPathIs('/email/verify');
});
it('resends the verification link', function () {
$user = UserFactory::user()->unverified()->create(['password' => 'password']);
visit('/login')
->fill('#username', $user->email)
->fill('#password', 'password')
->click('Log In')
->assertSee('Verify Your Email Address')
->click('click here to request another')
->assertSee('A fresh verification link has been sent to your email address.');
});
it('renders the approval notice', function () {
browserLogin();
visit('/approval-notice')
->assertNoJavaScriptErrors()
->assertSee('Waiting Approval')
->assertSee('Your account is waiting approval from administrators.');
});
it('asks for a two-factor token and then completes the login', function () {
$this->setSettings(['2fa.enabled' => true, 'throttle_enabled' => false]);
$google2fa = new Google2FA;
// two_factor_secret is not fillable, so it is assigned the way the server
// suite assigns it rather than through the factory.
$user = UserFactory::admin()->create(['password' => 'password']);
$user->two_factor_secret = encrypt($google2fa->generateSecretKey());
$user->two_factor_confirmed_at = Carbon::now();
$user->save();
visit('/login')
->fill('#username', $user->email)
->fill('#password', 'password')
->click('Log In')
->assertSee('Two-Factor Authentication')
->assertPathIs('/auth/two-factor-authentication')
->fill('#code', $google2fa->getCurrentOtp(decrypt($user->two_factor_secret)))
->click('Validate')
->assertSee('Dashboard')
->assertPathIs('/');
$this->assertAuthenticatedAs($user->fresh());
});
it('rejects an invalid two-factor token', function () {
$this->setSettings(['2fa.enabled' => true, 'throttle_enabled' => false]);
$user = UserFactory::admin()->create(['password' => 'password']);
$user->two_factor_secret = encrypt((new Google2FA)->generateSecretKey());
$user->two_factor_confirmed_at = Carbon::now();
$user->save();
visit('/login')
->fill('#username', $user->email)
->fill('#password', 'password')
->click('Log In')
->assertSee('Two-Factor Authentication')
->fill('#code', '000000')
->click('Validate')
->assertSee('Invalid 2FA token.');
$this->assertGuest();
});