TwoFAccountStoreRequestTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Tests\Api\v1\Requests;
  3. use App\Api\v1\Requests\TwoFAccountStoreRequest;
  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. * TwoFAccountStoreRequestTest test class
  14. */
  15. #[CoversClass(TwoFAccountStoreRequest::class)]
  16. #[CoversClass(IsBase32Encoded::class)]
  17. class TwoFAccountStoreRequestTest 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 TwoFAccountStoreRequest;
  27. $this->assertTrue($request->authorize());
  28. }
  29. #[Test]
  30. #[DataProvider('provideValidData')]
  31. public function test_valid_data(array $data) : void
  32. {
  33. $request = new TwoFAccountStoreRequest;
  34. $request->merge($data);
  35. $validator = Validator::make($data, $request->rules());
  36. $this->assertFalse($validator->fails());
  37. }
  38. /**
  39. * Provide Valid data for validation test
  40. */
  41. public static function provideValidData() : array
  42. {
  43. return [
  44. [[
  45. 'service' => 'MyService',
  46. 'account' => 'MyAccount',
  47. 'icon' => 'icon.png',
  48. 'otp_type' => 'totp',
  49. 'secret' => 'A4GRFHZVRBGY7UIW',
  50. 'digits' => 6,
  51. 'algorithm' => 'sha1',
  52. 'period' => 30,
  53. ]],
  54. [[
  55. 'service' => 'MyService',
  56. 'account' => 'MyAccount',
  57. 'icon' => 'icon.png',
  58. 'otp_type' => 'hotp',
  59. 'secret' => 'A4GRFHZVRBGY7UIW',
  60. 'digits' => 8,
  61. 'algorithm' => 'sha1',
  62. 'counter' => 10,
  63. ]],
  64. [[
  65. 'service' => null,
  66. 'account' => 'MyAccount',
  67. 'icon' => null,
  68. 'otp_type' => 'hotp',
  69. 'secret' => 'A4GRFHZVRBGY7UIW',
  70. 'digits' => null,
  71. 'algorithm' => null,
  72. 'counter' => null,
  73. ]],
  74. [[
  75. 'account' => 'MyAccount',
  76. 'otp_type' => 'totp',
  77. ]],
  78. [[
  79. 'account' => 'MyAccount',
  80. 'otp_type' => 'totp',
  81. 'algorithm' => 'sha256',
  82. ]],
  83. [[
  84. 'account' => 'MyAccount',
  85. 'otp_type' => 'totp',
  86. 'algorithm' => 'sha512',
  87. ]],
  88. [[
  89. 'account' => 'MyAccount',
  90. 'otp_type' => 'totp',
  91. 'algorithm' => 'md5',
  92. ]],
  93. [[
  94. 'account' => 'MyAccount',
  95. 'otp_type' => 'totp',
  96. 'algorithm' => 'md5',
  97. 'secret' => 'eee',
  98. ]],
  99. ];
  100. }
  101. #[Test]
  102. #[DataProvider('provideInvalidData')]
  103. public function test_invalid_data(array $data) : void
  104. {
  105. $request = new TwoFAccountStoreRequest;
  106. $request->merge($data);
  107. $validator = Validator::make($data, $request->rules());
  108. $this->assertTrue($validator->fails());
  109. }
  110. /**
  111. * Provide invalid data for validation test
  112. */
  113. public static function provideInvalidData() : array
  114. {
  115. return [
  116. [[
  117. 'account' => 'My:Account',
  118. 'otp_type' => 'totp',
  119. ]],
  120. [[
  121. 'account' => 'MyAccount',
  122. 'otp_type' => 'Xotp',
  123. ]],
  124. [[
  125. 'account' => 'MyAccount',
  126. 'otp_type' => null,
  127. ]],
  128. [[
  129. 'account' => 'MyAccount',
  130. 'otp_type' => 'totp',
  131. 'service' => 'My:Service',
  132. ]],
  133. [[
  134. 'account' => 'MyAccount',
  135. 'otp_type' => 'totp',
  136. 'secret' => true,
  137. ]],
  138. [[
  139. 'account' => 'MyAccount',
  140. 'otp_type' => 'totp',
  141. 'secret' => 123456,
  142. ]],
  143. [[
  144. 'account' => 'MyAccount',
  145. 'otp_type' => 'totp',
  146. 'secret' => '1.0',
  147. ]],
  148. [[
  149. 'account' => 'MyAccount',
  150. 'otp_type' => 'totp',
  151. 'digits' => 4,
  152. ]],
  153. [[
  154. 'account' => 'MyAccount',
  155. 'otp_type' => 'totp',
  156. 'digits' => 11,
  157. ]],
  158. [[
  159. 'account' => 'MyAccount',
  160. 'otp_type' => 'totp',
  161. 'period' => 0,
  162. ]],
  163. [[
  164. 'account' => 'MyAccount',
  165. 'otp_type' => 'hotp',
  166. 'counter' => -1,
  167. ]],
  168. [[
  169. 'account' => 'MyAccount',
  170. 'otp_type' => 'totp',
  171. 'algorithm' => 'shaX',
  172. ]],
  173. ];
  174. }
  175. }