@@ -9,11 +9,17 @@ chai.use(chaiAsPromised);
9
9
var request = require ( 'supertest' ) ;
10
10
var router = require ( '../../routes' ) ;
11
11
var express = require ( 'express' ) ;
12
+ var sinon = require ( "sinon" ) ;
13
+ var sinonChai = require ( "sinon-chai" ) ;
14
+ chai . use ( sinonChai ) ;
12
15
13
16
var app = express ( ) ;
17
+
14
18
app . use ( '/' , router ) ;
15
19
20
+
16
21
var agent = request . agent ( app ) ;
22
+ var requestId ;
17
23
18
24
describe ( 'Test rate limiting' , function ( ) {
19
25
@@ -73,15 +79,95 @@ describe('Test rate limiting', function(){
73
79
} )
74
80
. then ( function ( res ) {
75
81
console . log ( 'Limit: ' , res . headers [ 'x-ratelimit-limit' ] , '| Remaining: ' , res . headers [ 'x-ratelimit-remaining' ] , ' | Body: ' , res . body ) ;
82
+ requestId = res . headers [ 'x-request-id' ] ;
76
83
done ( ) ;
77
84
} )
78
85
. catch ( function ( err ) {
79
86
done ( err ) ;
80
87
} ) ;
81
88
} ) ;
89
+
90
+ it ( 'should save rate limit error on request log' , function ( done ) {
91
+ var RequestLog = require ( '../../models/RequestLogs' ) ;
92
+ // It takes a while for the request log to update. So let us delay the test for 1 sec
93
+ setTimeout ( function ( ) {
94
+ RequestLog . findOne ( { RequestId : requestId } )
95
+ . then ( function ( res ) {
96
+ console . log ( 'the request' , res ) ;
97
+ res . response . data . statusCode . should . equal ( 429 ) ;
98
+ done ( ) ;
99
+ } )
100
+ . catch ( function ( err ) {
101
+ done ( err ) ;
102
+ } ) ;
103
+ } , 1000 ) ;
104
+
105
+ } ) ;
106
+
107
+ var res = { } ;
108
+ var req = { } ;
109
+
110
+ var nextChecker = false ;
111
+ var next = function ( ) {
112
+ if ( arguments . length > 0 ) {
113
+ console . log ( arguments [ 0 ] ) ;
114
+ } else {
115
+ nextChecker = true ;
116
+ }
117
+
118
+ return nextChecker ;
119
+ } ;
120
+ res . json = function ( data ) {
121
+ return res ;
122
+ } ;
123
+
124
+ res . badRequest = sinon . spy ( ) ;
125
+
126
+ res . status = function ( status ) {
127
+ return res ;
128
+ } ;
129
+
130
+ var header = { } ;
131
+ res . set = function ( key , value ) {
132
+ header [ key ] = value ;
133
+ return header [ key ] ;
134
+ } ;
135
+ req . get = function ( key ) {
136
+ return header [ key ] ;
137
+ } ;
138
+
139
+ header . set = function ( data ) {
140
+ header . temp = data ;
141
+ return header . temp ;
142
+ } ;
143
+
144
+ req . method = '' ;
145
+
146
+ it ( 'should contain a param function' , function ( done ) {
147
+ router . _allRequestData ( req , res , next ) ;
148
+ nextChecker . should . be . true ; /* jslint ignore:line */
149
+ nextChecker = false ;
150
+ req . param . should . be . a ( 'function' ) ;
151
+ done ( ) ;
82
152
} ) ;
83
153
84
- // test rate limiting
85
- // Test that the request limit error is logged on the DB
86
- // Test allRequestData middleware
87
- // Test enforceUserId middleware
154
+ it ( 'should enforce UserId' , function ( done ) {
155
+ router . _allRequestData ( req , res , next ) ;
156
+ nextChecker = false ;
157
+ router . _enforceUserIdAndAppId ( req , res , next ) ;
158
+ res . badRequest . should . be . called . once ; /* jslint ignore:line */
159
+ res . badRequest . should . be . calledWith ( false , 'No userId parameter was passed in the payload of this request. Please pass a userId.' ) ;
160
+ req . body = { } ;
161
+ req . body . userId = 'jdjdjdjd' ;
162
+ router . _enforceUserIdAndAppId ( req , res , next ) ;
163
+ res . badRequest . should . be . called . twice ; /* jslint ignore:line */
164
+ res . badRequest . should . be . calledWith ( false , 'No appId parameter was passed in the payload of this request. Please pass an appId.' ) ;
165
+ nextChecker . should . be . false ; /* jslint ignore:line */
166
+ req . body . appId = 'jdjdjdjd' ;
167
+ router . _enforceUserIdAndAppId ( req , res , next ) ;
168
+ res . badRequest . should . be . called . twice ; /* jslint ignore:line */
169
+ nextChecker . should . be . true ; /* jslint ignore:line */
170
+ done ( ) ;
171
+ } ) ;
172
+
173
+ } ) ;
0 commit comments