<s> """<STR_LIT>""" <EOL> import mock <EOL> from google . apputils import app <EOL> from google . apputils import basetest <EOL> from cauliflowervest . server . cron import group_sync <EOL> class GroupSyncTest ( basetest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . g = group_sync . GroupSync ( ) <EOL> def testBatchDatastoreOp ( self ) : <EOL> batch_size = <NUM_LIT:2> <EOL> mock_op = mock . Mock ( ) <EOL> entities = [ '<STR_LIT:foo>' , '<STR_LIT:bar>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . g . _BatchDatastoreOp ( mock_op , entities , batch_size = batch_size ) <EOL> mock_op . assert_has_calls ( [ <EOL> mock . call ( [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) , <EOL> mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> mock . call ( [ '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> mock . call ( [ '<STR_LIT>' ] ) ] ) <EOL> @ mock . patch . object ( group_sync . models , '<STR_LIT>' ) <EOL> @ mock . patch . object ( group_sync . users , '<STR_LIT>' ) <EOL> def testMakeUserEntity ( self , users_user , models_user ) : <EOL> email = '<STR_LIT>' <EOL> user_perms = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> for permission_type in group_sync . permissions . TYPES : <EOL> user_perms [ permission_type ] = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> mock_obj = mock . MagicMock ( ) <EOL> users_user . return_value = '<STR_LIT>' <EOL> models_user . return_value = mock_obj <EOL> self . assertEqual ( mock_obj , self . g . _MakeUserEntity ( email , user_perms ) ) <EOL> mock_obj . SetPerms . assert_has_calls ( <EOL> [ mock . call ( user_perms [ t ] , t ) for t in group_sync . permissions . TYPES ] ) <EOL> @ mock . patch . object ( group_sync . models , '<STR_LIT>' ) <EOL> @ mock . patch . object ( group_sync . users , '<STR_LIT>' ) <EOL> def testMakeUserEntityNoPermissions ( self , users_user , models_user ) : <EOL> email = '<STR_LIT>' <EOL> user_perms = { } <EOL> mock_obj = mock . MagicMock ( ) <EOL> users_user . return_value = '<STR_LIT>' <EOL> models_user . return_value = mock_obj <EOL> self . assertEqual ( mock_obj , self . g . _MakeUserEntity ( email , user_perms ) ) <EOL> calls = [ ] <EOL> for permission_type in group_sync . permissions . TYPES : <EOL> calls . append ( mock . call ( [ ] , permission_type ) ) <EOL> mock_obj . SetPerms . assert_has_calls ( calls ) <EOL> def testGetGroupMembersAndPermissions ( self ) : <EOL> group_sync . settings . GROUPS = { <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:list>' ) ) , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ] , <EOL> } <EOL> group1 = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> group2 = [ '<STR_LIT>' ] <EOL> expected_return = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:list>' ] ) , <EOL> '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } , <EOL> } <EOL> self . g . _GetGroupMembers = mock . Mock ( ) <EOL> arg2res = { '<STR_LIT>' : group1 , '<STR_LIT>' : group2 } <EOL> self . g . _GetGroupMembers . side_effect = lambda x : arg2res [ x ] <EOL> ret = self . g . _GetGroupMembersAndPermissions ( ) <EOL> self . assertEqual ( expected_return , ret ) <EOL> @ mock . patch . object ( group_sync . db . Key , '<STR_LIT>' ) <EOL> @ mock . patch . object ( group_sync . models . User , '<STR_LIT:all>' ) <EOL> def testGet ( self , all_mock , from_path_mock ) : <EOL> self . g . _BatchDatastoreOp = mock . Mock ( ) <EOL> self . g . _GetGroupMembersAndPermissions = mock . Mock ( ) <EOL> self . g . _MakeUserEntity = mock . Mock ( ) <EOL> group_users = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:list>' ] ) , <EOL> '<STR_LIT>' : set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) } , <EOL> } <EOL> to_del_user = '<STR_LIT>' <EOL> self . g . _GetGroupMembersAndPermissions . return_value = group_users <EOL> mock_key_to_del = mock . MagicMock ( ) <EOL> mock_key_u1 = mock . MagicMock ( ) <EOL> all_mock . return_value = [ mock_key_to_del , mock_key_u1 ] <EOL> mock_key_to_del . name . return_value = to_del_user <EOL> mock_key_u1 . name . return_value = '<STR_LIT>' <EOL> from_path_mock . return_value = '<STR_LIT>' <EOL> to_add = [ ] <EOL> calls = [ ] <EOL> for u , p in group_users . iteritems ( ) : <EOL> calls . append ( ( u , p , '<STR_LIT>' % u ) ) <EOL> to_add . append ( '<STR_LIT>' % u ) <EOL> def MatchCallSideEffect ( a1 , a2 ) : <EOL> for u , p , r in calls : <EOL> if u != a1 or p != a2 : <EOL> continue <EOL> return r <EOL> raise ValueError <EOL> self . g . _MakeUserEntity . side_effect = MatchCallSideEffect <EOL> self . g . get ( ) <EOL> from_path_mock . assert_called_once_with ( <EOL> group_sync . models . User . __name__ , to_del_user ) <EOL> self . g . _BatchDatastoreOp . assert_has_calls ( [ <EOL> mock . call ( group_sync . db . delete , [ '<STR_LIT>' ] ) , <EOL> mock . call ( group_sync . db . put , to_add ) ] ) <EOL> def main ( unused_argv ) : <EOL> basetest . main ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( ) </s>
