Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Add custom DSN validation for Sparkpost region
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-navghane committed Aug 4, 2023
1 parent a446c7a commit f53045c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Mailer/Factory/SparkpostTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use Symfony\Component\Mailer\Transport\Dsn;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class SparkpostTransportFactory extends AbstractTransportFactory
{
public function __construct(
private TransportCallback $transportCallback,
private TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher,
HttpClientInterface $client = null,
LoggerInterface $logger = null
Expand All @@ -38,11 +40,15 @@ public function create(Dsn $dsn): TransportInterface
{
if (SparkpostTransport::MAUTIC_SPARKPOST_API_SCHEME === $dsn->getScheme()) {
if (!$region = $dsn->getOption('region')) {
throw new InvalidArgumentException('Empty region');
throw new InvalidArgumentException(
$this->translator->trans('mautic.sparkpost.plugin.region.empty', [], 'validators')
);
}

if (!array_key_exists($region, SparkpostTransport::SPARK_POST_HOSTS)) {
throw new InvalidArgumentException('Invalid region');
throw new InvalidArgumentException(
$this->translator->trans('mautic.sparkpost.plugin.region.invalid', [], 'validators')
);
}

return new SparkpostTransport(
Expand Down
45 changes: 45 additions & 0 deletions Tests/Functional/Mailer/Transport/SparkpostTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class SparkpostTransportTest extends MauticMysqlTestCase
{
private TranslatorInterface $translator;

protected function setUp(): void
{
$this->configParams['mailer_dsn'] = 'mautic+sparkpost+api://:some_api@some_host:25?region=us';
$this->configParams['messenger_dsn_email'] = 'sync://';
$this->configParams['mailer_custom_headers'] = ['x-global-custom-header' => 'value123'];
$this->configParams['mailer_from_email'] = '[email protected]';
parent::setUp();
$this->translator = self::getContainer()->get('translator');
}

public function testEmailSendToContactSync(): void
Expand Down Expand Up @@ -114,4 +118,45 @@ private function createContact(string $email): Lead

return $lead;
}

/**
* @dataProvider dataInvalidDsn
*
* @param array<string, string> $data
*/
public function testInvalidDsn(array $data, string $expectedMessage): void
{
// Request config edit page
$crawler = $this->client->request(Request::METHOD_GET, '/s/config/edit');
Assert::assertTrue($this->client->getResponse()->isOk());

// Set form data
$form = $crawler->selectButton('config[buttons][save]')->form();
$form->setValues($data + ['config[leadconfig][contact_columns]' => ['name', 'email', 'id']]);

// Check if there is the given validation error
$crawler = $this->client->submit($form);
Assert::assertTrue($this->client->getResponse()->isOk());
Assert::assertStringContainsString($this->translator->trans($expectedMessage, [], 'validators'), $crawler->text());
}

/**
* @return array<string, mixed[]>
*/
public function dataInvalidDsn(): iterable
{
yield 'Empty region' => [
[
'config[emailconfig][mailer_dsn][options][list][0][value]' => '',
],
'mautic.sparkpost.plugin.region.empty'
];

yield 'Invalid region' => [
[
'config[emailconfig][mailer_dsn][options][list][0][value]' => 'invalid_region',
],
'mautic.sparkpost.plugin.region.invalid'
];
}
}
2 changes: 2 additions & 0 deletions Translations/en_US/validators.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mautic.sparkpost.plugin.region.empty="Sparkpost region is empty. Add 'region' as a option."
mautic.sparkpost.plugin.region.invalid="Sparkpost region is invalid. Add 'us' or 'eu' as a suitable region."

0 comments on commit f53045c

Please sign in to comment.