TwoFAccountModelTest.php 4.4 KB

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