Skip to content

Commit 5e64635

Browse files
Add tests for the static API authentication features
1 parent 68c450d commit 5e64635

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,20 @@ jobs:
1515
name: Tests on PHP ${{ matrix.php }} - ${{ matrix.dependency-version }}
1616

1717
services:
18-
chroma:
18+
chroma-wo-auth:
1919
image: chromadb/chroma
2020
ports:
2121
- 8000:8000
2222

23+
chroma-w-auth:
24+
image: chromadb/chroma
25+
ports:
26+
- 8001:8000
27+
env:
28+
CHROMA_SERVER_AUTH_CREDENTIALS: 'test-token'
29+
CHROMA_SERVER_AUTH_CREDENTIALS_PROVIDER: 'chromadb.auth.token.TokenConfigServerAuthCredentialsProvider'
30+
CHROMA_SERVER_AUTH_PROVIDER: 'chromadb.auth.token.TokenAuthServerProvider'
31+
2332
steps:
2433
- name: Checkout
2534
uses: actions/checkout@v3

docker-compose.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
version: '3.9'
22

33
services:
4-
server:
4+
chroma_wo_auth:
55
image: 'chromadb/chroma'
66
ports:
77
- '8000:8000'
8-
volumes:
9-
- chroma-data:/chroma/chroma
108

11-
volumes:
12-
chroma-data:
13-
driver: local
9+
chroma_w_auth:
10+
image: 'chromadb/chroma'
11+
ports:
12+
- '8001:8000'
13+
environment:
14+
CHROMA_SERVER_AUTH_CREDENTIALS: 'test-token'
15+
CHROMA_SERVER_AUTH_CREDENTIALS_PROVIDER: 'chromadb.auth.token.TokenConfigServerAuthCredentialsProvider'
16+
CHROMA_SERVER_AUTH_PROVIDER: 'chromadb.auth.token.TokenAuthServerProvider'

src/Generated/ChromaApiClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,14 @@ private function handleChromaApiException(\Exception|ClientExceptionInterface $e
357357
ChromaException::throwSpecific($message, $error_type, $e->getCode());
358358
}
359359

360+
// If the structure is {'error': 'Error Type', 'message' : 'Error message'}
361+
if (isset($error['error']) && isset($error['message'])) {
362+
ChromaException::throwSpecific($error['message'], $error['error'], $e->getCode());
363+
}
364+
360365
// If the structure is 'error' => 'Collection not found'
361366
if (isset($error['error'])) {
362-
$message = $error['message'] ?? $error['error'];
367+
$message = $error['error'];
363368
$error_type = ChromaException::inferTypeFromMessage($message);
364369

365370
ChromaException::throwSpecific($message, $error_type, $e->getCode());

src/Generated/Exceptions/ChromaException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static function inferTypeFromMessage(string $message): string
2626
{
2727
return match (true) {
2828
str_contains($message, 'NotFoundError') => 'NotFoundError',
29+
str_contains($message, 'AuthorizationError') => 'AuthorizationError',
2930
str_contains($message, 'UniqueConstraintError') => 'UniqueConstraintError',
3031
str_contains($message, 'ValueError') => 'ValueError',
3132
str_contains($message, 'dimensionality') => 'DimensionalityError',

tests/ChromaDB.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,41 @@
44

55
use Codewithkyrian\ChromaDB\Client;
66
use Codewithkyrian\ChromaDB\ChromaDB;
7+
use Codewithkyrian\ChromaDB\Generated\Exceptions\ChromaAuthorizationException;
78

8-
it('can create a client instance', function () {
9+
it('can connect to a normal chroma server', function () {
910
$client = ChromaDB::client();
1011

1112
expect($client)->toBeInstanceOf(Client::class);
1213
});
1314

14-
it('can create a client instance via factory', function () {
15+
it('can connect to a chroma server using factory', function () {
1516
$client = ChromaDB::factory()
1617
->withHost('http://localhost')
1718
->withPort(8000)
1819
->connect();
1920

2021
expect($client)->toBeInstanceOf(Client::class);
2122
});
23+
24+
test('can connect to an API token authenticated chroma server', function () {
25+
$client = ChromaDB::factory()
26+
->withPort(8001)
27+
->withBearerToken('test-token')
28+
->connect();
29+
30+
expect($client)->toBeInstanceOf(Client::class);
31+
});
32+
33+
it('cannot connect to an API token authenticated chroma server with wrong token', function () {
34+
ChromaDB::factory()
35+
->withPort(8001)
36+
->withBearerToken('wrong-token')
37+
->connect();
38+
})->throws(ChromaAuthorizationException::class);
39+
40+
it('throws exception when connecting to API token authenticated chroma server with no token', function () {
41+
ChromaDB::factory()
42+
->withPort(8001)
43+
->connect();
44+
})->throws(ChromaAuthorizationException::class);

0 commit comments

Comments
 (0)