Initial commit: add .gitignore and README

This commit is contained in:
defiQUG
2026-02-09 21:51:45 -08:00
commit 929fe6f6b6
240 changed files with 40977 additions and 0 deletions

View File

@@ -0,0 +1,161 @@
import { OperatorService } from '@/gateway/auth/operator-service';
import { JWTService } from '@/gateway/auth/jwt';
import { OperatorRole } from '@/gateway/auth/types';
import { TestHelpers } from '../utils/test-helpers';
describe('Authentication Security', () => {
let testOperatorId: string;
let testOperatorDbId: string;
const testPassword = 'SecurePass123!@#';
beforeAll(async () => {
const operator = await TestHelpers.createTestOperator('TEST_AUTH', 'MAKER' as any, testPassword);
testOperatorId = operator.operatorId;
testOperatorDbId = operator.id;
});
afterAll(async () => {
await TestHelpers.cleanDatabase();
});
describe('OperatorService.verifyCredentials', () => {
it('should authenticate with correct credentials', async () => {
const operator = await OperatorService.verifyCredentials({
operatorId: testOperatorId,
password: testPassword,
});
expect(operator).not.toBeNull();
expect(operator?.operatorId).toBe(testOperatorId);
expect(operator?.active).toBe(true);
});
it('should reject incorrect password', async () => {
const operator = await OperatorService.verifyCredentials({
operatorId: testOperatorId,
password: 'WrongPassword123!',
});
expect(operator).toBeNull();
});
it('should reject non-existent operator', async () => {
const operator = await OperatorService.verifyCredentials({
operatorId: 'NON_EXISTENT',
password: 'AnyPassword123!',
});
expect(operator).toBeNull();
});
it('should reject inactive operator', async () => {
// Create inactive operator
const inactiveOp = await TestHelpers.createTestOperator('INACTIVE_OP', 'MAKER' as any);
// In real scenario, would deactivate in database
// For now, test assumes active check works
const operator = await OperatorService.verifyCredentials({
operatorId: inactiveOp.operatorId,
password: 'Test123!@#',
});
// Should either be null or have active=false check
expect(operator).toBeDefined(); // Actual behavior depends on implementation
});
});
describe('JWTService', () => {
it('should generate valid JWT token', () => {
const token = JWTService.generateToken({
operatorId: testOperatorId,
id: testOperatorDbId,
role: OperatorRole.MAKER,
});
expect(token).toBeDefined();
expect(typeof token).toBe('string');
expect(token.split('.')).toHaveLength(3); // JWT has 3 parts
});
it('should verify valid JWT token', () => {
const payload = {
operatorId: testOperatorId,
id: testOperatorDbId,
role: OperatorRole.MAKER,
};
const token = JWTService.generateToken(payload);
const decoded = JWTService.verifyToken(token);
expect(decoded).toBeDefined();
expect(decoded.operatorId).toBe(payload.operatorId);
expect(decoded.id).toBe(payload.id);
expect(decoded.role).toBe(payload.role);
});
it('should reject invalid JWT token', () => {
const invalidToken = 'invalid.jwt.token';
expect(() => {
JWTService.verifyToken(invalidToken);
}).toThrow();
});
it('should reject expired JWT token', () => {
// Generate token with short expiration (if supported)
const payload = {
operatorId: testOperatorId,
id: testOperatorDbId,
role: OperatorRole.MAKER,
};
// For this test, we'd need to create a token with expiration
// and wait or mock time. This is a placeholder.
const token = JWTService.generateToken(payload);
// Token should be valid immediately
expect(() => {
JWTService.verifyToken(token);
}).not.toThrow();
});
it('should include correct claims in token', () => {
const payload = {
operatorId: 'TEST_CLAIMS',
id: 'test-id-123',
role: OperatorRole.CHECKER,
terminalId: 'TERM-001',
};
const token = JWTService.generateToken(payload);
const decoded = JWTService.verifyToken(token);
expect(decoded.operatorId).toBe(payload.operatorId);
expect(decoded.id).toBe(payload.id);
expect(decoded.role).toBe(payload.role);
if (payload.terminalId) {
expect(decoded.terminalId).toBe(payload.terminalId);
}
});
});
describe('Password Security', () => {
it('should hash passwords (not store plaintext)', async () => {
const newOperator = await TestHelpers.createTestOperator(
'TEST_PWD_HASH',
'MAKER' as any,
'PlainPassword123!'
);
// Verify we can authenticate (password is hashed in DB)
const operator = await OperatorService.verifyCredentials({
operatorId: newOperator.operatorId,
password: 'PlainPassword123!',
});
expect(operator).not.toBeNull();
// Password should be hashed in database (verify by checking DB if needed)
});
});
});

216
tests/security/rbac.test.ts Normal file
View File

@@ -0,0 +1,216 @@
import { requireRole } from '@/gateway/rbac/rbac';
import { OperatorRole } from '@/gateway/auth/types';
import { TestHelpers } from '../utils/test-helpers';
import { Response, NextFunction } from 'express';
describe('RBAC (Role-Based Access Control)', () => {
let makerOperator: any;
let checkerOperator: any;
let adminOperator: any;
beforeAll(async () => {
makerOperator = await TestHelpers.createTestOperator('TEST_RBAC_MAKER', 'MAKER' as any);
checkerOperator = await TestHelpers.createTestOperator('TEST_RBAC_CHECKER', 'CHECKER' as any);
adminOperator = await TestHelpers.createTestOperator('TEST_RBAC_ADMIN', 'ADMIN' as any);
});
afterAll(async () => {
await TestHelpers.cleanDatabase();
});
describe('requireRole', () => {
it('should allow MAKER role for MAKER endpoints', async () => {
const middleware = requireRole(OperatorRole.MAKER);
const req = {
operator: {
id: makerOperator.id,
operatorId: makerOperator.operatorId,
role: OperatorRole.MAKER,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
middleware(req, res, next);
// Middleware is synchronous, next should be called immediately
expect(next).toHaveBeenCalled(); // No error passed
});
it('should allow ADMIN role for MAKER endpoints', async () => {
const middleware = requireRole(OperatorRole.MAKER);
const req = {
operator: {
id: adminOperator.id,
operatorId: adminOperator.operatorId,
role: OperatorRole.ADMIN,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
middleware(req, res, next);
// Middleware is synchronous, next should be called immediately
expect(next).toHaveBeenCalled(); // No error passed
});
it('should reject CHECKER role for MAKER-only endpoints', async () => {
const middleware = requireRole(OperatorRole.MAKER);
const req = {
operator: {
id: checkerOperator.id,
operatorId: checkerOperator.operatorId,
role: OperatorRole.CHECKER,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
await middleware(req, res, next);
expect(res.status).toHaveBeenCalledWith(403);
});
it('should allow CHECKER role for CHECKER endpoints', async () => {
const middleware = requireRole(OperatorRole.CHECKER);
const req = {
operator: {
id: checkerOperator.id,
operatorId: checkerOperator.operatorId,
role: OperatorRole.CHECKER,
},
} as any;
const res = {} as Response;
const next = jest.fn() as NextFunction;
await middleware(req, res, next);
// Middleware is synchronous, next should be called immediately
expect(next).toHaveBeenCalled();
});
it('should allow ADMIN role for CHECKER endpoints', async () => {
const middleware = requireRole(OperatorRole.CHECKER);
const req = {
operator: {
id: adminOperator.id,
operatorId: adminOperator.operatorId,
role: OperatorRole.ADMIN,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
middleware(req, res, next);
// Middleware is synchronous, next should be called immediately
expect(next).toHaveBeenCalled();
});
it('should require ADMIN role for ADMIN-only endpoints', async () => {
const middleware = requireRole(OperatorRole.ADMIN);
const req = {
operator: {
id: adminOperator.id,
operatorId: adminOperator.operatorId,
role: OperatorRole.ADMIN,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
middleware(req, res, next);
// Middleware is synchronous, next should be called immediately
expect(next).toHaveBeenCalled();
});
it('should reject MAKER role for ADMIN-only endpoints', async () => {
const middleware = requireRole(OperatorRole.ADMIN);
const req = {
operator: {
id: makerOperator.id,
operatorId: makerOperator.operatorId,
role: OperatorRole.MAKER,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
await middleware(req, res, next);
expect(res.status).toHaveBeenCalledWith(403);
});
});
describe('Dual Control Enforcement', () => {
it('should enforce MAKER can initiate but not approve', () => {
// MAKER can use MAKER endpoints
const makerMiddleware = requireRole(OperatorRole.MAKER);
const makerReq = {
operator: {
id: makerOperator.id,
role: OperatorRole.MAKER,
},
} as any;
const res = {} as Response;
const next = jest.fn() as NextFunction;
makerMiddleware(makerReq, res, next);
expect(next).toHaveBeenCalledWith();
// MAKER cannot use CHECKER endpoints
const checkerMiddleware = requireRole(OperatorRole.CHECKER);
const checkerRes = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
checkerMiddleware(makerReq, checkerRes, next);
expect(checkerRes.status).toHaveBeenCalledWith(403);
});
it('should enforce CHECKER can approve but not initiate', () => {
// CHECKER can use CHECKER endpoints
const checkerMiddleware = requireRole(OperatorRole.CHECKER);
const checkerReq = {
operator: {
id: checkerOperator.id,
operatorId: checkerOperator.operatorId,
role: OperatorRole.CHECKER,
},
} as any;
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
const next = jest.fn() as NextFunction;
checkerMiddleware(checkerReq, res, next);
expect(next).toHaveBeenCalledWith();
// CHECKER cannot use MAKER-only endpoints (if restricted)
const makerMiddleware = requireRole(OperatorRole.MAKER);
const makerRes = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
} as unknown as Response;
makerMiddleware(checkerReq, makerRes, next);
expect(makerRes.status).toHaveBeenCalledWith(403);
});
});
});