QrcodeControllerTest.php 3.0 KB

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