Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
coverage/
.idea/
logs/
tsconfig.tsbuildinfo
9 changes: 9 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
require: ['ts-node/register'],
extension: ['ts'],
timeout: 0,
ui: 'bdd',
spec: 'src/**/__tests__/**/*.test.ts',
recursive: true,
exit: true
};
14 changes: 0 additions & 14 deletions jest.config.js

This file was deleted.

24 changes: 15 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,42 @@
"scripts": {
"start": "nodemon bin/enclaved-bitgo-express",
"build": "yarn tsc --build --incremental --verbose . && cp package.json dist/",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test": "mocha --require ts-node/register 'src/**/__tests__/**/*.test.ts'",
"test:watch": "mocha --require ts-node/register --watch 'src/**/__tests__/**/*.test.ts'",
"test:coverage": "nyc mocha --require ts-node/register 'src/**/__tests__/**/*.test.ts'",
"lint": "eslint --quiet .",
"generate-test-ssl": "openssl req -x509 -newkey rsa:2048 -keyout test-ssl-key.pem -out test-ssl-cert.pem -days 365 -nodes -subj '/CN=localhost'"
},
"dependencies": {
"@api-ts/io-ts-http": "^3.2.1",
"@api-ts/response": "^2.1.0",
"@api-ts/openapi-generator": "^5.7.0",
"@api-ts/typed-express-router": "^1.1.13",
"@api-ts/response": "^2.1.0",
"@api-ts/typed-express-router": "^1.1.13",
"@bitgo/sdk-core": "^35.2.0",
"bitgo": "^48.0.0",
"body-parser": "^1.20.3",
"connect-timeout": "^1.9.0",
"debug": "^3.1.0",
"io-ts": "2.1.3",
"winston": "^3.11.0",
"express": "4.17.3",
"io-ts": "2.1.3",
"lodash": "^4.17.20",
"morgan": "^1.9.1",
"proxy-agent": "6.4.0",
"proxyquire": "^2.1.3",
"superagent": "^8.0.9",
"winston": "^3.11.0",
"zod": "^3.25.48"
},
"devDependencies": {
"@api-ts/openapi-generator": "^5.7.0",
"nodemon": "^3.1.10",
"@types/body-parser": "^1.17.0",
"@types/connect-timeout": "^1.9.0",
"@types/debug": "^4.1.12",
"@types/express": "4.17.13",
"@types/jasmine": "^5.1.8",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.14.121",
"@types/mocha": "^10.0.10",
"@types/morgan": "^1.7.35",
"@types/node": "^16.18.46",
"@types/sinon": "^10.0.11",
Expand All @@ -55,8 +56,11 @@
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.0.0",
"eslint-plugin-prettier": "^4.0.0",
"jasmine": "^5.8.0",
"jest": "^29.7.0",
"mocha": "^11.6.0",
"nock": "^13.3.1",
"nodemon": "^3.1.10",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"should": "^13.2.3",
Expand All @@ -65,7 +69,9 @@
"sinon": "^13.0.1",
"supertest": "^4.0.2",
"ts-jest": "^29.1.2",
"typescript": "^4.2.4"
"ts-node": "^10.9.2",
"typescript": "^4.2.4",
"typescript-cached-transpile": "^0.0.6"
},
"engines": {
"node": ">=22.1.0"
Expand Down
80 changes: 43 additions & 37 deletions src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'should';
import { config, isEnclavedConfig, TlsMode } from '../config';

describe('Configuration', () => {
Expand All @@ -6,23 +7,26 @@ describe('Configuration', () => {
const mockTlsCert = '-----BEGIN CERTIFICATE-----\nMOCK_CERT\n-----END CERTIFICATE-----';

beforeEach(() => {
jest.resetModules();
process.env = { ...originalEnv };
// Clear TLS-related environment variables
delete process.env.TLS_MODE;
});

afterAll(() => {
after(() => {
process.env = originalEnv;
});

it('should throw error when APP_MODE is not set', () => {
expect(() => config()).toThrow('APP_MODE environment variable is required');
(() => config()).should.throw(
'APP_MODE environment variable is required. Set APP_MODE to either "enclaved" or "master-express"',
);
});

it('should throw error when APP_MODE is invalid', () => {
process.env.APP_MODE = 'invalid';
expect(() => config()).toThrow('Invalid APP_MODE: invalid');
(() => config()).should.throw(
'Invalid APP_MODE: invalid. Must be either "enclaved" or "master-express"',
);
});

describe('Enclaved Mode', () => {
Expand All @@ -36,64 +40,66 @@ describe('Configuration', () => {

it('should use default configuration when no environment variables are set', () => {
const cfg = config();
expect(isEnclavedConfig(cfg)).toBe(true);
isEnclavedConfig(cfg).should.be.true();
if (isEnclavedConfig(cfg)) {
expect(cfg.port).toBe(3080);
expect(cfg.bind).toBe('localhost');
expect(cfg.tlsMode).toBe(TlsMode.MTLS);
expect(cfg.timeout).toBe(305 * 1000);
expect(cfg.kmsUrl).toBe('http://localhost:3000');
expect(cfg.tlsKey).toBe(mockTlsKey);
expect(cfg.tlsCert).toBe(mockTlsCert);
cfg.port.should.equal(3080);
cfg.bind.should.equal('localhost');
cfg.tlsMode.should.equal(TlsMode.MTLS);
cfg.timeout.should.equal(305 * 1000);
cfg.kmsUrl.should.equal('http://localhost:3000');
cfg.tlsKey!.should.equal(mockTlsKey);
cfg.tlsCert!.should.equal(mockTlsCert);
}
});

it('should read port from environment variable', () => {
process.env.ENCLAVED_EXPRESS_PORT = '4000';
const cfg = config();
expect(isEnclavedConfig(cfg)).toBe(true);
isEnclavedConfig(cfg).should.be.true();
if (isEnclavedConfig(cfg)) {
expect(cfg.port).toBe(4000);
expect(cfg.kmsUrl).toBe('http://localhost:3000');
expect(cfg.tlsKey).toBe(mockTlsKey);
expect(cfg.tlsCert).toBe(mockTlsCert);
cfg.port.should.equal(4000);
cfg.kmsUrl.should.equal('http://localhost:3000');
cfg.tlsKey!.should.equal(mockTlsKey);
cfg.tlsCert!.should.equal(mockTlsCert);
}
});

it('should read TLS mode from environment variables', () => {
// Test with TLS disabled
process.env.TLS_MODE = 'disabled';
let cfg = config();
expect(isEnclavedConfig(cfg)).toBe(true);
isEnclavedConfig(cfg).should.be.true();
if (isEnclavedConfig(cfg)) {
expect(cfg.tlsMode).toBe(TlsMode.DISABLED);
expect(cfg.kmsUrl).toBe('http://localhost:3000');
cfg.tlsMode.should.equal(TlsMode.DISABLED);
cfg.kmsUrl.should.equal('http://localhost:3000');
}

// Test with mTLS explicitly enabled
process.env.TLS_MODE = 'mtls';
cfg = config();
expect(isEnclavedConfig(cfg)).toBe(true);
isEnclavedConfig(cfg).should.be.true();
if (isEnclavedConfig(cfg)) {
expect(cfg.tlsMode).toBe(TlsMode.MTLS);
expect(cfg.kmsUrl).toBe('http://localhost:3000');
expect(cfg.tlsKey).toBe(mockTlsKey);
expect(cfg.tlsCert).toBe(mockTlsCert);
cfg.tlsMode.should.equal(TlsMode.MTLS);
cfg.kmsUrl.should.equal('http://localhost:3000');
cfg.tlsKey!.should.equal(mockTlsKey);
cfg.tlsCert!.should.equal(mockTlsCert);
}

// Test with invalid TLS mode
process.env.TLS_MODE = 'invalid';
expect(() => config()).toThrow('Invalid TLS_MODE: invalid');
(() => config()).should.throw(
'Invalid TLS_MODE: invalid. Must be either "disabled" or "mtls"',
);

// Test with no TLS mode (should default to MTLS)
delete process.env.TLS_MODE;
cfg = config();
expect(isEnclavedConfig(cfg)).toBe(true);
isEnclavedConfig(cfg).should.be.true();
if (isEnclavedConfig(cfg)) {
expect(cfg.tlsMode).toBe(TlsMode.MTLS);
expect(cfg.kmsUrl).toBe('http://localhost:3000');
expect(cfg.tlsKey).toBe(mockTlsKey);
expect(cfg.tlsCert).toBe(mockTlsCert);
cfg.tlsMode.should.equal(TlsMode.MTLS);
cfg.kmsUrl.should.equal('http://localhost:3000');
cfg.tlsKey!.should.equal(mockTlsKey);
cfg.tlsCert!.should.equal(mockTlsCert);
}
});

Expand All @@ -103,13 +109,13 @@ describe('Configuration', () => {
process.env.MTLS_ALLOWED_CLIENT_FINGERPRINTS = 'ABC123,DEF456';

const cfg = config();
expect(isEnclavedConfig(cfg)).toBe(true);
isEnclavedConfig(cfg).should.be.true();
if (isEnclavedConfig(cfg)) {
expect(cfg.mtlsRequestCert).toBe(true);
expect(cfg.mtlsAllowedClientFingerprints).toEqual(['ABC123', 'DEF456']);
expect(cfg.kmsUrl).toBe('http://localhost:3000');
expect(cfg.tlsKey).toBe(mockTlsKey);
expect(cfg.tlsCert).toBe(mockTlsCert);
cfg.mtlsRequestCert!.should.be.true();
cfg.mtlsAllowedClientFingerprints!.should.deepEqual(['ABC123', 'DEF456']);
cfg.kmsUrl.should.equal('http://localhost:3000');
cfg.tlsKey!.should.equal(mockTlsKey);
cfg.tlsCert!.should.equal(mockTlsCert);
}
});
});
Expand Down
6 changes: 4 additions & 2 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'should';

describe('Basic test setup', () => {
it('should pass a basic test', () => {
expect(true).toBe(true);
true.should.be.true();
});

it('should handle basic math', () => {
expect(1 + 1).toBe(2);
(1 + 1).should.equal(2);
});
});
Loading