mirror of
https://github.com/usetrmnl/larapaper.git
synced 2026-08-13 14:28:54 -07:00
125 lines
3.8 KiB
PHP
125 lines
3.8 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Laravel\Fortify\Features;
|
|
use Livewire\Livewire;
|
|
|
|
uses(Illuminate\Foundation\Testing\RefreshDatabase::class);
|
|
|
|
beforeEach(function (): void {
|
|
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
|
|
|
|
config(['app.passkeys.enabled' => true]);
|
|
|
|
Features::twoFactorAuthentication([
|
|
'confirm' => true,
|
|
'confirmPassword' => true,
|
|
]);
|
|
|
|
Features::passkeys([
|
|
'confirmPassword' => true,
|
|
]);
|
|
|
|
$features = array_values(array_filter(config('fortify.features', [])));
|
|
$passkeysFeature = Features::passkeys();
|
|
if (! in_array($passkeysFeature, $features, true)) {
|
|
$features[] = $passkeysFeature;
|
|
}
|
|
config(['fortify.features' => $features]);
|
|
});
|
|
|
|
test('security settings page can be rendered', function (): void {
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)
|
|
->withSession(['auth.password_confirmed_at' => time()])
|
|
->get(route('security.edit'));
|
|
|
|
$response->assertOk();
|
|
$response->assertSee('Passkeys');
|
|
$response->assertSee('No passkeys yet');
|
|
$response->assertSee('Two-factor authentication');
|
|
$response->assertSee('Enable 2FA');
|
|
});
|
|
|
|
test('security settings page requires password confirmation when enabled', function (): void {
|
|
$user = User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)
|
|
->get(route('security.edit'));
|
|
|
|
$response->assertRedirect(route('password.confirm'));
|
|
});
|
|
|
|
test('security settings page renders without two factor when feature is disabled', function (): void {
|
|
config(['fortify.features' => []]);
|
|
|
|
$user = User::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->withSession(['auth.password_confirmed_at' => time()])
|
|
->get(route('security.edit'))
|
|
->assertOk()
|
|
->assertSee('Update password')
|
|
->assertDontSee('Manage your passkeys for passwordless sign-in')
|
|
->assertDontSee('Add a passkey to sign in without a password')
|
|
->assertDontSee('Two-factor authentication');
|
|
});
|
|
|
|
test('two factor authentication disabled when confirmation abandoned between requests', function (): void {
|
|
$user = User::factory()->create();
|
|
|
|
$user->forceFill([
|
|
'two_factor_secret' => encrypt('test-secret'),
|
|
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
|
|
'two_factor_confirmed_at' => null,
|
|
])->save();
|
|
|
|
$this->actingAs($user);
|
|
|
|
$component = Livewire::test('pages::settings.security');
|
|
|
|
$component->assertSet('twoFactorEnabled', false);
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'id' => $user->id,
|
|
'two_factor_secret' => null,
|
|
'two_factor_recovery_codes' => null,
|
|
]);
|
|
});
|
|
|
|
test('password can be updated on security page', function (): void {
|
|
$user = User::factory()->create([
|
|
'password' => Hash::make('password'),
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = Livewire::test('pages::settings.security')
|
|
->set('current_password', 'password')
|
|
->set('password', 'new-password')
|
|
->set('password_confirmation', 'new-password')
|
|
->call('updatePassword');
|
|
|
|
$response->assertHasNoErrors();
|
|
|
|
expect(Hash::check('new-password', $user->refresh()->password))->toBeTrue();
|
|
});
|
|
|
|
test('correct password must be provided to update password on security page', function (): void {
|
|
$user = User::factory()->create([
|
|
'password' => Hash::make('password'),
|
|
]);
|
|
|
|
$this->actingAs($user);
|
|
|
|
$response = Livewire::test('pages::settings.security')
|
|
->set('current_password', 'wrong-password')
|
|
->set('password', 'new-password')
|
|
->set('password_confirmation', 'new-password')
|
|
->call('updatePassword');
|
|
|
|
$response->assertHasErrors(['current_password']);
|
|
});
|