TwoFAccountUpdateRequestTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Api\v1\Requests\TwoFAccountUpdateRequest;
  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\TwoFAccountUpdateRequest
  10. * @covers \App\Rules\IsBase32Encoded
  11. */
  12. class TwoFAccountUpdateRequestTest 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 TwoFAccountUpdateRequest();
  24. $this->assertTrue($request->authorize());
  25. }
  26. /**
  27. * @dataProvider provideValidData
  28. */
  29. public function test_valid_data(array $data): void
  30. {
  31. $request = new TwoFAccountUpdateRequest();
  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' => 10,
  68. 'algorithm' => 'sha1',
  69. 'period' => null,
  70. 'counter' => 15,
  71. ]],
  72. ];
  73. }
  74. /**
  75. * @dataProvider provideInvalidData
  76. */
  77. public function test_invalid_data(array $data): void
  78. {
  79. $request = new TwoFAccountUpdateRequest();
  80. $validator = Validator::make($data, $request->rules());
  81. $this->assertTrue($validator->fails());
  82. }
  83. /**
  84. * Provide invalid data for validation test
  85. */
  86. public function provideInvalidData(): array
  87. {
  88. return [
  89. [[
  90. 'service' => null,
  91. 'account' => 'My:Account',
  92. 'icon' => null,
  93. 'otp_type' => 'hotp',
  94. 'secret' => 'A4GRFHZVRBGY7UIW',
  95. 'digits' => 6,
  96. 'algorithm' => 'sha1',
  97. 'period' => null,
  98. 'counter' => 15,
  99. ]],
  100. [[
  101. 'service' => 'My:Service',
  102. 'account' => 'MyAccount',
  103. 'icon' => null,
  104. 'otp_type' => 'hotp',
  105. 'secret' => 'A4GRFHZVRBGY7UIW',
  106. 'digits' => 6,
  107. 'algorithm' => 'sha1',
  108. 'period' => null,
  109. 'counter' => 15,
  110. ]],
  111. [[
  112. 'service' => null,
  113. 'account' => 'My:Account',
  114. 'icon' => null,
  115. 'otp_type' => 'Xotp',
  116. 'secret' => 'A4GRFHZVRBGY7UIW',
  117. 'digits' => 6,
  118. 'algorithm' => 'sha1',
  119. 'period' => null,
  120. 'counter' => 15,
  121. ]],
  122. [[
  123. 'service' => null,
  124. 'account' => 'MyAccount',
  125. 'icon' => null,
  126. 'otp_type' => 'hotp',
  127. 'secret' => 'notaBase32String',
  128. 'digits' => 6,
  129. 'algorithm' => 'sha1',
  130. 'period' => null,
  131. 'counter' => 15,
  132. ]],
  133. [[
  134. 'service' => 'MyService',
  135. 'account' => 'MyAccount',
  136. 'icon' => null,
  137. 'otp_type' => 'totp',
  138. 'secret' => 'A4GRFHZVRBGY7UIW',
  139. 'digits' => 4,
  140. 'algorithm' => 'sha1',
  141. 'period' => null,
  142. 'counter' => 15,
  143. ]],
  144. [[
  145. 'service' => 'MyService',
  146. 'account' => 'MyAccount',
  147. 'icon' => null,
  148. 'otp_type' => 'totp',
  149. 'secret' => 'A4GRFHZVRBGY7UIW',
  150. 'digits' => 11,
  151. 'algorithm' => 'sha1',
  152. 'period' => null,
  153. 'counter' => 15,
  154. ]],
  155. [[
  156. 'service' => 'MyService',
  157. 'account' => 'MyAccount',
  158. 'icon' => null,
  159. 'otp_type' => 'totp',
  160. 'secret' => 'A4GRFHZVRBGY7UIW',
  161. 'digits' => 6,
  162. 'algorithm' => 'Xsha1',
  163. 'period' => null,
  164. 'counter' => 15,
  165. ]],
  166. [[
  167. 'service' => 'MyService',
  168. 'account' => 'MyAccount',
  169. 'icon' => null,
  170. 'otp_type' => 'totp',
  171. 'secret' => 'A4GRFHZVRBGY7UIW',
  172. 'digits' => 6,
  173. 'algorithm' => 'sha1',
  174. 'period' => 0,
  175. 'counter' => 15,
  176. ]],
  177. [[
  178. 'service' => 'MyService',
  179. 'account' => 'MyAccount',
  180. 'icon' => null,
  181. 'otp_type' => 'totp',
  182. 'secret' => 'A4GRFHZVRBGY7UIW',
  183. 'digits' => 4,
  184. 'algorithm' => 'sha1',
  185. 'period' => null,
  186. 'counter' => -1,
  187. ]],
  188. [[
  189. 'service' => 'MyService',
  190. 'account' => 'MyAccount',
  191. 'icon' => null,
  192. 'otp_type' => 'totp',
  193. 'secret' => 'A4GRFHZVRBGY7UIW',
  194. 'digits' => null,
  195. 'algorithm' => 'sha1',
  196. 'period' => null,
  197. 'counter' => 15,
  198. ]],
  199. [[
  200. 'service' => 'MyService',
  201. 'account' => 'MyAccount',
  202. 'icon' => null,
  203. 'otp_type' => 'totp',
  204. 'secret' => 'A4GRFHZVRBGY7UIW',
  205. 'digits' => 6,
  206. 'algorithm' => null,
  207. 'period' => null,
  208. 'counter' => 15,
  209. ]],
  210. ];
  211. }
  212. }