QrCodeControllerTest.php 3.7 KB

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