Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_should_raises_invalid_python_code_error(self):
with pytest.raises(InvalidPythonCodeError) as excinfo:
CodeEvaluator.evaluate(
"${{response.url == 'abc'}}", {}, is_a_test_case=True
)
assert (
str(excinfo.value)
== "Invalid Python code defined in the API spec. "
"Exception: 'NoneType' object has no attribute 'url'. "
def test_should_raises_invalid_python_code_error(self):
with pytest.raises(InvalidPythonCodeError) as excinfo:
evaluate_python_code("${{1/0}}")
assert (
str(excinfo.value)
== "Invalid Python code defined in the API spec: division by zero"
)
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 evaluate(cls, sequence, vars):
try:
sequence = cls._evaluate_env_var(sequence)
except BadConfigurationError as e:
logger.error(e)
sys.exit()
sequence = cls._evaluate_custom_var(sequence, vars)
try:
return CodeEvaluator.evaluate(sequence, vars)
except InvalidPythonCodeError as e:
logger.error(e)
sys.exit()
def __init__(self, error_message, code, *args):
error_message = (
f"Invalid Python code defined in the API spec. "
f"Exception: {error_message}. "
f"Code: {code}."
)
super(InvalidPythonCodeError, self).__init__(error_message, *args)
def evaluate(cls, sequence, vars, is_a_test_case=False):
match = cls.python_code_pattern.search(sequence)
if not match:
return sequence
code = match.group("python_code")
response = vars.get("response")
try:
if is_a_test_case:
return cls._assert_code(code, response)
return cls._evaluate_sequence(sequence, match, code, response)
except Exception as e:
raise InvalidPythonCodeError(str(e), code)
match = python_code_pattern.search(sequence)
if not match:
return sequence
code = match.group("python_code")
responses = SimpleNamespace(**self.api_tree.responses)
try:
python_code_value = str(eval(code))
return self.string_evaluator.replace_var_with_value(
sequence, match.group(), python_code_value
)
except Exception as e:
raise InvalidPythonCodeError(str(e))