예제 #1
0
 def test_parse_content_template_unicode(self):
     """ Unicode parsing tests """
     node = {'template': u'myval 😽 $var'}
     handler = ContentHandler.parse_content(node)
     context = Context()
     context.bind_variable('var', 'cheese')
     self.assertEqual(u'myval 😽 cheese', handler.get_content(context))
예제 #2
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 = TestCase('', None, None)
        test.parse(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))
예제 #3
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))
예제 #4
0
 def test_update_context_variables(self):
     variable_binds = {'foo': 'correct', 'test': 'value'}
     context = Context()
     test = TestCase('', None, variable_binds, context=context)
     context.bind_variable('foo', 'broken')
     test.pre_update(context)
     self.assertEqual('correct', context.get_value('foo'))
     self.assertEqual('value', context.get_value('test'))
예제 #5
0
 def test_url_templating(self):
     context = Context()
     test = TestCase('', None, None, context)
     test.url = {'template': "$cheese"}
     self.assertTrue(test.is_dynamic())
     self.assertEqual({'template': '$cheese'}, test.url)
     self.assertTrue(test.templates['url'])
     context.bind_variable('cheese', 'liquid_cheese')
     self.assertEqual('liquid_cheese', test.url)
예제 #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)
예제 #7
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)
예제 #8
0
 def test_update_context_generators(self):
     """ Test updating context variables using generator """
     variable_binds = {'foo': 'initial_value'}
     generator_binds = {'foo': 'gen'}
     context = Context()
     test = TestCase('', None, variable_binds, context=context)
     test.generator_binds = generator_binds
     context.bind_variable('foo', 'broken')
     context.add_generator('gen', generators.generator_basic_ids())
     context.bind_generator_next('foo', 'gen')
     self.assertEqual(1, context.get_value('foo'))
     context.bind_generator_next('foo', 'gen')
     self.assertEqual(2, context.get_value('foo'))
예제 #9
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)
예제 #10
0
    def test_content_templating(self):
        """ Test content and templating of it """
        handler = ContentHandler()
        body = '$variable value'
        templated_body = 'bar 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))
        self.assertRaises(TypeError, handler.setup, [])
        # Templating
        handler.setup(body, is_template_content=True)
        self.assertEqual(body, handler.get_content())
        self.assertEqual(templated_body, handler.get_content(context))
예제 #11
0
    def test_abstract_extractor_templating(self):
        """ Test that abstract extractors template the query """

        class A(validators.AbstractExtractor):
            def extract_internal(self, query=None, body=None, headers=None, args=None):
                ...

        ext = A()
        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))
예제 #12
0
    def test_basic_ids(self):
        """ Test starting ids """
        ids1 = generators.generator_basic_ids()
        ids2 = generators.generator_basic_ids()
        self.generator_repeat_test(ids1)
        self.generator_repeat_test(ids2)
        self.assertEqual(next(ids1), next(ids2))

        c = Context()
        c.variables = {'x': '$x'}
        x = generators.factory_generate_ids(1, increment=0)()
        c.add_generator('x', x)

        self.assertEqual(c.bind_generator_next('x', 'x'), c.bind_generator_next('x', 'x'))

        c.bind_variable('x', 1)
        var1 = c.get_value('x')
        c.bind_variable('x', 1)
        var2 = c.get_value('x')
        self.assertEqual(var1, var2)
예제 #13
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))
예제 #14
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))
예제 #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)
        self.assertTrue(comp)

        validator = validators.parse_validator('comparator', config)
        my_body_str = '<html></html>'
        comp = validator.validate(body=my_body_str)
        self.assertEqual(comp.message, 'Extractor threw exception')

        validator = validators.parse_validator('comparator', config)
        myjson_1 = '{"key": {"val": 4}}'
        comp = validator.validate(body=myjson_1)
        self.assertFalse(comp)

        # Try it with templating
        # 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)
        self.assertTrue(comp)

        # Try it with templating
        del config['jsonpath_mini']
        config['jmespath'] = {'template': 'key.$node'}
        validator = validators.parse_validator('comparator', config)
        context = Context()
        context.bind_variable('node', 'val')
        comp = validator.validate(myjson, context=context)
        self.assertTrue(comp)
예제 #16
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))
예제 #17
0
    def test_unicode_templating(self):
        """ A couple combination of templating using Unicode data """
        handler = ContentHandler()
        context = Context()

        # ASCII body, unicode data
        body = '$variable value'
        context.bind_variable('variable', u'😽')
        handler.setup(body, is_template_content=True)
        self.assertEqual(body, handler.get_content())
        self.assertEqual(u'😽 value', handler.get_content(context))

        # Unicode body, ASCII data
        context.bind_variable('variable', u'string')
        body = u'$variable 😽 value'
        handler.setup(body, is_template_content=True)
        self.assertEqual(u'$variable 😽 value', handler.get_content())
        self.assertEqual(u'string 😽 value', handler.get_content(context))

        # All the Unicodes, all the times!
        context.bind_variable('variable', u'😽')
        body = u'$variable 😽 value'
        handler.setup(body, is_template_content=True)
        self.assertEqual(u'😽 😽 value', handler.get_content(context))