TwoFAccountStoreRequestTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. 'account' => 'MyAccount',
  92. 'otp_type' => 'totp',
  93. 'algorithm' => 'md5',
  94. 'secret' => 'eee',
  95. ]],
  96. ];
  97. }
  98. /**
  99. * @dataProvider provideInvalidData
  100. */
  101. public function test_invalid_data(array $data) : void
  102. {
  103. $request = new TwoFAccountStoreRequest();
  104. $validator = Validator::make($data, $request->rules());
  105. $this->assertTrue($validator->fails());
  106. }
  107. /**
  108. * Provide invalid data for validation test
  109. */
  110. public function provideInvalidData() : array
  111. {
  112. return [
  113. [[
  114. 'account' => 'My:Account',
  115. 'otp_type' => 'totp',
  116. ]],
  117. [[
  118. 'account' => 'MyAccount',
  119. 'otp_type' => 'Xotp',
  120. ]],
  121. [[
  122. 'account' => 'MyAccount',
  123. 'otp_type' => null,
  124. ]],
  125. [[
  126. 'account' => 'MyAccount',
  127. 'otp_type' => 'totp',
  128. 'service' => 'My:Service',
  129. ]],
  130. [[
  131. 'account' => 'MyAccount',
  132. 'otp_type' => 'totp',
  133. 'secret' => true,
  134. ]],
  135. [[
  136. 'account' => 'MyAccount',
  137. 'otp_type' => 'totp',
  138. 'secret' => 123456,
  139. ]],
  140. [[
  141. 'account' => 'MyAccount',
  142. 'otp_type' => 'totp',
  143. 'secret' => '1.0',
  144. ]],
  145. [[
  146. 'account' => 'MyAccount',
  147. 'otp_type' => 'totp',
  148. 'digits' => 4,
  149. ]],
  150. [[
  151. 'account' => 'MyAccount',
  152. 'otp_type' => 'totp',
  153. 'digits' => 11,
  154. ]],
  155. [[
  156. 'account' => 'MyAccount',
  157. 'otp_type' => 'totp',
  158. 'period' => 0,
  159. ]],
  160. [[
  161. 'account' => 'MyAccount',
  162. 'otp_type' => 'hotp',
  163. 'counter' => -1,
  164. ]],
  165. [[
  166. 'account' => 'MyAccount',
  167. 'otp_type' => 'totp',
  168. 'algorithm' => 'shaX',
  169. ]],
  170. ];
  171. }
  172. }