Skip to content

Commit 9921e8f

Browse files
committed
added more tests
1 parent 827a9ec commit 9921e8f

26 files changed

+239
-78
lines changed

config/development.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ module.exports = {
88
secret: process.env.SECRET || 'lakikihdgdfdjjjdgd67264664vdjhjdyncmxuei8336%%^#%gdvdhj????jjhdghduue',
99
mongoURL: process.env.MONGOLAB_URL || 'mongodb://192.168.99.100/snipe',
1010
noFrontendCaching: process.env.NO_CACHE || 'yes',
11-
rateLimit: process.env.RATE_LIMIT || '180',
11+
rateLimit: process.env.RATE_LIMIT || '1800',
1212
rateLimitExpiry: process.env.RATE_LIMIT_EXPIRY || '3600000',
1313
redisURL: process.env.REDIS_URL || 'redis://192.168.99.100:6379/1',
14-
userVerificationEndpoint: process.env.USER_VERIFICATION_API || 'http://mockbin.org/bin/9edd4bf7-bb36-47b6-adb2-54d7a7236e80',
15-
appVerificationEndpoint: process.env.APP_VERIFICATION_API || 'http://mockbin.org/bin/9edd4bf7-bb36-47b6-adb2-54d7a7236e80',
1614
letsencryptSSLVerificationURL: process.env.LETSENCRYPT_VERIFICATION_URL || '/.well-known/acme-challenge/xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8',
1715
letsencryptSSLVerificationBody: process.env.LETSENCRYPT_VERIFICATION_BODY || 'xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8._v6aFbaRYWeOmSebtlD-X4Ixf5tPsyULMsXM8HjsK-Q',
1816
maxContentLength: process.env.MAX_CONTENT_LENGTH || '9999',

config/production.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
module.exports = {
33
env: process.env.NODE_ENV || 'production',
44
port: process.env.PORT || 80,
5-
trustProxy: process.env.TRUST_PROXY || 'no',
5+
trustProxy: process.env.TRUST_PROXY || 'yes',
66
bugsnagKey: process.env.BUGSNAG_KEY || false,
77
secureMode: process.env.SECURE_MODE || true,
88
secret: process.env.SECRET || 'lakikihdgdfdjjjdgd67264660okjnbgtrdxswerfgytg373745ei8336%%^#%gdvdhj????jjhdghduue',
99
mongoURL: process.env.MONGOLAB_URL || 'mongodb://192.168.99.100/snipe',
10-
noFrontendCaching: process.env.NO_CACHE || 'yes',
11-
rateLimit: process.env.RATE_LIMIT || '180',
10+
noFrontendCaching: process.env.NO_CACHE || 'no',
11+
rateLimit: process.env.RATE_LIMIT || '1800',
1212
rateLimitExpiry: process.env.RATE_LIMIT_EXPIRY || '3600000',
1313
redisURL: process.env.REDIS_URL || 'redis://192.168.99.100:6379/1',
14-
userVerificationEndpoint: process.env.USER_VERIFICATION_API || 'http://mockbin.org/bin/9edd4bf7-bb36-47b6-adb2-54d7a7236e80',
15-
appVerificationEndpoint: process.env.APP_VERIFICATION_API || 'http://mockbin.org/bin/9edd4bf7-bb36-47b6-adb2-54d7a7236e80',
1614
letsencryptSSLVerificationURL: process.env.LETSENCRYPT_VERIFICATION_URL || '/.well-known/acme-challenge/xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8',
1715
letsencryptSSLVerificationBody: process.env.LETSENCRYPT_VERIFICATION_BODY || 'xvArhQBSilF4V30dGUagNAZ96ASipB0b0ex0kXn0za8._v6aFbaRYWeOmSebtlD-X4Ixf5tPsyULMsXM8HjsK-Q',
1816
maxContentLength: process.env.MAX_CONTENT_LENGTH || '9999',

gulpfile.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,34 @@ var stylish = require('jshint-stylish');
55
var nodemon = require('gulp-nodemon');
66
var debug = require('debug')('gulp');
77
var mocha = require('gulp-mocha');
8-
8+
99
gulp.task('lint', function() {
1010
return gulp.src(['./*.js','./**/*.js','!./node_modules/**','!./node_modules/*.js'])
11-
.pipe(jshint())
12-
.pipe(jshint.reporter(stylish));
11+
.pipe(jshint())
12+
.pipe(jshint.reporter(stylish));
1313
});
1414

1515
gulp.task('default', function(){
1616
var stream = nodemon({ script: 'app.js' , env: { 'NODE_ENV': 'development', 'DEBUG':'gulp' }, tasks: ['lint','test'] });
17-
17+
1818
stream
19-
.on('restart', function () {
19+
.on('restart', function () {
2020
debug('restarted!');
21-
})
22-
.on('crash', function() {
21+
})
22+
.on('crash', function() {
2323
debug('Application has crashed!\n');
2424
stream.emit('restart', 10); // restart the server in 10 seconds
25-
});
25+
});
2626
});
27-
27+
2828
gulp.task('test', function() {
29-
gulp.src('./test', {read: false})
29+
// Override RATE LIMIT HERE FOR UNIT TEST
30+
process.env.RATE_LIMIT = 10;
31+
gulp.src('./test', {read: false})
3032
// `gulp-mocha` needs filepaths so you can't have any plugins before it
3133
.pipe(mocha({reporter: 'spec'}));
3234
}
33-
);
35+
);
3436

3537
gulp.task('create', function(){
3638

models/RequestLogs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ Schema.pre('save', function(next) {
111111
// Indexing for search
112112
var ourDoc = this._doc;
113113
var split = [];
114+
// ToDo: Move this to a queue and index only pure strings
114115
for(var n in ourDoc){
115116
if(typeof ourDoc[n] === 'string'){
116117
split.push(ourDoc[n].split(' '));
@@ -165,6 +166,7 @@ Schema.pre('update', function(next) {
165166
// Indexing for search
166167
var ourDoc = this._update.$set;
167168
var split = [];
169+
// ToDo: Move this to a queue and index only pure strings
168170
for(var n in ourDoc){
169171
if(typeof ourDoc[n] === 'string'){
170172
split.push(ourDoc[n].split(' '));
@@ -181,5 +183,6 @@ Schema.pre('update', function(next) {
181183
});
182184

183185
var Model = db.model(collection, Schema);
186+
Model._mongoose = db;
184187

185188
module.exports = Model;

models/Trash.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Schema.pre('save', function(next) {
8585
// Indexing for search
8686
var ourDoc = this._doc;
8787
var split = [];
88+
// ToDo: Move this to a queue and index only pure strings
8889
for(var n in ourDoc){
8990
if(typeof ourDoc[n] === 'string'){
9091
split.push(ourDoc[n].split(' '));
@@ -139,6 +140,7 @@ Schema.pre('update', function(next) {
139140
// Indexing for search
140141
var ourDoc = this._update.$set;
141142
var split = [];
143+
// ToDo: Move this to a queue and index only pure strings
142144
for(var n in ourDoc){
143145
if(typeof ourDoc[n] === 'string'){
144146
split.push(ourDoc[n].split(' '));
@@ -155,5 +157,6 @@ Schema.pre('update', function(next) {
155157
});
156158

157159
var Model = db.model(collection, Schema);
160+
Model._mongoose = db;
158161

159162
module.exports = Model;

models/Users.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Schema.pre('save', function(next) {
8282
// Indexing for search
8383
var ourDoc = this._doc;
8484
var split = [];
85+
// ToDo: Move this to a queue and index only pure strings
8586
for(var n in ourDoc){
8687
if(typeof ourDoc[n] === 'string'){
8788
split.push(ourDoc[n].split(' '));
@@ -136,6 +137,7 @@ Schema.pre('update', function(next) {
136137
// Indexing for search
137138
var ourDoc = this._update.$set;
138139
var split = [];
140+
// ToDo: Move this to a queue and index only pure strings
139141
for(var n in ourDoc){
140142
if(typeof ourDoc[n] === 'string'){
141143
split.push(ourDoc[n].split(' '));
@@ -152,5 +154,6 @@ Schema.pre('update', function(next) {
152154
});
153155

154156
var Model = db.model(collection, Schema);
157+
Model._mongoose = db;
155158

156159
module.exports = Model;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"mongoose-mock": "^0.4.0",
3434
"proxyquire": "^1.8.0",
3535
"sinon": "^2.3.4",
36-
"sinon-chai": "^2.11.0"
36+
"sinon-chai": "^2.11.0",
37+
"supertest": "^3.0.0"
3738
},
3839
"dependencies": {
3940
"aes-js": "^3.0.0",

routes/index.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var url = require('url');
2121
var fnv = require('fnv-plus');
2222
var RequestLogs = require('../models/RequestLogs');
2323

24-
var sanitizeRequestUrl = function(req) {
24+
router._sanitizeRequestUrl = function(req) {
2525
var requestUrl = url.format({
2626
protocol: req.protocol,
2727
host: req.hostname,
@@ -32,7 +32,7 @@ var sanitizeRequestUrl = function(req) {
3232
return requestUrl.replace(/(password=).*?(&|$)/ig, '$1<hidden>$2');
3333
};
3434

35-
var allRequestData = function(req,res,next){
35+
router._allRequestData = function(req,res,next){
3636
var requestData = {};
3737
req.param = function(key, defaultValue){
3838
var newRequestData = _.assignIn(requestData, req.params, req.body, req.query);
@@ -47,7 +47,7 @@ var allRequestData = function(req,res,next){
4747
next();
4848
};
4949

50-
var enforceUserIdAndAppId = function(req,res,next){
50+
router._enforceUserIdAndAppId = function(req,res,next){
5151
var userId = req.param('userId');
5252
var appId = req.param('appId');
5353
if(!userId){
@@ -61,6 +61,37 @@ var enforceUserIdAndAppId = function(req,res,next){
6161
}
6262
};
6363

64+
// Log requests here
65+
router.use(function(req,res,next){
66+
var ipAddress = req.get('x-forwarded-for') || req.connection && req.connection.remoteAddress;
67+
req.requestId = fnv.hash(new Date().valueOf() + ipAddress, 128).str();
68+
69+
var reqLog = {
70+
RequestId: req.requestId,
71+
ipAddress: ipAddress,
72+
url: router._sanitizeRequestUrl(req),
73+
method: req.method,
74+
body: _.omit(req.body, ['password','cardno']),
75+
app: req.appId,
76+
user: req.userId,
77+
device: req.get('user-agent'),
78+
createdAt: new Date()
79+
};
80+
// ToDo: Move this to a queue. Not good for performance
81+
RequestLogs.create(reqLog)
82+
.then(function(res){
83+
return _.identity(res);
84+
})
85+
.catch(function(err){
86+
log.error(err);
87+
});
88+
89+
// persist RequestLog entry in the background; continue immediately
90+
91+
log.info(reqLog);
92+
next();
93+
});
94+
6495
router.use(helmet());
6596
// no client side caching
6697
if(config.noFrontendCaching === 'yes'){
@@ -76,7 +107,7 @@ router.use(encryption.interpreter);
76107
router.use(hpp());
77108
router.use(contentLength.validateMax({max: MAX_CONTENT_LENGTH_ACCEPTED, status: 400, message: "Stop! Maximum content length exceeded."})); // max size accepted for the content-length
78109
// add the param function to request object
79-
router.use(allRequestData);
110+
router.use(router._allRequestData);
80111

81112
// API Rate limiter
82113
limiter({
@@ -92,33 +123,6 @@ limiter({
92123

93124
router.use(response);
94125
router.use(expressValidator());
95-
// Log requests here
96-
router.use(function(req,res,next){
97-
var ipAddress = req.get('x-forwarded-for') || req.connection && req.connection.remoteAddress;
98-
req.requestId = fnv.hash(new Date().valueOf() + ipAddress, 128).str();
99-
100-
var reqLog = {
101-
RequestId: req.requestId,
102-
ipAddress: ipAddress,
103-
url: sanitizeRequestUrl(req),
104-
method: req.method,
105-
body: _.omit(req.body, ['password','cardno']),
106-
app: req.appId,
107-
user: req.userId,
108-
device: req.get('user-agent'),
109-
createdAt: new Date()
110-
};
111-
112-
RequestLogs.create(reqLog)
113-
.then(function(res){
114-
return _.identity(res);
115-
});
116-
117-
// persist RequestLog entry in the background; continue immediately
118-
119-
log.info(reqLog);
120-
next();
121-
});
122126

123127
router.get('/', function (req, res) {
124128
res.ok({name: me.name, version: me.version});
@@ -137,7 +141,7 @@ router.use('/', initialize);
137141

138142

139143
// Make userId compolsory in every request
140-
router.use(enforceUserIdAndAppId);
144+
router.use(router._enforceUserIdAndAppId);
141145

142146
// Other routes here
143147
//

services/encryption/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ module.exports = {
9999

100100
interpreter: function(req, res, next){
101101
var encryption = require('./');
102-
if(req.header('x-tag')){
103-
res.set('x-tag', req.header('x-tag'));
102+
if( req.get('x-tag') ){
103+
res.set('x-tag', req.get('x-tag'));
104104
res.set('Access-Control-Expose-Headers','x-tag');
105105

106-
var key = req.header('x-tag');
106+
var key = req.get('x-tag');
107107

108108
if(req.method === 'POST' && config.secureMode){
109109
if(req.body.secureData){

services/response/badRequest.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ module.exports = function(data, message){
77
log.warn('Sending bad request response: ', data, message || 'bad request');
88
var req = this.req;
99
var res = this;
10-
10+
// ToDo: Move this to a queue. Not good for performance
1111
RequestLogs.update({RequestId: req.requestId},{response: {status: 'error', data: data, message: message ? message : 'bad request'}})
1212
.then(function(res){
1313
return _.identity(res);
14+
})
15+
.catch(function(err){
16+
log.error(err);
1417
});
1518

1619
if (data !== undefined && data !== null) {

0 commit comments

Comments
 (0)