TwoFAccountUpdateRequestTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Api\v1\Requests\TwoFAccountUpdateRequest;
  4. use App\Rules\IsBase32Encoded;
  5. use Illuminate\Foundation\Testing\WithoutMiddleware;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Validator;
  8. use PHPUnit\Framework\Attributes\CoversClass;
  9. use PHPUnit\Framework\Attributes\DataProvider;
  10. use PHPUnit\Framework\Attributes\Test;
  11. use Tests\TestCase;
  12. /**
  13. * TwoFAccountUpdateRequestTest test class
  14. */
  15. #[CoversClass(TwoFAccountUpdateRequest::class)]
  16. #[CoversClass(IsBase32Encoded::class)]
  17. class TwoFAccountUpdateRequestTest extends TestCase
  18. {
  19. use WithoutMiddleware;
  20. #[Test]
  21. public function test_user_is_authorized()
  22. {
  23. Auth::shouldReceive('check')
  24. ->once()
  25. ->andReturn(true);
  26. $request = new TwoFAccountUpdateRequest;
  27. $this->assertTrue($request->authorize());
  28. }
  29. #[Test]
  30. #[DataProvider('provideValidData')]
  31. public function test_valid_data(array $data) : void
  32. {
  33. $request = new TwoFAccountUpdateRequest;
  34. $validator = Validator::make($data, $request->rules());
  35. $this->assertFalse($validator->fails());
  36. }
  37. /**
  38. * Provide Valid data for validation test
  39. */
  40. public static function provideValidData() : array
  41. {
  42. return [
  43. [[
  44. 'service' => 'MyService',
  45. 'account' => 'MyAccount',
  46. 'icon' => 'icon.png',
  47. 'otp_type' => 'totp',
  48. 'secret' => 'A4GRFHZVRBGY7UIW',
  49. 'digits' => 6,
  50. 'algorithm' => 'sha1',
  51. 'period' => 30,
  52. ]],
  53. [[
  54. 'service' => 'MyService',
  55. 'account' => 'MyAccount',
  56. 'icon' => 'icon.png',
  57. 'otp_type' => 'hotp',
  58. 'secret' => 'A4GRFHZVRBGY7UIW',
  59. 'digits' => 8,
  60. 'algorithm' => 'sha1',
  61. 'counter' => 10,
  62. ]],
  63. [[
  64. 'service' => null,
  65. 'account' => 'MyAccount',
  66. 'icon' => null,
  67. 'otp_type' => 'hotp',
  68. 'secret' => 'eeee',
  69. 'digits' => 10,
  70. 'algorithm' => 'sha1',
  71. 'period' => null,
  72. 'counter' => 15,
  73. ]],
  74. ];
  75. }
  76. #[Test]
  77. #[DataProvider('provideInvalidData')]
  78. public function test_invalid_data(array $data) : void
  79. {
  80. $request = new TwoFAccountUpdateRequest;
  81. $validator = Validator::make($data, $request->rules());
  82. $this->assertTrue($validator->fails());
  83. }
  84. /**
  85. * Provide invalid data for validation test
  86. */
  87. public static function provideInvalidData() : array
  88. {
  89. return [
  90. [[
  91. 'service' => null,
  92. 'account' => 'My:Account',
  93. 'icon' => null,
  94. 'otp_type' => 'hotp',
  95. 'secret' => 'A4GRFHZVRBGY7UIW',
  96. 'digits' => 6,
  97. 'algorithm' => 'sha1',
  98. 'period' => null,
  99. 'counter' => 15,
  100. ]],
  101. [[
  102. 'service' => 'My:Service',
  103. 'account' => 'MyAccount',
  104. 'icon' => null,
  105. 'otp_type' => 'hotp',
  106. 'secret' => 'A4GRFHZVRBGY7UIW',
  107. 'digits' => 6,
  108. 'algorithm' => 'sha1',
  109. 'period' => null,
  110. 'counter' => 15,
  111. ]],
  112. [[
  113. 'service' => null,
  114. 'account' => 'My:Account',
  115. 'icon' => null,
  116. 'otp_type' => 'Xotp',
  117. 'secret' => 'A4GRFHZVRBGY7UIW',
  118. 'digits' => 6,
  119. 'algorithm' => 'sha1',
  120. 'period' => null,
  121. 'counter' => 15,
  122. ]],
  123. [[
  124. 'service' => null,
  125. 'account' => 'MyAccount',
  126. 'icon' => null,
  127. 'otp_type' => 'hotp',
  128. 'secret' => 1000,
  129. 'digits' => 6,
  130. 'algorithm' => 'sha1',
  131. 'period' => null,
  132. 'counter' => 15,
  133. ]],
  134. [[
  135. 'service' => 'MyService',
  136. 'account' => 'MyAccount',
  137. 'icon' => null,
  138. 'otp_type' => 'totp',
  139. 'secret' => 'A4GRFHZVRBGY7UIW',
  140. 'digits' => 4,
  141. 'algorithm' => 'sha1',
  142. 'period' => null,
  143. 'counter' => 15,
  144. ]],
  145. [[
  146. 'service' => 'MyService',
  147. 'account' => 'MyAccount',
  148. 'icon' => null,
  149. 'otp_type' => 'totp',
  150. 'secret' => 'A4GRFHZVRBGY7UIW',
  151. 'digits' => 11,
  152. 'algorithm' => 'sha1',
  153. 'period' => null,
  154. 'counter' => 15,
  155. ]],
  156. [[
  157. 'service' => 'MyService',
  158. 'account' => 'MyAccount',
  159. 'icon' => null,
  160. 'otp_type' => 'totp',
  161. 'secret' => 'A4GRFHZVRBGY7UIW',
  162. 'digits' => 6,
  163. 'algorithm' => 'Xsha1',
  164. 'period' => null,
  165. 'counter' => 15,
  166. ]],
  167. [[
  168. 'service' => 'MyService',
  169. 'account' => 'MyAccount',
  170. 'icon' => null,
  171. 'otp_type' => 'totp',
  172. 'secret' => 'A4GRFHZVRBGY7UIW',
  173. 'digits' => 6,
  174. 'algorithm' => 'sha1',
  175. 'period' => 0,
  176. 'counter' => 15,
  177. ]],
  178. [[
  179. 'service' => 'MyService',
  180. 'account' => 'MyAccount',
  181. 'icon' => null,
  182. 'otp_type' => 'totp',
  183. 'secret' => 'A4GRFHZVRBGY7UIW',
  184. 'digits' => 4,
  185. 'algorithm' => 'sha1',
  186. 'period' => null,
  187. 'counter' => -1,
  188. ]],
  189. [[
  190. 'service' => 'MyService',
  191. 'account' => 'MyAccount',
  192. 'icon' => null,
  193. 'otp_type' => 'totp',
  194. 'secret' => 'A4GRFHZVRBGY7UIW',
  195. 'digits' => null,
  196. 'algorithm' => 'sha1',
  197. 'period' => null,
  198. 'counter' => 15,
  199. ]],
  200. [[
  201. 'service' => 'MyService',
  202. 'account' => 'MyAccount',
  203. 'icon' => null,
  204. 'otp_type' => 'totp',
  205. 'secret' => 'A4GRFHZVRBGY7UIW',
  206. 'digits' => 6,
  207. 'algorithm' => null,
  208. 'period' => null,
  209. 'counter' => 15,
  210. ]],
  211. ];
  212. }
  213. }