139 lines
5.2 KiB
PHP
139 lines
5.2 KiB
PHP
<?php declare(strict_types = 1);
|
|
|
|
namespace MailPoet\WPCOM;
|
|
|
|
use MailPoet\API\JSON\ErrorResponse;
|
|
use MailPoet\API\JSON\SuccessResponse;
|
|
use MailPoet\API\JSON\v1\Services;
|
|
use MailPoet\API\JSON\v1\Settings;
|
|
use MailPoet\Logging\LoggerFactory;
|
|
use MailPoet\Logging\LogRepository;
|
|
use MailPoet\Settings\SettingsRepository;
|
|
|
|
class DotcomLicenseProvisionerTest extends \MailPoetTest {
|
|
/** @var DotcomLicenseProvisioner */
|
|
private $provisioner;
|
|
|
|
public function _before() {
|
|
parent::_before();
|
|
$this->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 testItReturnsErrorIfCouldNotRefreshKey() {
|
|
$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 testItReturnsErrorIfCouldNotVerifyPremiumKey() {
|
|
$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(),
|
|
'refreshPremiumKeyStatus' => 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(),
|
|
'refreshPremiumKeyStatus' => 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();
|
|
}
|
|
}
|