106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
/**
|
|
* SDK-Tests (Phase 6): Vertrags-Helper des Modul-SDKs.
|
|
* Das SDK ist CommonJS und wird direkt mit Node getestet.
|
|
*/
|
|
const {
|
|
GATEWAY_HEADERS,
|
|
extractIdentity,
|
|
healthResponse,
|
|
meResponse,
|
|
} = require('../../../modules/demo/backend/platform-module-sdk');
|
|
|
|
describe('platform-module-sdk', () => {
|
|
describe('extractIdentity', () => {
|
|
it('extrahiert die Identität aus korrekten Gateway-Headern', () => {
|
|
const identity = extractIdentity({
|
|
[GATEWAY_HEADERS.userId]: 'user-123',
|
|
[GATEWAY_HEADERS.username]: 'max',
|
|
[GATEWAY_HEADERS.displayName]: 'Max Mustermann',
|
|
[GATEWAY_HEADERS.role]: 'USER',
|
|
});
|
|
|
|
expect(identity).toEqual({
|
|
userId: 'user-123',
|
|
username: 'max',
|
|
displayName: 'Max Mustermann',
|
|
role: 'USER',
|
|
});
|
|
});
|
|
|
|
it('ist case-insensitive bei Header-Namen', () => {
|
|
const identity = extractIdentity({
|
|
'X-User-Id': 'user-123',
|
|
'X-User-Username': 'max',
|
|
'X-User-Role': 'ADMIN',
|
|
});
|
|
|
|
expect(identity).not.toBeNull();
|
|
expect(identity?.role).toBe('ADMIN');
|
|
});
|
|
|
|
it('gibt null zurück, wenn Header fehlen (kein Gateway-Request)', () => {
|
|
expect(extractIdentity({})).toBeNull();
|
|
expect(extractIdentity({ 'x-user-id': 'user-123' })).toBeNull();
|
|
});
|
|
|
|
it('gibt null bei ungültiger Rolle zurück', () => {
|
|
const identity = extractIdentity({
|
|
[GATEWAY_HEADERS.userId]: 'user-123',
|
|
[GATEWAY_HEADERS.username]: 'max',
|
|
[GATEWAY_HEADERS.role]: 'SUPERADMIN',
|
|
});
|
|
expect(identity).toBeNull();
|
|
});
|
|
|
|
it('verwendet den Benutzernamen als Fallback für den Anzeigenamen', () => {
|
|
const identity = extractIdentity({
|
|
[GATEWAY_HEADERS.userId]: 'user-123',
|
|
[GATEWAY_HEADERS.username]: 'max',
|
|
[GATEWAY_HEADERS.role]: 'USER',
|
|
});
|
|
expect(identity?.displayName).toBe('max');
|
|
});
|
|
});
|
|
|
|
describe('healthResponse', () => {
|
|
it('erzeugt eine vertragskonforme /health-Antwort', () => {
|
|
expect(healthResponse('demo', '1.0.0')).toEqual({
|
|
moduleId: 'demo',
|
|
version: '1.0.0',
|
|
status: 'healthy',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('meResponse', () => {
|
|
it('erzeugt eine vertragskonforme /api/me-Antwort', () => {
|
|
const identity = {
|
|
userId: 'user-123',
|
|
username: 'max',
|
|
displayName: 'Max Mustermann',
|
|
role: 'USER' as const,
|
|
};
|
|
|
|
expect(meResponse(identity, 'demo', ['demo.read'])).toEqual({
|
|
user: {
|
|
id: 'user-123',
|
|
username: 'max',
|
|
displayName: 'Max Mustermann',
|
|
platformRole: 'USER',
|
|
},
|
|
module: { id: 'demo' },
|
|
permissions: ['demo.read'],
|
|
});
|
|
});
|
|
|
|
it('verwendet leere Permissions als Default', () => {
|
|
const identity = {
|
|
userId: 'user-123',
|
|
username: 'max',
|
|
displayName: 'Max',
|
|
role: 'ADMIN' as const,
|
|
};
|
|
expect(meResponse(identity, 'demo').permissions).toEqual([]);
|
|
});
|
|
});
|
|
}); |