0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-27 22:59:51 -05:00
verdaccio/packages/node-api/test/node-api.spec.ts

183 lines
6.3 KiB
TypeScript
Raw Normal View History

2018-06-28 18:33:37 +02:00
import path from 'path';
import selfsigned from 'selfsigned';
import os from 'os';
import fs from 'fs';
2018-06-28 18:33:37 +02:00
2020-03-03 23:59:19 +01:00
import { configExample } from '@verdaccio/mock';
2020-03-08 09:19:12 +01:00
import {DEFAULT_DOMAIN, DEFAULT_PROTOCOL} from '@verdaccio/dev-commons';
2020-03-03 23:59:19 +01:00
import {parseConfigFile} from '@verdaccio/utils';
import { logger } from '@verdaccio/logger';
import { startVerdaccio } from '../src';
jest.mock('@verdaccio/logger', () => ({
setup: jest.fn(),
logger: {
child: jest.fn(),
debug: jest.fn(),
trace: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
2020-03-03 23:59:19 +01:00
fatal: jest.fn()
}
}));
describe('startServer via API', () => {
2020-03-03 23:59:19 +01:00
const parseConfigurationFile = (name) => {
2020-03-03 23:59:19 +01:00
return parseConfigFile(path.join(__dirname, `./partials/config/yaml/${name}.yaml`));
};
describe('startServer launcher', () => {
test('should provide all HTTP server data', async (done) => {
const store = path.join(__dirname, 'partials/store');
2018-06-28 18:33:37 +02:00
const serverName = 'verdaccio-test';
const version = '1.0.0';
const port = '6000';
2020-03-03 23:59:19 +01:00
await startVerdaccio(configExample(), port, store, version, serverName,
(webServer, addrs, pkgName, pkgVersion) => {
expect(webServer).toBeDefined();
expect(addrs).toBeDefined();
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
expect(addrs.host).toBe(DEFAULT_DOMAIN);
expect(addrs.port).toBe(port);
expect(pkgName).toBeDefined();
expect(pkgVersion).toBeDefined();
expect(pkgVersion).toBe(version);
expect(pkgName).toBe(serverName);
done();
});
});
test('should set keepAliveTimeout to 0 seconds', async (done) => {
const store = path.join(__dirname, 'partials/store');
const serverName = 'verdaccio-test';
const version = '1.0.0';
const port = '6100';
2020-03-03 23:59:19 +01:00
await startVerdaccio(configExample(parseConfigurationFile('server/keepalivetimeout-0')), port, store, version, serverName,
(webServer, addrs, pkgName, pkgVersion) => {
expect(webServer).toBeDefined();
expect(webServer.keepAliveTimeout).toBeDefined();
expect(webServer.keepAliveTimeout).toBe(0);
expect(addrs).toBeDefined();
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
expect(addrs.host).toBe(DEFAULT_DOMAIN);
expect(addrs.port).toBe(port);
expect(pkgName).toBeDefined();
expect(pkgVersion).toBeDefined();
expect(pkgVersion).toBe(version);
expect(pkgName).toBe(serverName);
done();
});
});
test('should set keepAliveTimeout to 60 seconds', async (done) => {
const store = path.join(__dirname, 'partials/store');
const serverName = 'verdaccio-test';
const version = '1.0.0';
const port = '6200';
2020-03-03 23:59:19 +01:00
await startVerdaccio(configExample(parseConfigurationFile('server/keepalivetimeout-60')), port, store, version, serverName,
(webServer, addrs, pkgName, pkgVersion) => {
expect(webServer).toBeDefined();
expect(webServer.keepAliveTimeout).toBeDefined();
expect(webServer.keepAliveTimeout).toBe(60000);
expect(addrs).toBeDefined();
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
expect(addrs.host).toBe(DEFAULT_DOMAIN);
expect(addrs.port).toBe(port);
expect(pkgName).toBeDefined();
expect(pkgVersion).toBeDefined();
expect(pkgVersion).toBe(version);
expect(pkgName).toBe(serverName);
done();
});
});
test('should set keepAliveTimeout to 5 seconds per default', async (done) => {
const store = path.join(__dirname, 'partials/store');
const serverName = 'verdaccio-test';
const version = '1.0.0';
const port = '6300';
2020-03-03 23:59:19 +01:00
await startVerdaccio(configExample(parseConfigurationFile('server/keepalivetimeout-undefined')), port, store, version, serverName,
(webServer, addrs, pkgName, pkgVersion) => {
expect(webServer).toBeDefined();
expect(webServer.keepAliveTimeout).toBeDefined();
expect(webServer.keepAliveTimeout).toBe(5000);
expect(addrs).toBeDefined();
expect(addrs.proto).toBe(DEFAULT_PROTOCOL);
expect(addrs.host).toBe(DEFAULT_DOMAIN);
expect(addrs.port).toBe(port);
expect(pkgName).toBeDefined();
expect(pkgVersion).toBeDefined();
expect(pkgVersion).toBe(version);
expect(pkgName).toBe(serverName);
done();
});
});
2020-03-03 23:59:19 +01:00
test('should provide all HTTPS server fails', async (done) => {
const store = path.join(__dirname, 'partials/store');
const serverName = 'verdaccio-test';
const version = '1.0.0';
const address = 'https://www.domain.com:443';
2020-03-03 23:59:19 +01:00
const realProcess = process;
2020-03-03 23:59:19 +01:00
const conf = configExample();
conf.https = {};
// save process to catch exist
2020-03-03 23:59:19 +01:00
const exitMock = jest.fn();
// @ts-ignore
global.process = { ...realProcess, exit: exitMock };
await startVerdaccio(conf, address, store, version, serverName, () => {
2019-09-26 18:22:14 +02:00
expect(logger.fatal).toHaveBeenCalled();
expect(logger.fatal).toHaveBeenCalledTimes(2);
2020-03-03 23:59:19 +01:00
done();
});
2020-03-03 23:59:19 +01:00
expect(exitMock).toHaveBeenCalledWith(2);
// restore process
global.process = realProcess;
});
test('should start a https server with key and cert', async (done) => {
const store = path.join(__dirname, 'partials/store');
const serverName = 'verdaccio-test';
const version = '1.0.0';
const address = 'https://www.domain.com:443';
const { private: key, cert } = selfsigned.generate();
const keyPath = path.join(os.tmpdir(), 'key.pem');
const certPath = path.join(os.tmpdir(), 'crt.pem');
fs.writeFileSync(keyPath, key);
fs.writeFileSync(certPath, cert);
const conf = config();
conf.https = {
key: keyPath,
cert: certPath,
};
2020-03-03 23:59:19 +01:00
await startServer(conf, address, store, version, serverName,
(webServer, addrs) => {
expect(webServer).toBeDefined();
expect(addrs).toBeDefined();
expect(addrs.proto).toBe('https');
done();
});
2020-03-03 23:59:19 +01:00
})
test('should fails if config is missing', async () => {
try {
// @ts-ignore
2020-03-03 23:59:19 +01:00
await startVerdaccio();
} catch (e) {
expect(e.message).toEqual('config file must be an object');
}
});
2020-03-03 23:59:19 +01:00
});
});