QrCodeControllerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Api\v1\Controllers\QrCodeController;
  4. use App\Models\TwoFAccount;
  5. use App\Models\User;
  6. use PHPUnit\Framework\Attributes\CoversClass;
  7. use Tests\Classes\LocalFile;
  8. use Tests\FeatureTestCase;
  9. /**
  10. * QrCodeController test class
  11. */
  12. #[CoversClass(QrCodeController::class)]
  13. class QrCodeControllerTest extends FeatureTestCase
  14. {
  15. /**
  16. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  17. */
  18. protected $user;
  19. protected $anotherUser;
  20. /**
  21. * @var App\Models\TwoFAccount
  22. */
  23. protected $twofaccount;
  24. /**
  25. * @test
  26. */
  27. public function setUp() : void
  28. {
  29. parent::setUp();
  30. $this->user = User::factory()->create();
  31. $this->anotherUser = User::factory()->create();
  32. $this->twofaccount = TwoFAccount::factory()->for($this->user)->create([
  33. 'otp_type' => 'totp',
  34. 'account' => 'account',
  35. 'service' => 'service',
  36. 'secret' => 'A4GRFHZVRBGY7UIW',
  37. 'algorithm' => 'sha1',
  38. 'digits' => 6,
  39. 'period' => 30,
  40. 'legacy_uri' => 'otpauth://hotp/service:account?secret=A4GRFHZVRBGY7UIW&issuer=service',
  41. ]);
  42. }
  43. /**
  44. * @test
  45. */
  46. public function test_show_qrcode_returns_base64_image()
  47. {
  48. $response = $this->actingAs($this->user, 'api-guard')
  49. ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccount->id . '/qrcode')
  50. ->assertJsonStructure([
  51. 'qrcode',
  52. ])
  53. ->assertOk();
  54. $this->assertStringStartsWith('data:image/svg+xml;base64', $response->getData()->qrcode);
  55. }
  56. /**
  57. * @test
  58. */
  59. public function test_show_missing_qrcode_returns_not_found()
  60. {
  61. $response = $this->actingAs($this->user, 'api-guard')
  62. ->json('GET', '/api/v1/twofaccounts/1000/qrcode')
  63. ->assertNotFound()
  64. ->assertJsonStructure([
  65. 'message',
  66. ]);
  67. }
  68. /**
  69. * @test
  70. */
  71. public function test_show_qrcode_of_another_user_is_forbidden()
  72. {
  73. $response = $this->actingAs($this->anotherUser, 'api-guard')
  74. ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccount->id . '/qrcode')
  75. ->assertForbidden()
  76. ->assertJsonStructure([
  77. 'message',
  78. ]);
  79. }
  80. /**
  81. * @test
  82. */
  83. public function test_decode_qrcode_return_success()
  84. {
  85. $file = LocalFile::fake()->validQrcode();
  86. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  87. ->actingAs($this->user, 'api-guard')
  88. ->json('POST', '/api/v1/qrcode/decode', [
  89. 'qrcode' => $file,
  90. 'inputFormat' => 'fileUpload',
  91. ])
  92. ->assertOk()
  93. ->assertExactJson([
  94. 'data' => 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW',
  95. ]);
  96. }
  97. /**
  98. * @test
  99. */
  100. public function test_decode_missing_qrcode_return_validation_error()
  101. {
  102. $response = $this->actingAs($this->user, 'api-guard')
  103. ->json('POST', '/api/v1/qrcode/decode', [
  104. 'qrcode' => '',
  105. ])
  106. ->assertStatus(422);
  107. }
  108. /**
  109. * @test
  110. */
  111. public function test_decode_invalid_qrcode_return_bad_request()
  112. {
  113. $file = LocalFile::fake()->invalidQrcode();
  114. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  115. ->actingAs($this->user, 'api-guard')
  116. ->json('POST', '/api/v1/qrcode/decode', [
  117. 'qrcode' => $file,
  118. 'inputFormat' => 'fileUpload',
  119. ])
  120. ->assertStatus(400)
  121. ->assertJsonStructure([
  122. 'message',
  123. ]);
  124. }
  125. }