@@ -109,6 +109,54 @@ _beforeEach.givenModel = function(modelName, attrs, optionalHandler) {
109
109
110
110
_beforeEach . givenUser = function ( attrs , optionalHandler ) {
111
111
_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
+ } ) ;
112
160
}
113
161
114
162
_beforeEach . givenLoggedInUser = function ( credentials , optionalHandler ) {
@@ -134,6 +182,29 @@ _beforeEach.givenLoggedInUser = function(credentials, optionalHandler) {
134
182
} ) ;
135
183
}
136
184
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
+
137
208
_beforeEach . givenAnUnauthenticatedToken = function ( attrs , optionalHandler ) {
138
209
_beforeEach . givenModel ( 'accessToken' , attrs , optionalHandler ) ;
139
210
}
@@ -197,13 +268,27 @@ _describe.whenLoggedInAsUser = function(credentials, cb) {
197
268
} ) ;
198
269
}
199
270
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
+
200
278
_describe . whenCalledByUser = function ( credentials , verb , url , data , cb ) {
201
279
describe ( 'when called by logged in user' , function ( ) {
202
280
_beforeEach . givenLoggedInUser ( credentials ) ;
203
281
_describe . whenCalledRemotely ( verb , url , data , cb ) ;
204
282
} ) ;
205
283
}
206
284
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
+
207
292
_describe . whenCalledAnonymously = function ( verb , url , data , cb ) {
208
293
describe ( 'when called anonymously' , function ( ) {
209
294
_beforeEach . givenAnAnonymousToken ( ) ;
@@ -285,3 +370,17 @@ function(credentials, verb, url) {
285
370
_it . shouldBeDenied ( ) ;
286
371
} ) ;
287
372
}
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