Skip to content

Commit a6e1b55

Browse files
committed
Lint and refactor requests
1 parent 6985d11 commit a6e1b55

File tree

2 files changed

+96
-76
lines changed

2 files changed

+96
-76
lines changed

.eslintrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"root": true,
3+
"extends": "eslint:recommended",
4+
"rules": {
5+
"strict": 0,
6+
"quotes": [0, "single"],
7+
"curly": [1, "multi-line"],
8+
"no-console": 0,
9+
"no-use-before-define": [1, "nofunc"],
10+
"no-underscore-dangle": 0,
11+
"no-unused-vars": [1, {"vars": "local", "args": "none"}],
12+
"new-cap": 0,
13+
"consistent-return": 0,
14+
"camelcase": 0,
15+
"dot-notation": 0,
16+
"semi": [1, "always"]
17+
},
18+
env: {
19+
"node": true,
20+
"mocha": true
21+
}
22+
}

lib/helpers.js

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var _describe = {};
22
var _it = {};
33
var _beforeEach = {};
4-
var helpers = exports = module.exports = {
4+
module.exports = {
55
describe: _describe,
66
it: _it,
7-
beforeEach: _beforeEach
7+
beforeEach: _beforeEach,
8+
makeRequest: _makeRequest
89
};
910
var assert = require('assert');
1011
var request = require('supertest');
@@ -24,7 +25,7 @@ _beforeEach.withApp = function(app) {
2425
this.put = _request.put;
2526
this.del = _request.del;
2627
});
27-
}
28+
};
2829

2930
_beforeEach.cleanDatasource = function(dsName) {
3031
beforeEach(function(done) {
@@ -39,15 +40,7 @@ _beforeEach.cleanDatasource = function(dsName) {
3940
done();
4041
}
4142
});
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+
};
5144

5245
_describe.staticMethod = function(methodName, cb) {
5346
describe('.' + methodName, function() {
@@ -57,7 +50,7 @@ _describe.staticMethod = function(methodName, cb) {
5750
});
5851
cb();
5952
});
60-
}
53+
};
6154

6255
_describe.instanceMethod = function(methodName, cb) {
6356
describe('.prototype.' + methodName, function() {
@@ -67,14 +60,14 @@ _describe.instanceMethod = function(methodName, cb) {
6760
});
6861
cb();
6962
});
70-
}
63+
};
7164

7265
_beforeEach.withArgs = function() {
7366
var args = Array.prototype.slice.call(arguments, 0);
7467
beforeEach(function() {
7568
this.args = args;
7669
});
77-
}
70+
};
7871

7972
_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
8073
var modelKey = modelName;
@@ -125,7 +118,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
125118
afterEach(function(done) {
126119
this[modelKey].destroy(done);
127120
});
128-
}
121+
};
129122

130123
_beforeEach.withUserModel = function(model) {
131124
beforeEach(function(done) {
@@ -136,13 +129,13 @@ _beforeEach.withUserModel = function(model) {
136129

137130
_beforeEach.givenUser = function(attrs, optionalHandler) {
138131
_beforeEach.givenModel('__USERMODEL__', attrs, optionalHandler);
139-
}
132+
};
140133

141134
_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
142135
if (typeof role === 'string') {
143136
role = {
144137
name: role
145-
}
138+
};
146139
}
147140
_beforeEach.givenUser(attrs, function (done) {
148141
var test = this;
@@ -189,7 +182,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
189182
});
190183
});
191184
});
192-
}
185+
};
193186

194187
_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
195188
_beforeEach.givenUser(credentials, function(done) {
@@ -212,7 +205,7 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
212205
done();
213206
});
214207
});
215-
}
208+
};
216209

217210
_beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){
218211
_beforeEach.givenUserWithRole(credentials, role, function(done) {
@@ -235,15 +228,15 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
235228
done();
236229
});
237230
});
238-
}
231+
};
239232

240233
_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
241234
_beforeEach.givenModel('AccessToken', attrs, optionalHandler);
242-
}
235+
};
243236

244237
_beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
245238
_beforeEach.givenModel('AccessToken', {id: '$anonymous'}, optionalHandler);
246-
}
239+
};
247240

248241
_describe.whenCalledRemotely = function(verb, url, data, cb) {
249242
if (cb == undefined) {
@@ -261,89 +254,54 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
261254

262255
describe(verb.toUpperCase() + ' ' + urlStr, function() {
263256
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);
300258
});
301259

302260
cb();
303261
});
304-
}
262+
};
305263

306264
_describe.whenLoggedInAsUser = function(credentials, cb) {
307265
describe('when logged in as user', function () {
308266
_beforeEach.givenLoggedInUser(credentials);
309267
cb();
310268
});
311-
}
269+
};
312270

313271
_describe.whenLoggedInAsUserWithRole = function(credentials, role, cb) {
314272
describe('when logged in as user', function () {
315273
_beforeEach.givenLoggedInUserWithRole(credentials, role);
316274
cb();
317275
});
318-
}
276+
};
319277

320278
_describe.whenCalledByUser = function(credentials, verb, url, data, cb) {
321279
describe('when called by logged in user', function () {
322280
_beforeEach.givenLoggedInUser(credentials);
323281
_describe.whenCalledRemotely(verb, url, data, cb);
324282
});
325-
}
283+
};
326284

327285
_describe.whenCalledByUserWithRole = function (credentials, role, verb, url, data, cb) {
328286
describe('when called by logged in user with role ' + role, function () {
329287
_beforeEach.givenLoggedInUserWithRole(credentials, role);
330288
_describe.whenCalledRemotely(verb, url, data, cb);
331289
});
332-
}
290+
};
333291

334292
_describe.whenCalledAnonymously = function(verb, url, data, cb) {
335293
describe('when called anonymously', function () {
336294
_beforeEach.givenAnAnonymousToken();
337295
_describe.whenCalledRemotely(verb, url, data, cb);
338296
});
339-
}
297+
};
340298

341299
_describe.whenCalledUnauthenticated = function(verb, url, data, cb) {
342300
describe('when called with unauthenticated token', function () {
343301
_beforeEach.givenAnAnonymousToken();
344302
_describe.whenCalledRemotely(verb, url, data, cb);
345303
});
346-
}
304+
};
347305

348306
_it.shouldBeAllowed = function() {
349307
it('should be allowed', function() {
@@ -352,7 +310,7 @@ _it.shouldBeAllowed = function() {
352310
// expect success - status 2xx or 3xx
353311
expect(this.res.statusCode).to.be.within(100, 399);
354312
});
355-
}
313+
};
356314

357315
_it.shouldBeDenied = function() {
358316
it('should not be allowed', function() {
@@ -362,67 +320,107 @@ _it.shouldBeDenied = function() {
362320
401;
363321
expect(this.res.statusCode).to.equal(expectedStatus);
364322
});
365-
}
323+
};
366324

367325
_it.shouldNotBeFound = function() {
368326
it('should not be found', function() {
369327
assert(this.res);
370328
assert.equal(this.res.statusCode, 404);
371329
});
372-
}
330+
};
373331

374332
_it.shouldBeAllowedWhenCalledAnonymously =
375333
function(verb, url, data) {
376334
_describe.whenCalledAnonymously(verb, url, data, function() {
377335
_it.shouldBeAllowed();
378336
});
379-
}
337+
};
380338

381339
_it.shouldBeDeniedWhenCalledAnonymously =
382340
function(verb, url) {
383341
_describe.whenCalledAnonymously(verb, url, function() {
384342
_it.shouldBeDenied();
385343
});
386-
}
344+
};
387345

388346
_it.shouldBeAllowedWhenCalledUnauthenticated =
389347
function(verb, url, data) {
390348
_describe.whenCalledUnauthenticated(verb, url, data, function() {
391349
_it.shouldBeAllowed();
392350
});
393-
}
351+
};
394352

395353
_it.shouldBeDeniedWhenCalledUnauthenticated =
396354
function(verb, url) {
397355
_describe.whenCalledUnauthenticated(verb, url, function() {
398356
_it.shouldBeDenied();
399357
});
400-
}
358+
};
401359

402360
_it.shouldBeAllowedWhenCalledByUser =
403361
function(credentials, verb, url, data) {
404362
_describe.whenCalledByUser(credentials, verb, url, data, function() {
405363
_it.shouldBeAllowed();
406364
});
407-
}
365+
};
408366

409367
_it.shouldBeDeniedWhenCalledByUser =
410368
function(credentials, verb, url) {
411369
_describe.whenCalledByUser(credentials, verb, url, function() {
412370
_it.shouldBeDenied();
413371
});
414-
}
372+
};
415373

416374
_it.shouldBeAllowedWhenCalledByUserWithRole =
417375
function(credentials, role, verb, url, data) {
418376
_describe.whenCalledByUserWithRole(credentials, role, verb, url, data, function() {
419377
_it.shouldBeAllowed();
420378
});
421-
}
379+
};
422380

423381
_it.shouldBeDeniedWhenCalledByUserWithRole =
424382
function(credentials, role, verb, url) {
425383
_describe.whenCalledByUserWithRole(credentials, role, verb, url, function() {
426384
_it.shouldBeDenied();
427385
});
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+
});
428426
}

0 commit comments

Comments
 (0)