Make key check error messages more descriptive [MAILPOET-990]

This commit is contained in:
stoletniy
2017-07-07 12:44:38 +03:00
committed by pavel-mailpoet
parent 3499de05e8
commit 91ff008485
2 changed files with 94 additions and 6 deletions

View File

@ -60,8 +60,8 @@ class Services extends APIEndpoint {
default: default:
$code = !empty($result['code']) ? $result['code'] : Bridge::CHECK_ERROR_UNKNOWN; $code = !empty($result['code']) ? $result['code'] : Bridge::CHECK_ERROR_UNKNOWN;
$error = sprintf( $error = sprintf(
__('Error validating MailPoet Sending Service key, please try again later (code: %s)', 'mailpoet'), __('Error validating MailPoet Sending Service key, please try again later (%s)', 'mailpoet'),
$code $this->getErrorDescriptionByCode($code)
); );
break; break;
} }
@ -116,12 +116,28 @@ class Services extends APIEndpoint {
default: default:
$code = !empty($result['code']) ? $result['code'] : Bridge::CHECK_ERROR_UNKNOWN; $code = !empty($result['code']) ? $result['code'] : Bridge::CHECK_ERROR_UNKNOWN;
$error = sprintf( $error = sprintf(
__('Error validating Premium key, please try again later (code: %s)', 'mailpoet'), __('Error validating Premium key, please try again later (%s)', 'mailpoet'),
$code $this->getErrorDescriptionByCode($code)
); );
break; break;
} }
return $this->errorResponse(array(APIError::BAD_REQUEST => $error)); return $this->errorResponse(array(APIError::BAD_REQUEST => $error));
} }
private function getErrorDescriptionByCode($code) {
switch($code) {
case Bridge::CHECK_ERROR_UNAVAILABLE:
$text = __('Service unavailable', 'mailpoet');
break;
case Bridge::CHECK_ERROR_UNKNOWN:
$text = __('Contact your hosting support to check the connection between your host and https://bridge.mailpoet.com', 'mailpoet');
break;
default:
$text = sprintf(_x('code: %s', 'Error code (inside parentheses)', 'mailpoet'), $code);
break;
}
return $text;
}
} }

View File

@ -74,7 +74,43 @@ class ServicesTest extends MailPoetTest {
); );
$response = $this->services_endpoint->checkMSSKey($this->data); $response = $this->services_endpoint->checkMSSKey($this->data);
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND); expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->contains((string)Bridge::CHECK_ERROR_UNAVAILABLE); expect($response->errors[0]['message'])->contains(
$this->invokeMethod(
$this->services_endpoint, 'getErrorDescriptionByCode', array(Bridge::CHECK_ERROR_UNAVAILABLE)
)
);
}
function testItRespondsWithErrorIfServiceDidNotReturnAResponseCodeDuringMSSCheck() {
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
array(
'checkMSSKey' => null,
'storeMSSKeyAndState' => Stub::once()
),
$this
);
$response = $this->services_endpoint->checkMSSKey($this->data);
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->contains(
$this->invokeMethod(
$this->services_endpoint, 'getErrorDescriptionByCode', array(Bridge::CHECK_ERROR_UNKNOWN)
)
);
}
function testItPrintsErrorCodeIfServiceReturnedAnUnexpectedResponseCodeDuringMSSCheck() {
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
array(
'checkMSSKey' => array('code' => 404),
'storeMSSKeyAndState' => Stub::once()
),
$this
);
$response = $this->services_endpoint->checkMSSKey($this->data);
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->contains('404');
} }
function testItRespondsWithErrorIfMSSCheckThrowsAnException() { function testItRespondsWithErrorIfMSSCheckThrowsAnException() {
@ -171,7 +207,43 @@ class ServicesTest extends MailPoetTest {
); );
$response = $this->services_endpoint->checkPremiumKey($this->data); $response = $this->services_endpoint->checkPremiumKey($this->data);
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND); expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->contains((string)Bridge::CHECK_ERROR_UNAVAILABLE); expect($response->errors[0]['message'])->contains(
$this->invokeMethod(
$this->services_endpoint, 'getErrorDescriptionByCode', array(Bridge::CHECK_ERROR_UNAVAILABLE)
)
);
}
function testItRespondsWithErrorIfServiceDidNotReturnAResponseCodeDuringPremiumCheck() {
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
array(
'checkPremiumKey' => null,
'storePremiumKeyAndState' => Stub::once()
),
$this
);
$response = $this->services_endpoint->checkPremiumKey($this->data);
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->contains(
$this->invokeMethod(
$this->services_endpoint, 'getErrorDescriptionByCode', array(Bridge::CHECK_ERROR_UNKNOWN)
)
);
}
function testItPrintsErrorCodeIfServiceReturnedAnUnexpectedResponseCodeDuringPremiumCheck() {
$this->services_endpoint->bridge = Stub::make(
new Bridge(),
array(
'checkPremiumKey' => array('code' => 404),
'storePremiumKeyAndState' => Stub::once()
),
$this
);
$response = $this->services_endpoint->checkPremiumKey($this->data);
expect($response->status)->equals(APIResponse::STATUS_NOT_FOUND);
expect($response->errors[0]['message'])->contains('404');
} }
function testItRespondsWithErrorIfPremiumCheckThrowsAnException() { function testItRespondsWithErrorIfPremiumCheckThrowsAnException() {