. */ namespace SP\Tests\Domain\Crypt\Services; use Dotenv\Repository\RepositoryInterface; use Exception; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\MockObject\MockObject; use SP\Domain\Account\Ports\AccountMasterPasswordService; use SP\Domain\Common\Ports\Repository; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Config\Ports\ConfigService; use SP\Domain\Core\Exceptions\ConstraintException; use SP\Domain\Core\Exceptions\QueryException; use SP\Domain\Crypt\Dtos\UpdateMasterPassRequest; use SP\Domain\Crypt\Services\MasterPass; use SP\Domain\CustomField\Ports\CustomFieldCryptService; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Tests\UnitaryTestCase; /** * Class MasterPassTest * */ #[Group('unitary')] class MasterPassTest extends UnitaryTestCase { private ConfigService|MockObject $configService; private AccountMasterPasswordService|MockObject $accountMasterPasswordService; private CustomFieldCryptService|MockObject $customFieldCryptService; private MockObject|RepositoryInterface $repository; private MasterPass $masterPass; public function testCheckUserUpdateMPassWithFutureTime() { $now = time(); $this->configService ->expects(self::once()) ->method('getByParam') ->with('lastupdatempass') ->willReturn((string)$now); $this->assertTrue($this->masterPass->checkUserUpdateMPass($now + 3600)); } public function testCheckUserUpdateMPassWithPastTime() { $now = time(); $this->configService ->expects(self::once()) ->method('getByParam') ->with('lastupdatempass') ->willReturn((string)$now); $this->assertFalse($this->masterPass->checkUserUpdateMPass($now - 3600)); } public function testCheckUserUpdateMPassWithEqualTime() { $now = time(); $this->configService ->expects(self::once()) ->method('getByParam') ->with('lastupdatempass') ->willReturn((string)$now); $this->assertTrue($this->masterPass->checkUserUpdateMPass($now)); } public function testCheckUserUpdateMPassWithServiceError() { $this->configService ->expects(self::once()) ->method('getByParam') ->with('lastupdatempass') ->willThrowException(ServiceException::error('test')); $this->assertTrue($this->masterPass->checkUserUpdateMPass(time())); } public function testCheckUserUpdateMPassWithNoSuchItemError() { $this->configService ->expects(self::once()) ->method('getByParam') ->with('lastupdatempass') ->willThrowException(NoSuchItemException::error('test')); $this->assertTrue($this->masterPass->checkUserUpdateMPass(time())); } public function testCheckMasterPassword() { $password = self::$faker->sha1(); $hash = password_hash($password, PASSWORD_BCRYPT); $this->configService ->expects(self::once()) ->method('getByParam') ->with('masterPwd') ->willReturn($hash); self::assertTrue($this->masterPass->checkMasterPassword($password)); } public function testCheckMasterPasswordWithServiceError() { $password = self::$faker->sha1(); $this->configService ->expects(self::once()) ->method('getByParam') ->with('masterPwd') ->willThrowException(ServiceException::error('test')); self::assertFalse($this->masterPass->checkMasterPassword($password)); } public function testCheckMasterPasswordWithNoSuchItemError() { $password = self::$faker->sha1(); $this->configService ->expects(self::once()) ->method('getByParam') ->with('masterPwd') ->willThrowException(NoSuchItemException::error('test')); self::assertFalse($this->masterPass->checkMasterPassword($password)); } /** * @throws Exception */ public function testChangeMasterPassword() { $hash = self::$faker->sha1(); $this->repository ->expects(self::once()) ->method('transactionAware') ->with(self::withResolveCallableCallback()); $request = new UpdateMasterPassRequest('123', '456', $hash); $this->accountMasterPasswordService ->expects(self::once()) ->method('updateMasterPassword') ->with($request); $this->accountMasterPasswordService ->expects(self::once()) ->method('updateHistoryMasterPassword') ->with($request); $this->customFieldCryptService ->expects(self::once()) ->method('updateMasterPassword') ->with($request); $this->configService ->expects(self::exactly(2)) ->method('save') ->with( ... self::withConsecutive(['masterPwd', $request->getHash()], ['lastupdatempass', self::anything()]) ); $this->masterPass->changeMasterPassword($request); } /** * @throws ConstraintException * @throws QueryException */ public function testUpdateConfig() { $hash = self::$faker->sha1(); $this->configService ->expects(self::exactly(2)) ->method('save') ->with( ... self::withConsecutive(['masterPwd', $hash], ['lastupdatempass', self::anything()]) ); $this->masterPass->updateConfig($hash); } protected function setUp(): void { parent::setUp(); $this->configService = $this->createMock(ConfigService::class); $this->accountMasterPasswordService = $this->createMock(AccountMasterPasswordService::class); $this->customFieldCryptService = $this->createMock(CustomFieldCryptService::class); $this->repository = $this->createMock(Repository::class); $this->masterPass = new MasterPass( $this->application, $this->configService, $this->accountMasterPasswordService, $this->customFieldCryptService, $this->repository ); } }