Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def missing_mandatory_key(*args, **kwargs):
raise MissingMandatoryKeyError(["foo", "bar"], "endpoint")
def test_missing_required_keys(self):
with pytest.raises(MissingMandatoryKeyError) as excinfo:
request = RequestNode(
spec={}, endpoint=EndpointNode({"name": "foo", "requests": [{}]})
)
assert str(excinfo.value) == "Missing 'name' key(s) at 'request' scope"
def test_should_raise_an_exception(self):
keys = ["key1"]
available_keys = ("key1", "key3")
mandatory_keys = ("key1", "key2")
scope = "endpoint"
with pytest.raises(MissingMandatoryKeyError) as excinfo:
validate_keys(keys, available_keys, mandatory_keys, scope)
assert str(excinfo.value) == "Missing 'key2' key(s) at 'endpoint' scope"
def test_missing_required_keys(self):
with pytest.raises(MissingMandatoryKeyError) as excinfo:
request_node = RequestNode(
spec={"name": "foo", "path": "bar"},
endpoint=EndpointNode({"name": "foo", "requests": [{}]}),
)
test_node = TestingNode(spec={}, request=request_node)
assert (
str(excinfo.value) == "Missing 'assert', 'name' key(s) at 'test' scope"
)
def test_missing_required_keys(self):
with pytest.raises(MissingMandatoryKeyError) as excinfo:
endpoints = [{}, {}]
node = EndpointNode({"endpoints": endpoints})
assert str(excinfo.value) == "Missing 'name' key(s) at 'endpoint' scope"
api_spec = load_config_file(spec_path)
except FileNotFoundError as e:
error_message = f"Could not find API spec file: {spec_path}. {str(e)}"
logger.error(error_message)
raise SystemExit(ExitCode.USAGE_ERROR)
except EmptyConfigFileError as e:
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:
def _validate_required_keys(keys, required_keys, scope):
if not set(required_keys) <= set(keys):
missing_keys = set(required_keys) - set(keys)
raise MissingMandatoryKeyError(missing_keys, scope)
def __init__(self, missing_keys, scope, *args):
missing_keys_str = ", ".join(f"'{k}'" for k in sorted(missing_keys))
message = f"Missing {missing_keys_str} key(s) at '{scope}' scope"
super(MissingMandatoryKeyError, self).__init__(message, *args)