TwoFAccountModelTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Events\TwoFAccountDeleted;
  4. use App\Helpers\Helpers;
  5. use App\Models\TwoFAccount;
  6. use App\Services\SettingService;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Support\Facades\Crypt;
  9. use Mockery\MockInterface;
  10. use PHPUnit\Framework\Attributes\CoversClass;
  11. use PHPUnit\Framework\Attributes\DataProvider;
  12. use PHPUnit\Framework\Attributes\PreserveGlobalState;
  13. use PHPUnit\Framework\Attributes\RequiresPhp;
  14. use PHPUnit\Framework\Attributes\RunInSeparateProcess;
  15. use Tests\ModelTestCase;
  16. /**
  17. * TwoFAccountModelTest test class
  18. */
  19. #[CoversClass(TwoFAccount::class)]
  20. class TwoFAccountModelTest extends ModelTestCase
  21. {
  22. /**
  23. * @test
  24. */
  25. public function test_model_configuration()
  26. {
  27. $this->runConfigurationAssertions(
  28. new TwoFAccount(),
  29. [],
  30. [],
  31. ['*'],
  32. [],
  33. [
  34. 'id' => 'int',
  35. 'user_id' => 'integer',
  36. ],
  37. ['deleted' => TwoFAccountDeleted::class],
  38. ['created_at', 'updated_at'],
  39. \Illuminate\Database\Eloquent\Collection::class,
  40. 'twofaccounts',
  41. 'id',
  42. true
  43. );
  44. }
  45. /**
  46. * @test
  47. */
  48. #[DataProvider('provideSensitiveAttributes')]
  49. public function test_sensitive_attributes_are_stored_encrypted(string $attribute)
  50. {
  51. $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
  52. $settingService->shouldReceive('get')
  53. ->with('useEncryption')
  54. ->andReturn(true);
  55. });
  56. $twofaccount = TwoFAccount::factory()->make([
  57. $attribute => 'STRING==',
  58. ]);
  59. $this->assertEquals('STRING==', Crypt::decryptString($twofaccount->getAttributes()[$attribute]));
  60. }
  61. /**
  62. * Provide attributes to test for encryption
  63. */
  64. public static function provideSensitiveAttributes() : array
  65. {
  66. return [
  67. [
  68. 'legacy_uri',
  69. ],
  70. [
  71. 'secret',
  72. ],
  73. [
  74. 'account',
  75. ],
  76. ];
  77. }
  78. /**
  79. * @test
  80. */
  81. #[DataProvider('provideSensitiveAttributes')]
  82. public function test_sensitive_attributes_are_returned_clear(string $attribute)
  83. {
  84. $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
  85. $settingService->shouldReceive('get')
  86. ->with('useEncryption')
  87. ->andReturn(false);
  88. });
  89. $twofaccount = TwoFAccount::factory()->make();
  90. $this->assertEquals($twofaccount->getAttributes()[$attribute], $twofaccount->$attribute);
  91. }
  92. /**
  93. * @test
  94. */
  95. #[DataProvider('provideSensitiveAttributes')]
  96. public function test_indecipherable_attributes_returns_masked_value(string $attribute)
  97. {
  98. $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
  99. $settingService->shouldReceive('get')
  100. ->with('useEncryption')
  101. ->andReturn(true);
  102. });
  103. Crypt::shouldReceive('encryptString')
  104. ->andReturn('indecipherableString');
  105. $twofaccount = TwoFAccount::factory()->make();
  106. $this->assertEquals(__('errors.indecipherable'), $twofaccount->$attribute);
  107. }
  108. /**
  109. * @test
  110. */
  111. #[RunInSeparateProcess]
  112. #[PreserveGlobalState(false)]
  113. #[RequiresPhp('< 8.3.0')]
  114. public function test_secret_is_uppercased_and_padded_at_setup()
  115. {
  116. $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
  117. $settingService->shouldReceive('get')
  118. ->with('useEncryption')
  119. ->andReturn(false);
  120. });
  121. $helpers = $this->mock('alias:' . Helpers::class, function (MockInterface $helpers) {
  122. $helpers->shouldReceive('PadToBase32Format')
  123. ->andReturn('YYYY====');
  124. });
  125. $twofaccount = TwoFAccount::factory()->make([
  126. 'secret' => 'yyyy',
  127. ]);
  128. $this->assertEquals('YYYY====', $twofaccount->secret);
  129. }
  130. /**
  131. * @test
  132. */
  133. public function test_user_relation()
  134. {
  135. $model = new TwoFAccount();
  136. $relation = $model->user();
  137. $this->assertInstanceOf(BelongsTo::class, $relation);
  138. $this->assertEquals('user_id', $relation->getForeignKeyName());
  139. }
  140. }