How to use the scanapi.errors.InvalidPythonCodeError function in scanapi

To help you get started, we’ve selected a few scanapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github camilamaia / scanapi / tests / unit / evaluators / test_code_evaluator.py View on Github external
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'. "
github camilamaia / scanapi / tests / unit / test_variable_parser.py View on Github external
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"
                    )
github camilamaia / scanapi / scanapi / scan.py View on Github external
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()
github camilamaia / scanapi / scanapi / refactor / evaluators / string_evaluator.py View on Github external
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()
github camilamaia / scanapi / scanapi / errors.py View on Github external
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)
github camilamaia / scanapi / scanapi / evaluators / code_evaluator.py View on Github external
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)
github camilamaia / scanapi / scanapi / evaluators / code_evaluator.py View on Github external
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))