0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-20 22:52:46 -05:00

test(unit): add unit test for ping and whoiam

This commit is contained in:
Juan Picado @jotadeveloper 2018-01-25 21:36:26 +01:00
parent 19579a2e26
commit eaa0e3f8bd
No known key found for this signature in database
GPG key ID: 18AC54485952D158
8 changed files with 141 additions and 55 deletions

View file

@ -121,6 +121,7 @@
"stylelint-config-recommended-scss": "3.0.0",
"stylelint-scss": "2.2.0",
"stylelint-webpack-plugin": "0.10.1",
"supertest": "^3.0.0",
"url-loader": "0.6.2",
"webpack": "3.10.0",
"webpack-dev-server": "2.11.1",

20
src/api/debug/index.js Normal file
View file

@ -0,0 +1,20 @@
import _ from 'lodash';
export default (app, selfPath) => {
// Hook for tests only
app.get('/-/_debug', function(req, res, next) {
const doGarbabeCollector = _.isNil(global.gc) === false;
if (doGarbabeCollector) {
global.gc();
}
next({
pid: process.pid,
main: process.mainModule.filename,
conf: selfPath,
mem: process.memoryUsage(),
gc: doGarbabeCollector,
});
});
};

View file

@ -1,5 +1,3 @@
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const Middleware = require('../web/middleware');

View file

@ -5,6 +5,7 @@ import _ from 'lodash';
import cors from 'cors';
import Storage from '../lib/storage';
import {loadPlugin} from '../lib/plugin-loader';
import hookDebug from './debug';
const Auth = require('../lib/auth');
const Logger = require('../lib/logger');
@ -24,34 +25,9 @@ module.exports = function(configHash) {
app.set('env', process.env.NODE_ENV || 'production');
app.use(cors());
// Middleware
const error_reporting_middleware = function(req, res, next) {
res.report_error = res.report_error || function(err) {
if (err.status && err.status >= 400 && err.status < 600) {
if (_.isNil(res.headersSent) === false) {
res.status(err.status);
next({error: err.message || 'unknown error'});
}
} else {
Logger.logger.error( {err: err}
, 'unexpected error: @{!err.message}\n@{err.stack}');
if (!res.status || !res.send) {
Logger.logger.error('this is an error in express.js, please report this');
res.destroy();
} else if (!res.headersSent) {
res.status(500);
next({error: 'internal server error'});
} else {
// socket should be already closed
}
}
};
next();
};
// Router setup
app.use(Middleware.log);
app.use(error_reporting_middleware);
app.use(Middleware.errorReportingMiddleware);
app.use(function(req, res, next) {
res.setHeader('X-Powered-By', config.user_agent);
next();
@ -66,20 +42,9 @@ module.exports = function(configHash) {
// Hook for tests only
if (config._debug) {
app.get('/-/_debug', function(req, res, next) {
const do_gc = _.isNil(global.gc) === false;
if (do_gc) {
global.gc();
}
next({
pid: process.pid,
main: process.mainModule.filename,
conf: config.self_path,
mem: process.memoryUsage(),
gc: do_gc,
});
});
hookDebug(app, config.self_path);
}
// register middleware plugins
const plugin_params = {
config: config,
@ -118,7 +83,7 @@ module.exports = function(configHash) {
if (_.isFunction(res.report_error) === false) {
// in case of very early error this middleware may not be loaded before error is generated
// fixing that
error_reporting_middleware(req, res, _.noop);
Middleware.errorReportingMiddleware(req, res, _.noop);
}
res.report_error(err);
} else {

View file

@ -251,3 +251,29 @@ module.exports.log = function(req, res, next) {
};
next();
};
// Middleware
module.exports.errorReportingMiddleware = function(req, res, next) {
res.report_error = res.report_error || function(err) {
if (err.status && err.status >= 400 && err.status < 600) {
if (_.isNil(res.headersSent) === false) {
res.status(err.status);
next({error: err.message || 'unknown error'});
}
} else {
Logger.logger.error( {err: err}
, 'unexpected error: @{!err.message}\n@{err.stack}');
if (!res.status || !res.send) {
Logger.logger.error('this is an error in express.js, please report this');
res.destroy();
} else if (!res.headersSent) {
res.status(500);
next({error: 'internal server error'});
} else {
// socket should be already closed
}
}
};
next();
};

48
test/unit/api/api.spec.js Normal file
View file

@ -0,0 +1,48 @@
import request from 'supertest';
import configDefault from '../partials/config';
import Config from '../../../src/lib/config';
import Storage from '../../../src/lib/storage';
import Auth from '../../../src/lib/auth';
import indexAPI from '../../../src/api/index';
require('../../../src/lib/logger').setup([]);
describe('endpoint unit test', () => {
let config;
let storage;
let auth;
let app;
beforeAll(function() {
config = new Config(configDefault);
storage = new Storage(config);
auth = new Auth(config);
app = indexAPI(config, auth, storage);
});
describe('ping unit test', () => {
test('test /-/ping', (done) => {
request(app)
.get('/-/ping')
.expect('Content-Type', /json/)
.expect(200, done)
});
});
describe('whoami unit test', () => {
test('test /-/whoami', (done) => {
request(app)
.get('/-/whoami')
.expect('Content-Type', /json/)
.expect(200, done)
});
test('test /whoami', (done) => {
request(app)
.get('/-/whoami')
.expect('Content-Type', /json/)
.expect(200, done)
});
});
});

View file

@ -1,5 +1,3 @@
'use strict';
const config = {
storage: __dirname + '/store/test-storage',
uplinks: {

View file

@ -1973,7 +1973,7 @@ compare-func@^1.3.1:
array-ify "^1.0.0"
dot-prop "^3.0.0"
component-emitter@^1.2.1:
component-emitter@^1.2.0, component-emitter@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.2.1.tgz#137918d6d78283f7df7a6b7c5a63e140e69425e6"
@ -2188,6 +2188,10 @@ cookie@0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"
cookiejar@^2.1.0:
version "2.1.1"
resolved "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.1.tgz#41ad57b1b555951ec171412a81942b1e8200d34a"
cookies@^0.7.0:
version "0.7.1"
resolved "https://registry.yarnpkg.com/cookies/-/cookies-0.7.1.tgz#7c8a615f5481c61ab9f16c833731bcb8f663b99b"
@ -3521,6 +3525,14 @@ forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
form-data@^2.3.1, form-data@~2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.1.tgz#6fb94fbd71885306d73d15cc497fe4cc4ecd44bf"
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.5"
mime-types "^2.1.12"
form-data@~2.1.1:
version "2.1.4"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.1.4.tgz#33c183acf193276ecaa98143a69e94bfee1750d1"
@ -3529,13 +3541,9 @@ form-data@~2.1.1:
combined-stream "^1.0.5"
mime-types "^2.1.12"
form-data@~2.3.1:
version "2.3.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.1.tgz#6fb94fbd71885306d73d15cc497fe4cc4ecd44bf"
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.5"
mime-types "^2.1.12"
formidable@^1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/formidable/-/formidable-1.1.1.tgz#96b8886f7c3c3508b932d6bd70c4d3a88f35f1a9"
forwarded@~0.1.2:
version "0.1.2"
@ -5571,7 +5579,7 @@ merge@^1.1.3:
version "1.2.0"
resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da"
methods@~1.1.2:
methods@^1.1.1, methods@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
@ -6846,7 +6854,7 @@ q@^1.1.2, q@^1.4.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
qs@6.5.1, qs@~6.5.1:
qs@6.5.1, qs@^6.5.1, qs@~6.5.1:
version "6.5.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8"
@ -7089,7 +7097,7 @@ readable-stream@1.0:
isarray "0.0.1"
string_decoder "~0.10.x"
readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.2.9, readable-stream@^2.3.3:
readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@^2.1.4, readable-stream@^2.1.5, readable-stream@^2.2.2, readable-stream@^2.2.9, readable-stream@^2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.3.tgz#368f2512d79f9d46fdfc71349ae7878bbc1eb95c"
dependencies:
@ -8157,6 +8165,28 @@ sugarss@^1.0.0:
dependencies:
postcss "^6.0.14"
superagent@^3.0.0:
version "3.8.2"
resolved "https://registry.npmjs.org/superagent/-/superagent-3.8.2.tgz#e4a11b9d047f7d3efeb3bbe536d9ec0021d16403"
dependencies:
component-emitter "^1.2.0"
cookiejar "^2.1.0"
debug "^3.1.0"
extend "^3.0.0"
form-data "^2.3.1"
formidable "^1.1.1"
methods "^1.1.1"
mime "^1.4.1"
qs "^6.5.1"
readable-stream "^2.0.5"
supertest@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/supertest/-/supertest-3.0.0.tgz#8d4bb68fd1830ee07033b1c5a5a9a4021c965296"
dependencies:
methods "~1.1.2"
superagent "^3.0.0"
supports-color@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"