@@ -683,4 +683,62 @@ describe('Parse.Relation testing', () => {
683
683
} )
684
684
} ) ;
685
685
} ) ;
686
+
687
+ it ( 'can query roles in Cloud Code (regession test #1489)' , done => {
688
+ Parse . Cloud . define ( 'isAdmin' , ( request , response ) => {
689
+ let query = new Parse . Query ( Parse . Role ) ;
690
+ query . equalTo ( 'name' , 'admin' ) ;
691
+ query . first ( { useMasterKey : true } )
692
+ . then ( role => {
693
+ let relation = new Parse . Relation ( role , 'users' ) ;
694
+ let admins = relation . query ( ) ;
695
+ admins . equalTo ( 'username' , request . user . get ( 'username' ) ) ;
696
+ admins . first ( { useMasterKey : true } )
697
+ . then ( user => {
698
+ if ( user ) {
699
+ Parse . Cloud . _removeHook ( 'Functions' , 'isAdmin' ) ;
700
+ done ( ) ;
701
+ } else {
702
+ Parse . Cloud . _removeHook ( 'Functions' , 'isAdmin' ) ;
703
+ fail ( 'Should have found admin user, found nothing instead' ) ;
704
+ done ( ) ;
705
+ }
706
+ } , error => {
707
+ Parse . Cloud . _removeHook ( 'Functions' , 'isAdmin' ) ;
708
+ fail ( 'User not admin' ) ;
709
+ done ( ) ;
710
+ } )
711
+ } , error => {
712
+ Parse . Cloud . _removeHook ( 'Functions' , 'isAdmin' ) ;
713
+ fail ( 'Should have found admin user, errored instead' ) ;
714
+ fail ( error ) ;
715
+ done ( ) ;
716
+ } ) ;
717
+ } ) ;
718
+
719
+ let adminUser = new Parse . User ( ) ;
720
+ adminUser . set ( 'username' , 'name' ) ;
721
+ adminUser . set ( 'password' , 'pass' ) ;
722
+ adminUser . signUp ( )
723
+ . then ( adminUser => {
724
+ let adminACL = new Parse . ACL ( ) ;
725
+ adminACL . setPublicReadAccess ( true ) ;
726
+
727
+ // Create admin role
728
+ let adminRole = new Parse . Role ( 'admin' , adminACL ) ;
729
+ adminRole . getUsers ( ) . add ( adminUser ) ;
730
+ adminRole . save ( )
731
+ . then ( ( ) => {
732
+ Parse . Cloud . run ( 'isAdmin' ) ;
733
+ } , error => {
734
+ fail ( 'failed to save role' ) ;
735
+ fail ( error ) ;
736
+ done ( )
737
+ } ) ;
738
+ } , error => {
739
+ fail ( 'failed to sign up' ) ;
740
+ fail ( error ) ;
741
+ done ( ) ;
742
+ } ) ;
743
+ } ) ;
686
744
} ) ;
0 commit comments