Skip to content

Commit 1d0c5d7

Browse files
committed
added tests for responses
1 parent b51c5fb commit 1d0c5d7

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ gulp.task('default', function(){
2828
gulp.task('test', function() {
2929
// Override RATE LIMIT HERE FOR UNIT TEST
3030
process.env.RATE_LIMIT = 10;
31+
process.env.SECURE_MODE = true;
3132
gulp.src('./test', {read: false})
3233
// `gulp-mocha` needs filepaths so you can't have any plugins before it
3334
.pipe(mocha({reporter: 'spec'}));

test/mocha.opts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
--recursive
2-
--timeout 180000
2+
--timeout 5000

test/services/response.js

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
process.env.SECURE_MODE = true;
4-
53
var chai = require('chai');
64
chai.should();
75
var config = require('../../config');
@@ -53,9 +51,63 @@ header.set = function(data){
5351

5452
req.method = '';
5553

54+
var response = require('../../services/response');
55+
56+
var express = require('express');
57+
var app = express();
58+
var bodyParser = require('body-parser');
59+
var request = require('supertest');
60+
61+
// Dummy App
62+
app.use(bodyParser.urlencoded({ extended: false }));
63+
app.use(bodyParser.json());
64+
app.use(response);
65+
66+
app.get('/ok', function(req,res){
67+
res.ok('It worked!');
68+
});
69+
70+
app.get('/badRequest', function(req,res){
71+
res.badRequest('It worked!');
72+
});
73+
74+
app.get('/forbidden', function(req,res){
75+
res.forbidden('It worked!');
76+
});
77+
78+
app.get('/notFound', function(req,res){
79+
res.notFound('It worked!');
80+
});
81+
82+
app.get('/serverError', function(req,res){
83+
res.serverError('It worked!');
84+
});
85+
86+
app.get('/unauthorized', function(req,res){
87+
res.unauthorized('It worked!');
88+
});
89+
90+
91+
var encryption = require('../../services/encryption');
92+
var app2 = express();
93+
94+
// Dummy App
95+
app2.use(bodyParser.urlencoded({ extended: false }));
96+
app2.use(bodyParser.json());
97+
app2.use(response);
98+
app2.use(encryption.interpreter);
99+
100+
app2.post('/secure', function(req,res){
101+
res.ok('It worked!');
102+
});
103+
104+
var agent = request(app);
105+
106+
var agent2 = request(app2);
107+
56108
// Testing response service
57109

58-
var response = require('../../services/response');
110+
59111
describe('#Response service test', function(){
60112
it('should add property ok, badRequest, forbidden, notFound, serverError and unauthorized to res object', function(done){
61113
response(req,res,next);
@@ -68,4 +120,90 @@ describe('#Response service test', function(){
68120
res.should.have.property('unauthorized');
69121
done();
70122
});
123+
124+
it('should be ok', function(done){
125+
agent.
126+
get('/ok')
127+
.expect(200,done);
128+
});
129+
130+
it('should be a badRequest', function(done){
131+
agent.
132+
get('/badRequest')
133+
.expect(400,done);
134+
});
135+
it('should be forbidden', function(done){
136+
agent.
137+
get('/forbidden')
138+
.expect(503,done);
139+
});
140+
it('should not be found', function(done){
141+
agent.
142+
get('/notFound')
143+
.expect(404,done);
144+
});
145+
it('should be unauthorized', function(done){
146+
agent.
147+
get('/unauthorized')
148+
.expect(401,done);
149+
});
150+
it('should be a serverError', function(done){
151+
agent.
152+
get('/serverError')
153+
.expect(500,done);
154+
});
155+
156+
it('should be an encrypted response', function(done){
157+
var tag;
158+
encryption.generateKey()
159+
.then(function(res){
160+
tag = res;
161+
return encryption.encrypt(demoData, tag);
162+
})
163+
.then(function(res){
164+
console.log('Our encrypted data: ', res);
165+
return agent2.
166+
post('/secure')
167+
.set('x-tag', tag)
168+
.send({truth: demoDataHash,secureData: res})
169+
.expect(200);
170+
})
171+
.then(function(res){
172+
console.log('Our response body: ', res.body);
173+
var data = res.body;
174+
data.secure.should.be.true; /* jslint ignore:line */
175+
done();
176+
})
177+
.catch(function(err){
178+
done(err);
179+
});
180+
});
181+
182+
it('should detect tampered data', function(done){
183+
var tag;
184+
encryption.generateKey()
185+
.then(function(res){
186+
tag = res;
187+
var demoData2 = '{"escribimos": "silencio es dorado"}';
188+
return encryption.encrypt(demoData2, tag);
189+
})
190+
.then(function(res){
191+
console.log('Our encrypted data: ', res);
192+
return agent2.
193+
post('/secure')
194+
.set('x-tag', tag)
195+
.send({truth: demoDataHash,secureData: res})
196+
.expect(500);
197+
})
198+
.then(function(res){
199+
console.log('Our response body: ', res.body);
200+
done();
201+
})
202+
.catch(function(err){
203+
done(err);
204+
});
205+
});
206+
71207
});
208+
209+
// ToDo: Test all responses and also encrypted responses

0 commit comments

Comments
 (0)