test(IT): Test Config Account use cases

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-08-29 18:59:58 +02:00
parent e2d67fef14
commit 7b24cded97
4 changed files with 114 additions and 60 deletions

View File

@@ -24,21 +24,23 @@
namespace SP\Modules\Web\Controllers\ConfigAccount;
use JsonException;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Domain\Common\Attributes\Action;
use SP\Domain\Common\Dtos\ActionResponse;
use SP\Domain\Common\Enums\ResponseType;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Core\Acl\AclActionsInterface;
use SP\Domain\Core\Acl\UnauthorizedPageException;
use SP\Domain\Core\Exceptions\SessionTimeout;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Core\Exceptions\ValidationException;
use SP\Modules\Web\Controllers\SimpleControllerBase;
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
use function SP\__u;
/**
* Class ConfigAccountController
*
* @package SP\Modules\Web\Controllers
* Class SaveController
*/
final class SaveController extends SimpleControllerBase
{
@@ -47,31 +49,25 @@ final class SaveController extends SimpleControllerBase
private const MAX_FILES_SIZE = 16384;
/**
* @return bool
* @throws JsonException
* @return ActionResponse
* @throws ValidationException
* @throws SPException
*/
public function saveAction(): bool
#[Action(ResponseType::JSON)]
public function saveAction(): ActionResponse
{
$configData = $this->config->getConfigData();
$eventMessage = EventMessage::build();
try {
$this->handleAccountsConfig($configData);
$this->handleFilesConfig($configData, $eventMessage);
$this->handlePublicLinksConfig($configData, $eventMessage);
} catch (ValidationException $e) {
$this->eventDispatcher->notify('exception', new Event($e));
$this->returnJsonResponseException($e);
}
$this->handleAccountsConfig($configData);
$this->handleFilesConfig($configData, $eventMessage);
$this->handlePublicLinksConfig($configData, $eventMessage);
return $this->saveConfig(
$configData,
$this->config,
function () use ($eventMessage) {
$this->eventDispatcher->notify('save.config.account', new Event($this, $eventMessage));
}
fn() => $this->eventDispatcher->notify('save.config.account', new Event($this, $eventMessage))
);
}
@@ -152,18 +148,12 @@ final class SaveController extends SimpleControllerBase
/**
* @return void
* @throws JsonException
* @throws SPException
* @throws SessionTimeout
*/
protected function initialize(): void
{
try {
$this->checks();
$this->checkAccess(AclActionsInterface::CONFIG_ACCOUNT);
} catch (UnauthorizedPageException $e) {
$this->eventDispatcher->notify('exception', new Event($e));
$this->returnJsonResponseException($e);
}
$this->checks();
$this->checkAccess(AclActionsInterface::CONFIG_ACCOUNT);
}
}

View File

@@ -54,6 +54,11 @@ final class XmlExportController extends SimpleControllerBase
{
use JsonTrait;
/**
* @throws SessionTimeout
* @throws SPException
* @throws UnauthorizedPageException
*/
public function __construct(
Application $application,
SimpleControllerHelper $simpleControllerHelper,
@@ -62,6 +67,9 @@ final class XmlExportController extends SimpleControllerBase
private readonly PathsContext $pathsContext
) {
parent::__construct($application, $simpleControllerHelper);
$this->checks();
$this->checkAccess(AclActionsInterface::CONFIG_BACKUP);
}
/**
@@ -132,22 +140,4 @@ final class XmlExportController extends SimpleControllerBase
return $this->returnJsonResponseException($e);
}
}
/**
* initialize
*
* @throws SPException
* @throws SessionTimeout
*/
protected function initialize(): void
{
try {
$this->checks();
$this->checkAccess(AclActionsInterface::CONFIG_BACKUP);
} catch (UnauthorizedPageException $e) {
$this->eventDispatcher->notify('exception', new Event($e));
$this->returnJsonResponseException($e);
}
}
}

View File

@@ -25,11 +25,10 @@
namespace SP\Modules\Web\Controllers\Traits;
use Exception;
use JsonException;
use SP\Domain\Common\Dtos\ActionResponse;
use SP\Domain\Config\Ports\ConfigDataInterface;
use SP\Domain\Config\Ports\ConfigFileService;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Http\Dtos\JsonMessage;
use function SP\__u;
use function SP\processException;
@@ -41,22 +40,19 @@ use function SP\processException;
*/
trait ConfigTrait
{
use JsonTrait;
/**
* Guardar la configuración
*
* @throws JsonException
* @throws SPException
*/
protected function saveConfig(
ConfigDataInterface $configData,
ConfigFileService $config,
callable $onSuccess = null
): bool {
): ActionResponse {
try {
if ($configData->isDemoEnabled()) {
return $this->returnJsonResponse(JsonMessage::JSON_WARNING, __u('Ey, this is a DEMO!!'));
return ActionResponse::warning(__u('Ey, this is a DEMO!!'));
}
$config->save($configData);
@@ -65,15 +61,11 @@ trait ConfigTrait
$onSuccess();
}
return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('Configuration updated'));
return ActionResponse::ok(__u('Configuration updated'));
} catch (Exception $e) {
processException($e);
return $this->returnJsonResponse(
JsonMessage::JSON_ERROR,
__u('Error while saving the configuration'),
[$e]
);
return ActionResponse::error(__u('Error while saving the configuration'));
}
}
}

View File

@@ -0,0 +1,82 @@
<?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\ConfigAccount;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use SP\Domain\Core\Exceptions\InvalidClassException;
use SP\Infrastructure\File\FileException;
use SP\Tests\IntegrationTestCase;
/**
* Class ConfigAccountControllerTest
*/
#[Group('integration')]
class ConfigAccountControllerTest extends IntegrationTestCase
{
/**
* @throws ContainerExceptionInterface
* @throws Exception
* @throws NotFoundExceptionInterface
* @throws InvalidClassException
* @throws FileException
*/
#[Test]
public function save()
{
$data = [
'publiclinks_enabled' => true,
'publiclinks_image_enabled' => self::$faker->boolean(),
'publiclinks_maxtime' => self::$faker->randomNumber(4),
'publiclinks_maxviews' => self::$faker->randomNumber(4),
'files_enabled' => true,
'files_allowed_size' => self::$faker->randomNumber(3),
'files_allowed_mimetypes' => [self::$faker->mimeType(), self::$faker->mimeType()],
'account_globalsearch_enabled' => self::$faker->boolean(),
'account_passtoimage_enabled' => self::$faker->boolean(),
'account_link_enabled' => self::$faker->boolean(),
'account_fullgroup_access_enabled' => self::$faker->boolean(),
'account_count' => self::$faker->randomNumber(3),
'account_resultsascards_enabled' => self::$faker->boolean(),
'account_expire_enabled' => self::$faker->boolean(),
'account_expire_time' => self::$faker->randomNumber(8),
];
$container = $this->buildContainer(
$this->getModuleDefinitions(),
$this->buildRequest('post', 'index.php', ['r' => 'configAccount/save'], $data)
);
$this->runApp($container);
$this->expectOutputString('{"status":"OK","description":"Configuration updated","data":null}');
}
}