TwoFactorAuthenticationSettingsTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\User;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Laravel\Jetstream\Http\Livewire\TwoFactorAuthenticationForm;
  6. use Livewire\Livewire;
  7. use Tests\TestCase;
  8. class TwoFactorAuthenticationSettingsTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. public function test_two_factor_authentication_can_be_enabled()
  12. {
  13. $this->actingAs($user = User::factory()->create());
  14. $this->withSession(['auth.password_confirmed_at' => time()]);
  15. Livewire::test(TwoFactorAuthenticationForm::class)
  16. ->call('enableTwoFactorAuthentication');
  17. $user = $user->fresh();
  18. $this->assertNotNull($user->two_factor_secret);
  19. $this->assertCount(8, $user->recoveryCodes());
  20. }
  21. public function test_recovery_codes_can_be_regenerated()
  22. {
  23. $this->actingAs($user = User::factory()->create());
  24. $this->withSession(['auth.password_confirmed_at' => time()]);
  25. $component = Livewire::test(TwoFactorAuthenticationForm::class)
  26. ->call('enableTwoFactorAuthentication')
  27. ->call('regenerateRecoveryCodes');
  28. $user = $user->fresh();
  29. $component->call('regenerateRecoveryCodes');
  30. $this->assertCount(8, $user->recoveryCodes());
  31. $this->assertCount(8, array_diff($user->recoveryCodes(), $user->fresh()->recoveryCodes()));
  32. }
  33. public function test_two_factor_authentication_can_be_disabled()
  34. {
  35. $this->actingAs($user = User::factory()->create());
  36. $this->withSession(['auth.password_confirmed_at' => time()]);
  37. $component = Livewire::test(TwoFactorAuthenticationForm::class)
  38. ->call('enableTwoFactorAuthentication');
  39. $this->assertNotNull($user->fresh()->two_factor_secret);
  40. $component->call('disableTwoFactorAuthentication');
  41. $this->assertNull($user->fresh()->two_factor_secret);
  42. }
  43. }