1
1
'use strict' ;
2
2
3
- process . env . SECURE_MODE = true ;
4
-
5
3
var chai = require ( 'chai' ) ;
6
4
chai . should ( ) ;
7
5
var config = require ( '../../config' ) ;
@@ -53,9 +51,63 @@ header.set = function(data){
53
51
54
52
req . method = '' ;
55
53
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
+
56
108
// Testing response service
57
109
58
- var response = require ( '../../services/response' ) ;
110
+
59
111
describe ( '#Response service test' , function ( ) {
60
112
it ( 'should add property ok, badRequest, forbidden, notFound, serverError and unauthorized to res object' , function ( done ) {
61
113
response ( req , res , next ) ;
@@ -68,4 +120,90 @@ describe('#Response service test', function(){
68
120
res . should . have . property ( 'unauthorized' ) ;
69
121
done ( ) ;
70
122
} ) ;
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
+
71
207
} ) ;
208
+
209
+ // ToDo: Test all responses and also encrypted responses
0 commit comments