Add bridge ping endpoint

[MAILPOET-6030]
This commit is contained in:
alex-mpoet
2024-06-04 13:54:43 +03:00
committed by Aschepikov
parent 7aa006d9f7
commit a6caf0a1e6
5 changed files with 74 additions and 4 deletions

View File

@@ -290,6 +290,23 @@ class Services extends APIEndpoint {
]); ]);
} }
public function pingBridge() {
try {
$bridgePingResponse = $this->bridge->pingBridge();
} catch (\Exception $e) {
return $this->errorResponse([
APIError::UNKNOWN => $e->getMessage(),
]);
}
if (!$this->bridge->validateBridgePingResponse($bridgePingResponse)) {
$code = $bridgePingResponse ?: Bridge::CHECK_ERROR_UNKNOWN;
return $this->errorResponse([
APIError::UNKNOWN => $this->getErrorDescriptionByCode($code),
]);
}
return $this->successResponse();
}
public function refreshMSSKeyStatus() { public function refreshMSSKeyStatus() {
$key = $this->settings->get('mta.mailpoet_api_key'); $key = $this->settings->get('mta.mailpoet_api_key');
return $this->checkMSSKey(['key' => $key]); return $this->checkMSSKey(['key' => $key]);

View File

@@ -83,7 +83,7 @@ class Help {
], ],
'mss' => [ 'mss' => [
'enabled' => $this->bridge->isMailpoetSendingServiceEnabled(), 'enabled' => $this->bridge->isMailpoetSendingServiceEnabled(),
'isReachable' => $this->bridge->pingBridge(), 'isReachable' => $this->bridge->validateBridgePingResponse($this->bridge->pingBridge()),
], ],
'cronStatus' => $this->cronHelper->getDaemon(), 'cronStatus' => $this->cronHelper->getDaemon(),
'queueStatus' => $mailerLog, 'queueStatus' => $mailerLog,

View File

@@ -98,14 +98,18 @@ class Bridge {
return !empty($key); return !empty($key);
} }
public static function pingBridge() { public function pingBridge() {
$params = [ $params = [
'blocking' => true, 'blocking' => true,
'timeout' => 10, 'timeout' => 10,
]; ];
$wp = new WPFunctions(); $wp = new WPFunctions();
$result = $wp->wpRemoteGet(self::BRIDGE_URL, $params); $result = $wp->wpRemoteGet(self::BRIDGE_URL, $params);
return $wp->wpRemoteRetrieveResponseCode($result) === 200; return $wp->wpRemoteRetrieveResponseCode($result);
}
public function validateBridgePingResponse($responseCode) {
return $responseCode === 200;
} }
/** /**

View File

@@ -615,6 +615,54 @@ class ServicesTest extends \MailPoetTest {
); );
} }
public function testItReturnsErrorIfBridgePingThrowsException() {
$errorMessage = 'some error';
$bridge = $this->make(
new Bridge(),
[
'pingBridge' => function () use ($errorMessage) {
throw new \Exception($errorMessage);
},
]
);
$servicesEndpoint = $this->createServicesEndpointWithMocks(['bridge' => $bridge]);
$response = $servicesEndpoint->pingBridge();
$response = $response->getData();
verify($response['errors'][0])->isArray();
verify($response['errors'][0]['message'])->stringContainsString($errorMessage);
verify($response['errors'][0]['error'])->stringContainsString('unknown');
}
public function testItReturnsErrorIfBridgePingResultIsUnsuccessful() {
$bridge = $this->make(
new Bridge(),
[
'pingBridge' => false,
]
);
$servicesEndpoint = $this->createServicesEndpointWithMocks(['bridge' => $bridge]);
$response = $servicesEndpoint->pingBridge();
$response = $response->getData();
verify($response['errors'][0])->isArray();
verify($response['errors'][0]['message'])->stringContainsString('Contact your hosting support');
verify($response['errors'][0]['error'])->stringContainsString('unknown');
}
public function testItPingsBridgeSuccessfully() {
$bridge = $this->make(
new Bridge(),
[
'pingBridge' => 200, // HTTP OK
]
);
$servicesEndpoint = $this->createServicesEndpointWithMocks(['bridge' => $bridge]);
$response = $servicesEndpoint->pingBridge();
verify($response->status)->equals(200);
$response = $response->getData();
verify($response['data'])->empty();
verify(empty($response['errors']))->true();
}
private function createServicesEndpointWithMocks(array $mocks) { private function createServicesEndpointWithMocks(array $mocks) {
return new Services( return new Services(
$mocks['bridge'] ?? $this->diContainer->get(Bridge::class), $mocks['bridge'] ?? $this->diContainer->get(Bridge::class),

View File

@@ -232,7 +232,8 @@ class BridgeTest extends \MailPoetTest {
public function testItPingsBridge() { public function testItPingsBridge() {
if (getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') $this->markTestSkipped(); if (getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') $this->markTestSkipped();
verify(Bridge::pingBridge())->true(); $result = $this->bridge->pingBridge();
verify($this->bridge->validateBridgePingResponse($result))->true();
} }
public function testItAllowsChangingRequestTimeout() { public function testItAllowsChangingRequestTimeout() {