Exemple #1
0
        def _add_test(test_runner, test_dict):
            """ add test to testcase.
            """
            def test(self):
                try:
                    test_runner.run_test(test_dict)
                except exceptions.MyBaseFailure as ex:
                    self.fail(str(ex))
                finally:
                    self.meta_datas = test_runner.meta_datas

            if "config" in test_dict:
                # run nested testcase
                test.__doc__ = test_dict["config"].get("name")
                variables = test_dict["config"].get("variables", {})
            else:
                # run api test
                test.__doc__ = test_dict.get("name")
                variables = test_dict.get("variables", {})

            if isinstance(test.__doc__, parser.LazyString):
                try:
                    parsed_variables = parser.parse_variables_mapping(variables)
                    test.__doc__ = parser.parse_lazy_data(
                        test.__doc__, parsed_variables
                    )
                except exceptions.VariableNotFound:
                    test.__doc__ = str(test.__doc__)

            return test
    def test_get_parsed_request(self):
        variables = {
            "random": "${gen_random_string(5)}",
            "data": '{"name": "user", "password": "******"}',
            "authorization": "${gen_md5($TOKEN, $data, $random)}",
            "TOKEN": "debugtalk"
        }
        functions = {
            "gen_random_string": gen_random_string,
            "gen_md5": gen_md5
        }
        variables = parser.prepare_lazy_data(variables, functions,
                                             variables.keys())
        variables = parser.parse_variables_mapping(variables)
        self.context.init_test_variables(variables)

        request = {
            "url": "http://127.0.0.1:5000/api/users/1000",
            "method": "POST",
            "headers": {
                "Content-Type": "application/json",
                "authorization": "$authorization",
                "random": "$random",
                "secret_key": "$SECRET_KEY"
            },
            "data": "$data"
        }
        prepared_request = parser.prepare_lazy_data(
            request, functions,
            {"authorization", "random", "SECRET_KEY", "data"})
        parsed_request = self.context.eval_content(prepared_request)
        self.assertIn("authorization", parsed_request["headers"])
        self.assertEqual(len(parsed_request["headers"]["authorization"]), 32)
        self.assertIn("random", parsed_request["headers"])
        self.assertEqual(len(parsed_request["headers"]["random"]), 5)
        self.assertIn("data", parsed_request)
        self.assertEqual(parsed_request["data"],
                         '{"name": "user", "password": "******"}')
        self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk")
 def test_init_test_variables(self):
     variables = {
         "random": "${gen_random_string($num)}",
         "authorization": "${gen_md5($TOKEN, $data, $random)}",
         "data": "$username",
         # TODO: escape '{' and '}'
         # "data": '{"name": "$username", "password": "******"}',
         "TOKEN": "debugtalk",
         "username": "******",
         "num": 6
     }
     functions = {
         "gen_random_string": gen_random_string,
         "gen_md5": gen_md5
     }
     variables = parser.prepare_lazy_data(variables, functions,
                                          variables.keys())
     variables = parser.parse_variables_mapping(variables)
     self.context.init_test_variables(variables)
     variables_mapping = self.context.test_variables_mapping
     self.assertEqual(len(variables_mapping["random"]), 6)
     self.assertEqual(len(variables_mapping["authorization"]), 32)
     self.assertEqual(variables_mapping["data"], 'user1')
Exemple #4
0
    def init_test_variables(self, variables_mapping=None):
        """ init test variables, called when each test(api) starts.
            variables_mapping will be evaluated first.

        Args:
            variables_mapping (dict)
                {
                    "random": "${gen_random_string(5)}",
                    "authorization": "${gen_md5($TOKEN, $data, $random)}",
                    "data": '{"name": "user", "password": "******"}',
                    "TOKEN": "debugtalk",
                }

        """
        variables_mapping = variables_mapping or {}
        variables_mapping = utils.ensure_mapping_format(variables_mapping)
        variables_mapping.update(self.session_variables_mapping)
        parsed_variables_mapping = parser.parse_variables_mapping(
            variables_mapping)

        self.test_variables_mapping = {}
        # priority: extracted variable > teststep variable
        self.test_variables_mapping.update(parsed_variables_mapping)
        self.test_variables_mapping.update(self.session_variables_mapping)
Exemple #5
0
 def __init__(self, variables=None):
     variables_mapping = utils.ensure_mapping_format(variables or {})
     self.session_variables_mapping = parser.parse_variables_mapping(
         variables_mapping)
     self.test_variables_mapping = {}
     self.init_test_variables()