mirror of
https://github.com/nuxsmin/sysPass.git
synced 2026-03-13 11:57:50 +01:00
test(IT): Test Mail save use cases
Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
@@ -24,25 +24,27 @@
|
||||
|
||||
namespace SP\Modules\Web\Controllers\ConfigMail;
|
||||
|
||||
|
||||
use Exception;
|
||||
use JsonException;
|
||||
use SP\Core\Application;
|
||||
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\Common\Services\ServiceException;
|
||||
use SP\Domain\Config\Services\ConfigUtil;
|
||||
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\Domain\Http\Dtos\JsonMessage;
|
||||
use SP\Domain\Notification\Dtos\MailParams;
|
||||
use SP\Domain\Notification\Ports\MailService;
|
||||
use SP\Modules\Web\Controllers\SimpleControllerBase;
|
||||
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
|
||||
use SP\Mvc\Controller\SimpleControllerHelper;
|
||||
|
||||
use function SP\__u;
|
||||
|
||||
/**
|
||||
* Class CheckController
|
||||
*/
|
||||
@@ -59,45 +61,31 @@ final class CheckController extends SimpleControllerBase
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws JsonException
|
||||
* @throws ValidationException
|
||||
* @throws ServiceException
|
||||
*/
|
||||
public function checkAction(): bool
|
||||
#[Action(ResponseType::JSON)]
|
||||
public function checkAction(): ActionResponse
|
||||
{
|
||||
try {
|
||||
$mailParams = $this->handleMailConfig();
|
||||
$mailParams = $this->handleMailConfig();
|
||||
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients'));
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients'));
|
||||
|
||||
// Valores para la configuración del Correo
|
||||
if (empty($mailParams->getServer()) || empty($mailParams->getFrom()) || count($mailRecipients) === 0) {
|
||||
throw new ValidationException(SPException::ERROR, __u('Missing Mail parameters'));
|
||||
}
|
||||
|
||||
$this->mailService->check($mailParams, $mailRecipients[0]);
|
||||
|
||||
$this->eventDispatcher->notify(
|
||||
'send.mail.check',
|
||||
new Event(
|
||||
$this,
|
||||
EventMessage::build()
|
||||
->addDescription(__u('Email sent'))
|
||||
->addDetail(__u('Recipient'), $mailRecipients[0])
|
||||
)
|
||||
);
|
||||
|
||||
return $this->returnJsonResponse(
|
||||
JsonMessage::JSON_SUCCESS,
|
||||
__u('Email sent'),
|
||||
[__u('Please, check your inbox')]
|
||||
);
|
||||
} catch (Exception $e) {
|
||||
processException($e);
|
||||
|
||||
$this->eventDispatcher->notify('exception', new Event($e));
|
||||
|
||||
return $this->returnJsonResponseException($e);
|
||||
if (empty($mailParams->getServer()) || empty($mailParams->getFrom()) || count($mailRecipients) === 0) {
|
||||
throw new ValidationException(SPException::ERROR, __u('Missing Mail parameters'));
|
||||
}
|
||||
|
||||
$this->mailService->check($mailParams, $mailRecipients[0]);
|
||||
|
||||
$this->eventDispatcher->notify(
|
||||
'send.mail.check',
|
||||
new Event(
|
||||
$this,
|
||||
EventMessage::build(__u('Email sent'))->addDetail(__u('Recipient'), $mailRecipients[0])
|
||||
)
|
||||
);
|
||||
|
||||
return ActionResponse::ok(__u('Email sent'), [__u('Please, check your inbox')]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,19 +105,13 @@ final class CheckController extends SimpleControllerBase
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws JsonException
|
||||
* @throws SPException
|
||||
* @throws SessionTimeout
|
||||
* @throws UnauthorizedPageException
|
||||
*/
|
||||
protected function initialize(): void
|
||||
{
|
||||
try {
|
||||
$this->checks();
|
||||
$this->checkAccess(AclActionsInterface::CONFIG_MAIL);
|
||||
} catch (UnauthorizedPageException $e) {
|
||||
$this->eventDispatcher->notify('exception', new Event($e));
|
||||
|
||||
$this->returnJsonResponseException($e);
|
||||
}
|
||||
$this->checks();
|
||||
$this->checkAccess(AclActionsInterface::CONFIG_MAIL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,31 +24,33 @@
|
||||
|
||||
namespace SP\Modules\Web\Controllers\ConfigMail;
|
||||
|
||||
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\Services\ConfigUtil;
|
||||
use SP\Domain\Core\Acl\AclActionsInterface;
|
||||
use SP\Domain\Core\Acl\UnauthorizedPageException;
|
||||
use SP\Domain\Core\Exceptions\SessionTimeout;
|
||||
use SP\Domain\Http\Dtos\JsonMessage;
|
||||
use SP\Domain\Core\Exceptions\SPException;
|
||||
use SP\Modules\Web\Controllers\SimpleControllerBase;
|
||||
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
|
||||
|
||||
use function SP\__u;
|
||||
|
||||
/**
|
||||
* Class SaveController
|
||||
*
|
||||
* @package SP\Modules\Web\Controllers
|
||||
*/
|
||||
final class SaveController extends SimpleControllerBase
|
||||
{
|
||||
use ConfigTrait;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws JsonException
|
||||
* @throws SPException
|
||||
*/
|
||||
public function saveAction(): bool
|
||||
#[Action(ResponseType::JSON)]
|
||||
public function saveAction(): ActionResponse
|
||||
{
|
||||
$eventMessage = EventMessage::build();
|
||||
$configData = $this->config->getConfigData();
|
||||
@@ -63,11 +65,10 @@ final class SaveController extends SimpleControllerBase
|
||||
$mailFrom = $this->request->analyzeEmail('mail_from');
|
||||
$mailRequests = $this->request->analyzeBool('mail_requests_enabled', false);
|
||||
$mailAuth = $this->request->analyzeBool('mail_auth_enabled', false);
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients'));
|
||||
$mailRecipients = ConfigUtil::mailAddressesAdapter($this->request->analyzeString('mail_recipients', ''));
|
||||
|
||||
if ($mailEnabled && (empty($mailServer) || empty($mailFrom))
|
||||
) {
|
||||
return $this->returnJsonResponse(JsonMessage::JSON_ERROR, __u('Missing Mail parameters'));
|
||||
if ($mailEnabled && (empty($mailServer) || empty($mailFrom))) {
|
||||
return ActionResponse::error(__u('Missing Mail parameters'));
|
||||
}
|
||||
|
||||
if ($mailEnabled) {
|
||||
@@ -101,7 +102,7 @@ final class SaveController extends SimpleControllerBase
|
||||
|
||||
$eventMessage->addDescription(__u('Mail disabled'));
|
||||
} else {
|
||||
return $this->returnJsonResponse(JsonMessage::JSON_SUCCESS, __u('No changes'));
|
||||
return ActionResponse::ok(__u('No changes'));
|
||||
}
|
||||
|
||||
return $this->saveConfig(
|
||||
@@ -114,19 +115,13 @@ final class SaveController extends SimpleControllerBase
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws JsonException
|
||||
* @throws SPException
|
||||
* @throws SessionTimeout
|
||||
* @throws UnauthorizedPageException
|
||||
*/
|
||||
protected function initialize(): void
|
||||
{
|
||||
try {
|
||||
$this->checks();
|
||||
$this->checkAccess(AclActionsInterface::CONFIG_MAIL);
|
||||
} catch (UnauthorizedPageException $e) {
|
||||
$this->eventDispatcher->notify('exception', new Event($e));
|
||||
|
||||
$this->returnJsonResponseException($e);
|
||||
}
|
||||
$this->checks();
|
||||
$this->checkAccess(AclActionsInterface::CONFIG_MAIL);
|
||||
}
|
||||
}
|
||||
|
||||
156
tests/SP/Modules/Web/Controllers/ConfigMail/ConfigMailTest.php
Normal file
156
tests/SP/Modules/Web/Controllers/ConfigMail/ConfigMailTest.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?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\ConfigMail;
|
||||
|
||||
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\Tests\IntegrationTestCase;
|
||||
|
||||
/**
|
||||
* Class ConfigMailTest
|
||||
*/
|
||||
#[Group('integration')]
|
||||
class ConfigMailTest extends IntegrationTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Test]
|
||||
public function check()
|
||||
{
|
||||
$data = [
|
||||
'mail_enabled' => true,
|
||||
'mail_server' => self::$faker->domainName(),
|
||||
'mail_port' => self::$faker->randomNumber(3),
|
||||
'mail_user' => self::$faker->userName(),
|
||||
'mail_pass' => self::$faker->password(),
|
||||
'mail_security' => 'tls',
|
||||
'mail_from' => self::$faker->email(),
|
||||
'mail_auth_enabled' => self::$faker->boolean(),
|
||||
'mail_recipients' => self::$faker->email()
|
||||
];
|
||||
|
||||
$container = $this->buildContainer(
|
||||
IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'configMail/check'], $data),
|
||||
);
|
||||
|
||||
$this->expectOutputString('{"status":"OK","description":"Email sent","data":["Please, check your inbox"]}');
|
||||
|
||||
IntegrationTestCase::runApp($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Test]
|
||||
public function save()
|
||||
{
|
||||
$data = [
|
||||
'mail_enabled' => true,
|
||||
'mail_server' => self::$faker->domainName(),
|
||||
'mail_port' => self::$faker->randomNumber(3),
|
||||
'mail_user' => self::$faker->userName(),
|
||||
'mail_pass' => self::$faker->password(),
|
||||
'mail_security' => 'tls',
|
||||
'mail_from' => self::$faker->email(),
|
||||
'mail_auth_enabled' => self::$faker->boolean(),
|
||||
'mail_recipients' => self::$faker->email()
|
||||
];
|
||||
|
||||
$container = $this->buildContainer(
|
||||
IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'configMail/save'], $data)
|
||||
);
|
||||
|
||||
$this->expectOutputString('{"status":"OK","description":"Configuration updated","data":null}');
|
||||
|
||||
IntegrationTestCase::runApp($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Test]
|
||||
public function saveWithNoChanges()
|
||||
{
|
||||
$data = [
|
||||
'mail_enabled' => false
|
||||
];
|
||||
|
||||
$container = $this->buildContainer(
|
||||
IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'configMail/save'], $data)
|
||||
);
|
||||
|
||||
$this->expectOutputString('{"status":"OK","description":"Configuration updated","data":null}');
|
||||
|
||||
IntegrationTestCase::runApp($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Test]
|
||||
#[TestWith(['', ''])]
|
||||
#[TestWith(['test_server', ''])]
|
||||
#[TestWith(['', 'me@email.com'])]
|
||||
public function saveWithMissingParameters(?string $mailServer, ?string $mailFrom)
|
||||
{
|
||||
$data = [
|
||||
'mail_enabled' => true,
|
||||
'mail_server' => $mailServer,
|
||||
'mail_from' => $mailFrom
|
||||
];
|
||||
|
||||
$container = $this->buildContainer(
|
||||
IntegrationTestCase::buildRequest('post', 'index.php', ['r' => 'configMail/save'], $data)
|
||||
);
|
||||
|
||||
$this->expectOutputString('{"status":"ERROR","description":"Missing Mail parameters","data":null}');
|
||||
|
||||
IntegrationTestCase::runApp($container);
|
||||
}
|
||||
|
||||
protected function getConfigData(): array
|
||||
{
|
||||
$configData = parent::getConfigData();
|
||||
$configData['isMailEnabled'] = true;
|
||||
|
||||
return $configData;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user