Example #1
0
    def parse(self, testcase_dict):
        testcase_dict = Parser.flatten_lowercase_keys_dict(testcase_dict)

        for keyword in TestCase.KEYWORD_DICT.keys():
            value = testcase_dict.get(keyword)
            if value is None:
                continue

            if keyword == TestCaseKeywords.auth_username:
                self.auth_username = value
            elif keyword == TestCaseKeywords.auth_password:
                self.auth_password = value
            elif keyword == TestCaseKeywords.method:
                self.http_method = value
            elif keyword == TestCaseKeywords.delay:
                self.__delay = int(value)
            elif keyword == TestCaseKeywords.group:
                self.__group = value
            elif keyword == TestCaseKeywords.name:
                self.__name = value
            elif keyword == TestCaseKeywords.url:
                self.url = value
            elif keyword == TestCaseKeywords.extract_binds:
                self.extract_binds = value
            elif keyword == TestCaseKeywords.validators:
                self.validators = value
            elif keyword == TestCaseKeywords.headers:
                self.headers = value
            elif keyword == TestCaseKeywords.variable_binds:
                self.__variable_binds_dict = Parser.flatten_dictionaries(value)
            elif keyword == TestCaseKeywords.generator_binds:
                self.__generator_binds_dict = {
                    str(k): str(v)
                    for k, v in Parser.flatten_dictionaries(value)
                }
            elif keyword == TestCaseKeywords.options:
                raise NotImplementedError("Yet to Support")
            elif keyword == TestCaseKeywords.body:
                self.body = value
            elif keyword == TestCaseKeywords.absolute_urls:
                self.__abs_url = Parser.safe_to_bool(value)

        expected_status = testcase_dict.get(TestCaseKeywords.expected_status,
                                            [])
        if expected_status:
            self.expected_http_status_code_list = expected_status
        else:
            if self.http_method in ["POST", "PUT", "DELETE"]:
                self.expected_http_status_code_list = [200, 201, 204]
Example #2
0
    def parse(config):
        """ Create a validator that does an extract from body and applies a comparator,
            Then does comparison vs expected value
            Syntax sample:
              { jsonpath_mini: 'node.child',
                operator: 'eq',
                expected: 'myValue'
              }
        """
        from resttest3.utils import Parser
        output = ComparatorValidator()
        config = Parser.lowercase_keys(Parser.flatten_dictionaries(config))
        output.config = config

        output.extractor = _get_extractor(config)

        if output.extractor is None:
            raise ValueError(
                "Extract function for comparison is not valid or not found!")

        if 'comparator' not in config:  # Equals comparator if unspecified
            output.comparator_name = 'eq'
        else:
            output.comparator_name = config['comparator'].lower()
        try:
            output.comparator = COMPARATORS[output.comparator_name]
        except KeyError:
            raise ValueError("Invalid comparator given! %s  "
                             "available options are %s" % (output.comparator_name, COMPARATORS.keys()))
        if not output.comparator:
            raise ValueError("Invalid comparator given!")

        try:
            expected = config['expected']
        except KeyError:
            raise ValueError(
                "No expected value found in comparator validator config, one must be!")

        # Expected value can be another extractor query, or a single value, or
        # a templated value

        if isinstance(expected, (str, int, float, complex)):
            output.expected = expected
        elif isinstance(expected, dict):

            expected = Parser.lowercase_keys(expected)
            template = expected.get('template')
            if template:  # Templated string
                if not isinstance(template, str):
                    raise ValueError(
                        "Can't template a comparator-validator unless template value is a string")
                output.is_template_expected = True
                output.expected = template
            else:  # Extractor to compare against
                output.expected = _get_extractor(expected)
                if not output.expected:
                    raise ValueError(
                        "Can't supply a non-template, non-extract dictionary to comparator-validator")

        return output
Example #3
0
def parse_generator(configuration):
    """ Parses a configuration built from yaml and returns a generator
        Configuration should be a map
    """
    from resttest3.utils import Parser
    configuration = Parser.lowercase_keys(
        Parser.flatten_dictionaries(configuration))
    gen_type = str(configuration.get(u'type')).lower()

    if gen_type not in GENERATOR_TYPES:
        raise ValueError(
            'Generator type given {0} is not valid '.format(gen_type))

    # Do the easy parsing, delegate more complex logic to parsing functions
    if gen_type == 'env_variable':
        return factory_env_variable(configuration['variable_name'])()
    elif gen_type == 'env_string':
        return factory_env_string(configuration['string'])()
    elif gen_type == 'number_sequence':
        start = int(configuration.get('start', 1))
        increment = int(configuration.get('increment', 1))
        return factory_generate_ids(start, increment)()
    elif gen_type == 'random_int':
        return generator_random_int32()
    elif gen_type == 'random_text':
        return parse_random_text_generator(configuration)
    elif gen_type in GENERATOR_TYPES:
        return GENERATOR_PARSING[gen_type](configuration)

    raise Exception("Unknown generator type: {0}".format('gen_type'))
Example #4
0
 def headers(self, headers):
     config_value = Parser.flatten_dictionaries(headers)
     if isinstance(config_value, dict):
         for key, value in config_value.items():
             if isinstance(value, dict):
                 if value.get('template'):
                     self.set_template("headers", value.get('template'))
         self.__header_dict.update(config_value)
     else:
         raise ValidatorError("Illegal header type: headers must be a dictionary or list of dictionary keys")
Example #5
0
    def test_flatten(self):
        """ Test flattening of lists of dictionaries to single dictionaries """

        # Test happy path: list of single-item dictionaries in
        array = [{"url": "/cheese"}, {"method": "POST"}]
        expected = {"url": "/cheese", "method": "POST"}
        output = Parser.flatten_dictionaries(array)
        self.assertTrue(isinstance(output, dict))
        # Test that expected output matches actual
        self.assertFalse(len(set(output.items()) ^ set(expected.items())))

        # Test dictionary input
        array = {"url": "/cheese", "method": "POST"}
        expected = {"url": "/cheese", "method": "POST"}
        output = Parser.flatten_dictionaries(array)
        self.assertTrue(isinstance(output, dict))
        # Test that expected output matches actual
        self.assertTrue(len(set(output.items()) ^ set(expected.items())) == 0)

        # Test empty list input
        array = []
        expected = {}
        output = Parser.flatten_dictionaries(array)
        self.assertTrue(isinstance(output, dict))
        # Test that expected output matches actual
        self.assertFalse(len(set(output.items()) ^ set(expected.items())))

        # Test empty dictionary input
        array = {}
        expected = {}
        output = Parser.flatten_dictionaries(array)
        self.assertTrue(isinstance(output, dict))
        # Test that expected output matches actual
        self.assertFalse(len(set(output.items()) ^ set(expected.items())))

        # Test mixed-size input dictionaries
        array = [{"url": "/cheese"}, {"method": "POST", "foo": "bar"}]
        expected = {"url": "/cheese", "method": "POST", "foo": "bar"}
        output = Parser.flatten_dictionaries(array)
        self.assertTrue(isinstance(output, dict))
        # Test that expected output matches actual
        self.assertFalse(len(set(output.items()) ^ set(expected.items())))
Example #6
0
    def extract_binds(self, bind_dict):

        bind_dict = Parser.flatten_dictionaries(bind_dict)

        for variable_name, extractor in bind_dict.items():

            if not isinstance(extractor, dict) or len(extractor) == 0:
                raise BindError("Extractors must be defined as maps of extractorType:{configs} with 1 entry")
            if len(extractor) > 1:
                raise BindError("Cannot define multiple extractors for given variable name")
            for extractor_type, extractor_config in extractor.items():
                self.__extract_binds_dict[variable_name] = parse_extractor(extractor_type, extractor_config)
Example #7
0
    def parse(config):
        from resttest3.utils import Parser
        output = ExtractTestValidator()
        config = Parser.lowercase_keys(Parser.flatten_dictionaries(config))
        output.config = config
        extractor = _get_extractor(config)
        output.extractor = extractor

        test_name = config['test']
        output.test_name = test_name
        test_fn = VALIDATOR_TESTS[test_name]
        output.test_fn = test_fn
        return output
Example #8
0
    def test_flatten_dictionaries(self):
        input_dict = {"x": 1, "y": 2}
        result_dict = Parser.flatten_dictionaries(input_dict)
        self.assertEqual(input_dict, result_dict)
        input_dict.update({"y": {"a": 1}})
        result_dict = Parser.flatten_dictionaries(input_dict)
        self.assertEqual(input_dict, result_dict)

        result_dict = Parser.flatten_dictionaries([input_dict, input_dict, input_dict])
        self.assertEqual(input_dict, result_dict)
        result_dict = Parser.flatten_dictionaries([input_dict])
        self.assertEqual(input_dict, result_dict)
        result_dict = Parser.flatten_dictionaries([{'x': 1}, input_dict])
        self.assertEqual(input_dict, result_dict)
        result_dict = Parser.flatten_dictionaries([input_dict, {'x': 2}])
        self.assertNotEqual(input_dict, result_dict)
        result_dict = Parser.flatten_dictionaries([{'x': 2}, input_dict])
        self.assertEqual(input_dict, result_dict)
Example #9
0
    def parse(self, config_node):
        node = Parser.flatten_lowercase_keys_dict(config_node)

        for key, value in node.items():
            if key == 'timeout':
                self.timeout = int(value)
            elif key == u'print_bodies':
                self.print_bodies = Parser.safe_to_bool(value)
            elif key == 'retries':
                self.retries = int(value)
            elif key == 'variable_binds':
                self.variable_binds = value
            elif key == u'generators':
                if not isinstance(value, list):
                    raise TypeError("generators in config should defined as list(array).")
                flat = Parser.flatten_dictionaries(value)
                gen_dict = {}
                for generator_name, generator_config in flat.items():
                    gen = parse_generator(generator_config)
                    gen_dict[str(generator_name)] = gen
                self.generators = gen_dict
Example #10
0
 def variable_binds(self, variable_dict):
     """Variable binding """
     if isinstance(variable_dict, dict):
         self.__variable_binds_dict.update(Parser.flatten_dictionaries(variable_dict))
Example #11
0
 def generator_binds(self, value: Dict):
     binds_dict = Parser.flatten_dictionaries(value)
     __binds_dict = {str(k): str(v) for k, v in binds_dict.items()}
     self.__generator_binds_dict.update(__binds_dict)