diff --git a/lib/Services/Bridge.php b/lib/Services/Bridge.php index 30c9e29f3e..a62d12f28c 100644 --- a/lib/Services/Bridge.php +++ b/lib/Services/Bridge.php @@ -64,11 +64,17 @@ class Bridge { $state_map = array( 200 => self::PREMIUM_KEY_VALID, 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']])) { - $key_state = $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']]; + } } else { $key_state = self::PREMIUM_KEY_CHECK_ERROR; } diff --git a/tests/unit/Services/BridgeTest.php b/tests/unit/Services/BridgeTest.php index 4988efb82f..412a32df82 100644 --- a/tests/unit/Services/BridgeTest.php +++ b/tests/unit/Services/BridgeTest.php @@ -14,7 +14,8 @@ class BridgeTest extends \MailPoetTest { function _before() { $this->valid_key = 'abcdefghijklmnopqrstuvwxyz'; $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->expiring_premium_key = 'expiring' . $this->valid_key; @@ -67,6 +68,12 @@ class BridgeTest extends \MailPoetTest { 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() { $api = Stub::make(new API(null), array('checkMSSKey' => array()), $this); $this->bridge->api = $api; diff --git a/tests/unit/Services/BridgeTestMockAPI.php b/tests/unit/Services/BridgeTestMockAPI.php index ac0f1eafdf..d1e4b41f3a 100644 --- a/tests/unit/Services/BridgeTestMockAPI.php +++ b/tests/unit/Services/BridgeTestMockAPI.php @@ -14,7 +14,7 @@ class BridgeTestMockAPI { function checkMSSKey() { // 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; return $this->processAPICheckResponse($code); } @@ -40,16 +40,17 @@ class BridgeTestMockAPI { private function processAPICheckResponse($code) { switch($code) { - case 200: - $body = array('subscriber_limit' => 10000); - break; - case 402: + case 'expiring': + // a special case of a valid key + $code = 200; $body = array( 'subscriber_limit' => 10000, 'expire_at' => Carbon::createFromTimestamp(current_time('timestamp')) ->addMonth()->format('c') ); break; + case 200: + case 401: case 401: default: $body = null;