1. AgentRepository::updateActivity() schrieb current_ticket_id ungeprueft. Eine
unbekannte Ticket-ID lief in die FOREIGN-KEY-Constraint und kam als
ungefangene PDOException samt Stacktrace und Dateipfaden beim Client an.
Jetzt wird die Existenz vorab geprueft (422). Zusaetzlich faengt
Application::handle() unerwartete Throwables ab und antwortet mit einem
generischen 500 - Details gehen nur noch ins Log.
2. Die Normalisierung der Codeberg-Label-Farbe ("#" davor) lag im
CurlCodebergClient statt im CodebergSyncService. Damit haetten andere
Client-Implementierungen unterschiedliche Farbformate ins Ticket geschrieben.
Verschoben in die Domaenenschicht, leere Farben ergeben jetzt null.
3. Config::get() griff selbst auf getenv() zurueck, wodurch ein in der Umgebung
gesetzter CODEBERG_TOKEN in eine explizit gebaute Config durchschlug - im
Testlauf landete der Token im Klartext in der PHPUnit-Ausgabe. Die Umgebung
wird jetzt ausschliesslich in fromEnvironment() eingesammelt.
Regressionstests fuer alle drei Punkte ergaenzt: 104 Tests, 181 Assertions.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
121 lines
3.9 KiB
PHP
121 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Monitor\Api\Tests;
|
|
|
|
use Monitor\Api\Application;
|
|
use Monitor\Api\Config;
|
|
use Monitor\Api\Db\Database;
|
|
use Monitor\Api\Http\Request;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ApplicationTest extends TestCase
|
|
{
|
|
private Application $app;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$config = new Config(['MONITOR_ADMIN_API_KEY' => 'admin-secret', 'MONITOR_STATUS_FILE' => '/does/not/exist']);
|
|
$this->app = new Application(Database::inMemory(), $config);
|
|
}
|
|
|
|
public function testUnknownRouteReturns404(): void
|
|
{
|
|
$response = $this->app->handle(Request::create('GET', '/nope'));
|
|
|
|
self::assertSame(404, $response->status);
|
|
}
|
|
|
|
public function testWrongMethodReturns405(): void
|
|
{
|
|
$response = $this->app->handle(Request::create('DELETE', '/api/v1/tickets'));
|
|
|
|
self::assertSame(405, $response->status);
|
|
}
|
|
|
|
public function testWritingWithoutAgentKeyReturns401(): void
|
|
{
|
|
$response = $this->app->handle(Request::create('POST', '/api/v1/tickets', body: ['title' => 'X']));
|
|
|
|
self::assertSame(401, $response->status);
|
|
}
|
|
|
|
public function testRegisterAgentRequiresAdminKey(): void
|
|
{
|
|
$response = $this->app->handle(Request::create('POST', '/api/v1/agents', body: [
|
|
'display_name' => 'Alice', 'role' => 'entwickler',
|
|
]));
|
|
|
|
self::assertSame(401, $response->status);
|
|
}
|
|
|
|
public function testFullFlowRegisterAgentThenCreateTicket(): void
|
|
{
|
|
$registerResponse = $this->app->handle(Request::create(
|
|
'POST',
|
|
'/api/v1/agents',
|
|
body: ['display_name' => 'Alice', 'role' => 'entwickler'],
|
|
headers: ['X-Admin-Key' => 'admin-secret']
|
|
));
|
|
self::assertSame(201, $registerResponse->status);
|
|
$apiKey = $registerResponse->body()['api_key'];
|
|
|
|
$createResponse = $this->app->handle(Request::create(
|
|
'POST',
|
|
'/api/v1/tickets',
|
|
body: ['title' => 'Neues Ticket'],
|
|
headers: ['X-Api-Key' => $apiKey]
|
|
));
|
|
self::assertSame(201, $createResponse->status);
|
|
|
|
$listResponse = $this->app->handle(Request::create('GET', '/api/v1/tickets'));
|
|
self::assertCount(1, $listResponse->body()['tickets']);
|
|
}
|
|
|
|
/**
|
|
* Regression: Ein unbekanntes Ticket lief bis in die FOREIGN-KEY-Constraint und kam als
|
|
* ungefangene PDOException samt Stacktrace beim Client an.
|
|
*/
|
|
public function testActivityWithUnknownTicketReturns422WithoutInternals(): void
|
|
{
|
|
$registerResponse = $this->app->handle(Request::create(
|
|
'POST',
|
|
'/api/v1/agents',
|
|
body: ['display_name' => 'Alice', 'role' => 'entwickler'],
|
|
headers: ['X-Admin-Key' => 'admin-secret']
|
|
));
|
|
$apiKey = $registerResponse->body()['api_key'];
|
|
$agentId = $registerResponse->body()['agent']['id'];
|
|
|
|
$response = $this->app->handle(Request::create(
|
|
'POST',
|
|
'/api/v1/agents/' . $agentId . '/activity',
|
|
body: ['state' => 'working', 'current_ticket' => 999],
|
|
headers: ['X-Api-Key' => $apiKey]
|
|
));
|
|
|
|
self::assertSame(422, $response->status);
|
|
self::assertStringNotContainsString('PDOException', json_encode($response->body()));
|
|
}
|
|
|
|
public function testPatchUnknownTicketReturns404(): void
|
|
{
|
|
$registerResponse = $this->app->handle(Request::create(
|
|
'POST',
|
|
'/api/v1/agents',
|
|
body: ['display_name' => 'Alice', 'role' => 'entwickler'],
|
|
headers: ['X-Admin-Key' => 'admin-secret']
|
|
));
|
|
$apiKey = $registerResponse->body()['api_key'];
|
|
|
|
$response = $this->app->handle(Request::create(
|
|
'PATCH',
|
|
'/api/v1/tickets/999',
|
|
body: ['column' => 'bereit'],
|
|
headers: ['X-Api-Key' => $apiKey]
|
|
));
|
|
|
|
self::assertSame(404, $response->status);
|
|
}
|
|
}
|