FixServiceFieldEncryptionTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Tests\Feature\Console;
  3. use App\Console\Commands\Maintenance\FixServiceFieldEncryption;
  4. use App\Facades\Settings;
  5. use App\Models\TwoFAccount;
  6. use App\Models\User;
  7. use Illuminate\Support\Facades\DB;
  8. use PHPUnit\Framework\Attributes\CoversClass;
  9. use PHPUnit\Framework\Attributes\Test;
  10. use Tests\FeatureTestCase;
  11. /**
  12. * FixServiceFieldEncryptionTest test class
  13. */
  14. #[CoversClass(FixServiceFieldEncryption::class)]
  15. class FixServiceFieldEncryptionTest extends FeatureTestCase
  16. {
  17. /**
  18. * The name of the migration that changed the data this command will try to fix
  19. */
  20. protected string $relatedMigration = '2024_08_08_133136_encrypt_twofaccount_service_field';
  21. /**
  22. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  23. */
  24. protected $user;
  25. /**
  26. * @var string
  27. */
  28. protected $command = '2fauth:fix-service-encryption';
  29. protected function setUp() : void
  30. {
  31. parent::setUp();
  32. $this->user = User::factory()->create();
  33. }
  34. #[Test]
  35. public function test_it_does_not_run_if_migration_has_not_been_run()
  36. {
  37. DB::table('migrations')->where('migration', $this->relatedMigration)->delete();
  38. $this->artisan($this->command)
  39. ->assertFailed();
  40. }
  41. #[Test]
  42. public function test_it_does_not_run_if_encryption_is_off()
  43. {
  44. Settings::set('useEncryption', false);
  45. $this->artisan($this->command)
  46. ->assertFailed();
  47. }
  48. #[Test]
  49. public function test_it_tells_the_field_is_fully_encrypted_when_it_is()
  50. {
  51. TwoFAccount::factory()->for($this->user)->count(3)->create();
  52. Settings::set('useEncryption', true);
  53. $this->artisan($this->command)
  54. ->expectsOutputToContain('The Service field is fully encrypted.')
  55. ->assertSuccessful();
  56. }
  57. #[Test]
  58. public function test_it_encrypts_the_field_of_all_records()
  59. {
  60. TwoFAccount::factory()->for($this->user)->count(3)->create();
  61. $expectedServiceName = 'unencrypted_text';
  62. Settings::set('useEncryption', true);
  63. DB::table('twofaccounts')->update(['service' => $expectedServiceName]);
  64. $twofaccounts = TwoFAccount::all();
  65. foreach ($twofaccounts as $twofaccount) {
  66. $this->assertEquals(__('errors.indecipherable'), $twofaccount->service);
  67. }
  68. $this->artisan($this->command)
  69. ->expectsConfirmation('Do you want to fix encryption of those records?', 'yes')
  70. ->assertSuccessful();
  71. foreach ($twofaccounts as $twofaccount) {
  72. $twofaccount->refresh();
  73. $this->assertEquals($expectedServiceName, $twofaccount->service);
  74. }
  75. }
  76. #[Test]
  77. public function test_it_does_not_encrypt_the_field_without_confirmation()
  78. {
  79. TwoFAccount::factory()->for($this->user)->count(3)->create();
  80. $expectedServiceName = 'unencrypted_text';
  81. Settings::set('useEncryption', true);
  82. DB::table('twofaccounts')->update(['service' => $expectedServiceName]);
  83. $twofaccounts = TwoFAccount::all();
  84. foreach ($twofaccounts as $twofaccount) {
  85. $this->assertEquals(__('errors.indecipherable'), $twofaccount->service);
  86. }
  87. $this->artisan($this->command)
  88. ->expectsConfirmation('Do you want to fix encryption of those records?', 'no')
  89. ->assertSuccessful();
  90. foreach ($twofaccounts as $twofaccount) {
  91. $twofaccount->refresh();
  92. $this->assertEquals(__('errors.indecipherable'), $twofaccount->service);
  93. }
  94. }
  95. #[Test]
  96. public function test_it_encrypts_the_field_of_invalid_records_only()
  97. {
  98. Settings::set('useEncryption', true);
  99. $expectedServiceName = 'myService';
  100. $twofaccounts = TwoFAccount::factory()->for($this->user)->count(3)->create([
  101. 'service' => $expectedServiceName,
  102. ]);
  103. $testedAccount = $twofaccounts[2];
  104. DB::table('twofaccounts')->where('id', $testedAccount->id)->update(['service' => $expectedServiceName]);
  105. $testedAccount->refresh();
  106. $this->assertEquals($expectedServiceName, $twofaccounts[0]->service);
  107. $this->assertEquals($expectedServiceName, $twofaccounts[1]->service);
  108. $this->assertEquals(__('errors.indecipherable'), $testedAccount->service);
  109. $this->artisan($this->command)
  110. ->expectsConfirmation('Do you want to fix encryption of those records?', 'yes')
  111. ->assertSuccessful();
  112. $testedAccount->refresh();
  113. $this->assertEquals($expectedServiceName, $twofaccounts[0]->service);
  114. $this->assertEquals($expectedServiceName, $twofaccounts[1]->service);
  115. $this->assertEquals($expectedServiceName, $testedAccount->service);
  116. }
  117. #[Test]
  118. public function test_it_does_not_encrypt_the_record_if_encryption_is_not_consistent()
  119. {
  120. Settings::set('useEncryption', true);
  121. $expectedServiceName = 'myService';
  122. $twofaccounts = TwoFAccount::factory()->for($this->user)->count(3)->create([
  123. 'service' => $expectedServiceName,
  124. ]);
  125. $testedAccount = $twofaccounts[2];
  126. DB::table('twofaccounts')->where('id', $testedAccount->id)->update(['legacy_uri' => 'indecipherable_payload']);
  127. DB::table('twofaccounts')->where('id', $testedAccount->id)->update(['service' => $expectedServiceName]);
  128. $testedAccount->refresh();
  129. $this->assertEquals($expectedServiceName, $twofaccounts[0]->service);
  130. $this->assertEquals($expectedServiceName, $twofaccounts[1]->service);
  131. $this->assertEquals(__('errors.indecipherable'), $testedAccount->service);
  132. $this->artisan($this->command)
  133. ->expectsConfirmation('Do you want to fix encryption of those records?', 'yes')
  134. ->expectsOutput('1 record could not be fixed, see log above for details.');
  135. $testedAccount->refresh();
  136. $this->assertEquals($expectedServiceName, $twofaccounts[0]->service);
  137. $this->assertEquals($expectedServiceName, $twofaccounts[1]->service);
  138. $this->assertEquals(__('errors.indecipherable'), $testedAccount->service);
  139. }
  140. }