Ejemplo n.º 1
0
 def test_parse_variables_mapping_not_found(self):
     variables = {
         "varA": "123$varB",
         "varB": "456$varC",
         "varC": "${sum_two($a, $b)}",
         "b": 2
     }
     functions = {"sum_two": sum_two}
     with self.assertRaises(exceptions.VariableNotFound):
         parser.prepare_lazy_data(variables, functions, variables.keys())
Ejemplo n.º 2
0
 def test_prepare_lazy_data_not_found(self):
     variables = {
         "host": "https://apiautotest.org",
         "num4": "${sum_two($num0, 5)}",
         "num3": "${sum_two($num2, 4)}",
         "num2": "${sum_two($num1, 3)}",
         "num1": "${sum_two(1, 2)}"
     }
     functions = {"sum_two": sum_two}
     with self.assertRaises(exceptions.VariableNotFound):
         parser.prepare_lazy_data(variables, functions, variables.keys())
Ejemplo n.º 3
0
 def test_prepare_lazy_data(self):
     variables = {
         "host": "https://apiautotest.org",
         "num4": "${sum_two($num0, 5)}",
         "num3": "${sum_two($num2, 4)}",
         "num2": "${sum_two($num1, 3)}",
         "num1": "${sum_two(1, 2)}",
         "num0": 0
     }
     functions = {"sum_two": sum_two}
     parser.prepare_lazy_data(variables, functions, variables.keys())
Ejemplo n.º 4
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)
Ejemplo n.º 5
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"])
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
 def test_extract_variables(self):
     prepared_content = parser.prepare_lazy_data("123$a", {}, {"a"})
     self.assertEqual(parser.extract_variables(prepared_content), {"a"})
     prepared_content = parser.prepare_lazy_data("$a$b", {}, {"a", "b"})
     self.assertEqual(parser.extract_variables(prepared_content),
                      {"a", "b"})
     prepared_content = parser.prepare_lazy_data(["$a$b", "$c", "d"], {},
                                                 {"a", "b", "c", "d"})
     self.assertEqual(parser.extract_variables(prepared_content),
                      {"a", "b", "c"})
     prepared_content = parser.prepare_lazy_data(
         {
             "a": 1,
             "b": {
                 "c": "$d",
                 "e": 3
             }
         }, {}, {"d"})
     self.assertEqual(parser.extract_variables(prepared_content), {"d"})
     prepared_content = parser.prepare_lazy_data(
         {
             "a": ["$b"],
             "b": {
                 "c": "$d",
                 "e": 3
             }
         }, {}, {"b", "d"})
     self.assertEqual(parser.extract_variables(prepared_content),
                      {"b", "d"})
     prepared_content = parser.prepare_lazy_data(
         ["$a$b", "$c", {
             "c": "$d"
         }], {}, {"a", "b", "c", "d"})
     self.assertEqual(parser.extract_variables(prepared_content),
                      {"a", "b", "c", "d"})
Ejemplo n.º 8
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")
Ejemplo n.º 9
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)
Ejemplo n.º 10
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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
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")
Ejemplo n.º 13
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')
Ejemplo n.º 14
0
 def test_eval_content_variables(self):
     variables = {"SECRET_KEY": "DebugTalk"}
     content = parser.prepare_lazy_data("abc$SECRET_KEY", {},
                                        variables.keys())
     self.assertEqual(self.context.eval_content(content), "abcDebugTalk")