Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TwoFactorAuthenticationSettingsTest.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\User;
  4. use Illuminate\Foundation\Testing\RefreshDatabase;
  5. use Laravel\Fortify\Features;
  6. use Livewire\Livewire;
  7. use Tests\TestCase;
  8. use Wallo\FilamentCompanies\Http\Livewire\TwoFactorAuthenticationForm;
  9. class TwoFactorAuthenticationSettingsTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. public function test_two_factor_authentication_can_be_enabled(): void
  13. {
  14. if (! Features::canManageTwoFactorAuthentication()) {
  15. $this->markTestSkipped('Two factor authentication is not enabled.');
  16. return;
  17. }
  18. $this->actingAs($user = User::factory()->create());
  19. $this->withSession(['auth.password_confirmed_at' => time()]);
  20. Livewire::test(TwoFactorAuthenticationForm::class)
  21. ->call('enableTwoFactorAuthentication');
  22. $user = $user->fresh();
  23. $this->assertNotNull($user->two_factor_secret);
  24. $this->assertCount(8, $user->recoveryCodes());
  25. }
  26. public function test_recovery_codes_can_be_regenerated(): void
  27. {
  28. if (! Features::canManageTwoFactorAuthentication()) {
  29. $this->markTestSkipped('Two factor authentication is not enabled.');
  30. return;
  31. }
  32. $this->actingAs($user = User::factory()->create());
  33. $this->withSession(['auth.password_confirmed_at' => time()]);
  34. $component = Livewire::test(TwoFactorAuthenticationForm::class)
  35. ->call('enableTwoFactorAuthentication')
  36. ->call('regenerateRecoveryCodes');
  37. $user = $user->fresh();
  38. $component->call('regenerateRecoveryCodes');
  39. $this->assertCount(8, $user->recoveryCodes());
  40. $this->assertCount(8, array_diff($user->recoveryCodes(), $user->fresh()->recoveryCodes()));
  41. }
  42. public function test_two_factor_authentication_can_be_disabled(): void
  43. {
  44. if (! Features::canManageTwoFactorAuthentication()) {
  45. $this->markTestSkipped('Two factor authentication is not enabled.');
  46. return;
  47. }
  48. $this->actingAs($user = User::factory()->create());
  49. $this->withSession(['auth.password_confirmed_at' => time()]);
  50. $component = Livewire::test(TwoFactorAuthenticationForm::class)
  51. ->call('enableTwoFactorAuthentication');
  52. $this->assertNotNull($user->fresh()->two_factor_secret);
  53. $component->call('disableTwoFactorAuthentication');
  54. $this->assertNull($user->fresh()->two_factor_secret);
  55. }
  56. }