forge/tests/Feature/User/TwoFactorAuthenticationSettingsTest.php

59 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2024-07-18 00:11:32 -04:00
<?php
use App\Models\User;
use Laravel\Fortify\Features;
use Laravel\Jetstream\Http\Livewire\TwoFactorAuthenticationForm;
use Livewire\Livewire;
test('two factor authentication can be enabled', function () {
$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);
})->skip(function () {
return ! Features::canManageTwoFactorAuthentication();
}, 'Two factor authentication is not enabled.');
test('recovery codes can be regenerated', function () {
$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);
})->skip(function () {
return ! Features::canManageTwoFactorAuthentication();
}, 'Two factor authentication is not enabled.');
test('two factor authentication can be disabled', function () {
$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();
})->skip(function () {
return ! Features::canManageTwoFactorAuthentication();
}, 'Two factor authentication is not enabled.');