Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Suggestions #56

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 94 additions & 10 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _beforeEach.cleanDatasource = function(dsName) {
this.app.datasources[dsName].automigrate();
this.app.datasources[dsName].connector.ids = {};
}

done();
});
}
Expand Down Expand Up @@ -95,6 +95,18 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
modelName = this.userModel ? this.userModel : 'user';
}

if(modelName === '__ACCESSTOKENMODEL__') {
modelName = this.accessTokenModel ? this.accessTokenModel : 'accessToken';
}

if(modelName === '__ROLEMAPPINGMODEL__') {
modelName = this.roleMappingModel ? this.roleMappingModel : 'roleMapping';
}

if(modelName === '__ROLEMODEL__') {
modelName = this.roleModel ? this.roleModel : 'role';
}

var test = this;
var app = this.app;
var model = app.models[modelName];
Expand Down Expand Up @@ -134,32 +146,53 @@ _beforeEach.withUserModel = function(model) {
});
};

_beforeEach.withAccessTokenModel = function(model) {
beforeEach(function(done) {
this.accessTokenModel = model;
done();
});
};

_beforeEach.withRoleMappingModel = function(model) {
beforeEach(function(done) {
this.roleMappingModel = model;
done();
});
};

_beforeEach.withRoleModel = function(model) {
beforeEach(function(done) {
this.roleModel = model;
done();
});
};

_beforeEach.givenUser = function(attrs, optionalHandler) {
_beforeEach.givenModel('__USERMODEL__', attrs, optionalHandler);
}

_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
_beforeEach.givenUser(attrs, function (done) {
var test = this;
test.app.models.Role.findOrCreate({name: role}, function (err, result) {
test.app.models[this.roleModel].findOrCreate({name: role}, function (err, result) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fall back on app.models.Role for backwards compatibility.

if(err) {
console.error(err.message);
if(err.details) console.error(err.details);
return done(err);
}

test.userRole = result;
test.app.models.roleMapping.create(
test.app.models[this.roleMappingModel].create(
{principalId: test.user.id,
principalType: test.app.models.roleMapping.USER,
principalType: test.app.models[this.roleMappingModel].USER,
roleId: result.id},
function (err, result) {
if(err) {
console.error(err.message);
if(err.details) console.error(err.details);
return done(err);
}

test.userRoleMapping = result;
done();
}
Expand Down Expand Up @@ -189,7 +222,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
_beforeEach.givenUser(credentials, function(done) {
var test = this;
this.app.models[this.userModel].constructor.login(credentials, function(err, token) {
this.app.models[this.userModel].login(credentials, function(err, token) {
if(err) {
done(err);
} else {
Expand All @@ -212,7 +245,7 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
_beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){
_beforeEach.givenUserWithRole(credentials, role, function(done) {
var test = this;
this.app.models[this.userModel].constructor.login(credentials, function(err, token) {
this.app.models[this.userModel].login(credentials, function(err, token) {
if(err) {
done(err);
} else {
Expand All @@ -233,11 +266,11 @@ _beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHand
}

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

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

_describe.whenCalledRemotely = function(verb, url, data, cb) {
Expand Down Expand Up @@ -323,7 +356,7 @@ _describe.whenCalledByUserWithRole = function (credentials, role, verb, url, dat
describe('when called by logged in user with role ' + role, function () {
_beforeEach.givenLoggedInUserWithRole(credentials, role);
_describe.whenCalledRemotely(verb, url, data, cb);
});
});
}

_describe.whenCalledAnonymously = function(verb, url, data, cb) {
Expand Down Expand Up @@ -421,3 +454,54 @@ function(credentials, role, verb, url) {
_it.shouldBeDenied();
});
}

_it.shouldBeValidCreateResponse =
function () {
_it.shouldBeAllowed();
it('should respond with a valid POST response', function () {
assert.equal(this.res.statusCode, 200);
assert(this.res.body.id);
});
}

_it.shouldBeValidGetAllResponse =
function () {
_it.shouldBeAllowed();
it('should respond with a valid GET response', function () {
assert.equal(this.res.statusCode, 200);
assert(Array.isArray(this.res.body));
});
}

_it.shouldBeValidGetByIdResponse =
function (id) {
_it.shouldBeAllowed();
it('should respond with a valid GET response',
function () {
assert.equal(this.res.statusCode, 200);
assert.equal(this.res.body.id, id);
});
}

_it.shouldBeValidUpdateResponse =
function (newVal) {
_it.shouldBeAllowed();
it('should respond with a valid PUT response',
function () {
assert.equal(this.res.statusCode, 200);
var props = Object.keys(newVal);
var val = this.res.body;
props.forEach(function (prop) {
assert.equal(val[prop], newVal[prop]);
});
});
}

_it.shouldBeValidDeleteResponse =
function () {
_it.shouldBeAllowed();
it('should have statusCode 204',
function () {
assert.equal(this.res.statusCode, 204);
});
}
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ describe('helpers', function () {
'shouldBeAllowedWhenCalledUnauthenticated',
'shouldBeDeniedWhenCalledUnauthenticated',
'shouldBeAllowedWhenCalledByUser',
'shouldBeValidCreateResponse',
'shouldBeValidGetAllResponse',
'shouldBeValidGetByIdResponse',
'shouldBeValidUpdateResponse',
'shouldBeValidDeleteResponse',
'shouldBeDeniedWhenCalledByUser']
.forEach(function(func) {
it('should have a method named ' + func, function () {
Expand Down