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

Commit 4c7fd3e

Browse files
offlinehackerAris Kemper
authored andcommitted
add support for testing with users with roles
Signed-off-by: Jaka Hudoklin <[email protected]>
1 parent 26e573f commit 4c7fd3e

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
@@ -109,6 +109,54 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
109109

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

114162
_beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
@@ -134,6 +182,29 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
134182
});
135183
}
136184

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

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

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

0 commit comments

Comments
 (0)