Setzt Phase 1 aus Visualization.md um: Vanilla-PHP-REST-API mit SQLite-Persistenz, statischen API-Keys pro Agent, Codeberg-Sync fuer Backlog/Done, PHPUnit-Tests und vollstaendiger API.md-Dokumentation - Vorgaben aus Detailantworten.md (F1-F16) umgesetzt. Entwicklung/Build/Test laufen komplett in Docker-Compose-Containern (PHP 8.2 + SQLite + Composer + PHPUnit/pcov), analog zum NC-Gantt-Projekt ist auf dem Host kein PHP/Composer noetig. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Monitor\Api\Tests\Auth;
|
|
|
|
use Monitor\Api\Auth\AgentAuthenticator;
|
|
use Monitor\Api\Db\Database;
|
|
use Monitor\Api\Http\Request;
|
|
use Monitor\Api\Repository\AgentRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class AgentAuthenticatorTest extends TestCase
|
|
{
|
|
public function testAuthenticateReturnsAgentForValidKey(): void
|
|
{
|
|
$agents = new AgentRepository(Database::inMemory());
|
|
$registration = $agents->register('Alice', 'entwickler');
|
|
$auth = new AgentAuthenticator($agents);
|
|
|
|
$request = Request::create('GET', '/x', headers: ['X-Api-Key' => $registration['api_key']]);
|
|
|
|
$result = $auth->authenticate($request);
|
|
|
|
self::assertNotNull($result);
|
|
self::assertSame($registration['agent']['id'], $result['id']);
|
|
}
|
|
|
|
public function testAuthenticateReturnsNullWithoutHeader(): void
|
|
{
|
|
$agents = new AgentRepository(Database::inMemory());
|
|
$auth = new AgentAuthenticator($agents);
|
|
|
|
self::assertNull($auth->authenticate(Request::create('GET', '/x')));
|
|
}
|
|
|
|
public function testAuthenticateReturnsNullForWrongKey(): void
|
|
{
|
|
$agents = new AgentRepository(Database::inMemory());
|
|
$agents->register('Alice', 'entwickler');
|
|
$auth = new AgentAuthenticator($agents);
|
|
|
|
$request = Request::create('GET', '/x', headers: ['X-Api-Key' => 'wrong']);
|
|
|
|
self::assertNull($auth->authenticate($request));
|
|
}
|
|
}
|