UNPKG

@whook/example

Version:
154 lines (141 loc) 5.43 kB
"use strict"; var _package = _interopRequireDefault(require("../../../package")); var _knifecycle = require("knifecycle"); var _index = require("./index"); var _axios = _interopRequireDefault(require("axios")); var _yerror = _interopRequireDefault(require("yerror")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } describe('runServer', () => { const logger = { info: jest.fn(), error: jest.fn() }; const debug = jest.fn(); const time = jest.fn(); const exit = jest.fn(); const PORT = 9999; const HOST = 'localhost'; const BASE_PATH = '/v4'; async function prepareEnvironment() { const $ = await (0, _index.prepareEnvironment)(); $.register((0, _knifecycle.constant)('API_VERSION', _package.default.version)); $.register((0, _knifecycle.constant)('BASE_PATH', BASE_PATH)); $.register((0, _knifecycle.constant)('ENV', {})); $.register((0, _knifecycle.constant)('PORT', PORT)); $.register((0, _knifecycle.constant)('HOST', HOST)); $.register((0, _knifecycle.constant)('NODE_ENV', 'test')); $.register((0, _knifecycle.constant)('DEBUG_NODE_ENVS', ['test'])); $.register((0, _knifecycle.constant)('NODE_ENVS', ['test'])); $.register((0, _knifecycle.constant)('exit', exit)); $.register((0, _knifecycle.constant)('time', time)); $.register((0, _knifecycle.constant)('logger', logger)); $.register((0, _knifecycle.constant)('debug', debug)); return $; } process.env.ISOLATED_ENV = 1; let $destroy; beforeAll(async () => { const { $destroy: _destroy } = await (0, _index.runServer)(prepareEnvironment, _index.prepareServer, ['$destroy', 'httpServer', 'process']); $destroy = _destroy; }, 5000); afterAll(async () => { await $destroy(); }, 1000); afterEach(() => { time.mockReset(); debug.mockReset(); logger.info.mockReset(); logger.error.mockReset(); }); it('should work', async () => { expect({ debugCalls: debug.mock.calls.map(filterPaths), logInfoCalls: logger.info.mock.calls.map(filterPaths), logErrorCalls: logger.error.mock.calls.map(filterPaths) }).toMatchSnapshot(); }); it('should ping', async () => { time.mockReturnValue(new Date('2014-01-26T00:00:00.000Z').getTime()); const { status, headers, data } = await (0, _axios.default)({ method: 'get', url: `http://${HOST}:${PORT}${BASE_PATH}/ping` }); expect({ status, headers: _objectSpread({}, headers, { // Erasing the Date header that may be added by Axios :/ date: {}.undef }), data, debugCalls: debug.mock.calls.map(filterPaths), logInfoCalls: logger.info.mock.calls.map(filterPaths), logErrorCalls: logger.error.mock.calls.map(filterPaths) }).toMatchSnapshot(); }); it('should authenticate users', async () => { time.mockReturnValue(new Date('2014-01-26T00:00:00.000Z').getTime()); const { status, headers, data } = await (0, _axios.default)({ method: 'get', url: `http://${HOST}:${PORT}${BASE_PATH}/diag`, headers: { authorization: `Fake 1-admin` } }); expect({ status, headers: _objectSpread({}, headers, { // Erasing the Date header that may be added by Axios :/ date: {}.undef }), data, debugCalls: debug.mock.calls.map(filterPaths), logInfoCalls: logger.info.mock.calls.map(filterPaths), logErrorCalls: logger.error.mock.calls.map(filterPaths) }).toMatchSnapshot(); }); it('should fail with bad fake tokens', async () => { time.mockReturnValue(new Date('2014-01-26T00:00:00.000Z').getTime()); try { await (0, _axios.default)({ method: 'get', url: `http://${HOST}:${PORT}${BASE_PATH}/diag`, headers: { authorization: `Fake e-admin` } }); throw new _yerror.default('E_UNEXPECTED_SUCCESS'); } catch (err) { const { status, headers, data } = err.response; expect({ status, headers: _objectSpread({}, headers, { // Erasing the Date header that may be added by Axios :/ date: {}.undef }), data, debugCalls: debug.mock.calls.map(filterPaths), logInfoCalls: logger.info.mock.calls.map(filterPaths), logErrorCalls: logger.error.mock.calls.map(filterPaths).filter(([arg1]) => arg1 !== 'An error occured') }).toMatchSnapshot(); } }); }); function filterPaths(strs) { return strs.map(str => 'string' !== typeof str ? str : str.replace(/ (\/[^/]+){1,}\/whook\//g, ' /home/whoiam/projects/whook/')); }