mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-04 07:34:09 +01:00
210 lines
6.6 KiB
PHP
210 lines
6.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
/*
|
|
* sysPass
|
|
*
|
|
* @author nuxsmin
|
|
* @link https://syspass.org
|
|
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
|
|
*
|
|
* This file is part of sysPass.
|
|
*
|
|
* sysPass is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* sysPass 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace SP\Tests\Domain\CustomField\Services;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
use PHPUnit\Framework\Constraint\Callback;
|
|
use PHPUnit\Framework\MockObject\Exception;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use RuntimeException;
|
|
use SP\Domain\Common\Services\ServiceException;
|
|
use SP\Domain\Core\Crypt\CryptInterface;
|
|
use SP\Domain\Crypt\Dtos\UpdateMasterPassRequest;
|
|
use SP\Domain\CustomField\Models\CustomFieldData as CustomFieldDataModel;
|
|
use SP\Domain\CustomField\Ports\CustomFieldDataService;
|
|
use SP\Domain\CustomField\Services\CustomFieldCrypt;
|
|
use SP\Domain\Task\Ports\TaskInterface;
|
|
use SP\Tests\Generators\CustomFieldDataGenerator;
|
|
use SP\Tests\UnitaryTestCase;
|
|
|
|
/**
|
|
* Class CustomFieldCryptTest
|
|
*
|
|
*/
|
|
#[Group('unitary')]
|
|
class CustomFieldCryptTest extends UnitaryTestCase
|
|
{
|
|
|
|
private CustomFieldDataService|MockObject $customFieldService;
|
|
private CryptInterface|MockObject $crypt;
|
|
private CustomFieldCrypt $customFieldCrypt;
|
|
|
|
/**
|
|
* @throws ServiceException
|
|
*/
|
|
public function testUpdateMasterPassword()
|
|
{
|
|
$hash = self::$faker->sha1;
|
|
$request = new UpdateMasterPassRequest('secret', 'test_secret', $hash);
|
|
$customFieldData = CustomFieldDataGenerator::factory()->buildCustomFieldData();
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('getAllEncrypted')
|
|
->willReturn([$customFieldData]);
|
|
|
|
$data = self::$faker->text();
|
|
|
|
$this->crypt
|
|
->expects(self::once())
|
|
->method('decrypt')
|
|
->with($customFieldData->getData(), $customFieldData->getKey(), $request->getCurrentMasterPass())
|
|
->willReturn($data);
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('updateMasterPass')
|
|
->with(
|
|
new Callback(static function (CustomFieldDataModel $customFieldData) use ($data) {
|
|
return $customFieldData->getData() === $data;
|
|
}),
|
|
$request->getNewMasterPass()
|
|
);
|
|
|
|
$this->customFieldCrypt->updateMasterPassword($request);
|
|
}
|
|
|
|
/**
|
|
* @throws ServiceException
|
|
*/
|
|
public function testUpdateMasterPasswordWithNoData()
|
|
{
|
|
$hash = self::$faker->sha1;
|
|
$request = new UpdateMasterPassRequest('secret', 'test_secret', $hash);
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('getAllEncrypted')
|
|
->willReturn([]);
|
|
|
|
$this->crypt
|
|
->expects(self::never())
|
|
->method('decrypt');
|
|
|
|
$this->customFieldService
|
|
->expects(self::never())
|
|
->method('updateMasterPass');
|
|
|
|
$this->customFieldCrypt->updateMasterPassword($request);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws ServiceException
|
|
*/
|
|
public function testUpdateMasterPasswordWithTask()
|
|
{
|
|
$hash = self::$faker->sha1;
|
|
$request = new UpdateMasterPassRequest('secret', 'test_secret', $hash);
|
|
$customFieldData = CustomFieldDataGenerator::factory()->buildCustomFieldData();
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('getAllEncrypted')
|
|
->willReturn([$customFieldData]);
|
|
|
|
$data = self::$faker->text();
|
|
|
|
$this->crypt
|
|
->expects(self::once())
|
|
->method('decrypt')
|
|
->with($customFieldData->getData(), $customFieldData->getKey(), $request->getCurrentMasterPass())
|
|
->willReturn($data);
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('updateMasterPass')
|
|
->with(
|
|
new Callback(static function (CustomFieldDataModel $customFieldData) use ($data) {
|
|
return $customFieldData->getData() === $data;
|
|
}),
|
|
$request->getNewMasterPass()
|
|
);
|
|
|
|
$this->customFieldCrypt->updateMasterPassword($request);
|
|
}
|
|
|
|
/**
|
|
* @throws ServiceException
|
|
*/
|
|
public function testUpdateMasterPasswordWithCryptError()
|
|
{
|
|
$hash = self::$faker->sha1;
|
|
$request = new UpdateMasterPassRequest('secret', 'test_secret', $hash);
|
|
$customFieldData = CustomFieldDataGenerator::factory()->buildCustomFieldData();
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('getAllEncrypted')
|
|
->willReturn([$customFieldData]);
|
|
|
|
$this->crypt
|
|
->expects(self::once())
|
|
->method('decrypt')
|
|
->willThrowException(new RuntimeException('test'));
|
|
|
|
$this->customFieldService
|
|
->expects(self::never())
|
|
->method('updateMasterPass');
|
|
|
|
$this->customFieldCrypt->updateMasterPassword($request);
|
|
}
|
|
|
|
public function testUpdateMasterPasswordWithError()
|
|
{
|
|
$hash = self::$faker->sha1;
|
|
$request = new UpdateMasterPassRequest('secret', 'test_secret', $hash);
|
|
|
|
$this->customFieldService
|
|
->expects(self::once())
|
|
->method('getAllEncrypted')
|
|
->willThrowException(new RuntimeException('test'));
|
|
|
|
$this->crypt
|
|
->expects(self::never())
|
|
->method('decrypt');
|
|
|
|
$this->customFieldService
|
|
->expects(self::never())
|
|
->method('updateMasterPass');
|
|
|
|
$this->expectException(ServiceException::class);
|
|
$this->expectExceptionMessage('Error while updating the custom fields data');
|
|
|
|
$this->customFieldCrypt->updateMasterPassword($request);
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->customFieldService = $this->createMock(CustomFieldDataService::class);
|
|
$this->crypt = $this->createMock(CryptInterface::class);
|
|
|
|
$this->customFieldCrypt = new CustomFieldCrypt($this->application, $this->customFieldService, $this->crypt);
|
|
}
|
|
}
|