Files
sysPass/tests/SP/Modules/Web/Controllers/ConfigEncryption/ConfigEncryptionControllerTest.php
2024-09-05 06:28:54 +02:00

159 lines
5.0 KiB
PHP

<?php
/**
* 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/>.
*/
declare(strict_types=1);
namespace SP\Tests\Modules\Web\Controllers\ConfigEncryption;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Ports\AppLockHandler;
use SP\Domain\User\Dtos\UserDto;
use SP\Tests\InjectConfigParam;
use SP\Tests\InjectVault;
use SP\Tests\IntegrationTestCase;
/**
* Class RefreshControllerTest
*/
#[Group('integration')]
#[InjectVault]
class ConfigEncryptionControllerTest extends IntegrationTestCase
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
#[Test]
public function refresh()
{
$container = $this->buildContainer(
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'configEncryption/refresh'])
);
$this->expectOutputString('{"status":"OK","description":"Master password hash updated","data":null}');
IntegrationTestCase::runApp($container);
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
#[Test]
#[InjectConfigParam(['masterPwd' => '$2y$10$6imglyA01feP1AnEmlAgUeQPa7vysHeKQLz0MDA1Zf7iG6ep.7PaC'])]
#[TestWith([false], 'Change accounts encryption')]
#[TestWith([true], 'Change only hash')]
public function save(bool $noAccountChange)
{
$data = [
'current_masterpass' => 'a_password',
'new_masterpass' => 'a_new_password',
'new_masterpass_repeat' => 'a_new_password',
'confirm_masterpass_change' => 'true',
'no_account_change' => $noAccountChange,
'isAjax' => 1
];
$configData = $this->getConfigData();
$configData['isMaintenance'] = true;
$appLockHandler = self::createStub(AppLockHandler::class);
$appLockHandler
->method('getLock')
->willReturn(100);
$configData = self::createConfiguredStub(ConfigDataInterface::class, $configData);
$configFileService = self::createStub(ConfigFileService::class);
$configFileService
->method('getConfigData')
->willReturn($configData);
$definitions = [
ConfigFileService::class => $configFileService,
AppLockHandler::class => $appLockHandler,
];
$container = $this->buildContainer(
IntegrationTestCase::buildRequest(
'post',
'index.php',
['r' => 'configEncryption/save'],
$data
),
$definitions
);
if ($noAccountChange) {
$this->expectOutputString(
'{"status":"OK","description":"Master password updated","data":["No accounts updated, only hash","Please, restart the session to update it"]}'
);
} else {
$this->expectOutputString(
'{"status":"OK","description":"Master password updated","data":"Please, restart the session to update it"}'
);
}
IntegrationTestCase::runApp($container);
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
#[Test]
public function saveTemp()
{
$data = [
'temporary_masterpass_maxtime' => self::$faker->randomNumber(4),
'temporary_masterpass_group' => self::$faker->randomNumber(3),
'temporary_masterpass_email' => self::$faker->email()
];
$container = $this->buildContainer(
IntegrationTestCase::buildRequest('get', 'index.php', ['r' => 'configEncryption/saveTemp']),
$data
);
$this->expectOutputString('{"status":"OK","description":"Temporary password generated","data":null}');
IntegrationTestCase::runApp($container);
}
protected function getUserDataDto(): UserDto
{
return parent::getUserDataDto()->mutate(['id' => 100]);
}
}