QrCodeControllerTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Models\User;
  4. use Tests\FeatureTestCase;
  5. use App\Models\TwoFAccount;
  6. use Tests\Classes\LocalFile;
  7. /**
  8. * @covers \App\Api\v1\Controllers\QrCodeController
  9. */
  10. class QrCodeControllerTest extends FeatureTestCase
  11. {
  12. /**
  13. * @var \App\Models\User
  14. */
  15. protected $user;
  16. /**
  17. * @test
  18. */
  19. public function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->user = User::factory()->create();
  23. }
  24. /**
  25. * @test
  26. */
  27. public function test_show_qrcode_returns_base64_image()
  28. {
  29. $twofaccount = TwoFAccount::factory()->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. $response = $this->actingAs($this->user, 'api')
  40. ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/qrcode')
  41. ->assertJsonStructure([
  42. 'qrcode',
  43. ])
  44. ->assertOk();
  45. $this->assertStringStartsWith('data:image/png;base64', $response->getData()->qrcode);
  46. }
  47. /**
  48. * @test
  49. */
  50. public function test_show_missing_qrcode_returns_not_found()
  51. {
  52. $response = $this->actingAs($this->user, 'api')
  53. ->json('GET', '/api/v1/twofaccounts/1000/qrcode')
  54. ->assertNotFound()
  55. ->assertJsonStructure([
  56. 'message'
  57. ]);
  58. }
  59. /**
  60. * @test
  61. */
  62. public function test_decode_qrcode_return_success()
  63. {
  64. $file = LocalFile::fake()->validQrcode();
  65. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  66. ->actingAs($this->user, 'api')
  67. ->json('POST', '/api/v1/qrcode/decode', [
  68. 'qrcode' => $file,
  69. 'inputFormat' => 'fileUpload'
  70. ])
  71. ->assertOk()
  72. ->assertExactJson([
  73. 'data' => 'otpauth://totp/test@test.com?secret=A4GRFHVIRBGY7UIW',
  74. ]);
  75. }
  76. /**
  77. * @test
  78. */
  79. public function test_decode_missing_qrcode_return_validation_error()
  80. {
  81. $response = $this->actingAs($this->user, 'api')
  82. ->json('POST', '/api/v1/qrcode/decode', [
  83. 'qrcode' => '',
  84. ])
  85. ->assertStatus(422);
  86. }
  87. /**
  88. * @test
  89. */
  90. public function test_decode_invalid_qrcode_return_bad_request()
  91. {
  92. $file = LocalFile::fake()->invalidQrcode();
  93. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  94. ->actingAs($this->user, 'api')
  95. ->json('POST', '/api/v1/qrcode/decode', [
  96. 'qrcode' => $file,
  97. 'inputFormat' => 'fileUpload'
  98. ])
  99. ->assertStatus(400)
  100. ->assertJsonStructure([
  101. 'message',
  102. ]);
  103. }
  104. }