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

Commit 1569d18

Browse files
committed
Merge pull request #31 from ariskemper/patch-2
add support for testing with users with roles
2 parents ed5638d + 4c7fd3e commit 1569d18

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

lib/helpers.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,54 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
110110

111111
_beforeEach.givenUser = function(attrs, optionalHandler) {
112112
_beforeEach.givenModel('user', attrs, optionalHandler);
113+
}
114+
115+
_beforeEach.givenUserWithRole = function (attrs, role, optionalHandler) {
116+
_beforeEach.givenUser(attrs, function (done) {
117+
var test = this;
118+
test.app.models.Role.create({name: role}, function (err, result) {
119+
if(err) {
120+
console.error(err.message);
121+
if(err.details) console.error(err.details);
122+
return done(err);
123+
}
124+
125+
test.userRole = result;
126+
test.app.models.roleMapping.create(
127+
{principalId: test.user.id,
128+
principalType: test.app.models.roleMapping.USER,
129+
roleId: result.id},
130+
function (err, result) {
131+
if(err) {
132+
console.error(err.message);
133+
if(err.details) console.error(err.details);
134+
return done(err);
135+
}
136+
137+
test.userRoleMapping = result;
138+
done();
139+
}
140+
);
141+
});
142+
});
143+
144+
if(typeof optionalHandler === 'function') {
145+
beforeEach(optionalHandler);
146+
}
147+
148+
afterEach(function(done) {
149+
var test = this;
150+
this.userRole.destroy(function(err) {
151+
if(err) return done(err);
152+
test.userRole = undefined;
153+
154+
test.userRoleMapping.destroy(function(err) {
155+
if(err) return done(err);
156+
test.userRoleMapping = undefined;
157+
done();
158+
});
159+
});
160+
});
113161
}
114162

115163
_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
@@ -135,6 +183,29 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
135183
});
136184
}
137185

186+
_beforeEach.givenLoggedInUserWithRole = function(credentials, role, optionalHandler){
187+
_beforeEach.givenUserWithRole(credentials, role, function(done) {
188+
var test = this;
189+
this.user.constructor.login(credentials, function(err, token) {
190+
if(err) {
191+
done(err);
192+
} else {
193+
test.loggedInAccessToken = token;
194+
done();
195+
}
196+
});
197+
});
198+
199+
afterEach(function(done) {
200+
var test = this;
201+
this.loggedInAccessToken.destroy(function(err) {
202+
if(err) return done(err);
203+
test.loggedInAccessToken = undefined;
204+
done();
205+
});
206+
});
207+
}
208+
138209
_beforeEach.givenAnUnauthenticatedToken = function(attrs, optionalHandler) {
139210
_beforeEach.givenModel('accessToken', attrs, optionalHandler);
140211
}
@@ -198,13 +269,27 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
198269
});
199270
}
200271

272+
_describe.whenLoggedInAsUserWithRole = function(credentials, role, cb) {
273+
describe('when logged in as user', function () {
274+
_beforeEach.givenLoggedInUser(credentials, role);
275+
cb();
276+
});
277+
}
278+
201279
_describe.whenCalledByUser = function(credentials, verb, url, data, cb) {
202280
describe('when called by logged in user', function () {
203281
_beforeEach.givenLoggedInUser(credentials);
204282
_describe.whenCalledRemotely(verb, url, data, cb);
205283
});
206284
}
207285

286+
_describe.whenCalledByUserWithRole = function (credentials, role, verb, url, data, cb) {
287+
describe('when called by logged in user with role ' + role, function () {
288+
_beforeEach.givenLoggedInUserWithRole(credentials, role);
289+
_describe.whenCalledRemotely(verb, url, data, cb);
290+
});
291+
}
292+
208293
_describe.whenCalledAnonymously = function(verb, url, data, cb) {
209294
describe('when called anonymously', function () {
210295
_beforeEach.givenAnAnonymousToken();
@@ -286,3 +371,17 @@ function(credentials, verb, url) {
286371
_it.shouldBeDenied();
287372
});
288373
}
374+
375+
_it.shouldBeAllowedWhenCalledByUserWithRole =
376+
function(credentials, role, verb, url, data) {
377+
_describe.whenCalledByUserWithRole(credentials, role, verb, url, data, function() {
378+
_it.shouldBeAllowed();
379+
});
380+
}
381+
382+
_it.shouldBeDeniedWhenCalledByUserWithRole =
383+
function(credentials, role, verb, url) {
384+
_describe.whenCalledByUserWithRole(credentials, role, verb, url, function() {
385+
_it.shouldBeDenied();
386+
});
387+
}

0 commit comments

Comments
 (0)