Add meta to response when email is unauthorized
[MAILPOET-3881]
This commit is contained in:
@ -126,7 +126,7 @@ class Settings extends APIEndpoint {
|
||||
$this->bridge->onSettingsSave($settings);
|
||||
}
|
||||
|
||||
$this->authorizedEmailsController->onSettingsSave($settings);
|
||||
$meta = $this->authorizedEmailsController->onSettingsSave($settings);
|
||||
if ($signupConfirmation !== $this->settings->get('signup_confirmation.enabled')) {
|
||||
$this->messageController->updateSuccessMessages();
|
||||
}
|
||||
|
@ -62,13 +62,13 @@ class AuthorizedEmailsController {
|
||||
if (!Bridge::isMPSendingServiceEnabled()) {
|
||||
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null);
|
||||
$this->updateMailerLog();
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
$authorizedEmails = $this->bridge->getAuthorizedEmailAddresses();
|
||||
// Keep previous check result for an invalid response from API
|
||||
if ($authorizedEmails === false) {
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
$authorizedEmails = array_map('strtolower', $authorizedEmails);
|
||||
|
||||
@ -77,14 +77,16 @@ class AuthorizedEmailsController {
|
||||
$result = $this->validateAddressesInScheduledAndAutomaticEmails($authorizedEmails, $result);
|
||||
$this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, $result ?: null);
|
||||
$this->updateMailerLog($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function onSettingsSave($settings) {
|
||||
public function onSettingsSave($settings): ?array {
|
||||
$senderAddressSet = !empty($settings['sender']['address']);
|
||||
$mailpoetSendingMethodSet = ($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['method'] ?? null) === Mailer::METHOD_MAILPOET;
|
||||
if ($senderAddressSet || $mailpoetSendingMethodSet) {
|
||||
$this->checkAuthorizedEmailAddresses();
|
||||
return $this->checkAuthorizedEmailAddresses();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function onNewsletterSenderAddressUpdate(NewsletterEntity $newsletter, string $oldSenderAddress = null) {
|
||||
|
@ -13,6 +13,7 @@ use MailPoet\Cron\Workers\WooCommerceSync;
|
||||
use MailPoet\Entities\NewsletterEntity;
|
||||
use MailPoet\Entities\ScheduledTaskEntity;
|
||||
use MailPoet\Form\FormMessageController;
|
||||
use MailPoet\Mailer\Mailer;
|
||||
use MailPoet\Mailer\MailerLog;
|
||||
use MailPoet\Models\ScheduledTask;
|
||||
use MailPoet\Newsletter\NewslettersRepository;
|
||||
@ -54,7 +55,7 @@ class SettingsTest extends \MailPoetTest {
|
||||
$this->endpoint = new Settings(
|
||||
$this->settings,
|
||||
new Bridge,
|
||||
$this->make(AuthorizedEmailsController::class, ['onSettingsSave' => true ]),
|
||||
$this->make(AuthorizedEmailsController::class, ['onSettingsSave' => null ]),
|
||||
$this->make(TransactionalEmails::class),
|
||||
WPFunctions::get(),
|
||||
$this->diContainer->get(EntityManager::class),
|
||||
@ -148,6 +149,36 @@ class SettingsTest extends \MailPoetTest {
|
||||
expect(MailerLog::isSendingPaused())->false();
|
||||
}
|
||||
|
||||
public function testItSaveUnauthorizedAddressAndReturnsMeta() {
|
||||
$this->settings->set(Mailer::MAILER_CONFIG_SETTING_NAME, ['method' => Mailer::METHOD_MAILPOET]);
|
||||
$bridgeMock = $this->make(Bridge::class, ['getAuthorizedEmailAddresses' => Expected::once(['authorized@email.com'])]);
|
||||
$this->endpoint = new Settings(
|
||||
$this->settings,
|
||||
$bridgeMock,
|
||||
new AuthorizedEmailsController($this->settings, $bridgeMock, $this->diContainer->get(NewslettersRepository::class)),
|
||||
$this->make(TransactionalEmails::class),
|
||||
WPFunctions::get(),
|
||||
$this->diContainer->get(EntityManager::class),
|
||||
$this->diContainer->get(NewslettersRepository::class),
|
||||
$this->diContainer->get(StatisticsOpensRepository::class),
|
||||
$this->diContainer->get(ScheduledTasksRepository::class),
|
||||
$this->diContainer->get(FormMessageController::class),
|
||||
$this->make(ServicesChecker::class, ['isMailPoetAPIKeyPendingApproval' => false]),
|
||||
$this->diContainer->get(SegmentsRepository::class),
|
||||
$this->diContainer->get(SubscribersCountsController::class)
|
||||
);
|
||||
|
||||
$response = $this->endpoint->set([
|
||||
'sender' => ['address' => 'invalid@email.com'],
|
||||
]);
|
||||
expect($response->status)->same(200);
|
||||
expect($this->settings->get('sender.address'))->same('invalid@email.com');
|
||||
expect($response->meta)->equals([
|
||||
'invalid_sender_address' => 'invalid@email.com',
|
||||
'showNotice' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testItRejectsUnauthorizedFromAddress() {
|
||||
$bridgeMock = $this->make(Bridge::class, ['getAuthorizedEmailAddresses' => Expected::once(['authorized@email.com'])]);
|
||||
$this->endpoint = new Settings(
|
||||
|
Reference in New Issue
Block a user