Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alallema committed Feb 22, 2022
1 parent 1de659b commit 75f6027
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Delegates/HandlesSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
113 changes: 113 additions & 0 deletions tests/Endpoints/TenantTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace Tests\Endpoints;

use DateTimeInterface;
use Tests\TestCase;
use MeiliSearch\Client;
use MeiliSearch\Exceptions\ApiException;
use \Datetime;

final class TenantTokenTest extends TestCase
{
private string $privateKey;
private Client $privateClient;

protected function setUp(): void
{
parent::setUp();
$index = $this->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('');
}
}

0 comments on commit 75f6027

Please sign in to comment.