Pause sending when pending approval, resume when not anymore
[MAILPOET-2806]
This commit is contained in:
@ -7,8 +7,10 @@ use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
|||||||
use MailPoet\API\JSON\Error as APIError;
|
use MailPoet\API\JSON\Error as APIError;
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
use MailPoet\Config\Installer;
|
use MailPoet\Config\Installer;
|
||||||
|
use MailPoet\Config\ServicesChecker;
|
||||||
use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck;
|
use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck;
|
||||||
use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
|
use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
|
||||||
|
use MailPoet\Mailer\MailerLog;
|
||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
use MailPoet\Services\SPFCheck;
|
use MailPoet\Services\SPFCheck;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
@ -37,11 +39,22 @@ class Services extends APIEndpoint {
|
|||||||
/** @var PremiumKeyCheck */
|
/** @var PremiumKeyCheck */
|
||||||
private $premiumWorker;
|
private $premiumWorker;
|
||||||
|
|
||||||
|
/** @var ServicesChecker */
|
||||||
|
private $servicesChecker;
|
||||||
|
|
||||||
public $permissions = [
|
public $permissions = [
|
||||||
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
|
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(Bridge $bridge, SettingsController $settings, AnalyticsHelper $analytics, SPFCheck $spfCheck, SendingServiceKeyCheck $mssWorker, PremiumKeyCheck $premiumWorker) {
|
public function __construct(
|
||||||
|
Bridge $bridge,
|
||||||
|
SettingsController $settings,
|
||||||
|
AnalyticsHelper $analytics,
|
||||||
|
SPFCheck $spfCheck,
|
||||||
|
SendingServiceKeyCheck $mssWorker,
|
||||||
|
PremiumKeyCheck $premiumWorker,
|
||||||
|
ServicesChecker $servicesChecker
|
||||||
|
) {
|
||||||
$this->bridge = $bridge;
|
$this->bridge = $bridge;
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->analytics = $analytics;
|
$this->analytics = $analytics;
|
||||||
@ -49,6 +62,7 @@ class Services extends APIEndpoint {
|
|||||||
$this->mssWorker = $mssWorker;
|
$this->mssWorker = $mssWorker;
|
||||||
$this->premiumWorker = $premiumWorker;
|
$this->premiumWorker = $premiumWorker;
|
||||||
$this->dateTime = new DateTime();
|
$this->dateTime = new DateTime();
|
||||||
|
$this->servicesChecker = $servicesChecker;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function checkSPFRecord($data = []) {
|
public function checkSPFRecord($data = []) {
|
||||||
@ -76,6 +90,8 @@ class Services extends APIEndpoint {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$wasPendingApproval = $this->servicesChecker->isMailPoetAPIKeyPendingApproval();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$result = $this->bridge->checkMSSKey($key);
|
$result = $this->bridge->checkMSSKey($key);
|
||||||
$this->bridge->storeMSSKeyAndState($key, $result);
|
$this->bridge->storeMSSKeyAndState($key, $result);
|
||||||
@ -85,6 +101,13 @@ class Services extends APIEndpoint {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pause sending when key is pending approval, resume when not pending anymore
|
||||||
|
if ($this->servicesChecker->isMailPoetAPIKeyPendingApproval()) {
|
||||||
|
MailerLog::pauseSending(MailerLog::getMailerLog());
|
||||||
|
} elseif ($wasPendingApproval) {
|
||||||
|
MailerLog::resumeSending();
|
||||||
|
}
|
||||||
|
|
||||||
$state = !empty($result['state']) ? $result['state'] : null;
|
$state = !empty($result['state']) ? $result['state'] : null;
|
||||||
|
|
||||||
$successMessage = null;
|
$successMessage = null;
|
||||||
|
@ -7,11 +7,15 @@ use MailPoet\Analytics\Analytics;
|
|||||||
use MailPoet\API\JSON\Response as APIResponse;
|
use MailPoet\API\JSON\Response as APIResponse;
|
||||||
use MailPoet\API\JSON\v1\Services;
|
use MailPoet\API\JSON\v1\Services;
|
||||||
use MailPoet\Config\Installer;
|
use MailPoet\Config\Installer;
|
||||||
|
use MailPoet\Config\ServicesChecker;
|
||||||
use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck;
|
use MailPoet\Cron\Workers\KeyCheck\PremiumKeyCheck;
|
||||||
use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
|
use MailPoet\Cron\Workers\KeyCheck\SendingServiceKeyCheck;
|
||||||
|
use MailPoet\Mailer\Mailer;
|
||||||
|
use MailPoet\Mailer\MailerLog;
|
||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
use MailPoet\Services\SPFCheck;
|
use MailPoet\Services\SPFCheck;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
|
use MailPoet\Settings\SettingsRepository;
|
||||||
|
|
||||||
class ServicesTest extends \MailPoetTest {
|
class ServicesTest extends \MailPoetTest {
|
||||||
public $data;
|
public $data;
|
||||||
@ -175,6 +179,75 @@ class ServicesTest extends \MailPoetTest {
|
|||||||
expect($response->errors[0]['message'])->equals('test');
|
expect($response->errors[0]['message'])->equals('test');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testItDoesNotPauseSendingWhenMSSKeyValidAndApproved() {
|
||||||
|
$this->settings->set(Mailer::MAILER_CONFIG_SETTING_NAME, ['method' => Mailer::METHOD_MAILPOET]);
|
||||||
|
expect(MailerLog::isSendingPaused())->false();
|
||||||
|
|
||||||
|
$bridge = $this->make(
|
||||||
|
Bridge::class,
|
||||||
|
[
|
||||||
|
'settings' => SettingsController::getInstance(),
|
||||||
|
'checkMSSKey' => [
|
||||||
|
'state' => Bridge::KEY_VALID,
|
||||||
|
'data' => ['is_approved' => true],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$servicesEndpoint = $this->createServicesEndpointWithMockedBridge($bridge);
|
||||||
|
$response = $servicesEndpoint->checkMSSKey($this->data);
|
||||||
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
expect(MailerLog::isSendingPaused())->false();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItPausesSendingWhenMSSKeyValidButNotApproved() {
|
||||||
|
$this->settings->set(Mailer::MAILER_CONFIG_SETTING_NAME, ['method' => Mailer::METHOD_MAILPOET]);
|
||||||
|
expect(MailerLog::isSendingPaused())->false();
|
||||||
|
|
||||||
|
$bridge = $this->make(
|
||||||
|
Bridge::class,
|
||||||
|
[
|
||||||
|
'settings' => SettingsController::getInstance(),
|
||||||
|
'checkMSSKey' => [
|
||||||
|
'state' => Bridge::KEY_VALID,
|
||||||
|
'data' => ['is_approved' => false],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$servicesEndpoint = $this->createServicesEndpointWithMockedBridge($bridge);
|
||||||
|
$response = $servicesEndpoint->checkMSSKey($this->data);
|
||||||
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
expect(MailerLog::isSendingPaused())->true();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItResumesSendingWhenMSSKeyBecomesApproved() {
|
||||||
|
$this->settings->set(Mailer::MAILER_CONFIG_SETTING_NAME, ['method' => Mailer::METHOD_MAILPOET]);
|
||||||
|
$this->settings->set(Bridge::API_KEY_SETTING_NAME, 'key');
|
||||||
|
$this->settings->set(Bridge::API_KEY_STATE_SETTING_NAME, [
|
||||||
|
'state' => Bridge::KEY_VALID,
|
||||||
|
'data' => ['is_approved' => false],
|
||||||
|
]);
|
||||||
|
MailerLog::pauseSending(MailerLog::getMailerLog());
|
||||||
|
expect(MailerLog::isSendingPaused())->true();
|
||||||
|
|
||||||
|
$bridge = $this->make(
|
||||||
|
Bridge::class,
|
||||||
|
[
|
||||||
|
'settings' => SettingsController::getInstance(),
|
||||||
|
'checkMSSKey' => [
|
||||||
|
'state' => Bridge::KEY_VALID,
|
||||||
|
'data' => ['is_approved' => true],
|
||||||
|
],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$servicesEndpoint = $this->createServicesEndpointWithMockedBridge($bridge);
|
||||||
|
$response = $servicesEndpoint->checkMSSKey($this->data);
|
||||||
|
expect($response->status)->equals(APIResponse::STATUS_OK);
|
||||||
|
expect(MailerLog::isSendingPaused())->false();
|
||||||
|
}
|
||||||
|
|
||||||
public function testItRespondsWithErrorIfNoPremiumKeyIsGiven() {
|
public function testItRespondsWithErrorIfNoPremiumKeyIsGiven() {
|
||||||
$response = $response = $this->diContainer->get(Services::class)->checkPremiumKey(['key' => '']);
|
$response = $response = $this->diContainer->get(Services::class)->checkPremiumKey(['key' => '']);
|
||||||
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
expect($response->status)->equals(APIResponse::STATUS_BAD_REQUEST);
|
||||||
@ -400,6 +473,10 @@ class ServicesTest extends \MailPoetTest {
|
|||||||
expect($this->settings->get('new_public_id', null))->null();
|
expect($this->settings->get('new_public_id', null))->null();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function _after() {
|
||||||
|
$this->diContainer->get(SettingsRepository::class)->truncate();
|
||||||
|
}
|
||||||
|
|
||||||
private function createServicesEndpointWithMockedSPFCheck($spfCheck) {
|
private function createServicesEndpointWithMockedSPFCheck($spfCheck) {
|
||||||
return new Services(
|
return new Services(
|
||||||
$this->diContainer->get(Bridge::class),
|
$this->diContainer->get(Bridge::class),
|
||||||
@ -407,7 +484,8 @@ class ServicesTest extends \MailPoetTest {
|
|||||||
$this->diContainer->get(Analytics::class),
|
$this->diContainer->get(Analytics::class),
|
||||||
$spfCheck,
|
$spfCheck,
|
||||||
$this->diContainer->get(SendingServiceKeyCheck::class),
|
$this->diContainer->get(SendingServiceKeyCheck::class),
|
||||||
$this->diContainer->get(PremiumKeyCheck::class)
|
$this->diContainer->get(PremiumKeyCheck::class),
|
||||||
|
$this->diContainer->get(ServicesChecker::class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -418,7 +496,8 @@ class ServicesTest extends \MailPoetTest {
|
|||||||
$this->diContainer->get(Analytics::class),
|
$this->diContainer->get(Analytics::class),
|
||||||
$this->diContainer->get(SPFCheck::class),
|
$this->diContainer->get(SPFCheck::class),
|
||||||
$this->diContainer->get(SendingServiceKeyCheck::class),
|
$this->diContainer->get(SendingServiceKeyCheck::class),
|
||||||
$this->diContainer->get(PremiumKeyCheck::class)
|
$this->diContainer->get(PremiumKeyCheck::class),
|
||||||
|
$this->diContainer->get(ServicesChecker::class)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user