<s> import httplib <EOL> import mock <EOL> import six <EOL> import st2common . validators . api . action as action_validator <EOL> from st2common . rbac . types import PermissionType <EOL> from st2common . rbac . types import ResourceType <EOL> from st2common . persistence . auth import User <EOL> from st2common . persistence . rbac import Role <EOL> from st2common . persistence . rbac import UserRoleAssignment <EOL> from st2common . persistence . rbac import PermissionGrant <EOL> from st2common . models . db . auth import UserDB <EOL> from st2common . models . db . rbac import RoleDB <EOL> from st2common . models . db . rbac import UserRoleAssignmentDB <EOL> from st2common . models . db . rbac import PermissionGrantDB <EOL> from st2tests . fixturesloader import FixturesLoader <EOL> from tests . base import APIControllerWithRBACTestCase <EOL> http_client = six . moves . http_client <EOL> __all__ = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> FIXTURES_PACK = '<STR_LIT>' <EOL> TEST_FIXTURES = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> } <EOL> ACTION_2 = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:c>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT:default>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> '<STR_LIT:d>' : { '<STR_LIT:type>' : '<STR_LIT:string>' , '<STR_LIT:default>' : '<STR_LIT>' , '<STR_LIT>' : True } <EOL> } <EOL> } <EOL> class ActionControllerRBACTestCase ( APIControllerWithRBACTestCase ) : <EOL> fixtures_loader = FixturesLoader ( ) <EOL> def setUp ( self ) : <EOL> super ( ActionControllerRBACTestCase , self ) . setUp ( ) <EOL> self . fixtures_loader . save_fixtures_to_db ( fixtures_pack = FIXTURES_PACK , <EOL> fixtures_dict = TEST_FIXTURES ) <EOL> file_name = '<STR_LIT>' <EOL> ActionControllerRBACTestCase . ACTION_1 = self . fixtures_loader . load_fixtures ( <EOL> fixtures_pack = FIXTURES_PACK , <EOL> fixtures_dict = { '<STR_LIT>' : [ file_name ] } ) [ '<STR_LIT>' ] [ file_name ] <EOL> self = self <EOL> self . users = { } <EOL> self . roles = { } <EOL> user_1_db = UserDB ( name = '<STR_LIT>' ) <EOL> user_1_db = User . add_or_update ( user_1_db ) <EOL> self . users [ '<STR_LIT>' ] = user_1_db <EOL> user_2_db = UserDB ( name = '<STR_LIT>' ) <EOL> user_2_db = User . add_or_update ( user_2_db ) <EOL> self . users [ '<STR_LIT>' ] = user_2_db <EOL> grant_db = PermissionGrantDB ( resource_uid = '<STR_LIT>' , <EOL> resource_type = ResourceType . PACK , <EOL> permission_types = [ PermissionType . ACTION_CREATE ] ) <EOL> grant_db = PermissionGrant . add_or_update ( grant_db ) <EOL> permission_grants = [ str ( grant_db . id ) ] <EOL> role_1_db = RoleDB ( name = '<STR_LIT>' , permission_grants = permission_grants ) <EOL> role_1_db = Role . add_or_update ( role_1_db ) <EOL> self . roles [ '<STR_LIT>' ] = role_1_db <EOL> user_db = self . users [ '<STR_LIT>' ] <EOL> role_assignment_db = UserRoleAssignmentDB ( <EOL> user = user_db . name , <EOL> role = self . roles [ '<STR_LIT>' ] . name ) <EOL> UserRoleAssignment . add_or_update ( role_assignment_db ) <EOL> def test_create_action_no_action_create_permission ( self ) : <EOL> user_db = self . users [ '<STR_LIT>' ] <EOL> self . use_user ( user_db ) <EOL> resp = self . __do_post ( ActionControllerRBACTestCase . ACTION_1 ) <EOL> expected_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( resp . status_code , httplib . FORBIDDEN ) <EOL> self . assertEqual ( resp . json [ '<STR_LIT>' ] , expected_msg ) <EOL> self . use_user ( { } ) <EOL> @ mock . patch . object ( action_validator , '<STR_LIT>' , mock . MagicMock ( <EOL> return_value = True ) ) <EOL> def test_create_action_success ( self ) : <EOL> user_db = self . users [ '<STR_LIT>' ] <EOL> self . use_user ( user_db ) <EOL> resp = self . __do_post ( ACTION_2 ) <EOL> self . assertEqual ( resp . status_code , httplib . CREATED ) <EOL> @ staticmethod <EOL> def __get_action_id ( resp ) : <EOL> return resp . json [ '<STR_LIT:id>' ] <EOL> def __do_post ( self , rule ) : <EOL> return self . app . post_json ( '<STR_LIT>' , rule , expect_errors = True ) <EOL> def __do_delete ( self , action_id , expect_errors = False ) : <EOL> return self . app . delete ( '<STR_LIT>' % action_id , expect_errors = expect_errors ) </s>
