Skip to content

Commit

Permalink
Merge branch 'remotely-once'
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
  • Loading branch information
Ryan Schumacher committed Mar 11, 2015
2 parents db17ebe + 245e0a2 commit e5b5240
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 57 deletions.
99 changes: 45 additions & 54 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
var _describe = {};
var _it = {};
var _beforeEach = {};
var _before = {};
var helpers = exports = module.exports = {
describe: _describe,
it: _it,
beforeEach: _beforeEach
before: _before
};
var assert = require('assert');
var request = require('supertest');
var expect = require('chai').expect;

_beforeEach.withApp = function(app) {
_before.withApp = function(app) {
if (app.models.User) {
// Speed up the password hashing algorithm
app.models.User.settings.saltWorkFactor = 4;
}

beforeEach(function() {
before(function() {
this.app = app;
var _request = this.request = request(app);
this.post = _request.post;
Expand All @@ -26,8 +26,8 @@ _beforeEach.withApp = function(app) {
});
}

_beforeEach.cleanDatasource = function(dsName) {
beforeEach(function(done) {
_before.cleanDatasource = function(dsName) {
before(function(done) {
if(!dsName) dsName = 'db';

if (typeof this.app === 'function'
Expand All @@ -36,13 +36,13 @@ _beforeEach.cleanDatasource = function(dsName) {
this.app.datasources[dsName].automigrate();
this.app.datasources[dsName].connector.ids = {};
}

done();
});
}

function mixin(obj, into) {
Object.keys(obj).forEach(function(key) {
Object.keys(obj).for(function(key) {
if(typeof obj[key] === 'function') {
into[key] = obj[key];
}
Expand All @@ -51,7 +51,7 @@ function mixin(obj, into) {

_describe.staticMethod = function(methodName, cb) {
describe('.' + methodName, function() {
beforeEach(function() {
before(function() {
this.method = methodName;
this.isStaticMethod = true;
});
Expand All @@ -61,22 +61,22 @@ _describe.staticMethod = function(methodName, cb) {

_describe.instanceMethod = function(methodName, cb) {
describe('.prototype.' + methodName, function() {
beforeEach(function() {
before(function() {
this.method = methodName;
this.isInstanceMethod = true;
});
cb();
});
}

_beforeEach.withArgs = function() {
_before.withArgs = function() {
var args = Array.prototype.slice.call(arguments, 0);
beforeEach(function() {
before(function() {
this.args = args;
});
}

_beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
_before.givenModel = function(modelName, attrs, optionalHandler) {
var modelKey = modelName;

if(typeof attrs === 'function') {
Expand All @@ -90,7 +90,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {

attrs = attrs || {};

beforeEach(function(done) {
before(function(done) {
var test = this;
var app = this.app;
var model = app.models[modelName];
Expand All @@ -115,20 +115,20 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
});

if(typeof optionalHandler === 'function') {
beforeEach(optionalHandler);
before(optionalHandler);
}

afterEach(function(done) {
after(function(done) {
this[modelKey].destroy(done);
});
}

_beforeEach.givenUser = function(attrs, optionalHandler) {
_beforeEach.givenModel('user', attrs, optionalHandler);
}
_before.givenUser = function(attrs, optionalHandler) {
_before.givenModel('person', attrs, optionalHandler);
}

_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
_beforeEach.givenUser(attrs, function (done) {
_before.givenUserWithRole = function (attrs, role, optionalHandler) {
_before.givenUser(attrs, function (done) {
var test = this;
test.app.models.Role.create({name: role}, function (err, result) {
if(err) {
Expand All @@ -148,7 +148,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
if(err.details) console.error(err.details);
return done(err);
}

test.userRoleMapping = result;
done();
}
Expand All @@ -157,10 +157,10 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
});

if(typeof optionalHandler === 'function') {
beforeEach(optionalHandler);
before(optionalHandler);
}

afterEach(function(done) {
after(function(done) {
var test = this;
this.userRole.destroy(function(err) {
if(err) return done(err);
Expand All @@ -175,10 +175,10 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
});
}

_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
_beforeEach.givenUser(credentials, function(done) {
_before.givenLoggedInUser = function(credentials, optionalHandler) {
_before.givenUser(credentials, function(done) {
var test = this;
this.user.constructor.login(credentials, function(err, token) {
this.person.constructor.login(credentials, function(err, token) {
if(err) {
done(err);
} else {
Expand All @@ -188,7 +188,7 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
});
});

afterEach(function(done) {
after(function(done) {
var test = this;
this.loggedInAccessToken.destroy(function(err) {
if(err) return done(err);
Expand All @@ -198,10 +198,10 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
});
}

_beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){
_beforeEach.givenUserWithRole(credentials, role, function(done) {
_before.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){
_before.givenUserWithRole(credentials, role, function(done) {
var test = this;
this.user.constructor.login(credentials, function(err, token) {
this.person.constructor.login(credentials, function(err, token) {
if(err) {
done(err);
} else {
Expand All @@ -211,7 +211,7 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
});
});

afterEach(function(done) {
after(function(done) {
var test = this;
this.loggedInAccessToken.destroy(function(err) {
if(err) return done(err);
Expand All @@ -221,12 +221,12 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
});
}

_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
_beforeEach.givenModel('accessToken', attrs, optionalHandler);
_before.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
_before.givenModel('accessToken', attrs, optionalHandler);
}

_beforeEach.givenAnAnonymousToken = function(attrs, optionalHandler) {
_beforeEach.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
_before.givenAnAnonymousToken = function(attrs, optionalHandler) {
_before.givenModel('accessToken', {id: '$anonymous'}, optionalHandler);
}

_describe.whenCalledRemotely = function(verb, url, data, cb) {
Expand All @@ -240,17 +240,8 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
urlStr = '/<dynamic>';
}

var once = cb.toString().match(/^function \([^\)]*\$once[^\)]*\)/) ? false : null;

describe(verb.toUpperCase() + ' ' + urlStr, function() {
beforeEach(function(cb) {
if(once !== null) {
if(once === true) {
cb();
return;
}
once = true;
}
before(function(cb) {

if(typeof url === 'function') {
this.url = url.call(this);
Expand All @@ -262,7 +253,7 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {
if(methodForVerb === 'delete') methodForVerb = 'del';

if (this.request === undefined) {
throw new Error('App is not specified. Please use lt.beforeEach.withApp to specify the app.');
throw new Error('App is not specified. Please use lt.before.withApp to specify the app.');
}

this.http = this.request[methodForVerb](this.url);
Expand Down Expand Up @@ -293,42 +284,42 @@ _describe.whenCalledRemotely = function(verb, url, data, cb) {

_describe.whenLoggedInAsUser = function(credentials, cb) {
describe('when logged in as user', function () {
_beforeEach.givenLoggedInUser(credentials);
_before.givenLoggedInUser(credentials);
cb();
});
}

_describe.whenLoggedInAsUserWithRole = function(credentials, role, cb) {
describe('when logged in as user', function () {
_beforeEach.givenLoggedInUser(credentials, role);
_before.givenLoggedInUser(credentials, role);
cb();
});
}

_describe.whenCalledByUser = function(credentials, verb, url, data, cb) {
describe('when called by logged in user', function () {
_beforeEach.givenLoggedInUser(credentials);
_before.givenLoggedInUser(credentials);
_describe.whenCalledRemotely(verb, url, data, cb);
});
}

_describe.whenCalledByUserWithRole = function (credentials, role, verb, url, data, cb) {
describe('when called by logged in user with role ' + role, function () {
_beforeEach.givenLoggedInUserWithRole(credentials, role);
_before.givenLoggedInUserWithRole(credentials, role);
_describe.whenCalledRemotely(verb, url, data, cb);
});
});
}

_describe.whenCalledAnonymously = function(verb, url, data, cb) {
describe('when called anonymously', function () {
_beforeEach.givenAnAnonymousToken();
_before.givenAnAnonymousToken();
_describe.whenCalledRemotely(verb, url, data, cb);
});
}

_describe.whenCalledUnauthenticated = function(verb, url, data, cb) {
describe('when called with unauthenticated token', function () {
_beforeEach.givenAnAnonymousToken();
_before.givenAnAnonymousToken();
_describe.whenCalledRemotely(verb, url, data, cb);
});
}
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "loopback-testing",
"version": "1.1.0",
"name": "hatch-loopback-testing",
"version": "2.0.2",
"description": "Utilities for testing LoopBack applications",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/strongloop/loopback-testing"
"url": "https://github.com/hatch-is/loopback-testing"
},
"author": "Ritchie Martori",
"dependencies": {
Expand Down

0 comments on commit e5b5240

Please sign in to comment.