feat: Phase 1 MVP — complete AgentIdP implementation
Implements all P0 features per OpenSpec change phase-1-mvp-implementation: - Agent Registry Service (CRUD) — full lifecycle management - OAuth 2.0 Token Service (Client Credentials flow) - Credential Management (generate, rotate, revoke) - Immutable Audit Log Service Tech: Node.js 18+, TypeScript 5.3+ strict, Express 4.18+, PostgreSQL 14+, Redis 7+ Standards: OpenAPI 3.0 specs, DRY/SOLID, zero `any` types Quality: 18 unit test suites, 244 tests passing, 97%+ coverage OpenAPI: 4 complete specs (14 endpoints total) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
225
tests/unit/controllers/AuditController.test.ts
Normal file
225
tests/unit/controllers/AuditController.test.ts
Normal file
@@ -0,0 +1,225 @@
|
||||
/**
|
||||
* Unit tests for src/controllers/AuditController.ts
|
||||
* AuditService is mocked; handlers are invoked with mock req/res/next.
|
||||
*/
|
||||
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { AuditController } from '../../../src/controllers/AuditController';
|
||||
import { AuditService } from '../../../src/services/AuditService';
|
||||
import { ITokenPayload, IAuditEvent } from '../../../src/types/index';
|
||||
import {
|
||||
ValidationError,
|
||||
AuthenticationError,
|
||||
InsufficientScopeError,
|
||||
AuditEventNotFoundError,
|
||||
} from '../../../src/utils/errors';
|
||||
|
||||
jest.mock('../../../src/services/AuditService');
|
||||
|
||||
const MockAuditService = AuditService as jest.MockedClass<typeof AuditService>;
|
||||
|
||||
// ─── helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
function makeUser(scope: string): ITokenPayload {
|
||||
return {
|
||||
sub: 'agent-id-001',
|
||||
client_id: 'agent-id-001',
|
||||
scope,
|
||||
jti: 'jti-001',
|
||||
iat: 1000,
|
||||
exp: 9999999999,
|
||||
};
|
||||
}
|
||||
|
||||
const MOCK_AUDIT_EVENT: IAuditEvent = {
|
||||
eventId: 'evt-id-001',
|
||||
agentId: 'agent-id-001',
|
||||
action: 'agent.created',
|
||||
outcome: 'success',
|
||||
ipAddress: '127.0.0.1',
|
||||
userAgent: 'test-agent/1.0',
|
||||
metadata: {},
|
||||
timestamp: new Date('2026-03-28T09:00:00Z'),
|
||||
};
|
||||
|
||||
function buildMocks(scope = 'audit:read'): {
|
||||
req: Partial<Request>;
|
||||
res: Partial<Response>;
|
||||
next: NextFunction;
|
||||
} {
|
||||
const res: Partial<Response> = {
|
||||
status: jest.fn().mockReturnThis(),
|
||||
json: jest.fn().mockReturnThis(),
|
||||
send: jest.fn().mockReturnThis(),
|
||||
};
|
||||
return {
|
||||
req: {
|
||||
user: makeUser(scope),
|
||||
body: {},
|
||||
params: {},
|
||||
query: {},
|
||||
headers: {},
|
||||
ip: '127.0.0.1',
|
||||
},
|
||||
res,
|
||||
next: jest.fn() as NextFunction,
|
||||
};
|
||||
}
|
||||
|
||||
// ─── suite ───────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('AuditController', () => {
|
||||
let auditService: jest.Mocked<AuditService>;
|
||||
let controller: AuditController;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
auditService = new MockAuditService({} as never) as jest.Mocked<AuditService>;
|
||||
controller = new AuditController(auditService);
|
||||
});
|
||||
|
||||
// ── queryAuditLog ────────────────────────────────────────────────────────────
|
||||
|
||||
describe('queryAuditLog()', () => {
|
||||
it('should return 200 with paginated audit events', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.query = { page: '1', limit: '50' };
|
||||
const paginatedResponse = { data: [MOCK_AUDIT_EVENT], total: 1, page: 1, limit: 50 };
|
||||
auditService.queryEvents.mockResolvedValue(paginatedResponse);
|
||||
|
||||
await controller.queryAuditLog(req as Request, res as Response, next);
|
||||
|
||||
expect(auditService.queryEvents).toHaveBeenCalledTimes(1);
|
||||
expect(res.status).toHaveBeenCalledWith(200);
|
||||
expect(res.json).toHaveBeenCalledWith(paginatedResponse);
|
||||
});
|
||||
|
||||
it('should call next(AuthenticationError) when req.user is missing', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.user = undefined;
|
||||
|
||||
await controller.queryAuditLog(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(expect.any(AuthenticationError));
|
||||
});
|
||||
|
||||
it('should call next(InsufficientScopeError) when scope does not include audit:read', async () => {
|
||||
const { req, res, next } = buildMocks('agents:read');
|
||||
|
||||
await controller.queryAuditLog(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(expect.any(InsufficientScopeError));
|
||||
expect(auditService.queryEvents).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call next(ValidationError) when query params are invalid', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.query = { page: 'not-a-number' };
|
||||
|
||||
await controller.queryAuditLog(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(expect.any(ValidationError));
|
||||
expect(auditService.queryEvents).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should pass all optional filters to auditService.queryEvents', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
// agentId must be a valid UUID per auditQuerySchema
|
||||
req.query = {
|
||||
page: '2',
|
||||
limit: '10',
|
||||
agentId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
|
||||
action: 'agent.created',
|
||||
outcome: 'success',
|
||||
fromDate: '2026-01-01T00:00:00Z',
|
||||
toDate: '2026-12-31T23:59:59Z',
|
||||
};
|
||||
const emptyResponse = { data: [], total: 0, page: 2, limit: 10 };
|
||||
auditService.queryEvents.mockResolvedValue(emptyResponse);
|
||||
|
||||
await controller.queryAuditLog(req as Request, res as Response, next);
|
||||
|
||||
expect(auditService.queryEvents).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
page: 2,
|
||||
limit: 10,
|
||||
agentId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
|
||||
action: 'agent.created',
|
||||
outcome: 'success',
|
||||
// Joi normalises ISO dates: "2026-01-01T00:00:00Z" → "2026-01-01T00:00:00.000Z"
|
||||
fromDate: expect.stringContaining('2026-01-01'),
|
||||
toDate: expect.stringContaining('2026-12-31'),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should forward service errors to next', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.query = {};
|
||||
const serviceError = new Error('Service error');
|
||||
auditService.queryEvents.mockRejectedValue(serviceError);
|
||||
|
||||
await controller.queryAuditLog(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(serviceError);
|
||||
});
|
||||
});
|
||||
|
||||
// ── getAuditEventById ────────────────────────────────────────────────────────
|
||||
|
||||
describe('getAuditEventById()', () => {
|
||||
it('should return 200 with the audit event', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.params = { eventId: MOCK_AUDIT_EVENT.eventId };
|
||||
auditService.getEventById.mockResolvedValue(MOCK_AUDIT_EVENT);
|
||||
|
||||
await controller.getAuditEventById(req as Request, res as Response, next);
|
||||
|
||||
expect(auditService.getEventById).toHaveBeenCalledWith(MOCK_AUDIT_EVENT.eventId);
|
||||
expect(res.status).toHaveBeenCalledWith(200);
|
||||
expect(res.json).toHaveBeenCalledWith(MOCK_AUDIT_EVENT);
|
||||
});
|
||||
|
||||
it('should call next(AuthenticationError) when req.user is missing', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.user = undefined;
|
||||
req.params = { eventId: 'any' };
|
||||
|
||||
await controller.getAuditEventById(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(expect.any(AuthenticationError));
|
||||
});
|
||||
|
||||
it('should call next(InsufficientScopeError) when scope does not include audit:read', async () => {
|
||||
const { req, res, next } = buildMocks('agents:read');
|
||||
req.params = { eventId: MOCK_AUDIT_EVENT.eventId };
|
||||
|
||||
await controller.getAuditEventById(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(expect.any(InsufficientScopeError));
|
||||
expect(auditService.getEventById).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should forward AuditEventNotFoundError to next', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.params = { eventId: 'nonexistent' };
|
||||
const notFound = new AuditEventNotFoundError('nonexistent');
|
||||
auditService.getEventById.mockRejectedValue(notFound);
|
||||
|
||||
await controller.getAuditEventById(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(notFound);
|
||||
});
|
||||
|
||||
it('should forward service errors to next', async () => {
|
||||
const { req, res, next } = buildMocks();
|
||||
req.params = { eventId: MOCK_AUDIT_EVENT.eventId };
|
||||
const serviceError = new Error('DB error');
|
||||
auditService.getEventById.mockRejectedValue(serviceError);
|
||||
|
||||
await controller.getAuditEventById(req as Request, res as Response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledWith(serviceError);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user