def test_verify_account_access_failure_unknown_reason(self): """Assert that account access fails for an unknown reason.""" action = "ec2:DescribeSnapshots" function_name = "describe_snapshots" mock_function = Mock() mock_function.side_effect = ClientError( error_response={"Error": {"Code": "itisamystery.gif"}}, operation_name=action, ) mock_session = Mock() mock_client = mock_session.client.return_value mock_client.attach_mock(mock_function, function_name) with self.assertRaises(ClientError): helper._verify_policy_action(mock_session, action)
def assert_verify_policy_action_success(self, action, function_name, func_args=(), func_kwargs=dict()): """ Assert _verify_policy_action succeeds with dry run "exception". This helper function is intended to simplify testing _verify_policy_action by offloading all the mocking and DryRun assertion stuff. Args: action (str): the action to verify function_name (str): the ec2 method name that would be called func_args (list): positional arguments that would be sent to the ec2 method called by _verify_policy_action func_kwargs (dict): keyword arguments that would be sent to the ec2 method called by _verify_policy_action """ mock_dryrun_function = Mock() mock_dryrun_function.side_effect = ClientError( error_response={'Error': { 'Code': 'DryRunOperation' }}, operation_name=action, ) mock_session = Mock() mock_client = mock_session.client.return_value mock_client.attach_mock(mock_dryrun_function, function_name) result = helper._verify_policy_action(mock_session, action) self.assertTrue(result) mock_dryrun_function.assert_called_once_with(*func_args, **func_kwargs)
def test_verify_account_access_failure_unauthorized(self): """Assert that account access fails for an unauthorized operation.""" action = "ec2:DescribeSnapshots" function_name = "describe_snapshots" mock_function = Mock() mock_function.side_effect = ClientError( error_response={"Error": {"Code": "UnauthorizedOperation"}}, operation_name=action, ) mock_session = Mock() mock_client = mock_session.client.return_value mock_client.attach_mock(mock_function, function_name) result = helper._verify_policy_action(mock_session, action) self.assertFalse(result)
def test_verify_policy_action_unknown(self): """Assert trying to verify an unknown action returns False.""" mock_session = Mock() bogus_action = str(uuid.uuid4()) result = helper._verify_policy_action(mock_session, bogus_action) self.assertFalse(result)