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 all 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
161 changes: 152 additions & 9 deletions lib/helpers.js
Original file line number Diff line number Diff line change
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 All @@ -109,6 +121,7 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
model.create(attrs, function(err, result) {
if(err) {
console.error(err.message);
console.error('Tried creating ' + modelName, attrs);
if(err.details) console.error(err.details);
done(err);
} else {
Expand All @@ -134,6 +147,27 @@ _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);
}
Expand All @@ -146,25 +180,25 @@ _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 @@ -194,7 +228,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 @@ -217,7 +251,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 @@ -238,11 +272,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 @@ -328,7 +362,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 All @@ -350,6 +384,9 @@ _it.shouldBeAllowed = function() {
assert(this.req);
assert(this.res);
// expect success - status 2xx or 3xx
if (this.res.statusCode < 100 || this.res.statusCode > 399) {
console.log(this.res.body);
}
expect(this.res.statusCode).to.be.within(100, 399);
});
}
Expand All @@ -360,13 +397,46 @@ _it.shouldBeDenied = function() {
var expectedStatus = this.aclErrorStatus ||
this.app && this.app.get('aclErrorStatus') ||
401;
if (this.res.statusCode !== expectedStatus) {
console.log(this.res.body);
}
expect(this.res.statusCode).to.equal(expectedStatus);
});
}

_it.shouldBeRejected = function(statusCode) {
it('should be rejected' + (statusCode ? ' with status code ' + statusCode : ''), function() {
assert(this.res);
if (statusCode) {
if (this.res.statusCode !== statusCode) {
console.log(this.res.body);
}
expect(this.res.statusCode).to.equal(statusCode);
} else {
if (this.res.statusCode < 400 || this.res.statusCode > 499) {
console.log(this.res.body);
}
expect(this.res.statusCode).to.be.within(400, 499);
}
});
}

_it.shouldBeForbidden = function() {
it('should be forbidden', function() {
assert(this.res);
if (this.res.statusCode !== 403) {
console.log(this.res.body);
}
assert.equal(this.res.statusCode, 403);
});
}

_it.shouldNotBeFound = function() {
it('should not be found', function() {
assert(this.res);
if (this.res.statusCode !== 404) {
console.log(this.res.body);
}
assert.equal(this.res.statusCode, 404);
});
}
Expand Down Expand Up @@ -399,6 +469,13 @@ function(verb, url) {
});
}

_it.shouldBeNotFoundWhenCalledUnauthenticated =
function(verb, url) {
_describe.whenCalledUnauthenticated(verb, url, function() {
_it.shouldNotBeFound();
});
}

_it.shouldBeAllowedWhenCalledByUser =
function(credentials, verb, url, data) {
_describe.whenCalledByUser(credentials, verb, url, data, function() {
Expand Down Expand Up @@ -426,3 +503,69 @@ function(credentials, role, verb, url) {
_it.shouldBeDenied();
});
}

_it.shouldBeValidCreateResponse =
function () {
_it.shouldBeAllowed();
it('should respond with a valid POST response', function () {
if (this.res.statusCode !== 200) {
console.log(this.res.body);
}
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 () {
if (this.res.statusCode !== 200) {
console.log(this.res.body);
}
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 () {
if (this.res.statusCode !== 200) {
console.log(this.res.body);
}
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 () {
if (this.res.statusCode !== 200) {
console.log(this.res.body);
}
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 200',
function () {
if (this.res.statusCode !== 200) {
console.log(this.res.body);
}
assert.equal(this.res.statusCode, 200);
});
}
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ describe('helpers', function () {
describe('helpers.it', function() {
['shouldBeAllowed',
'shouldBeDenied',
'shouldBeRejected',
'shouldNotBeFound',
'shouldBeAllowedWhenCalledAnonymously',
'shouldBeDeniedWhenCalledAnonymously',
'shouldBeAllowedWhenCalledUnauthenticated',
'shouldBeDeniedWhenCalledUnauthenticated',
'shouldBeNotFoundWhenCalledUnauthenticated',
'shouldBeAllowedWhenCalledByUser',
'shouldBeValidCreateResponse',
'shouldBeValidGetAllResponse',
'shouldBeValidGetByIdResponse',
'shouldBeValidUpdateResponse',
'shouldBeValidDeleteResponse',
'shouldBeDeniedWhenCalledByUser']
.forEach(function(func) {
it('should have a method named ' + func, function () {
Expand Down