WebAuthnManageControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\Str;
  7. use Webauthn\TrustPath\EmptyTrustPath;
  8. class WebAuthnManageControllerTest extends FeatureTestCase
  9. {
  10. /**
  11. * @var \App\Models\User
  12. */
  13. protected $user;
  14. /**
  15. * @test
  16. */
  17. public function setUp(): void
  18. {
  19. parent::setUp();
  20. $this->user = User::factory()->create();
  21. }
  22. /**
  23. * @test
  24. */
  25. public function test_index_returns_success_with_credentials()
  26. {
  27. DB::table('web_authn_credentials')->insert([
  28. 'id' => 'test_credential_id',
  29. 'user_id' => $this->user->id,
  30. 'type' => 'public_key',
  31. 'transports' => json_encode([]),
  32. 'attestation_type' => 'none',
  33. 'trust_path' => json_encode(['type' => EmptyTrustPath::class]),
  34. 'aaguid' => Str::uuid(),
  35. 'public_key' => 'public_key_bar',
  36. 'counter' => 0,
  37. 'user_handle' => 'test_id',
  38. 'created_at' => now()->toDateTimeString(),
  39. 'updated_at' => now()->toDateTimeString(),
  40. 'disabled_at' => null,
  41. ]);
  42. $response = $this->actingAs($this->user, 'web-guard')
  43. ->json('GET', '/webauthn/credentials')
  44. ->assertStatus(200)
  45. ->assertJsonStructure([
  46. '*' => [
  47. 'id',
  48. 'name',
  49. 'type',
  50. 'transports'
  51. ]
  52. ]);
  53. }
  54. /**
  55. * @test
  56. */
  57. public function test_rename_returns_success_with_new_name()
  58. {
  59. DB::table('web_authn_credentials')->insert([
  60. 'id' => 'test_credential_id',
  61. 'name' => 'MyCredential',
  62. 'user_id' => $this->user->id,
  63. 'type' => 'public_key',
  64. 'transports' => json_encode([]),
  65. 'attestation_type' => 'none',
  66. 'trust_path' => json_encode(['type' => EmptyTrustPath::class]),
  67. 'aaguid' => Str::uuid(),
  68. 'public_key' => 'public_key_bar',
  69. 'counter' => 0,
  70. 'user_handle' => 'test_id',
  71. 'created_at' => now()->toDateTimeString(),
  72. 'updated_at' => now()->toDateTimeString(),
  73. 'disabled_at' => null,
  74. ]);
  75. $response = $this->actingAs($this->user, 'web-guard')
  76. ->json('PATCH', '/webauthn/credentials/test_credential_id/name',[
  77. 'name' => 'MyNewCredential',
  78. ])
  79. ->assertStatus(200)
  80. ->assertExactJson([
  81. 'name' => 'MyNewCredential',
  82. ]);
  83. }
  84. /**
  85. * @test
  86. */
  87. public function test_rename_invalid_data_returns_validation_error()
  88. {
  89. $response = $this->actingAs($this->user, 'web-guard')
  90. ->json('PATCH', '/webauthn/credentials/test_credential_id/name', [
  91. 'name' => null,
  92. ])
  93. ->assertStatus(422);
  94. }
  95. /**
  96. * @test
  97. */
  98. public function test_rename_missing_credential_returns_not_found()
  99. {
  100. $response = $this->actingAs($this->user, 'web-guard')
  101. ->json('PATCH', '/webauthn/credentials/unknown/name', [
  102. 'name' => 'MyNewCredential',
  103. ])
  104. ->assertNotFound()
  105. ->assertJsonStructure([
  106. 'message'
  107. ]);
  108. }
  109. /**
  110. * @test
  111. */
  112. public function test_index_as_reverse_proxy_returns_error()
  113. {
  114. $response = $this->actingAs($this->user, 'reverse-proxy-guard')
  115. ->json('GET', '/webauthn/credentials')
  116. ->assertStatus(400);
  117. }
  118. /**
  119. * @test
  120. */
  121. public function test_rename_as_reverse_proxy_returns_error()
  122. {
  123. $response = $this->actingAs($this->user, 'reverse-proxy-guard')
  124. ->json('PATCH', '/webauthn/credentials/fqsdfqsdf/name')
  125. ->assertStatus(400);
  126. }
  127. /**
  128. * @test
  129. */
  130. public function test_delete_as_reverse_proxy_returns_error()
  131. {
  132. $response = $this->actingAs($this->user, 'reverse-proxy-guard')
  133. ->json('DELETE', '/webauthn/credentials/dcnskldjnkljsrn')
  134. ->assertStatus(400);
  135. }
  136. /**
  137. * @test
  138. */
  139. public function test_delete_returns_no_content()
  140. {
  141. $response = $this->actingAs($this->user, 'web-guard')
  142. ->json('DELETE', '/webauthn/credentials/sdCKktnsdK')
  143. ->assertNoContent();
  144. }
  145. }