. */ namespace SP\Tests\Services; use Defuse\Crypto\Exception\CryptoException; use PHPUnit\DbUnit\DataSet\IDataSet; use SP\Core\Crypt\Crypt; use SP\Services\Account\AccountCryptService; use SP\Services\Account\AccountService; use SP\Services\Crypt\UpdateMasterPassRequest; use SP\Services\ServiceException; use SP\Storage\Database\DatabaseConnectionData; use SP\Tests\DatabaseTestCase; use function SP\Tests\setupContext; /** * Class AccountCryptServiceTest * * @package SP\Tests\Services */ class AccountCryptServiceTest extends DatabaseTestCase { const CURRENT_MASTERPASS = '12345678900'; const NEW_MASTERPASS = '00123456789'; const CURRENT_HASH = '$2y$10$p/x8ECCP1Lz/SjbGQy.6/OJ/.OLICBLGkXPiVkaA5Kf1.1HvRQOWG'; /** * @var AccountService */ private static $accountService; /** * @var AccountCryptService */ private static $service; /** * @throws \DI\NotFoundException * @throws \SP\Core\Context\ContextException * @throws \DI\DependencyException */ public static function setUpBeforeClass() { $dic = setupContext(); // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); // Inicializar el servicio self::$service = $dic->get(AccountCryptService::class); self::$accountService = $dic->get(AccountService::class); } /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\ServiceException * @throws \Defuse\Crypto\Exception\CryptoException */ public function testUpdateMasterPassword() { $request = new UpdateMasterPassRequest(self::CURRENT_MASTERPASS, self::NEW_MASTERPASS, self::CURRENT_HASH); self::$service->updateMasterPassword($request); $account = self::$accountService->getPasswordForId(1); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals('&¿\'f!i$XwSwc', $pass); $account = self::$accountService->getPasswordForId(2); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals('&¿\'f!i$XwSwc', $pass); $request = new UpdateMasterPassRequest('12345', self::NEW_MASTERPASS, self::CURRENT_HASH); self::$service->updateMasterPassword($request); $account = self::$accountService->getPasswordForId(1); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals('&¿\'f!i$XwSwc', $pass); } /** * @throws CryptoException * @throws ServiceException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function testUpdateHistoryMasterPassword() { $request = new UpdateMasterPassRequest(self::CURRENT_MASTERPASS, self::NEW_MASTERPASS, '$2y$10$xtsuN2PUvgSH/0mrfBlsbOActVgCjYcqDqC6L3T9QraNxZC4RXGYa'); self::$service->updateHistoryMasterPassword($request); $account = self::$accountService->getPasswordHistoryForId(3); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals($request->getHash(), $account->getMPassHash()); $this->assertEquals('_{/uHL\>\'Oj0', $pass); $account = self::$accountService->getPasswordHistoryForId(4); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals($request->getHash(), $account->getMPassHash()); $this->assertEquals('-{?^··\mjCgetPasswordHistoryForId(5); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals($request->getHash(), $account->getMPassHash()); $this->assertEquals('-{?^··\mjCgetPasswordHistoryForId(6); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals($request->getHash(), $account->getMPassHash()); $this->assertEquals('-{?^··\mjCgetPasswordHistoryForId(7); $pass = Crypt::decrypt($account->getPass(), $account->getKey(), self::NEW_MASTERPASS); $this->assertEquals($request->getHash(), $account->getMPassHash()); $this->assertEquals('-{?^··\mjCcreateMySQLXMLDataSet(RESOURCE_DIR . DIRECTORY_SEPARATOR . 'datasets' . DIRECTORY_SEPARATOR . 'syspass_accountCrypt.xml'); } }