diff --git a/lib/helpers.js b/lib/helpers.js index d747c70..7dace91 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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]; @@ -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 { @@ -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); } @@ -146,7 +180,7 @@ _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) { if(err) { console.error(err.message); if(err.details) console.error(err.details); @@ -154,9 +188,9 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { } 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) { @@ -164,7 +198,7 @@ _beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) { if(err.details) console.error(err.details); return done(err); } - + test.userRoleMapping = result; done(); } @@ -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 { @@ -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 { @@ -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) { @@ -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) { @@ -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); }); } @@ -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); }); } @@ -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() { @@ -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); + }); +} diff --git a/test/test.js b/test/test.js index 70b4448..2ac4d6c 100644 --- a/test/test.js +++ b/test/test.js @@ -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 () {