def test_use_validators_jmespath_fail(self): try: import jmespath except ImportError: print("Skipping jmespath import test because library absent") raise unittest.SkipTest("JMESPath module absent") """ Test validators that should fail """ test = Test() test.url = self.prefix + '/api/person/' test.validators = list() cfg_exists = {'jmespath': 'objects[500]', 'test': 'exists'} test.validators.append( validators.parse_validator('extract_test', cfg_exists)) cfg_not_exists = {'jmespath': "objects[1]", 'test': 'not_exists'} test.validators.append( validators.parse_validator('extract_test', cfg_not_exists)) cfg_compare = { 'jmespath': "objects[1].last_name", 'expected': 'NotJenkins' } test.validators.append( validators.parse_validator('compare', cfg_compare)) test_response = resttest.run_test(test) self.assertFalse(test_response.passed) self.assertTrue(test_response.failures) self.assertEqual(3, len(test_response.failures))
def test_parse_validator_nocomparator(self): """ Test that comparator validator with no comparator defaults to eq """ config = { 'jsonpath_mini': 'key.val', 'expected': 3 } validator = validators.parse_validator('assertEqual', config) self.assertEqual('eq', validator.comparator_name) self.assertEqual(validators.COMPARATORS['eq'], validator.comparator)
def validators(self, validator_list): if not isinstance(validator_list, list): raise ValidatorError( 'Misconfigured validator section, must be a list of validators' ) for validator in validator_list: if not isinstance(validator, dict): raise ValidatorError( "Validators must be defined as validatorType:{configs} ") for validator_type, validator_config in validator.items(): validator = parse_validator(validator_type, validator_config) self.__validator_list.append(validator)
def test_get_validators_fail(self): """ Test validators that should fail """ test = Test() test.url = self.prefix + '/api/person/' test.validators = list() cfg_exists = {'jsonpath_mini': "objects.500", 'test': 'exists'} test.validators.append( validators.parse_validator('extract_test', cfg_exists)) cfg_not_exists = {'jsonpath_mini': "objects.1", 'test': 'not_exists'} test.validators.append( validators.parse_validator('extract_test', cfg_not_exists)) cfg_compare = { 'jsonpath_mini': "objects.1.last_name", 'expected': 'NotJenkins' } test.validators.append( validators.parse_validator('compare', cfg_compare)) test_response = resttest.run_test(test) self.assertFalse(test_response.passed) self.assertTrue(test_response.failures) self.assertEqual(3, len(test_response.failures))
def test_get_validators(self): """ Test that validators work correctly """ test = Test() test.url = self.prefix + '/api/person/' # Validators need library calls to configure them test.validators = list() cfg_exists = {'jsonpath_mini': "objects.0", 'test': 'exists'} test.validators.append( validators.parse_validator('extract_test', cfg_exists)) cfg_exists_0 = {'jsonpath_mini': "meta.offset", 'test': 'exists'} test.validators.append( validators.parse_validator('extract_test', cfg_exists_0)) cfg_not_exists = {'jsonpath_mini': "objects.100", 'test': 'not_exists'} test.validators.append( validators.parse_validator('extract_test', cfg_not_exists)) cfg_compare_login = { 'jsonpath_mini': 'objects.0.login', 'expected': 'gbaltar' } test.validators.append( validators.parse_validator('compare', cfg_compare_login)) cfg_compare_id = { 'jsonpath_mini': 'objects.1.id', 'comparator': 'gt', 'expected': -1 } test.validators.append( validators.parse_validator('compare', cfg_compare_id)) test_response = resttest.run_test(test) for failure in test_response.failures: print("REAL FAILURE") print("Test Failure, failure type: {0}, Reason: {1}".format( failure.failure_type, failure.message)) if failure.details: print("Validator/Error details: " + str(failure.details)) self.assertFalse(test_response.failures) self.assertTrue(test_response.passed)
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)
def test_header_validators(self): test = Test() test.url = self.prefix + '/api/person/1/' config = { 'header': 'server', 'comparator': 'contains', 'expected': 'WSGI' } test.validators = list() test.validators.append(validators.parse_validator( 'comparator', config)) result = resttest.run_test(test) if result.failures: for fail in result.failures: print(fail) self.assertTrue(result.passed)