Adhere to updated MSS /me response

[MAILPOET-1035]
This commit is contained in:
Pavel Dohnal
2017-07-31 16:47:20 +02:00
parent 7cf3d0960d
commit ccb751b44a
3 changed files with 22 additions and 8 deletions

View File

@@ -64,11 +64,17 @@ class Bridge {
$state_map = array( $state_map = array(
200 => self::PREMIUM_KEY_VALID, 200 => self::PREMIUM_KEY_VALID,
401 => self::PREMIUM_KEY_INVALID, 401 => self::PREMIUM_KEY_INVALID,
402 => self::PREMIUM_KEY_EXPIRING 402 => self::PREMIUM_KEY_ALREADY_USED
); );
if(!empty($result['code']) && isset($state_map[$result['code']])) { if(!empty($result['code']) && isset($state_map[$result['code']])) {
if($state_map[$result['code']] == self::PREMIUM_KEY_VALID
&& !empty($result['data']['expire_at'])
) {
$key_state = self::PREMIUM_KEY_EXPIRING;
} else {
$key_state = $state_map[$result['code']]; $key_state = $state_map[$result['code']];
}
} else { } else {
$key_state = self::PREMIUM_KEY_CHECK_ERROR; $key_state = self::PREMIUM_KEY_CHECK_ERROR;
} }

View File

@@ -14,7 +14,8 @@ class BridgeTest extends \MailPoetTest {
function _before() { function _before() {
$this->valid_key = 'abcdefghijklmnopqrstuvwxyz'; $this->valid_key = 'abcdefghijklmnopqrstuvwxyz';
$this->invalid_key = '401' . $this->valid_key; $this->invalid_key = '401' . $this->valid_key;
$this->expiring_key = '402' . $this->valid_key; $this->expiring_key = 'expiring' . $this->valid_key;
$this->used_key = '402' . $this->valid_key;
$this->uncheckable_key = '503' . $this->valid_key; $this->uncheckable_key = '503' . $this->valid_key;
$this->expiring_premium_key = 'expiring' . $this->valid_key; $this->expiring_premium_key = 'expiring' . $this->valid_key;
@@ -67,6 +68,12 @@ class BridgeTest extends \MailPoetTest {
expect($result['data']['expire_at'])->notEmpty(); expect($result['data']['expire_at'])->notEmpty();
} }
function testItChecksAlreadyUsed() {
$result = $this->bridge->checkMSSKey($this->used_key);
expect($result)->notEmpty();
expect($result['state'])->equals(Bridge::PREMIUM_KEY_ALREADY_USED);
}
function testItReturnsErrorStateOnEmptyAPIResponseCodeDuringMSSCheck() { function testItReturnsErrorStateOnEmptyAPIResponseCodeDuringMSSCheck() {
$api = Stub::make(new API(null), array('checkMSSKey' => array()), $this); $api = Stub::make(new API(null), array('checkMSSKey' => array()), $this);
$this->bridge->api = $api; $this->bridge->api = $api;

View File

@@ -14,7 +14,7 @@ class BridgeTestMockAPI {
function checkMSSKey() { function checkMSSKey() {
// if a key begins with these codes, return them // if a key begins with these codes, return them
$regex = '/^(401|402|503)/'; $regex = '/^(expiring|401|402|503)/';
$code = preg_match($regex, $this->api_key, $m) ? $m[1] : 200; $code = preg_match($regex, $this->api_key, $m) ? $m[1] : 200;
return $this->processAPICheckResponse($code); return $this->processAPICheckResponse($code);
} }
@@ -40,16 +40,17 @@ class BridgeTestMockAPI {
private function processAPICheckResponse($code) { private function processAPICheckResponse($code) {
switch($code) { switch($code) {
case 200: case 'expiring':
$body = array('subscriber_limit' => 10000); // a special case of a valid key
break; $code = 200;
case 402:
$body = array( $body = array(
'subscriber_limit' => 10000, 'subscriber_limit' => 10000,
'expire_at' => Carbon::createFromTimestamp(current_time('timestamp')) 'expire_at' => Carbon::createFromTimestamp(current_time('timestamp'))
->addMonth()->format('c') ->addMonth()->format('c')
); );
break; break;
case 200:
case 401:
case 401: case 401:
default: default:
$body = null; $body = null;