user = User::factory()->create(); } #[Test] public function test_it_does_not_run_if_migration_has_not_been_run() { DB::table('migrations')->where('migration', $this->relatedMigration)->delete(); $this->artisan($this->command) ->assertFailed(); } #[Test] public function test_it_does_not_run_if_encryption_is_off() { Settings::set('useEncryption', false); $this->artisan($this->command) ->assertFailed(); } #[Test] public function test_it_tells_the_field_is_fully_encrypted_when_it_is() { TwoFAccount::factory()->for($this->user)->count(3)->create(); Settings::set('useEncryption', true); $this->artisan($this->command) ->expectsOutputToContain('The Service field is fully encrypted.') ->assertSuccessful(); } #[Test] public function test_it_encrypts_the_field_of_all_records() { TwoFAccount::factory()->for($this->user)->count(3)->create(); $expectedServiceName = 'unencrypted_text'; Settings::set('useEncryption', true); DB::table('twofaccounts')->update(['service' => $expectedServiceName]); $twofaccounts = TwoFAccount::all(); foreach ($twofaccounts as $twofaccount) { $this->assertEquals(__('errors.indecipherable'), $twofaccount->service); } $this->artisan($this->command) ->expectsConfirmation('Do you want to fix encryption of those records?', 'yes') ->assertSuccessful(); foreach ($twofaccounts as $twofaccount) { $twofaccount->refresh(); $this->assertEquals($expectedServiceName, $twofaccount->service); } } #[Test] public function test_it_does_not_encrypt_the_field_without_confirmation() { TwoFAccount::factory()->for($this->user)->count(3)->create(); $expectedServiceName = 'unencrypted_text'; Settings::set('useEncryption', true); DB::table('twofaccounts')->update(['service' => $expectedServiceName]); $twofaccounts = TwoFAccount::all(); foreach ($twofaccounts as $twofaccount) { $this->assertEquals(__('errors.indecipherable'), $twofaccount->service); } $this->artisan($this->command) ->expectsConfirmation('Do you want to fix encryption of those records?', 'no') ->assertSuccessful(); foreach ($twofaccounts as $twofaccount) { $twofaccount->refresh(); $this->assertEquals(__('errors.indecipherable'), $twofaccount->service); } } #[Test] public function test_it_encrypts_the_field_of_invalid_records_only() { Settings::set('useEncryption', true); $expectedServiceName = 'myService'; $twofaccounts = TwoFAccount::factory()->for($this->user)->count(3)->create([ 'service' => $expectedServiceName ]); $testedAccount = $twofaccounts[2]; DB::table('twofaccounts')->where('id', $testedAccount->id)->update(['service' => $expectedServiceName]); $testedAccount->refresh(); $this->assertEquals($expectedServiceName, $twofaccounts[0]->service); $this->assertEquals($expectedServiceName, $twofaccounts[1]->service); $this->assertEquals(__('errors.indecipherable'), $testedAccount->service); $this->artisan($this->command) ->expectsConfirmation('Do you want to fix encryption of those records?', 'yes') ->assertSuccessful(); $testedAccount->refresh(); $this->assertEquals($expectedServiceName, $twofaccounts[0]->service); $this->assertEquals($expectedServiceName, $twofaccounts[1]->service); $this->assertEquals($expectedServiceName, $testedAccount->service); } #[Test] public function test_it_does_not_encrypt_the_record_if_encryption_is_not_consistent() { Settings::set('useEncryption', true); $expectedServiceName = 'myService'; $twofaccounts = TwoFAccount::factory()->for($this->user)->count(3)->create([ 'service' => $expectedServiceName ]); $testedAccount = $twofaccounts[2]; DB::table('twofaccounts')->where('id', $testedAccount->id)->update(['legacy_uri' => 'indecipherable_payload']); DB::table('twofaccounts')->where('id', $testedAccount->id)->update(['service' => $expectedServiceName]); $testedAccount->refresh(); $this->assertEquals($expectedServiceName, $twofaccounts[0]->service); $this->assertEquals($expectedServiceName, $twofaccounts[1]->service); $this->assertEquals(__('errors.indecipherable'), $testedAccount->service); $this->artisan($this->command) ->expectsConfirmation('Do you want to fix encryption of those records?', 'yes') ->expectsOutput('1 record could not be fixed, see log above for details.'); $testedAccount->refresh(); $this->assertEquals($expectedServiceName, $twofaccounts[0]->service); $this->assertEquals($expectedServiceName, $twofaccounts[1]->service); $this->assertEquals(__('errors.indecipherable'), $testedAccount->service); } }