<s> from . . import base <EOL> from girder . models . model_base import AccessControlledModel , Model , AccessType <EOL> from girder . utility . model_importer import ModelImporter <EOL> class FakeAcModel ( AccessControlledModel ) : <EOL> def initialize ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> self . exposeFields ( level = AccessType . READ , fields = '<STR_LIT>' ) <EOL> self . exposeFields ( level = AccessType . WRITE , fields = ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . exposeFields ( level = AccessType . ADMIN , fields = '<STR_LIT>' ) <EOL> self . exposeFields ( level = AccessType . SITE_ADMIN , fields = '<STR_LIT>' ) <EOL> def validate ( self , doc ) : <EOL> return doc <EOL> class FakeModel ( Model ) : <EOL> def initialize ( self ) : <EOL> self . name = '<STR_LIT>' <EOL> self . exposeFields ( level = AccessType . READ , fields = '<STR_LIT>' ) <EOL> self . exposeFields ( level = AccessType . SITE_ADMIN , fields = '<STR_LIT>' ) <EOL> def validate ( self , doc ) : <EOL> return doc <EOL> def setUpModule ( ) : <EOL> base . startServer ( ) <EOL> def tearDownModule ( ) : <EOL> base . stopServer ( ) <EOL> class ModelTestCase ( base . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> base . TestCase . setUp ( self ) <EOL> ModelImporter . registerModel ( '<STR_LIT>' , FakeAcModel ( ) ) <EOL> ModelImporter . registerModel ( '<STR_LIT>' , FakeModel ( ) ) <EOL> def testModelFiltering ( self ) : <EOL> users = ( { <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' <EOL> } , { <EOL> '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:password>' : '<STR_LIT>' <EOL> } ) <EOL> adminUser , regUser = [ <EOL> self . model ( '<STR_LIT:user>' ) . createUser ( ** user ) for user in users ] <EOL> fields = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> fakeAc = self . model ( '<STR_LIT>' ) . save ( fields ) <EOL> fakeAc = self . model ( '<STR_LIT>' ) . setUserAccess ( <EOL> fakeAc , regUser , level = AccessType . READ ) <EOL> filtered = self . model ( '<STR_LIT>' ) . filter ( fakeAc , adminUser ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertFalse ( '<STR_LIT>' in filtered ) <EOL> self . model ( '<STR_LIT>' ) . exposeFields ( <EOL> level = AccessType . READ , fields = '<STR_LIT>' ) <EOL> filtered = self . model ( '<STR_LIT>' ) . filter ( fakeAc , regUser ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertFalse ( '<STR_LIT>' in filtered ) <EOL> self . assertFalse ( '<STR_LIT>' in filtered ) <EOL> self . assertFalse ( '<STR_LIT>' in filtered ) <EOL> self . model ( '<STR_LIT>' ) . hideFields ( level = AccessType . READ , fields = '<STR_LIT>' ) <EOL> fakeAc = self . model ( '<STR_LIT>' ) . setUserAccess ( <EOL> fakeAc , regUser , level = AccessType . ADMIN ) <EOL> filtered = self . model ( '<STR_LIT>' ) . filter ( fakeAc , regUser ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertTrue ( '<STR_LIT>' in filtered ) <EOL> self . assertFalse ( '<STR_LIT>' in filtered ) <EOL> self . assertFalse ( '<STR_LIT>' in filtered ) <EOL> fake = self . model ( '<STR_LIT>' ) . save ( fields ) <EOL> filtered = self . model ( '<STR_LIT>' ) . filter ( fake , regUser ) <EOL> self . assertEqual ( filtered , { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> filtered = self . model ( '<STR_LIT>' ) . filter ( fake , adminUser ) <EOL> self . assertEqual ( filtered , { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> def testAccessControlCleanup ( self ) : <EOL> user1 = self . model ( '<STR_LIT:user>' ) . createUser ( <EOL> email = '<STR_LIT>' , <EOL> login = '<STR_LIT>' , <EOL> firstName = '<STR_LIT>' , <EOL> lastName = '<STR_LIT>' , <EOL> password = '<STR_LIT>' <EOL> ) <EOL> user2 = self . model ( '<STR_LIT:user>' ) . createUser ( <EOL> email = '<STR_LIT>' , <EOL> login = '<STR_LIT>' , <EOL> firstName = '<STR_LIT>' , <EOL> lastName = '<STR_LIT>' , <EOL> password = '<STR_LIT>' <EOL> ) <EOL> group1 = self . model ( '<STR_LIT>' ) . createGroup ( <EOL> name = '<STR_LIT>' , <EOL> creator = user2 <EOL> ) <EOL> doc1 = { <EOL> '<STR_LIT>' : user1 [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> doc1 = self . model ( '<STR_LIT>' ) . setUserAccess ( <EOL> doc1 , user1 , level = AccessType . ADMIN ) <EOL> doc1 = self . model ( '<STR_LIT>' ) . setUserAccess ( <EOL> doc1 , user2 , level = AccessType . READ ) <EOL> doc1 = self . model ( '<STR_LIT>' ) . setGroupAccess ( <EOL> doc1 , group1 , level = AccessType . WRITE ) <EOL> doc1 = self . model ( '<STR_LIT>' ) . save ( doc1 ) <EOL> doc1Id = doc1 [ '<STR_LIT>' ] <EOL> doc1 = self . model ( '<STR_LIT>' ) . load ( doc1Id , force = True , exc = True ) <EOL> self . assertEqual ( len ( doc1 [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( len ( doc1 [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( doc1 [ '<STR_LIT>' ] , user1 [ '<STR_LIT>' ] ) <EOL> self . model ( '<STR_LIT:user>' ) . remove ( user1 ) <EOL> doc1 = self . model ( '<STR_LIT>' ) . load ( doc1Id , force = True , exc = True ) <EOL> self . assertEqual ( len ( doc1 [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( doc1 [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertIsNone ( doc1 . get ( '<STR_LIT>' ) ) <EOL> self . model ( '<STR_LIT>' ) . remove ( group1 ) <EOL> doc1 = self . model ( '<STR_LIT>' ) . load ( doc1Id , force = True , exc = True ) <EOL> self . assertEqual ( len ( doc1 [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( len ( doc1 [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:0> ) <EOL> self . assertIsNone ( doc1 . get ( '<STR_LIT>' ) ) </s>
