2019-06-10 13:14:00 -05:00
|
|
|
<?php
|
2019-10-01 23:45:03 -05:00
|
|
|
/**
|
|
|
|
* DecryptDatabaseTest.php
|
2020-02-16 06:59:55 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-10-01 23:45:03 -05:00
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 05:09:03 -05:00
|
|
|
declare(strict_types=1);
|
2019-06-10 13:14:00 -05:00
|
|
|
|
|
|
|
namespace Tests\Unit\Console\Commands;
|
|
|
|
|
|
|
|
|
|
|
|
use Crypt;
|
|
|
|
use FireflyConfig;
|
|
|
|
use FireflyIII\Models\Account;
|
|
|
|
use FireflyIII\Models\Configuration;
|
|
|
|
use Log;
|
|
|
|
use Mockery;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DecryptDatabaseTest
|
2019-08-17 03:48:28 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
|
2019-06-10 13:14:00 -05:00
|
|
|
*/
|
|
|
|
class DecryptDatabaseTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Log::info(sprintf('Now in %s.', get_class($this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Console\Commands\DecryptDatabase
|
|
|
|
*/
|
|
|
|
public function testHandle(): void
|
|
|
|
{
|
|
|
|
// create encrypted account:
|
|
|
|
$name = 'Encrypted name';
|
|
|
|
$iban = 'HR1723600001101234565';
|
|
|
|
$account = Account::create(
|
|
|
|
[
|
|
|
|
'user_id' => 1,
|
|
|
|
'account_type_id' => 1,
|
|
|
|
'name' => Crypt::encrypt($name),
|
|
|
|
'iban' => Crypt::encrypt($iban),
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
FireflyConfig::shouldReceive('get')->withArgs([Mockery::any(), false])->atLeast()->once()->andReturn(null);
|
|
|
|
FireflyConfig::shouldReceive('set')->withArgs([Mockery::any(), true])->atLeast()->once();
|
|
|
|
|
|
|
|
$this->artisan('firefly-iii:decrypt-all')
|
|
|
|
->expectsOutput('Done!')
|
|
|
|
->assertExitCode(0);
|
|
|
|
|
|
|
|
$this->assertCount(1, Account::where('id', $account->id)->where('name', $name)->get());
|
|
|
|
$this->assertCount(1, Account::where('id', $account->id)->where('iban', $iban)->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \FireflyIII\Console\Commands\DecryptDatabase
|
|
|
|
*/
|
|
|
|
public function testHandleDecrypted(): void
|
|
|
|
{
|
|
|
|
// create encrypted account:
|
|
|
|
$name = 'Encrypted name';
|
|
|
|
$iban = 'HR1723600001101234565';
|
|
|
|
$encryptedName = Crypt::encrypt($name);
|
|
|
|
$encryptedIban = Crypt::encrypt($iban);
|
|
|
|
$account = Account::create(
|
|
|
|
[
|
|
|
|
'user_id' => 1,
|
|
|
|
'account_type_id' => 1,
|
|
|
|
'name' => $encryptedName,
|
|
|
|
'iban' => $encryptedIban,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// pretend its not yet decrypted.
|
|
|
|
$true = new Configuration;
|
|
|
|
$true->data = true;
|
|
|
|
|
|
|
|
FireflyConfig::shouldReceive('get')->withArgs([Mockery::any(), false])->atLeast()->once()->andReturn($true);
|
|
|
|
|
|
|
|
$this->artisan('firefly-iii:decrypt-all')
|
|
|
|
->expectsOutput('Done!')
|
|
|
|
->assertExitCode(0);
|
|
|
|
|
|
|
|
$this->assertCount(1, Account::where('id', $account->id)->where('name', $encryptedName)->get());
|
|
|
|
$this->assertCount(1, Account::where('id', $account->id)->where('iban', $encryptedIban)->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to decrypt data that isn't actually encrypted.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Console\Commands\DecryptDatabase
|
|
|
|
*/
|
|
|
|
public function testHandleNotEncrypted(): void
|
|
|
|
{
|
|
|
|
// create encrypted account:
|
2019-06-13 11:07:49 -05:00
|
|
|
$name = 'Encrypted name';
|
|
|
|
$iban = 'HR1723600001101234565';
|
|
|
|
$account = Account::create(
|
2019-06-10 13:14:00 -05:00
|
|
|
[
|
|
|
|
'user_id' => 1,
|
|
|
|
'account_type_id' => 1,
|
|
|
|
'name' => $name,
|
|
|
|
'iban' => $iban,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// pretend its not yet decrypted.
|
|
|
|
$true = new Configuration;
|
|
|
|
$true->data = true;
|
|
|
|
|
|
|
|
FireflyConfig::shouldReceive('get')->withArgs([Mockery::any(), false])->atLeast()->once()->andReturn($true);
|
|
|
|
|
|
|
|
$this->artisan('firefly-iii:decrypt-all')
|
|
|
|
->expectsOutput('Done!')
|
|
|
|
->assertExitCode(0);
|
|
|
|
|
|
|
|
$this->assertCount(1, Account::where('id', $account->id)->where('name', $name)->get());
|
|
|
|
$this->assertCount(1, Account::where('id', $account->id)->where('iban', $iban)->get());
|
|
|
|
}
|
|
|
|
|
2019-08-17 05:09:03 -05:00
|
|
|
}
|