Example #1
0
 def test_parse_variables_mapping_dead_circle(self):
     variables = {"varA": "$varB", "varB": "123$varC"}
     check_variables_set = {"varA", "varB", "varC"}
     prepared_variables = parser.prepare_lazy_data(variables, {},
                                                   check_variables_set)
     with self.assertRaises(exceptions.VariableNotFound):
         parser.parse_variables_mapping(prepared_variables)
Example #2
0
 def test_parse_variables_mapping_ref_self(self):
     variables = {
         "varC": "${sum_two($a, $b)}",
         "a": 1,
         "b": 2,
         "token": "$token"
     }
     functions = {"sum_two": sum_two}
     prepared_variables = parser.prepare_lazy_data(variables, functions,
                                                   variables.keys())
     with self.assertRaises(exceptions.VariableNotFound):
         parser.parse_variables_mapping(prepared_variables)
Example #3
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):
                parsed_variables = parser.parse_variables_mapping(variables,
                                                                  ignore=True)
                test.__doc__ = parser.parse_lazy_data(test.__doc__,
                                                      parsed_variables)

            return test
Example #4
0
 def test_parse_variables_mapping_fix_duplicate_function_call(self):
     # fix duplicate function calling
     variables = {"varA": "$varB", "varB": "${gen_random_string(5)}"}
     functions = {"gen_random_string": gen_random_string}
     prepared_variables = parser.prepare_lazy_data(variables, functions,
                                                   variables.keys())
     parsed_variables = parser.parse_variables_mapping(prepared_variables)
     self.assertEqual(parsed_variables["varA"], parsed_variables["varB"])
Example #5
0
 def test_parse_variables_mapping_2(self):
     variables = {
         "host2": "https://apiautotest.org",
         "num3": "${sum_two($num2, 4)}",
         "num2": "${sum_two($num1, 3)}",
         "num1": "${sum_two(1, 2)}"
     }
     functions = {"sum_two": sum_two}
     prepared_variables = parser.prepare_lazy_data(variables, functions,
                                                   variables.keys())
     parsed_testcase = parser.parse_variables_mapping(prepared_variables)
     self.assertEqual(parsed_testcase["num3"], 10)
     self.assertEqual(parsed_testcase["num2"], 6)
     self.assertEqual(parsed_testcase["num1"], 3)
Example #6
0
 def test_parse_variables_mapping(self):
     variables = {
         "varA": "123$varB",
         "varB": "456$varC",
         "varC": "${sum_two($a, $b)}",
         "a": 1,
         "b": 2
     }
     functions = {"sum_two": sum_two}
     prepared_variables = parser.prepare_lazy_data(variables, functions,
                                                   variables.keys())
     parsed_variables = parser.parse_variables_mapping(prepared_variables)
     self.assertEqual(parsed_variables["varA"], "1234563")
     self.assertEqual(parsed_variables["varB"], "4563")
     self.assertEqual(parsed_variables["varC"], 3)
Example #7
0
 def test_parse_variables_mapping_dollar_notation(self):
     variables = {
         "varA": "123$varB",
         "varB": "456$$0",
         "varC": "${sum_two($a, $b)}",
         "a": 1,
         "b": 2,
         "c": "abc"
     }
     functions = {"sum_two": sum_two}
     prepared_variables = parser.prepare_lazy_data(variables, functions,
                                                   variables.keys())
     parsed_testcase = parser.parse_variables_mapping(prepared_variables)
     self.assertEqual(parsed_testcase["varA"], "123456$0")
     self.assertEqual(parsed_testcase["varB"], "456$0")
     self.assertEqual(parsed_testcase["varC"], 3)
Example #8
0
 def test_parse_tests_variable_with_function(self):
     tests_mapping = {
         "project_mapping": {
             "functions": {
                 "sum_two": sum_two,
                 "gen_random_string": gen_random_string
             }
         },
         'testcases': [{
             "config": {
                 'name': '',
                 "base_url": "$host1",
                 'variables': {
                     "host1": "https://debugtalk.com",
                     "var_a": "${gen_random_string(5)}",
                     "var_b": "$var_a"
                 }
             },
             "teststeps": [{
                 'name': 'testcase1',
                 "base_url": "$host2",
                 "variables": {
                     "host2": "https://apiautotest.org",
                     "num3": "${sum_two($num2, 4)}",
                     "num2": "${sum_two($num1, 3)}",
                     "num1": "${sum_two(1, 2)}",
                     "str1": "${gen_random_string(5)}",
                     "str2": "$str1"
                 },
                 'request': {
                     'url': '/api1/?num1=$num1&num2=$num2&num3=$num3',
                     'method': 'GET'
                 }
             }]
         }]
     }
     parsed_testcases = parser.parse_tests(tests_mapping)
     test_dict = parsed_testcases[0]["teststeps"][0]
     variables = parser.parse_variables_mapping(test_dict["variables"])
     self.assertEqual(variables["num3"], 10)
     self.assertEqual(variables["num2"], 6)
     parsed_test_dict = parser.parse_lazy_data(test_dict, variables)
     self.assertEqual(parsed_test_dict["base_url"],
                      "https://apiautotest.org")
     self.assertEqual(parsed_test_dict["request"]["url"],
                      "/api1/?num1=3&num2=6&num3=10")
     self.assertEqual(variables["str1"], variables["str2"])
Example #9
0
    def test_prepare_lazy_data_dual_dollar(self):
        variables = {
            "num0": 123,
            "var1": "abc$$num0",
            "var2": "abc$$$num0",
            "var3": "abc$$$$num0",
        }
        functions = {"sum_two": sum_two}
        prepared_variables = parser.prepare_lazy_data(variables, functions,
                                                      variables.keys())
        self.assertEqual(prepared_variables["var1"], "abc$num0")
        self.assertIsInstance(prepared_variables["var2"], parser.LazyString)
        self.assertEqual(prepared_variables["var3"], "abc$$num0")

        parsed_variables = parser.parse_variables_mapping(prepared_variables)
        self.assertEqual(parsed_variables["var1"], "abc$num0")
        self.assertEqual(parsed_variables["var2"], "abc$123")
        self.assertEqual(parsed_variables["var3"], "abc$$num0")
Example #10
0
    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")
Example #11
0
 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')
Example #12
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)
Example #13
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.init_test_variables()
     self.validation_results = []