TwoFAccountStoreRequestTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Api\v1\Requests\TwoFAccountStoreRequest;
  4. use Illuminate\Foundation\Testing\WithoutMiddleware;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Validator;
  7. use Tests\TestCase;
  8. /**
  9. * @covers \App\Api\v1\Requests\TwoFAccountStoreRequest
  10. * @covers \App\Rules\IsBase32Encoded
  11. */
  12. class TwoFAccountStoreRequestTest extends TestCase
  13. {
  14. use WithoutMiddleware;
  15. /**
  16. * @test
  17. */
  18. public function test_user_is_authorized()
  19. {
  20. Auth::shouldReceive('check')
  21. ->once()
  22. ->andReturn(true);
  23. $request = new TwoFAccountStoreRequest();
  24. $this->assertTrue($request->authorize());
  25. }
  26. /**
  27. * @dataProvider provideValidData
  28. */
  29. public function test_valid_data(array $data): void
  30. {
  31. $request = new TwoFAccountStoreRequest();
  32. $validator = Validator::make($data, $request->rules());
  33. $this->assertFalse($validator->fails());
  34. }
  35. /**
  36. * Provide Valid data for validation test
  37. */
  38. public function provideValidData(): array
  39. {
  40. return [
  41. [[
  42. 'service' => 'MyService',
  43. 'account' => 'MyAccount',
  44. 'icon' => 'icon.png',
  45. 'otp_type' => 'totp',
  46. 'secret' => 'A4GRFHZVRBGY7UIW',
  47. 'digits' => 6,
  48. 'algorithm' => 'sha1',
  49. 'period' => 30,
  50. ]],
  51. [[
  52. 'service' => 'MyService',
  53. 'account' => 'MyAccount',
  54. 'icon' => 'icon.png',
  55. 'otp_type' => 'hotp',
  56. 'secret' => 'A4GRFHZVRBGY7UIW',
  57. 'digits' => 8,
  58. 'algorithm' => 'sha1',
  59. 'counter' => 10,
  60. ]],
  61. [[
  62. 'service' => null,
  63. 'account' => 'MyAccount',
  64. 'icon' => null,
  65. 'otp_type' => 'hotp',
  66. 'secret' => 'A4GRFHZVRBGY7UIW',
  67. 'digits' => null,
  68. 'algorithm' => null,
  69. 'counter' => null,
  70. ]],
  71. [[
  72. 'account' => 'MyAccount',
  73. 'otp_type' => 'totp',
  74. ]],
  75. [[
  76. 'account' => 'MyAccount',
  77. 'otp_type' => 'totp',
  78. 'algorithm' => 'sha256',
  79. ]],
  80. [[
  81. 'account' => 'MyAccount',
  82. 'otp_type' => 'totp',
  83. 'algorithm' => 'sha512',
  84. ]],
  85. [[
  86. 'account' => 'MyAccount',
  87. 'otp_type' => 'totp',
  88. 'algorithm' => 'md5',
  89. ]],
  90. ];
  91. }
  92. /**
  93. * @dataProvider provideInvalidData
  94. */
  95. public function test_invalid_data(array $data): void
  96. {
  97. $request = new TwoFAccountStoreRequest();
  98. $validator = Validator::make($data, $request->rules());
  99. $this->assertTrue($validator->fails());
  100. }
  101. /**
  102. * Provide invalid data for validation test
  103. */
  104. public function provideInvalidData(): array
  105. {
  106. return [
  107. [[
  108. 'account' => 'My:Account',
  109. 'otp_type' => 'totp',
  110. ]],
  111. [[
  112. 'account' => 'MyAccount',
  113. 'otp_type' => 'Xotp',
  114. ]],
  115. [[
  116. 'account' => 'MyAccount',
  117. 'otp_type' => null,
  118. ]],
  119. [[
  120. 'account' => 'MyAccount',
  121. 'otp_type' => 'totp',
  122. 'service' => 'My:Service',
  123. ]],
  124. [[
  125. 'account' => 'MyAccount',
  126. 'otp_type' => 'totp',
  127. 'secret' => 'notaBase32String',
  128. ]],
  129. [[
  130. 'account' => 'MyAccount',
  131. 'otp_type' => 'totp',
  132. 'secret' => 123456,
  133. ]],
  134. [[
  135. 'account' => 'MyAccount',
  136. 'otp_type' => 'totp',
  137. 'digits' => 4,
  138. ]],
  139. [[
  140. 'account' => 'MyAccount',
  141. 'otp_type' => 'totp',
  142. 'digits' => 11,
  143. ]],
  144. [[
  145. 'account' => 'MyAccount',
  146. 'otp_type' => 'totp',
  147. 'period' => 0,
  148. ]],
  149. [[
  150. 'account' => 'MyAccount',
  151. 'otp_type' => 'hotp',
  152. 'counter' => -1,
  153. ]],
  154. [[
  155. 'account' => 'MyAccount',
  156. 'otp_type' => 'totp',
  157. 'algorithm' => 'shaX',
  158. ]],
  159. ];
  160. }
  161. }