forge/tests/Feature/User/TwoFactorAuthenticationSettingsTest.php

55 lines
2.0 KiB
PHP
Raw Normal View History

2024-07-18 00:11:32 -04:00
<?php
2025-01-30 00:23:55 -05:00
declare(strict_types=1);
2024-07-18 00:11:32 -04:00
use App\Models\User;
use Laravel\Fortify\Features;
use Laravel\Jetstream\Http\Livewire\TwoFactorAuthenticationForm;
use Livewire\Livewire;
2025-01-30 00:23:55 -05:00
test('two factor authentication can be enabled', function (): void {
2024-07-18 00:11:32 -04:00
$this->actingAs($user = User::factory()->create()->fresh());
$this->withSession(['auth.password_confirmed_at' => time()]);
Livewire::test(TwoFactorAuthenticationForm::class)
->call('enableTwoFactorAuthentication');
$user = $user->fresh();
expect($user->two_factor_secret)->not->toBeNull();
expect($user->recoveryCodes())->toHaveCount(8);
2025-01-30 00:23:55 -05:00
})->skip(fn (): bool => ! Features::canManageTwoFactorAuthentication(), 'Two factor authentication is not enabled.');
2024-07-18 00:11:32 -04:00
2025-01-30 00:23:55 -05:00
test('recovery codes can be regenerated', function (): void {
2024-07-18 00:11:32 -04:00
$this->actingAs($user = User::factory()->create()->fresh());
$this->withSession(['auth.password_confirmed_at' => time()]);
$component = Livewire::test(TwoFactorAuthenticationForm::class)
->call('enableTwoFactorAuthentication')
->call('regenerateRecoveryCodes');
$user = $user->fresh();
$component->call('regenerateRecoveryCodes');
expect($user->recoveryCodes())->toHaveCount(8);
expect(array_diff($user->recoveryCodes(), $user->fresh()->recoveryCodes()))->toHaveCount(8);
2025-01-30 00:23:55 -05:00
})->skip(fn (): bool => ! Features::canManageTwoFactorAuthentication(), 'Two factor authentication is not enabled.');
2024-07-18 00:11:32 -04:00
2025-01-30 00:23:55 -05:00
test('two factor authentication can be disabled', function (): void {
2024-07-18 00:11:32 -04:00
$this->actingAs($user = User::factory()->create()->fresh());
$this->withSession(['auth.password_confirmed_at' => time()]);
$component = Livewire::test(TwoFactorAuthenticationForm::class)
->call('enableTwoFactorAuthentication');
$this->assertNotNull($user->fresh()->two_factor_secret);
$component->call('disableTwoFactorAuthentication');
expect($user->fresh()->two_factor_secret)->toBeNull();
2025-01-30 00:23:55 -05:00
})->skip(fn (): bool => ! Features::canManageTwoFactorAuthentication(), 'Two factor authentication is not enabled.');