From 75f6027bacfec583e1468f112afba930a6d60066 Mon Sep 17 00:00:00 2001 From: alallema Date: Tue, 22 Feb 2022 15:54:32 +0100 Subject: [PATCH] Adding tests --- src/Delegates/HandlesSystem.php | 6 +- tests/Endpoints/TenantTokenTest.php | 113 ++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tests/Endpoints/TenantTokenTest.php diff --git a/src/Delegates/HandlesSystem.php b/src/Delegates/HandlesSystem.php index eb674eec..f5ce70e3 100644 --- a/src/Delegates/HandlesSystem.php +++ b/src/Delegates/HandlesSystem.php @@ -39,11 +39,11 @@ public function base64url_encode($data) return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); } - public function generateTenantToken($searchRules, ?string $expiresAt = null, ?string $apiKey = null): string + public function generateTenantToken($searchRules, ?int $expiresAt = null, ?string $apiKey = null): string { $json = new Json(); - // Standar JWT header for encryption with SHA256/HS256 algorithm + // Standard JWT header for encryption with SHA256/HS256 algorithm $header = [ 'typ' => 'JWT', 'alg' => 'HS256', @@ -57,7 +57,7 @@ public function generateTenantToken($searchRules, ?string $expiresAt = null, ?st $payload['apiKeyPrefix'] = substr($apiKey, 0, 8); $payload['searchRules'] = $searchRules; if ($expiresAt) { - $payload['exp'] = $expiredAt; + $payload['exp'] = $expiresAt; } // Serialize the Header diff --git a/tests/Endpoints/TenantTokenTest.php b/tests/Endpoints/TenantTokenTest.php new file mode 100644 index 00000000..e3030272 --- /dev/null +++ b/tests/Endpoints/TenantTokenTest.php @@ -0,0 +1,113 @@ +createEmptyIndex('tenantToken'); + $promise = $index->addDocuments(self::DOCUMENTS); + $index->waitForTask($promise['uid']); + + $response = $this->client->getKeys(); + $this->privateKey = array_reduce($response['results'], function ($carry, $item) { + if (str_contains($item['description'], 'Default Admin API')) { + return $item['key']; + } + }); + $this->privateClient = new Client($this->host, $this->privateKey); + } + + public function testGenerateTenantTokenWithSearchRulesOnly(): void + { + $token = $this->privateClient->generateTenantToken(searchRules: array('*')); + $tokenClient = new Client('http://127.0.0.1:7700', $token); + $response = $tokenClient->index('tenantToken')->search(''); + + $this->assertArrayHasKey('hits', $response->toArray()); + $this->assertArrayHasKey('offset', $response->toArray()); + $this->assertArrayHasKey('limit', $response->toArray()); + $this->assertArrayHasKey('processingTimeMs', $response->toArray()); + $this->assertArrayHasKey('query', $response->toArray()); + $this->assertSame(7, $response->getNbHits()); + $this->assertCount(7, $response->getHits()); + } + + public function testGenerateTenantTokenWithApiKey(): void + { + $token = $this->client->generateTenantToken(searchRules: array('*'), apiKey: $this->privateKey); + $tokenClient = new Client('http://127.0.0.1:7700', $token); + $response = $tokenClient->index('tenantToken')->search(''); + + $this->assertArrayHasKey('hits', $response->toArray()); + $this->assertArrayHasKey('offset', $response->toArray()); + $this->assertArrayHasKey('limit', $response->toArray()); + $this->assertArrayHasKey('processingTimeMs', $response->toArray()); + $this->assertArrayHasKey('query', $response->toArray()); + $this->assertSame(7, $response->getNbHits()); + $this->assertCount(7, $response->getHits()); + } + + public function testGenerateTenantTokenWithExpiresAt(): void + { + $date = new DateTime(); + $tomorrow = $date->modify('+1 day')->getTimestamp(); + + $token = $this->privateClient->generateTenantToken(searchRules: array('*'), expiresAt: $tomorrow); + $tokenClient = new Client('http://127.0.0.1:7700', $token); + $response = $tokenClient->index('tenantToken')->search(''); + + $this->assertArrayHasKey('hits', $response->toArray()); + $this->assertArrayHasKey('offset', $response->toArray()); + $this->assertArrayHasKey('limit', $response->toArray()); + $this->assertArrayHasKey('processingTimeMs', $response->toArray()); + $this->assertArrayHasKey('query', $response->toArray()); + $this->assertSame(7, $response->getNbHits()); + $this->assertCount(7, $response->getHits()); + } + + + public function testGenerateTenantTokenWithoutSearchRules(): void + { + $token = $this->privateClient->generateTenantToken(searchRules: ''); + $tokenClient = new Client('http://127.0.0.1:7700', $token); + + $this->expectException(ApiException::class); + $tokenClient->index('tenantToken')->search(''); + } + + + public function testGenerateTenantTokenWithMasterKey(): void + { + $token = $this->client->generateTenantToken(array('*')); + $tokenClient = new Client('http://127.0.0.1:7700', $token); + + $this->expectException(ApiException::class); + $tokenClient->index('tenantToken')->search(''); + } + + public function testGenerateTenantTokenWithBadExpiresAt(): void + { + $date = new DateTime(); + $yesterday = $date->modify('-2 day')->getTimestamp(); + + $token = $this->privateClient->generateTenantToken(searchRules: array('*'), expiresAt: $yesterday); + $tokenClient = new Client('http://127.0.0.1:7700', $token); + + $this->expectException(ApiException::class); + $tokenClient->index('tenantToken')->search(''); + } +}