WebAuthnRecoveryControllerTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Tests\Feature\Auth;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Date;
  7. class WebAuthnRecoveryControllerTest extends FeatureTestCase
  8. {
  9. /**
  10. * @var \App\Models\User
  11. */
  12. protected $user;
  13. /**
  14. * @test
  15. */
  16. public function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->user = User::factory()->create();
  20. }
  21. /**
  22. * @test
  23. */
  24. public function test_options_returns_success()
  25. {
  26. $token = '$2y$10$hgGTVVTRLsSYSlAHpyydBu6m4ZuRheBqTTUfRE/aG89DaqEyo.HPu';
  27. Date::setTestNow($now = Date::create(2020, 01, 01, 16, 30));
  28. DB::table('web_authn_recoveries')->insert([
  29. 'email' => $this->user->email,
  30. 'token' => $token,
  31. 'created_at' => $now->toDateTimeString(),
  32. ]);
  33. $response = $this->json('POST', '/webauthn/recover/options', [
  34. 'token' => 'test_token',
  35. 'email' => $this->user->email,
  36. ])
  37. ->assertStatus(200);
  38. }
  39. /**
  40. * @test
  41. */
  42. public function test_options_with_invalid_token_returns_error()
  43. {
  44. $response = $this->json('POST', '/webauthn/recover/options', [
  45. 'token' => 'myToken',
  46. 'email' => $this->user->email,
  47. ])
  48. ->assertStatus(401);
  49. }
  50. /**
  51. * @test
  52. */
  53. public function test_options_without_inputs_returns_validation_errors()
  54. {
  55. $response = $this->json('POST', '/webauthn/recover/options', [
  56. 'token' => '',
  57. 'email' => '',
  58. ]);
  59. $response->assertStatus(422)
  60. ->assertJsonValidationErrors(['token'])
  61. ->assertJsonValidationErrors(['email']);
  62. }
  63. /**
  64. * @test
  65. */
  66. // public function test_recover_returns_success()
  67. // {
  68. // $token = '$2y$10$hgGTVVTRLsSYSlAHpyydBu6m4ZuRheBqTTUfRE/aG89DaqEyo.HPu';
  69. // Date::setTestNow($now = Date::create(2020, 01, 01, 16, 30));
  70. // DB::table('web_authn_recoveries')->insert([
  71. // 'email' => $this->user->email,
  72. // 'token' => $token,
  73. // 'created_at' => $now->toDateTimeString(),
  74. // ]);
  75. // $response = $this->json('POST', '/webauthn/recover', [], [
  76. // 'token' => $token,
  77. // 'email' => $this->user->email,
  78. // ])
  79. // ->assertStatus(200);
  80. // }
  81. /**
  82. * @test
  83. */
  84. public function test_recover_with_invalid_token_returns_validation_error()
  85. {
  86. $response = $this->json('POST', '/webauthn/recover', [], [
  87. 'token' => 'toekn',
  88. 'email' => $this->user->email,
  89. ])
  90. ->assertStatus(422)
  91. ->assertJsonValidationErrors(['email']);
  92. }
  93. }