QrCodeControllerTest.php 3.7 KB

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