1
1
var _describe = { } ;
2
2
var _it = { } ;
3
3
var _beforeEach = { } ;
4
- var helpers = exports = module . exports = {
4
+ module . exports = {
5
5
describe : _describe ,
6
6
it : _it ,
7
- beforeEach : _beforeEach
7
+ beforeEach : _beforeEach ,
8
+ makeRequest : _makeRequest
8
9
} ;
9
10
var assert = require ( 'assert' ) ;
10
11
var request = require ( 'supertest' ) ;
@@ -24,7 +25,7 @@ _beforeEach.withApp = function(app) {
24
25
this . put = _request . put ;
25
26
this . del = _request . del ;
26
27
} ) ;
27
- }
28
+ } ;
28
29
29
30
_beforeEach . cleanDatasource = function ( dsName ) {
30
31
beforeEach ( function ( done ) {
@@ -39,15 +40,7 @@ _beforeEach.cleanDatasource = function(dsName) {
39
40
done ( ) ;
40
41
}
41
42
} ) ;
42
- }
43
-
44
- function mixin ( obj , into ) {
45
- Object . keys ( obj ) . forEach ( function ( key ) {
46
- if ( typeof obj [ key ] === 'function' ) {
47
- into [ key ] = obj [ key ] ;
48
- }
49
- } ) ;
50
- }
43
+ } ;
51
44
52
45
_describe . staticMethod = function ( methodName , cb ) {
53
46
describe ( '.' + methodName , function ( ) {
@@ -57,7 +50,7 @@ _describe.staticMethod = function(methodName, cb) {
57
50
} ) ;
58
51
cb ( ) ;
59
52
} ) ;
60
- }
53
+ } ;
61
54
62
55
_describe . instanceMethod = function ( methodName , cb ) {
63
56
describe ( '.prototype.' + methodName , function ( ) {
@@ -67,14 +60,14 @@ _describe.instanceMethod = function(methodName, cb) {
67
60
} ) ;
68
61
cb ( ) ;
69
62
} ) ;
70
- }
63
+ } ;
71
64
72
65
_beforeEach . withArgs = function ( ) {
73
66
var args = Array . prototype . slice . call ( arguments , 0 ) ;
74
67
beforeEach ( function ( ) {
75
68
this . args = args ;
76
69
} ) ;
77
- }
70
+ } ;
78
71
79
72
_beforeEach . givenModel = function ( modelName , attrs , optionalHandler ) {
80
73
var modelKey = modelName ;
@@ -125,7 +118,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
125
118
afterEach ( function ( done ) {
126
119
this [ modelKey ] . destroy ( done ) ;
127
120
} ) ;
128
- }
121
+ } ;
129
122
130
123
_beforeEach . withUserModel = function ( model ) {
131
124
beforeEach ( function ( done ) {
@@ -136,13 +129,13 @@ _beforeEach.withUserModel = function(model) {
136
129
137
130
_beforeEach . givenUser = function ( attrs , optionalHandler ) {
138
131
_beforeEach . givenModel ( '__USERMODEL__' , attrs , optionalHandler ) ;
139
- }
132
+ } ;
140
133
141
134
_beforeEach . givenUserWithRole = function ( attrs , role , optionalHandler ) {
142
135
if ( typeof role === 'string' ) {
143
136
role = {
144
137
name : role
145
- }
138
+ } ;
146
139
}
147
140
_beforeEach . givenUser ( attrs , function ( done ) {
148
141
var test = this ;
@@ -189,7 +182,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
189
182
} ) ;
190
183
} ) ;
191
184
} ) ;
192
- }
185
+ } ;
193
186
194
187
_beforeEach . givenLoggedInUser = function ( credentials , optionalHandler ) {
195
188
_beforeEach . givenUser ( credentials , function ( done ) {
@@ -212,7 +205,7 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
212
205
done ( ) ;
213
206
} ) ;
214
207
} ) ;
215
- }
208
+ } ;
216
209
217
210
_beforeEach . givenLoggedInUserWithRole = function ( credentials , role , optionalHandler ) {
218
211
_beforeEach . givenUserWithRole ( credentials , role , function ( done ) {
@@ -235,15 +228,15 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
235
228
done ( ) ;
236
229
} ) ;
237
230
} ) ;
238
- }
231
+ } ;
239
232
240
233
_beforeEach . givenAnUnauthenticatedToken = function ( attrs , optionalHandler ) {
241
234
_beforeEach . givenModel ( 'AccessToken' , attrs , optionalHandler ) ;
242
- }
235
+ } ;
243
236
244
237
_beforeEach . givenAnAnonymousToken = function ( attrs , optionalHandler ) {
245
238
_beforeEach . givenModel ( 'AccessToken' , { id : '$anonymous' } , optionalHandler ) ;
246
- }
239
+ } ;
247
240
248
241
_describe . whenCalledRemotely = function ( verb , url , data , cb ) {
249
242
if ( cb == undefined ) {
@@ -261,89 +254,54 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
261
254
262
255
describe ( verb . toUpperCase ( ) + ' ' + urlStr , function ( ) {
263
256
beforeEach ( function ( cb ) {
264
- if ( typeof url === 'function' ) {
265
- this . url = url . call ( this ) ;
266
- }
267
- else if ( typeof url === 'object' && url . hasOwnProperty ( 'callback' ) ) {
268
- this . url = url . callback . call ( this ) ;
269
- }
270
- this . remotely = true ;
271
- this . verb = verb . toUpperCase ( ) ;
272
- this . url = this . url || url ;
273
- var methodForVerb = verb . toLowerCase ( ) ;
274
- if ( methodForVerb === 'delete' ) methodForVerb = 'del' ;
275
-
276
- if ( this . request === undefined ) {
277
- throw new Error ( 'App is not specified. Please use lt.beforeEach.withApp to specify the app.' ) ;
278
- }
279
-
280
- this . http = this . request [ methodForVerb ] ( this . url ) ;
281
- delete this . url ;
282
- this . http . set ( 'Accept' , 'application/json' ) ;
283
- if ( this . loggedInAccessToken ) {
284
- this . http . set ( 'authorization' , this . loggedInAccessToken . id ) ;
285
- }
286
- if ( data ) {
287
- var payload = data ;
288
- if ( typeof data === 'function' )
289
- payload = data . call ( this ) ;
290
- this . http . send ( payload ) ;
291
- }
292
- this . req = this . http . req ;
293
- var test = this ;
294
- this . http . end ( function ( err ) {
295
- test . req = test . http . req ;
296
- test . res = test . http . res ;
297
- delete test . url ;
298
- cb ( ) ;
299
- } ) ;
257
+ _makeRequest . call ( this , verb , url , data , cb ) ;
300
258
} ) ;
301
259
302
260
cb ( ) ;
303
261
} ) ;
304
- }
262
+ } ;
305
263
306
264
_describe . whenLoggedInAsUser = function ( credentials , cb ) {
307
265
describe ( 'when logged in as user' , function ( ) {
308
266
_beforeEach . givenLoggedInUser ( credentials ) ;
309
267
cb ( ) ;
310
268
} ) ;
311
- }
269
+ } ;
312
270
313
271
_describe . whenLoggedInAsUserWithRole = function ( credentials , role , cb ) {
314
272
describe ( 'when logged in as user' , function ( ) {
315
273
_beforeEach . givenLoggedInUserWithRole ( credentials , role ) ;
316
274
cb ( ) ;
317
275
} ) ;
318
- }
276
+ } ;
319
277
320
278
_describe . whenCalledByUser = function ( credentials , verb , url , data , cb ) {
321
279
describe ( 'when called by logged in user' , function ( ) {
322
280
_beforeEach . givenLoggedInUser ( credentials ) ;
323
281
_describe . whenCalledRemotely ( verb , url , data , cb ) ;
324
282
} ) ;
325
- }
283
+ } ;
326
284
327
285
_describe . whenCalledByUserWithRole = function ( credentials , role , verb , url , data , cb ) {
328
286
describe ( 'when called by logged in user with role ' + role , function ( ) {
329
287
_beforeEach . givenLoggedInUserWithRole ( credentials , role ) ;
330
288
_describe . whenCalledRemotely ( verb , url , data , cb ) ;
331
289
} ) ;
332
- }
290
+ } ;
333
291
334
292
_describe . whenCalledAnonymously = function ( verb , url , data , cb ) {
335
293
describe ( 'when called anonymously' , function ( ) {
336
294
_beforeEach . givenAnAnonymousToken ( ) ;
337
295
_describe . whenCalledRemotely ( verb , url , data , cb ) ;
338
296
} ) ;
339
- }
297
+ } ;
340
298
341
299
_describe . whenCalledUnauthenticated = function ( verb , url , data , cb ) {
342
300
describe ( 'when called with unauthenticated token' , function ( ) {
343
301
_beforeEach . givenAnAnonymousToken ( ) ;
344
302
_describe . whenCalledRemotely ( verb , url , data , cb ) ;
345
303
} ) ;
346
- }
304
+ } ;
347
305
348
306
_it . shouldBeAllowed = function ( ) {
349
307
it ( 'should be allowed' , function ( ) {
@@ -352,7 +310,7 @@ _it.shouldBeAllowed = function() {
352
310
// expect success - status 2xx or 3xx
353
311
expect ( this . res . statusCode ) . to . be . within ( 100 , 399 ) ;
354
312
} ) ;
355
- }
313
+ } ;
356
314
357
315
_it . shouldBeDenied = function ( ) {
358
316
it ( 'should not be allowed' , function ( ) {
@@ -362,67 +320,107 @@ _it.shouldBeDenied = function() {
362
320
401 ;
363
321
expect ( this . res . statusCode ) . to . equal ( expectedStatus ) ;
364
322
} ) ;
365
- }
323
+ } ;
366
324
367
325
_it . shouldNotBeFound = function ( ) {
368
326
it ( 'should not be found' , function ( ) {
369
327
assert ( this . res ) ;
370
328
assert . equal ( this . res . statusCode , 404 ) ;
371
329
} ) ;
372
- }
330
+ } ;
373
331
374
332
_it . shouldBeAllowedWhenCalledAnonymously =
375
333
function ( verb , url , data ) {
376
334
_describe . whenCalledAnonymously ( verb , url , data , function ( ) {
377
335
_it . shouldBeAllowed ( ) ;
378
336
} ) ;
379
- }
337
+ } ;
380
338
381
339
_it . shouldBeDeniedWhenCalledAnonymously =
382
340
function ( verb , url ) {
383
341
_describe . whenCalledAnonymously ( verb , url , function ( ) {
384
342
_it . shouldBeDenied ( ) ;
385
343
} ) ;
386
- }
344
+ } ;
387
345
388
346
_it . shouldBeAllowedWhenCalledUnauthenticated =
389
347
function ( verb , url , data ) {
390
348
_describe . whenCalledUnauthenticated ( verb , url , data , function ( ) {
391
349
_it . shouldBeAllowed ( ) ;
392
350
} ) ;
393
- }
351
+ } ;
394
352
395
353
_it . shouldBeDeniedWhenCalledUnauthenticated =
396
354
function ( verb , url ) {
397
355
_describe . whenCalledUnauthenticated ( verb , url , function ( ) {
398
356
_it . shouldBeDenied ( ) ;
399
357
} ) ;
400
- }
358
+ } ;
401
359
402
360
_it . shouldBeAllowedWhenCalledByUser =
403
361
function ( credentials , verb , url , data ) {
404
362
_describe . whenCalledByUser ( credentials , verb , url , data , function ( ) {
405
363
_it . shouldBeAllowed ( ) ;
406
364
} ) ;
407
- }
365
+ } ;
408
366
409
367
_it . shouldBeDeniedWhenCalledByUser =
410
368
function ( credentials , verb , url ) {
411
369
_describe . whenCalledByUser ( credentials , verb , url , function ( ) {
412
370
_it . shouldBeDenied ( ) ;
413
371
} ) ;
414
- }
372
+ } ;
415
373
416
374
_it . shouldBeAllowedWhenCalledByUserWithRole =
417
375
function ( credentials , role , verb , url , data ) {
418
376
_describe . whenCalledByUserWithRole ( credentials , role , verb , url , data , function ( ) {
419
377
_it . shouldBeAllowed ( ) ;
420
378
} ) ;
421
- }
379
+ } ;
422
380
423
381
_it . shouldBeDeniedWhenCalledByUserWithRole =
424
382
function ( credentials , role , verb , url ) {
425
383
_describe . whenCalledByUserWithRole ( credentials , role , verb , url , function ( ) {
426
384
_it . shouldBeDenied ( ) ;
427
385
} ) ;
386
+ } ;
387
+
388
+ function _makeRequest ( url , verb , data , cb ) {
389
+ if ( typeof url === 'function' ) {
390
+ this . url = url . call ( this ) ;
391
+ }
392
+ else if ( typeof url === 'object' && url . hasOwnProperty ( 'callback' ) ) {
393
+ this . url = url . callback . call ( this ) ;
394
+ }
395
+ this . remotely = true ;
396
+ this . verb = verb . toUpperCase ( ) ;
397
+ this . url = this . url || url ;
398
+ var methodForVerb = verb . toLowerCase ( ) ;
399
+ if ( methodForVerb === 'delete' ) methodForVerb = 'del' ;
400
+
401
+ if ( this . request === undefined ) {
402
+ throw new Error ( 'App is not specified. Please use lt.beforeEach.withApp to specify the app.' ) ;
403
+ }
404
+
405
+ this . http = this . request [ methodForVerb ] ( this . url ) ;
406
+ delete this . url ;
407
+ this . http . set ( 'Accept' , 'application/json' ) ;
408
+ if ( this . loggedInAccessToken ) {
409
+ this . http . set ( 'authorization' , this . loggedInAccessToken . id ) ;
410
+ }
411
+ if ( data ) {
412
+ var payload = data ;
413
+ if ( typeof data === 'function' ) {
414
+ payload = data . call ( this ) ;
415
+ }
416
+ this . http . send ( payload ) ;
417
+ }
418
+ this . req = this . http . req ;
419
+ var test = this ;
420
+ this . http . end ( function ( err ) {
421
+ test . req = test . http . req ;
422
+ test . res = test . http . res ;
423
+ delete test . url ;
424
+ cb ( ) ;
425
+ } ) ;
428
426
}
0 commit comments