clusev/tests/Feature/RecoveryCodesViewTest.php

56 lines
1.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Auth\RecoveryCodes;
use App\Livewire\Auth\TwoFactorSetup;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use PragmaRX\Google2FAQRCode\Google2FA;
use Tests\TestCase;
class RecoveryCodesViewTest extends TestCase
{
use RefreshDatabase;
public function test_confirming_2fa_generates_codes_and_redirects_to_recovery(): void
{
$user = User::factory()->create(['two_factor_secret' => null, 'two_factor_confirmed_at' => null]);
$this->actingAs($user);
$secret = (new Google2FA)->generateSecretKey();
$code = (new Google2FA)->getCurrentOtp($secret);
Livewire::test(TwoFactorSetup::class)
->set('secret', $secret)
->set('code', $code)
->call('confirm')
->assertRedirect(route('two-factor.recovery'));
$this->assertCount(8, $user->fresh()->recoveryCodes());
}
public function test_regenerate_replaces_codes(): void
{
$user = User::factory()->create(['two_factor_secret' => 'x', 'two_factor_confirmed_at' => now()]);
$old = $user->replaceRecoveryCodes();
$this->actingAs($user);
Livewire::test(RecoveryCodes::class)->call('regenerate');
$this->assertNotSame($old, $user->fresh()->recoveryCodes());
$this->assertCount(8, $user->fresh()->recoveryCodes());
}
public function test_download_returns_codes_as_text(): void
{
$user = User::factory()->create();
$user->replaceRecoveryCodes();
$this->actingAs($user);
$res = $this->get(route('two-factor.recovery.download'));
$res->assertOk();
$this->assertStringContainsString($user->recoveryCodes()[0], $res->streamedContent());
}
}