@@ -8,6 +8,7 @@ var helpers = exports = module.exports = {
8
8
} ;
9
9
var assert = require ( 'assert' ) ;
10
10
var request = require ( 'supertest' ) ;
11
+ var expect = require ( 'chai' ) . expect ;
11
12
12
13
_beforeEach . withApp = function ( app ) {
13
14
if ( app . models . User ) {
@@ -109,6 +110,54 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
109
110
110
111
_beforeEach . givenUser = function ( attrs , optionalHandler ) {
111
112
_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
+ } ) ;
112
161
}
113
162
114
163
_beforeEach . givenLoggedInUser = function ( credentials , optionalHandler ) {
@@ -134,6 +183,29 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
134
183
} ) ;
135
184
}
136
185
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
+
137
209
_beforeEach . givenAnUnauthenticatedToken = function ( attrs , optionalHandler ) {
138
210
_beforeEach . givenModel ( 'accessToken' , attrs , optionalHandler ) ;
139
211
}
@@ -197,13 +269,27 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
197
269
} ) ;
198
270
}
199
271
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
+
200
279
_describe . whenCalledByUser = function ( credentials , verb , url , data , cb ) {
201
280
describe ( 'when called by logged in user' , function ( ) {
202
281
_beforeEach . givenLoggedInUser ( credentials ) ;
203
282
_describe . whenCalledRemotely ( verb , url , data , cb ) ;
204
283
} ) ;
205
284
}
206
285
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
+
207
293
_describe . whenCalledAnonymously = function ( verb , url , data , cb ) {
208
294
describe ( 'when called anonymously' , function ( ) {
209
295
_beforeEach . givenAnAnonymousToken ( ) ;
@@ -285,3 +371,17 @@ function(credentials, verb, url) {
285
371
_it . shouldBeDenied ( ) ;
286
372
} ) ;
287
373
}
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