feat: initial commit of camelia/controllers
Framework-agnostic REST controller toolkit in vanilla PHP. - Router with path parameters, 404 and 405 with an Allow header. - Immutable Request, JsonRequest and Response; nothing is printed. - CrudController over a ResourceGateway port the application implements. - Composable Middleware/Pipeline chain. - Security middlewares: BearerAuth, RequireRole, RateLimit, Cors. - Transport middlewares: Logging, ContentNegotiation. - Nine typed HTTP exceptions with snake_case JSON bodies. - Declares PHP >=8.3 and no dependency on an ORM: ports are local. - MIT license.
This commit is contained in:
commit
341d1bdc2c
52 files changed
+7552
No files matched your search
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Controllers\Tests\Unit\Security;
|
||||
|
||||
use Camelia\Controllers\Exception\BadRequest;
|
||||
use Camelia\Controllers\JsonRequest;
|
||||
use Camelia\Controllers\Request;
|
||||
use Camelia\Controllers\Response;
|
||||
use Camelia\Controllers\Router;
|
||||
use Camelia\Controllers\Security\BearerAuth;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class BearerAuthTest extends TestCase
|
||||
{
|
||||
private static function passThrough(Request $request): Response
|
||||
{
|
||||
return new Response(['ok' => true]);
|
||||
}
|
||||
|
||||
public function testMissingHeaderIs401(): void
|
||||
{
|
||||
$auth = new BearerAuth(static fn(string $token): array => ['id' => 1]);
|
||||
|
||||
$response = $auth->process(new JsonRequest(), self::passThrough(...));
|
||||
|
||||
self::assertSame(401, $response->status());
|
||||
self::assertSame('unauthorized', $response->body()['code']);
|
||||
self::assertSame('Bearer', $response->headers()['WWW-Authenticate']);
|
||||
}
|
||||
|
||||
public function testNonBearerSchemeIs401(): void
|
||||
{
|
||||
$auth = new BearerAuth(static fn(string $token): array => ['id' => 1]);
|
||||
|
||||
$response = $auth->process(
|
||||
new JsonRequest(headers: ['authorization' => 'Basic dXNlcjpwYXNz']),
|
||||
self::passThrough(...),
|
||||
);
|
||||
|
||||
self::assertSame(401, $response->status());
|
||||
self::assertStringContainsString('Bearer scheme', $response->body()['message']);
|
||||
}
|
||||
|
||||
public function testInvalidTokenIs401(): void
|
||||
{
|
||||
$auth = new BearerAuth(static fn(string $token): ?array => null);
|
||||
|
||||
$response = $auth->process(
|
||||
new JsonRequest(headers: ['authorization' => 'Bearer expired']),
|
||||
self::passThrough(...),
|
||||
);
|
||||
|
||||
self::assertSame(401, $response->status());
|
||||
self::assertStringContainsString('invalid or expired', $response->body()['message']);
|
||||
}
|
||||
|
||||
public function testValidTokenAttachesIdentity(): void
|
||||
{
|
||||
$auth = new BearerAuth(static fn(string $token): array => [
|
||||
'id' => 7,
|
||||
'roles' => ['admin'],
|
||||
]);
|
||||
|
||||
$response = $auth->process(
|
||||
new JsonRequest(headers: ['authorization' => 'Bearer good-token']),
|
||||
static function (Request $request): Response {
|
||||
return new Response($request->attribute(BearerAuth::ATTRIBUTE));
|
||||
},
|
||||
);
|
||||
|
||||
self::assertSame(200, $response->status());
|
||||
self::assertSame(['id' => 7, 'roles' => ['admin']], $response->body());
|
||||
}
|
||||
|
||||
public function testSchemeIsCaseInsensitive(): void
|
||||
{
|
||||
$auth = new BearerAuth(static fn(string $token): array => ['id' => 1]);
|
||||
|
||||
$response = $auth->process(
|
||||
new JsonRequest(headers: ['authorization' => 'bearer abc']),
|
||||
self::passThrough(...),
|
||||
);
|
||||
|
||||
self::assertSame(200, $response->status());
|
||||
}
|
||||
|
||||
public function testRouterRejectsBeforeReachingController(): void
|
||||
{
|
||||
$router = (new Router())->withMiddleware(
|
||||
new BearerAuth(static fn(string $token): ?array => null),
|
||||
);
|
||||
$router->get('/x', static fn(): Response => new Response(['leaked' => true]));
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/x'));
|
||||
|
||||
self::assertSame(401, $response->status());
|
||||
self::assertStringNotContainsString('leaked', $response->toJson());
|
||||
}
|
||||
|
||||
public function testRouterAllowsValidToken(): void
|
||||
{
|
||||
$router = (new Router())->withMiddleware(new BearerAuth(static fn(string $token): array => [
|
||||
'id' => 1,
|
||||
]));
|
||||
$router->get(
|
||||
'/x',
|
||||
static fn(Request $request): Response => new Response($request->attribute(
|
||||
BearerAuth::ATTRIBUTE,
|
||||
)),
|
||||
);
|
||||
|
||||
$response = $router->dispatch(new JsonRequest(path: '/x', headers: [
|
||||
'authorization' => 'Bearer t',
|
||||
]));
|
||||
|
||||
self::assertSame(200, $response->status());
|
||||
self::assertSame(['id' => 1], $response->body());
|
||||
}
|
||||
|
||||
public function testEmptyRoleListIsRejectedAtConstruction(): void
|
||||
{
|
||||
$this->expectException(BadRequest::class);
|
||||
|
||||
new \Camelia\Controllers\Security\RequireRole([]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user