Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def invalid_key(*args, **kwargs):
raise InvalidKeyError("foo", "endpoint", ["bar", "other"])
def test_should_raise_an_exception(self):
keys = ["key1", "key2"]
available_keys = ("key1", "key3")
scope = "root"
with pytest.raises(InvalidKeyError) as excinfo:
APINode.validate_keys(keys, available_keys, scope)
assert (
str(excinfo.value)
== "Invalid key `key2` at `root` scope. Available keys are: ('key1', 'key3')"
)
def test_should_raise_an_exception(self):
keys = ["key1", "key2"]
available_keys = ("key1", "key3")
mandatory_keys = ("key1", "key2")
scope = "endpoint"
with pytest.raises(InvalidKeyError) as excinfo:
validate_keys(keys, available_keys, mandatory_keys, scope)
assert (
str(excinfo.value)
== "Invalid key 'key2' at 'endpoint' scope. Available keys are: ('key1', 'key3')"
)
error_message = f"API spec file is empty. {str(e)}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
except (yaml.YAMLError, FileFormatNotSupportedError) as e:
logger.error(e)
raise SystemExit(ExitCode.USAGE_ERROR)
try:
if API_KEY not in api_spec:
raise MissingMandatoryKeyError({API_KEY}, ROOT_SCOPE)
root_node = EndpointNode(api_spec[API_KEY])
results = root_node.run()
except (
InvalidKeyError,
MissingMandatoryKeyError,
KeyError,
InvalidPythonCodeError,
) as e:
error_message = "Error loading API spec."
error_message = "{} {}".format(error_message, str(e))
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
try:
write_report(results)
except (BadConfigurationError, InvalidPythonCodeError) as e:
logger.error(e)
raise SystemExit(ExitCode.USAGE_ERROR)
session.exit()
def _validate_allowed_keys(keys, available_keys, scope):
for key in keys:
if not key in available_keys:
raise InvalidKeyError(key, scope, available_keys)
def validate_keys(self, keys, available_keys, scope):
for key in keys:
if not key in available_keys:
raise InvalidKeyError(key, scope, available_keys)
def __init__(self, key, scope, available_keys, *args):
message = f"Invalid key '{key}' at '{scope}' scope. Available keys are: {available_keys}"
super(InvalidKeyError, self).__init__(message, *args)