<s> '''<STR_LIT>''' <EOL> import os <EOL> import unittest <EOL> from codalab . common import UsageError <EOL> from codalab . client . local_bundle_client import LocalBundleClient <EOL> from codalab . lib import path_util , spec_util <EOL> from codalab . lib . bundle_store import MultiDiskBundleStore <EOL> from codalab . model . sqlite_model import SQLiteModel <EOL> from codalab . server . auth import MockAuthHandler , User <EOL> class GroupsAndPermsTest ( unittest . TestCase ) : <EOL> '''<STR_LIT>''' <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . test_root = path_util . normalize ( "<STR_LIT>" ) <EOL> path_util . make_directory ( cls . test_root ) <EOL> cls . bundle_store = MultiDiskBundleStore ( cls . test_root ) <EOL> cls . model = SQLiteModel ( "<STR_LIT>" . format ( os . path . join ( cls . test_root , '<STR_LIT>' ) ) , { } ) <EOL> cls . model . root_user_id = '<STR_LIT:0>' <EOL> users = [ User ( '<STR_LIT:root>' , '<STR_LIT:0>' ) , User ( '<STR_LIT>' , '<STR_LIT:1>' ) , User ( '<STR_LIT>' , '<STR_LIT:2>' ) , User ( '<STR_LIT>' , '<STR_LIT:4>' ) ] <EOL> cls . auth_handler = MockAuthHandler ( users ) <EOL> cls . client = LocalBundleClient ( '<STR_LIT>' , cls . bundle_store , cls . model , None , None , cls . auth_handler , verbose = <NUM_LIT:1> ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> cls . model . engine . close ( ) <EOL> path_util . remove ( cls . test_root ) <EOL> def set_current_user ( self , username , password ) : <EOL> token_info = self . client . login ( '<STR_LIT>' , username , password ) <EOL> self . auth_handler . validate_token ( token_info [ '<STR_LIT>' ] ) <EOL> def test_new_group ( self ) : <EOL> defaultCount = <NUM_LIT:1> <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> groups = self . client . list_groups ( ) <EOL> self . assertEqual ( defaultCount + <NUM_LIT:0> , len ( groups ) ) <EOL> g1 = self . client . new_group ( '<STR_LIT>' ) <EOL> self . assertIsNotNone ( g1 ) <EOL> self . assertIn ( '<STR_LIT:name>' , g1 ) <EOL> self . assertEqual ( '<STR_LIT>' , g1 [ '<STR_LIT:name>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , g1 ) <EOL> groups = self . client . list_groups ( ) <EOL> self . assertEqual ( defaultCount + <NUM_LIT:1> , len ( groups ) ) <EOL> g1prime = [ g for g in groups if g [ '<STR_LIT:name>' ] != '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> self . assertIn ( '<STR_LIT:name>' , g1prime ) <EOL> self . assertEqual ( g1 [ '<STR_LIT:name>' ] , g1prime [ '<STR_LIT:name>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , g1prime ) <EOL> self . assertEqual ( g1 [ '<STR_LIT>' ] , g1prime [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , g1prime ) <EOL> self . assertEqual ( '<STR_LIT>' , g1prime [ '<STR_LIT>' ] ) <EOL> g1deleted = self . client . rm_group ( '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT:name>' , g1deleted ) <EOL> self . assertEqual ( g1 [ '<STR_LIT:name>' ] , g1deleted [ '<STR_LIT:name>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , g1deleted ) <EOL> self . assertEqual ( g1 [ '<STR_LIT>' ] , g1deleted [ '<STR_LIT>' ] ) <EOL> groups = self . client . list_groups ( ) <EOL> self . assertEqual ( defaultCount + <NUM_LIT:0> , len ( groups ) ) <EOL> def test_new_group_bad_name ( self ) : <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( UsageError ) : <EOL> self . client . new_group ( '<STR_LIT>' ) <EOL> def test_delete_nonexistent_group ( self ) : <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> uuid = spec_util . generate_uuid ( ) <EOL> with self . assertRaises ( UsageError ) : <EOL> self . client . rm_group ( uuid ) <EOL> def test_delete_group ( self ) : <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> grp_name = '<STR_LIT>' <EOL> g1 = self . client . new_group ( grp_name ) <EOL> with self . assertRaises ( UsageError ) : <EOL> self . client . new_group ( grp_name ) <EOL> g1deleted = self . client . rm_group ( g1 [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( g1 [ '<STR_LIT>' ] , g1deleted [ '<STR_LIT>' ] ) <EOL> g2 = self . client . new_group ( grp_name ) <EOL> g2deleted = self . client . rm_group ( grp_name ) <EOL> self . assertEqual ( g2 [ '<STR_LIT>' ] , g2deleted [ '<STR_LIT>' ] ) <EOL> with self . assertRaises ( UsageError ) : <EOL> self . client . rm_group ( grp_name ) <EOL> def test_membership ( self ) : <EOL> def _assert_group_count_for ( username , num_group ) : <EOL> self . set_current_user ( username , '<STR_LIT>' ) <EOL> groups = self . client . list_groups ( ) <EOL> num_group += <NUM_LIT:1> <EOL> if num_group != len ( groups ) : <EOL> print '<STR_LIT>' , groups <EOL> self . assertEqual ( num_group , len ( groups ) ) <EOL> _assert_group_count_for ( '<STR_LIT:root>' , <NUM_LIT:0> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> grp_name = '<STR_LIT>' <EOL> g1 = self . client . new_group ( grp_name ) <EOL> _assert_group_count_for ( '<STR_LIT:root>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> self . client . add_user ( '<STR_LIT>' , grp_name , True ) <EOL> _assert_group_count_for ( '<STR_LIT:root>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . set_current_user ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . client . add_user ( '<STR_LIT>' , g1 [ '<STR_LIT>' ] , False ) <EOL> _assert_group_count_for ( '<STR_LIT:root>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . set_current_user ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> g1_info = self . client . group_info ( grp_name ) <EOL> self . assertEqual ( g1 [ '<STR_LIT>' ] , g1_info [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , g1_info ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( g1_info [ '<STR_LIT>' ] ) ) <EOL> members = { member [ '<STR_LIT>' ] : member [ '<STR_LIT>' ] for member in g1_info [ '<STR_LIT>' ] } <EOL> self . assertIn ( '<STR_LIT:root>' , members ) <EOL> self . assertEqual ( '<STR_LIT>' , members [ '<STR_LIT:root>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , members ) <EOL> self . assertEqual ( '<STR_LIT>' , members [ '<STR_LIT>' ] ) <EOL> self . assertIn ( '<STR_LIT>' , members ) <EOL> self . assertEqual ( '<STR_LIT>' , members [ '<STR_LIT>' ] ) <EOL> self . set_current_user ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( UsageError ) : <EOL> self . client . add_user ( '<STR_LIT>' , g1 [ '<STR_LIT>' ] , False ) <EOL> self . set_current_user ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> with self . assertRaises ( UsageError ) : <EOL> self . client . rm_group ( grp_name ) <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> self . client . rm_user ( '<STR_LIT>' , grp_name ) <EOL> _assert_group_count_for ( '<STR_LIT:root>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . set_current_user ( '<STR_LIT:root>' , '<STR_LIT>' ) <EOL> self . client . rm_group ( grp_name ) <EOL> _assert_group_count_for ( '<STR_LIT:root>' , <NUM_LIT:0> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> _assert_group_count_for ( '<STR_LIT>' , <NUM_LIT:0> ) </s>
