@@ -620,10 +620,13 @@ def _make_client(self, form, classname='user', nodeid='1', userid='2'):
620620 cl = client .Client (self .instance , None , {'PATH_INFO' :'/' ,
621621 'REQUEST_METHOD' :'POST' }, makeForm (form ))
622622 cl .classname = 'user'
623- cl .nodeid = nodeid
623+ if nodeid is not None :
624+ cl .nodeid = nodeid
624625 cl .db = self .db
625626 cl .userid = userid
626627 cl .language = ('en' ,)
628+ cl .error_message = []
629+ cl .template = 'item'
627630 return cl
628631
629632 def testClassPermission (self ):
@@ -636,18 +639,91 @@ def testClassPermission(self):
636639
637640 def testCheckAndPropertyPermission (self ):
638641 self .db .security .permissions = {}
639- def own_record (db , userid , itemid ): return userid == itemid
642+ def own_record (db , userid , itemid ):
643+ return userid == itemid
640644 p = self .db .security .addPermission (name = 'Edit' , klass = 'user' ,
641645 check = own_record , properties = ("password" , ))
642646 self .db .security .addPermissionToRole ('User' , p )
643647
644648 cl = self ._make_client (dict (username = 'bob' ))
645649 self .assertRaises (exceptions .Unauthorised ,
646650 actions .EditItemAction (cl ).handle )
651+ cl = self ._make_client (dict (roles = 'User,Admin' ), userid = '4' , nodeid = '4' )
652+ self .assertRaises (exceptions .Unauthorised ,
653+ actions .EditItemAction (cl ).handle )
654+ cl = self ._make_client (dict (roles = 'User,Admin' ), userid = '4' )
655+ self .assertRaises (exceptions .Unauthorised ,
656+ actions .EditItemAction (cl ).handle )
657+ cl = self ._make_client (dict (roles = 'User,Admin' ))
658+ self .assertRaises (exceptions .Unauthorised ,
659+ actions .EditItemAction (cl ).handle )
660+ # working example, mary may change her pw
661+ cl = self ._make_client ({'password' :'ob' , '@confirm@password' :'ob' },
662+ nodeid = '4' , userid = '4' )
663+ self .assertRaises (exceptions .Redirect ,
664+ actions .EditItemAction (cl ).handle )
647665 cl = self ._make_client ({'password' :'bob' , '@confirm@password' :'bob' })
648666 self .failUnlessRaises (exceptions .Unauthorised ,
649667 actions .EditItemAction (cl ).handle )
650668
669+ def testCreatePermission (self ):
670+ # this checks if we properly differentiate between create and
671+ # edit permissions
672+ self .db .security .permissions = {}
673+ self .db .security .addRole (name = 'UserAdd' )
674+ # Don't allow roles
675+ p = self .db .security .addPermission (name = 'Create' , klass = 'user' ,
676+ properties = ("username" , "password" , "address" ,
677+ "alternate_address" , "realname" , "phone" , "organisation" ,
678+ "timezone" ))
679+ self .db .security .addPermissionToRole ('UserAdd' , p )
680+ # Don't allow roles *and* don't allow username
681+ p = self .db .security .addPermission (name = 'Edit' , klass = 'user' ,
682+ properties = ("password" , "address" , "alternate_address" ,
683+ "realname" , "phone" , "organisation" , "timezone" ))
684+ self .db .security .addPermissionToRole ('UserAdd' , p )
685+ self .db .user .set ('4' , roles = 'UserAdd' )
686+
687+ # anonymous may not
688+ cl = self ._make_client ({'username' :'new_user' , 'password' :'secret' ,
689+ '@confirm@password' :
'secret' ,
'address' :
'[email protected] ' ,
690+ 'roles' :'Admin' }, nodeid = None , userid = '2' )
691+ self .assertRaises (exceptions .Unauthorised ,
692+ actions .NewItemAction (cl ).handle )
693+ # Don't allow creating new user with roles
694+ cl = self ._make_client ({'username' :'new_user' , 'password' :'secret' ,
695+ '@confirm@password' :
'secret' ,
'address' :
'[email protected] ' ,
696+ 'roles' :'Admin' }, nodeid = None , userid = '4' )
697+ self .assertRaises (exceptions .Unauthorised ,
698+ actions .NewItemAction (cl ).handle )
699+ self .assertEqual (cl .error_message ,[])
700+ # this should work
701+ cl = self ._make_client ({'username' :'new_user' , 'password' :'secret' ,
702+ '@confirm@password' :
'secret' ,
'address' :
'[email protected] ' },
703+ nodeid = None , userid = '4' )
704+ self .assertRaises (exceptions .Redirect ,
705+ actions .NewItemAction (cl ).handle )
706+ self .assertEqual (cl .error_message ,[])
707+ # don't allow changing (my own) username (in this example)
708+ cl = self ._make_client (dict (username = 'new_user42' ), userid = '4' )
709+ self .assertRaises (exceptions .Unauthorised ,
710+ actions .EditItemAction (cl ).handle )
711+ cl = self ._make_client (dict (username = 'new_user42' ), userid = '4' ,
712+ nodeid = '4' )
713+ self .assertRaises (exceptions .Unauthorised ,
714+ actions .EditItemAction (cl ).handle )
715+ # don't allow changing (my own) roles
716+ cl = self ._make_client (dict (roles = 'User,Admin' ), userid = '4' ,
717+ nodeid = '4' )
718+ self .assertRaises (exceptions .Unauthorised ,
719+ actions .EditItemAction (cl ).handle )
720+ cl = self ._make_client (dict (roles = 'User,Admin' ), userid = '4' )
721+ self .assertRaises (exceptions .Unauthorised ,
722+ actions .EditItemAction (cl ).handle )
723+ cl = self ._make_client (dict (roles = 'User,Admin' ))
724+ self .assertRaises (exceptions .Unauthorised ,
725+ actions .EditItemAction (cl ).handle )
726+
651727 def testRoles (self ):
652728 cl = self ._make_client ({})
653729 self .db .user .set ('1' , roles = 'aDmin, uSer' )
0 commit comments