Skip to content

Commit 6557d2d

Browse files
authored
feat: Add verifyApiToken() function. (#2)
1 parent f654de9 commit 6557d2d

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

src/Client.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,31 @@ public function ping(): void
3939
throw new RuntimeException('Failed to ping');
4040
}
4141
}
42+
43+
public function verifyApiToken(): void
44+
{
45+
$response = $this->httpClient->post(
46+
'/api/v1/verify-api-token',
47+
[
48+
'headers' => [
49+
'Authorization' => 'Bearer ' . $this->apiToken,
50+
],
51+
],
52+
);
53+
$status = $response->getStatusCode();
54+
55+
if ($status !== 200) {
56+
throw new RuntimeException(sprintf(
57+
"Failed to verify API token, got HTTP status code '%d', expected '200'",
58+
$status
59+
));
60+
}
61+
62+
$body = (string) $response->getBody();
63+
$data = json_decode($body, true);
64+
65+
if (!isset($data['type']) || $data['type'] !== 'io.eventsourcingdb.api.api-token-verified') {
66+
throw new RuntimeException('Failed to verify API token');
67+
}
68+
}
4269
}

src/Container.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
class Container
1111
{
1212
private string $imageName = 'thenativeweb/eventsourcingdb';
13-
private string $imageTag;
13+
private string $imageTag = 'latest';
1414
private int $internalPort = 3000;
1515
private string $apiToken = 'secret';
1616
private ?StartedGenericContainer $container = null;
1717
private HttpClient $httpClient;
1818

1919
public function __construct()
2020
{
21-
$this->imageTag = $this->getImageVersionFromDockerfile();
2221
$this->httpClient = new HttpClient([
2322
'http_errors' => false
2423
]);
@@ -124,20 +123,4 @@ private function ensureRunning(): void
124123
throw new \RuntimeException('Container must be running');
125124
}
126125
}
127-
128-
private function getImageVersionFromDockerfile(): string
129-
{
130-
$dockerfile = __DIR__ . '/../docker/Dockerfile';
131-
132-
if (!file_exists($dockerfile)) {
133-
throw new \RuntimeException('Dockerfile not found at ' . $dockerfile);
134-
}
135-
136-
$content = file_get_contents($dockerfile);
137-
if (!preg_match('/^FROM\s+thenativeweb\/eventsourcingdb:(.+)$/m', $content, $matches)) {
138-
throw new \RuntimeException('Failed to extract image version from Dockerfile');
139-
}
140-
141-
return trim($matches[1]);
142-
}
143126
}

tests/ClientTest.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class ClientTest extends TestCase
88
{
99
public function testPingSucceedsWhenServerIsReachable(): void
1010
{
11-
$container = new Container();
11+
$imageVersion = $this->getImageVersionFromDockerfile();
12+
$container = new Container()->withImageTag($imageVersion);
1213
$container->start();
1314
try {
1415
$client = $container->getClient();
@@ -21,7 +22,8 @@ public function testPingSucceedsWhenServerIsReachable(): void
2122

2223
public function testPingFailsWhenServerIsUnreachable(): void
2324
{
24-
$container = new Container();
25+
$imageVersion = $this->getImageVersionFromDockerfile();
26+
$container = new Container()->withImageTag($imageVersion);
2527
$container->start();
2628
try {
2729
$port = $container->getMappedPort();
@@ -33,4 +35,50 @@ public function testPingFailsWhenServerIsUnreachable(): void
3335
$container->stop();
3436
}
3537
}
38+
39+
public function testVerifyApiTokenDoesNotThrowAnErrorIfTheTokenIsValid(): void
40+
{
41+
$imageVersion = $this->getImageVersionFromDockerfile();
42+
$container = new Container()->withImageTag($imageVersion);
43+
$container->start();
44+
try {
45+
$client = $container->getClient();
46+
$client->verifyApiToken();
47+
$this->expectNotToPerformAssertions();
48+
} finally {
49+
$container->stop();
50+
}
51+
}
52+
53+
public function testVerifyApiTokenThrowsAnErrorIfTheTokenIsInvalid(): void
54+
{
55+
$imageVersion = $this->getImageVersionFromDockerfile();
56+
$container = new Container()->withImageTag($imageVersion);
57+
$container->start();
58+
try {
59+
$baseUrl = $container->getBaseUrl();
60+
$apiToken = $container->getApiToken() . '-invalid';
61+
$client = new Client($baseUrl, $apiToken);
62+
$this->expectException(Throwable::class);
63+
$client->verifyApiToken();
64+
} finally {
65+
$container->stop();
66+
}
67+
}
68+
69+
private function getImageVersionFromDockerfile(): string
70+
{
71+
$dockerfile = __DIR__ . '/../docker/Dockerfile';
72+
73+
if (!file_exists($dockerfile)) {
74+
throw new \RuntimeException('Dockerfile not found at ' . $dockerfile);
75+
}
76+
77+
$content = file_get_contents($dockerfile);
78+
if (!preg_match('/^FROM\s+thenativeweb\/eventsourcingdb:(.+)$/m', $content, $matches)) {
79+
throw new \RuntimeException('Failed to extract image version from Dockerfile');
80+
}
81+
82+
return trim($matches[1]);
83+
}
3684
}

0 commit comments

Comments
 (0)