Esempio n. 1
0
    def test_abstract_extractor_readableconfig(self):
        """ Test human-readable extractor config string output """
        config = 'key.val'
        extractor = validators.parse_extractor('jsonpath_mini', config)
        expected_string = 'Extractor Type: jsonpath_mini,  Query: "key.val", Templated?: False'
        self.assertEqual(expected_string, extractor.get_readable_config())

        # Check empty context & args uses okay
        context = Context()
        self.assertEqual(expected_string, extractor.get_readable_config(context=context))
        context.bind_variable('foo', 'bar')
        self.assertEqual(expected_string, extractor.get_readable_config(context=context))
        extractor.args = dict()
        self.assertEqual(expected_string, extractor.get_readable_config(context=context))

        # Check args output is handled correctly
        extractor.args = {'caseSensitive': True}
        self.assertEqual(expected_string+", Args: "+str(extractor.args), extractor.get_readable_config(context=context))

        # Check template handling is okay
        config = {'template': 'key.$templated'}
        context.bind_variable('templated', 'val')
        extractor = validators.parse_extractor('jsonpath_mini', config)
        expected_string = 'Extractor Type: jsonpath_mini,  Query: "key.val", Templated?: True'
        self.assertEqual(expected_string, extractor.get_readable_config(context=context))
    def test_content_file_template(self):
        """ Test file read and templating of read files in this directory """
        variables = {'id':1, 'login':'******'}
        context = Context()

        file_path = os.path.dirname(os.path.realpath(__file__))
        file_path = os.path.join(file_path, 'person_body_template.json')

        file_content = None
        with open(file_path, 'r') as f:
            file_content = f.read()

        # Test basic read
        handler = ContentHandler()
        handler.setup(file_path, is_file=True)
        self.assertEqual(file_content, handler.get_content())

        # Test templating of read content
        handler.setup(file_path, is_file=True, is_template_content=True)
        self.assertEqual(file_content, handler.get_content())
        self.assertEqual(file_content, handler.get_content(context))  # No substitution
        substituted = string.Template(file_content).safe_substitute(variables)
        context.bind_variables(variables)
        self.assertEqual(substituted, handler.get_content(context))

        # Test path templating
        templated_file_path = '$filepath'
        context.bind_variable('filepath', file_path)
        handler.setup(file_path, is_file=True, is_template_path=True)
        self.assertEqual(file_content, handler.get_content(context))

        # Test double templating with files
        handler.setup(file_path, is_file=True, is_template_path=True, is_template_content=True)
        self.assertEqual(substituted, handler.get_content(context=context))
Esempio n. 3
0
    def test_parse_validator_comparator(self):
        """ Test parsing a comparator validator """
        test_config = {
            'name':
            'Default',
            'url':
            '/api',
            'validators': [{
                'comparator': {
                    'jsonpath_mini': 'id',
                    'comparator': 'eq',
                    'expected': {
                        'template': '$id'
                    }
                }
            }]
        }
        test = Test.parse_test('', test_config)
        self.assertTrue(test.validators)
        self.assertEqual(1, len(test.validators))

        context = Context()
        context.bind_variable('id', 3)

        myjson = '{"id": "3"}'
        failure = test.validators[0].validate(myjson, context=context)
        self.assertTrue(test.validators[0].validate(myjson, context=context))
        self.assertFalse(test.validators[0].validate(myjson))
Esempio n. 4
0
 def test_update_context_variables(self):
     test = Test()
     context = Context()
     context.bind_variable('foo', 'broken')
     test.variable_binds = {'foo': 'correct', 'test': 'value'}
     test.update_context_before(context)
     self.assertEqual('correct', context.get_value('foo'))
     self.assertEqual('value', context.get_value('test'))
Esempio n. 5
0
 def test_update_context_variables(self):
     test = Test()
     context = Context()
     context.bind_variable('foo', 'broken')
     test.variable_binds = {'foo': 'correct', 'test': 'value'}
     test.update_context_before(context)
     self.assertEqual('correct', context.get_value('foo'))
     self.assertEqual('value', context.get_value('test'))
Esempio n. 6
0
 def test_mixing_binds(self):
     """ Ensure that variables are set correctly when mixing explicit declaration and variables """
     context = Context()
     context.add_generator('gen', count_gen())
     context.bind_variable('foo', '100')
     self.assertEqual(1, context.mod_count)
     context.bind_generator_next('foo', 'gen')
     self.assertEqual(1, context.get_value('foo'))
     self.assertEqual(2, context.mod_count)
Esempio n. 7
0
 def test_mixing_binds(self):
     """ Ensure that variables are set correctly when mixing explicit declaration and variables """
     context = Context()
     context.add_generator('gen', count_gen())
     context.bind_variable('foo', '100')
     self.assertEqual(1, context.mod_count)
     context.bind_generator_next('foo', 'gen')
     self.assertEqual(1, context.get_value('foo'))
     self.assertEqual(2, context.mod_count)
 def test_parse_content_templated(self):
     """ Test parsing of templated content """
     node = {'template':'myval $var'}
     handler = ContentHandler.parse_content(node)
     context = Context()
     context.bind_variable('var','cheese')
     self.assertEqual(node['template'], handler.content)
     self.assertEqual('myval cheese', handler.get_content(context))
     self.assertTrue(handler.is_dynamic())
     self.assertFalse(handler.is_file)
     self.assertFalse(handler.is_template_path)
     self.assertTrue(handler.is_template_content)
Esempio n. 9
0
    def test_abstract_extractor_templating(self):
        """ Test that abstract extractors template the query """
        ext = validators.AbstractExtractor()
        ext.query = '$val.vee'
        ext.is_templated = True
        context = Context()
        context.bind_variable('val', 'foo')
        self.assertEqual('$val.vee', ext.templated_query())
        self.assertEqual('foo.vee', ext.templated_query(context=context))

        ext.is_templated = False
        self.assertEqual('$val.vee', ext.templated_query(context=context))
Esempio n. 10
0
 def test_parse_content_templated(self):
     """ Test parsing of templated content """
     node = {'template': 'myval $var'}
     handler = ContentHandler.parse_content(node)
     context = Context()
     context.bind_variable('var', 'cheese')
     self.assertEqual(node['template'], handler.content)
     self.assertEqual('myval cheese', handler.get_content(context))
     self.assertTrue(handler.is_dynamic())
     self.assertFalse(handler.is_file)
     self.assertFalse(handler.is_template_path)
     self.assertTrue(handler.is_template_content)
Esempio n. 11
0
    def test_update_context_generators(self):
        """ Test updating context variables using generator """
        test = Test()
        context = Context()
        context.bind_variable('foo', 'broken')
        test.variable_binds = {'foo': 'initial_value'}
        test.generator_binds = {'foo': 'gen'}
        context.add_generator('gen', generators.generator_basic_ids())

        test.update_context_before(context)
        self.assertEqual(1, context.get_value('foo'))
        test.update_context_before(context)
        self.assertEqual(2, context.get_value('foo'))
Esempio n. 12
0
    def test_test_url_templating(self):
        test = Test()
        test.set_url('$cheese', isTemplate=True)
        self.assertTrue(test.is_dynamic())
        self.assertEqual('$cheese', test.get_url())
        self.assertTrue(test.templates['url'])

        context = Context()
        context.bind_variable('cheese', 'stilton')
        self.assertEqual('stilton', test.get_url(context=context))

        realized = test.realize(context)
        self.assertEqual('stilton', realized.url)
Esempio n. 13
0
    def test_update_context_generators(self):
        """ Test updating context variables using generator """
        test = Test()
        context = Context()
        context.bind_variable('foo', 'broken')
        test.variable_binds = {'foo': 'initial_value'}
        test.generator_binds = {'foo': 'gen'}
        context.add_generator('gen', generators.generator_basic_ids())

        test.update_context_before(context)
        self.assertEqual(1, context.get_value('foo'))
        test.update_context_before(context)
        self.assertEqual(2, context.get_value('foo'))
Esempio n. 14
0
    def test_test_url_templating(self):
        test = Test()
        test.set_url('$cheese', isTemplate=True)
        self.assertTrue(test.is_dynamic())
        self.assertEqual('$cheese', test.get_url())
        self.assertTrue(test.templates['url'])

        context = Context()
        context.bind_variable('cheese', 'stilton')
        self.assertEqual('stilton', test.get_url(context=context))

        realized = test.realize(context)
        self.assertEqual('stilton', realized.url)
Esempio n. 15
0
    def test_parse_validator(self):
        """ Test basic parsing using registry """
        config = {"jsonpath_mini": "key.val", "comparator": "eq", "expected": 3}
        validator = validators.parse_validator("comparator", config)
        myjson = '{"key": {"val": 3}}'
        comp = validator.validate(body=myjson)

        # Try it with templating
        config["jsonpath_mini"] = {"template": "key.$node"}
        validator = validators.parse_validator("comparator", config)
        context = Context()
        context.bind_variable("node", "val")
        comp = validator.validate(myjson, context=context)
Esempio n. 16
0
    def test_variables(self):
        """ Test bind/return of variables """

        context = Context()
        self.assertTrue(context.get_value('foo') is None)
        self.assertEqual(0, context.mod_count)

        context.bind_variable('foo','bar')
        self.assertEqual('bar', context.get_value('foo'))
        self.assertEqual('bar', context.get_values()['foo'])
        self.assertEqual(1, context.mod_count)

        context.bind_variable('foo','bar2')
        self.assertEqual('bar2', context.get_value('foo'))
        self.assertEqual(2, context.mod_count)
Esempio n. 17
0
    def test_variables(self):
        """ Test bind/return of variables """

        context = Context()
        self.assertTrue(context.get_value('foo') is None)
        self.assertEqual(0, context.mod_count)

        context.bind_variable('foo', 'bar')
        self.assertEqual('bar', context.get_value('foo'))
        self.assertEqual('bar', context.get_values()['foo'])
        self.assertEqual(1, context.mod_count)

        context.bind_variable('foo', 'bar2')
        self.assertEqual('bar2', context.get_value('foo'))
        self.assertEqual(2, context.mod_count)
Esempio n. 18
0
    def test_content_templating(self):
        """ Test content and templating of it """
        handler = ContentHandler()
        body = '$variable value'
        context = Context()
        context.bind_variable('variable', 'bar')

        # No templating
        handler.setup(body, is_template_content=False)
        self.assertEqual(body, handler.get_content())
        self.assertEqual(body, handler.get_content(context))

        # Templating
        handler.setup(body, is_template_content=True)
        self.assertEqual(body, handler.get_content())
Esempio n. 19
0
    def test_content_templating(self):
        """ Test content and templating of it """
        handler = ContentHandler()
        body = '$variable value'
        context = Context()
        context.bind_variable('variable', 'bar')

        # No templating
        handler.setup(body, is_template_content=False)
        self.assertEqual(body, handler.get_content())
        self.assertEqual(body, handler.get_content(context))

        # Templating
        handler.setup(body, is_template_content=True)
        self.assertEqual(body, handler.get_content())
Esempio n. 20
0
    def test_parse_validator(self):
        """ Test basic parsing using registry """
        config = {
            'jsonpath_mini': 'key.val',
            'comparator': 'eq',
            'expected': 3
        }
        validator = validators.parse_validator('comparator', config)
        myjson = '{"key": {"val": 3}}'
        comp = validator.validate(body=myjson)

        # Try it with templating
        config['jsonpath_mini']={'template':'key.$node'}
        validator = validators.parse_validator('comparator', config)
        context = Context()
        context.bind_variable('node','val')
        comp = validator.validate(myjson, context=context)
Esempio n. 21
0
    def test_validator_comparator_templating(self):
        """ Try templating comparator validator """
        config = {"jsonpath_mini": {"template": "key.$node"}, "comparator": "eq", "expected": 3}
        context = Context()
        context.bind_variable("node", "val")
        myjson_pass = '******'
        myjson_fail = '{"id": 3, "key": {"val": 4}}'
        comp = validators.ComparatorValidator.parse(config)

        self.assertTrue(comp.validate(body=myjson_pass, context=context))
        self.assertFalse(comp.validate(body=myjson_fail, context=context))

        # Template expected
        config["expected"] = {"template": "$id"}
        context.bind_variable("id", 3)
        self.assertTrue(comp.validate(body=myjson_pass, context=context))
        self.assertFalse(comp.validate(body=myjson_fail, context=context))
Esempio n. 22
0
    def test_parse_extractor_minijson(self):
        config = 'key.val'
        extractor = validators.MiniJsonExtractor.parse(config)
        myjson = '{"key": {"val": 3}}'
        context = Context()
        context.bind_variable('node', 'val')

        extracted = extractor.extract(body=myjson)
        self.assertEqual(3, extracted)
        self.assertEqual(extracted, extractor.extract(body=myjson, context=context))

        try:
            val = extractor.extract(body='[31{]')
            self.fail("Should throw exception on invalid JSON")
        except ValueError:
            pass

        # Templating
        config = {'template': 'key.$node'}
        extract = validators.MiniJsonExtractor.parse(config)
        self.assertEqual(3, extract.extract(myjson, context=context))
Esempio n. 23
0
    def test_validator_comparator_templating(self):
        """ Try templating comparator validator """
        config = {
            'jsonpath_mini': {'template': 'key.$node'},
            'comparator': 'eq',
            'expected': 3
        }
        context = Context()
        context.bind_variable('node', 'val')
        myjson_pass = '******'
        myjson_fail = '{"id": 3, "key": {"val": 4}}'
        comp = validators.ComparatorValidator.parse(config)

        self.assertTrue(comp.validate(body=myjson_pass, context=context))
        self.assertFalse(comp.validate(body=myjson_fail, context=context))

        # Template expected
        config['expected'] = {'template' : '$id'}
        context.bind_variable('id', 3)
        self.assertTrue(comp.validate(body=myjson_pass, context=context))
        self.assertFalse(comp.validate(body=myjson_fail, context=context))
Esempio n. 24
0
    def test_parse_validator_comparator(self):
        """ Test parsing a comparator validator """
        test_config = {
            'name': 'Default',
            'url': '/api',
            'validators': [
                {'comparator': {'jsonpath_mini': 'id',
                                'comparator': 'eq',
                                'expected': {'template': '$id'}}}
            ]
        }
        test = Test.parse_test('', test_config)
        self.assertTrue(test.validators)
        self.assertEqual(1, len(test.validators))

        context = Context()
        context.bind_variable('id', 3)

        myjson = '{"id": "3"}'
        failure = test.validators[0].validate(myjson, context=context)
        self.assertTrue(test.validators[0].validate(myjson, context=context))
        self.assertFalse(test.validators[0].validate(myjson))
Esempio n. 25
0
    def test_content_file_template(self):
        """ Test file read and templating of read files in this directory """
        variables = {'id': 1, 'login': '******'}
        context = Context()

        file_path = os.path.dirname(os.path.realpath(__file__))
        file_path = os.path.join(file_path, 'person_body_template.json')

        file_content = None
        with open(file_path, 'r') as f:
            file_content = f.read()

        # Test basic read
        handler = ContentHandler()
        handler.setup(file_path, is_file=True)
        self.assertEqual(file_content, handler.get_content())

        # Test templating of read content
        handler.setup(file_path, is_file=True, is_template_content=True)
        self.assertEqual(file_content, handler.get_content())
        self.assertEqual(file_content,
                         handler.get_content(context))  # No substitution
        substituted = string.Template(file_content).safe_substitute(variables)
        context.bind_variables(variables)
        self.assertEqual(substituted, handler.get_content(context))

        # Test path templating
        templated_file_path = '$filepath'
        context.bind_variable('filepath', file_path)
        handler.setup(file_path, is_file=True, is_template_path=True)
        self.assertEqual(file_content, handler.get_content(context))

        # Test double templating with files
        handler.setup(file_path,
                      is_file=True,
                      is_template_path=True,
                      is_template_content=True)
        self.assertEqual(substituted, handler.get_content(context=context))