From c0eed5b919e7343526dc9c7f7e2f7fff8c0a053d Mon Sep 17 00:00:00 2001 From: Brezo Cordero <8002881+brezocordero@users.noreply.github.com> Date: Fri, 17 Mar 2023 21:36:44 -0500 Subject: [PATCH] Integration tests for provisioning [MAILPOET-5131] --- .../lib/WPCOM/DotcomLicenseProvisioner.php | 4 +- .../integration/API/JSON/v1/SettingsTest.php | 29 +++++ .../WPCOM/DotcomLicenseProvisionerTest.php | 113 ++++++++++++++++++ 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php diff --git a/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php b/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php index 3e78cf2946..dac2230782 100644 --- a/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php +++ b/mailpoet/lib/WPCOM/DotcomLicenseProvisioner.php @@ -38,7 +38,7 @@ class DotcomLicenseProvisioner { * * @return bool */ - private function isAtomicPlatform(): bool { + public function isAtomicPlatform(): bool { return defined('IS_ATOMIC') && IS_ATOMIC && defined('ATOMIC_CLIENT_ID') && (ATOMIC_CLIENT_ID === '2'); } @@ -83,7 +83,7 @@ class DotcomLicenseProvisioner { * @param string $apiKey * @return true|WP_Error */ - private function activateMSS(string $apiKey) { + public function activateMSS(string $apiKey) { $response = $this->settings->setupMSS($apiKey); if ($response instanceof ErrorResponse) { $this->loggerFactory->getLogger(LoggerFactory::TOPIC_PROVISIONING)->error( diff --git a/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php b/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php index 0a795be301..5964e64b1d 100644 --- a/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php +++ b/mailpoet/tests/integration/API/JSON/v1/SettingsTest.php @@ -313,6 +313,35 @@ class SettingsTest extends \MailPoetTest { expect($this->endpoint->delete('unexistent_setting'))->isInstanceOf(ErrorResponse::class); } + public function testItSetsUpMSSWithProvidedKey() { + $newKey = 'some-new-key'; + $this->endpoint = new Settings( + $this->settings, + $this->make(Bridge::class, ['onSettingsSave' => Expected::once()]), + $this->make(AuthorizedEmailsController::class, ['onSettingsSave' => Expected::once()]), + $this->diContainer->get(AuthorizedSenderDomainController::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(SettingsChangeHandler::class), + $this->diContainer->get(SubscribersCountsController::class), + $this->diContainer->get(TrackingConfig::class), + $this->diContainer->get(ConfirmationEmailCustomizer::class) + ); + + expect($this->endpoint->setupMSS($newKey))->isInstanceOf(SuccessResponse::class); + expect($this->settings->get('mta.mailpoet_api_key'))->equals($newKey); + expect($this->settings->get('mta_group'))->equals('mailpoet'); + expect($this->settings->get('mta.method'))->equals('MailPoet'); + expect($this->settings->get('signup_confirmation.enabled'))->equals(1); + } + private function createNewsletter(string $type, string $status = NewsletterEntity::STATUS_DRAFT, $parent = null): NewsletterEntity { $newsletter = new NewsletterEntity(); $newsletter->setType($type); diff --git a/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php b/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php new file mode 100644 index 0000000000..093c36886d --- /dev/null +++ b/mailpoet/tests/integration/WPCOM/DotcomLicenseProvisionerTest.php @@ -0,0 +1,113 @@ +provisioner = $this->construct( + DotcomLicenseProvisioner::class, + [ + $this->diContainer->get(LoggerFactory::class), + $this->make(Settings::class), + $this->make(Services::class), + ], + ['isAtomicPlatform' => true]); + } + + public function testItReturnsResultIfNotAtomic() { + $result = false; + $payload = ['apiKey' => 'some-key']; + $provisioner = $this->construct( + DotcomLicenseProvisioner::class, + [ + $this->make(LoggerFactory::class), + $this->make(Settings::class), + $this->make(Services::class), + ], + ['isAtomicPlatform' => false]); + expect($provisioner->provisionLicense($result, $payload, DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE))->equals($result); + } + + public function testItReturnsResultIfWrongEvent() { + $result = false; + $payload = ['apiKey' => 'some-key']; + $eventType = 'wrong-event'; + expect($this->provisioner->provisionLicense($result, $payload, $eventType))->equals($result); + } + + public function testItReturnsWPErrorIfMissingKey() { + $result = false; + $payload = []; + $eventType = DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE; + $error = $this->provisioner->provisionLicense($result, $payload, $eventType); + $this->assertInstanceOf(\WP_Error::class, $error); + expect($error->get_error_message())->equals('Invalid license payload: Missing API key.'); + } + + public function testItReturnsWPErrorIfErrorOnSettingUpMSS() { + $result = false; + $payload = ['apiKey' => 'some-key']; + $eventType = DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE; + $provisioner = $this->construct( + DotcomLicenseProvisioner::class, + [ + $this->diContainer->get(LoggerFactory::class), + $this->make(Settings::class, ['setupMSS' => new ErrorResponse(['error' => 'some-error'])]), + $this->make(Services::class, ['refreshMSSKeyStatus' => new SuccessResponse()]), + ], + ['isAtomicPlatform' => true]); + $error = $provisioner->provisionLicense($result, $payload, $eventType); + $this->assertInstanceOf(\WP_Error::class, $error); + expect($error->get_error_message())->equals('some-error '); + } + + public function testItReturnsTrueIfCouldNotRefreshKey() { + $result = false; + $payload = ['apiKey' => 'some-key']; + $eventType = DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE; + $provisioner = $this->construct( + DotcomLicenseProvisioner::class, + [ + $this->diContainer->get(LoggerFactory::class), + $this->make(Settings::class, ['setupMSS' => new SuccessResponse()]), + $this->make(Services::class, ['refreshMSSKeyStatus' => new ErrorResponse(['error' => 'some-error'])]), + ], + ['isAtomicPlatform' => true]); + $error = $provisioner->provisionLicense($result, $payload, $eventType); + $this->assertInstanceOf(\WP_Error::class, $error); + expect($error->get_error_message())->equals('some-error '); + } + + public function testItReturnsTrueIfKeyProvidedMSSActivatedAndRefreshed() { + $result = false; + $payload = ['apiKey' => 'some-key']; + $eventType = DotcomLicenseProvisioner::EVENT_TYPE_PROVISION_LICENSE; + $provisioner = $this->construct( + DotcomLicenseProvisioner::class, + [ + $this->diContainer->get(LoggerFactory::class), + $this->make(Settings::class, ['setupMSS' => new SuccessResponse()]), + $this->make(Services::class, ['refreshMSSKeyStatus' => new SuccessResponse()]), + ], + ['isAtomicPlatform' => true]); + $result = $provisioner->provisionLicense($result, $payload, $eventType); + expect($result)->equals(true); + } + + public function _after() { + $this->diContainer->get(SettingsRepository::class)->truncate(); + $this->diContainer->get(LogRepository::class)->truncate(); + } +}