From ef3f39fc89fd131aa7e423432dcff71a9f03596b Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:46:29 +0530 Subject: [PATCH] Added tests --- app/config/errors.php | 6 +++ app/controllers/api/health.php | 4 ++ src/Appwrite/Extend/Exception.php | 1 + .../Health/HealthCustomServerTest.php | 52 +++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/app/config/errors.php b/app/config/errors.php index e0c3a7097a..c0943d31e5 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -801,6 +801,12 @@ return [ 'code' => 404, ], + Exception::INVALID_HOST => [ + 'name' => Exception::INVALID_HOST, + 'description' => 'Host not found.', + 'code' => 404, + ], + /** Providers */ Exception::PROVIDER_NOT_FOUND => [ 'name' => Exception::PROVIDER_NOT_FOUND, diff --git a/app/controllers/api/health.php b/app/controllers/api/health.php index 7ad2235c6e..7fcd249a9f 100644 --- a/app/controllers/api/health.php +++ b/app/controllers/api/health.php @@ -407,6 +407,9 @@ App::get('/v1/health/certificate') ->action(function (string $domain, Response $response) { $get = stream_context_create(array("ssl" => array("capture_peer_cert" => true))); $read = stream_socket_client("ssl://" . $domain . ":443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $get); + if (!$read) { + throw new Exception(Exception::INVALID_HOST, 'The domain is not valid.'); + } $certificate = stream_context_get_params($read); $certificateInfo = openssl_x509_parse($certificate['options']['ssl']['peer_certificate']); $sslExpiration = $certificateInfo['validTo_time_t']; @@ -415,6 +418,7 @@ App::get('/v1/health/certificate') if ($status == 'fail') { throw new Exception(Exception::CERTIFICATE_EXPIRED, 'The certificate of the domain has expired.'); } + $response->dynamic(new Document([ 'name' => 'certificate', 'status' => $status, diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 0df64f021d..f227482835 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -240,6 +240,7 @@ class Exception extends \Exception /** Health */ public const QUEUE_SIZE_EXCEEDED = 'queue_size_exceeded'; public const CERTIFICATE_EXPIRED = 'certificate_expired'; + public const INVALID_HOST = 'invalid_host'; /** Provider */ public const PROVIDER_NOT_FOUND = 'provider_not_found'; diff --git a/tests/e2e/Services/Health/HealthCustomServerTest.php b/tests/e2e/Services/Health/HealthCustomServerTest.php index 3ea1b884a7..d93a8eb598 100644 --- a/tests/e2e/Services/Health/HealthCustomServerTest.php +++ b/tests/e2e/Services/Health/HealthCustomServerTest.php @@ -424,4 +424,56 @@ class HealthCustomServerTest extends Scope return []; } + + public function testCertificateValidity(): array + { + /** + * Test for SUCCESS + */ + $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=www.google.com', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['status']); + $this->assertIsString($response['body']['status']); + $this->assertEquals('pass', $response['body']['status']); + + $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=appwrite.io', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertNotEmpty($response['body']['status']); + $this->assertIsString($response['body']['status']); + $this->assertEquals('pass', $response['body']['status']); + + /** + * Test for FAILURE + */ + $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=localhost', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(400, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=doesnotexist.com', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(404, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_GET, '/health/certificate?domain=https://google.com', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), []); + + $this->assertEquals(404, $response['headers']['status-code']); + + return []; + } }