@@ -616,6 +616,84 @@ describe('role model', function() {
616616 } ) ;
617617 } ) ;
618618
619+ it ( 'should fetch all models only assigned to the role' , function ( done ) {
620+ var principalTypesToModels = { } ;
621+ var mappings ;
622+
623+ principalTypesToModels [ RoleMapping . USER ] = User ;
624+ principalTypesToModels [ RoleMapping . APPLICATION ] = Application ;
625+ principalTypesToModels [ RoleMapping . ROLE ] = Role ;
626+ mappings = Object . keys ( principalTypesToModels ) ;
627+
628+ async . each ( mappings , function ( principalType , eachCallback ) {
629+ var Model = principalTypesToModels [ principalType ] ;
630+
631+ async . waterfall ( [
632+ // Create models
633+ function ( next ) {
634+ Model . create ( [
635+ { name :
'test' , email :
'[email protected] ' , password :
'foobar' } , 636+ { name :
'test2' , email :
'[email protected] ' , password :
'bargoo' } , 637+ { name :
'test3' , email :
'[email protected] ' , password :
'bluegoo' } ] , 638+ function ( err , models ) {
639+ if ( err ) return next ( err ) ;
640+ next ( null , models ) ;
641+ } ) ;
642+ } ,
643+
644+ // Create Roles
645+ function ( models , next ) {
646+ var uniqueRoleName = 'testRoleFor' + principalType ;
647+ var otherUniqueRoleName = 'otherTestRoleFor' + principalType ;
648+ Role . create ( [
649+ { name : uniqueRoleName } ,
650+ { name : otherUniqueRoleName } ] ,
651+ function ( err , roles ) {
652+ if ( err ) return next ( err ) ;
653+ next ( null , models , roles ) ;
654+ } ) ;
655+ } ,
656+
657+ // Create principles
658+ function ( models , roles , next ) {
659+ async . parallel ( [
660+ function ( callback ) {
661+ roles [ 0 ] . principals . create (
662+ { principalType : principalType , principalId : models [ 0 ] . id } ,
663+ function ( err , p ) {
664+ if ( err ) return callback ( err ) ;
665+ callback ( p ) ;
666+ } ) ;
667+ } ,
668+ function ( callback ) {
669+ roles [ 1 ] . principals . create (
670+ { principalType : principalType , principalId : models [ 1 ] . id } ,
671+ function ( err , p ) {
672+ if ( err ) return callback ( err ) ;
673+ callback ( p ) ;
674+ } ) ;
675+ } ] ,
676+ function ( err , principles ) {
677+ next ( null , models , roles , principles ) ;
678+ } ) ;
679+ } ,
680+
681+ // Run tests against unique Role
682+ function ( models , roles , principles , next ) {
683+ var pluralName = Model . pluralModelName . toLowerCase ( ) ;
684+ uniqueRole = roles [ 0 ] ;
685+ uniqueRole [ pluralName ] ( function ( err , models ) {
686+ if ( err ) return done ( err ) ;
687+ assert . equal ( models . length , 1 ) ;
688+ next ( ) ;
689+ } ) ;
690+ } ] ,
691+ eachCallback ) ;
692+ } , function ( err ) {
693+ done ( ) ;
694+ } ) ;
695+ } ) ;
696+
619697 it ( 'should apply query' , function ( done ) {
620698 User . create ( { name :
'Raymond' , email :
'[email protected] ' , password :
'foobar' } , function ( err , user ) { 621699 if ( err ) return done ( err ) ;
0 commit comments