def test_body_assertion_var_failed(self): # Given response_body = 'Unexpected value' expected = 'Expected var value' config = Config.from_dict( { 'host': 'host', 'vars': { 'some_var': expected } }, __file__, ) spec = { 'type': 'body', 'value': { 'type': 'ref', 'var': 'some_var' }, } assertion = BodyAssertion.from_dict(spec) case = MockTestCase() response = Mock() response.text = response_body # When assertion.run(config, 'url', case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (response_body, expected)) self.assertIn('msg', kwargs)
def test_sha256_assertion_failure(self): # Given url = 'http://localhost' expected = hashlib.sha256('data'.encode('ascii')).hexdigest() body = 'other-data'.encode('ascii') actual = hashlib.sha256(body).hexdigest() responses.add( responses.GET, url, body=body, status=200, ) response = requests.get(url) config = Config.from_dict({'host': 'host'}, __file__) spec = { 'type': 'sha256', 'expected': expected, } assertion = Sha256BodyAssertion.from_dict(spec) case = MockTestCase() # When assertion.run(config, url, case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (actual, expected)) self.assertIn('msg', kwargs)
def test_body_from_var(self): # Given config = Config.from_dict( { 'host': 'name.domain', 'vars': { 'data': 'some-data', }, }, __file__) spec = { 'body': { 'format': 'plain', 'value': { 'type': 'ref', 'var': 'data' }, }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'headers': { 'Content-Type': 'text/plain' }, 'data': 'some-data', } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_body_from_var(self): # Given config = Config.from_dict( { 'host': 'name.domain', 'vars': { 'data': 'some-data', }, }, __file__) spec = { 'body': { 'format': 'plain', 'value': {'type': 'ref', 'var': 'data'}, }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'headers': {'Content-Type': 'text/plain'}, 'data': 'some-data', } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_load_variable(self): # Given config = Config.from_dict( { 'host': 'name.domain', 'vars': { 'some_var': 'application/json', }, }, __file__) spec = { 'headers': { 'Content-Type': { 'type': 'ref', 'var': 'some_var', }, }, } parameter = HeadersTestParameter.from_dict(spec) expected = {'headers': {'Content-Type': 'application/json'}} # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_no_expected_value(self): # Given config = Config.from_dict({'host': 'host'}, __file__) response = Mock() response.headers = {'Content-Type': 'application/json'} spec = { 'name': 'header', 'header': 'Content-Type', } # When assertion = HeaderAssertion.from_dict(spec) # Then self.assertEqual(assertion.header, spec['header']) self.assertEqual(assertion.expected_value, None) self.assertEqual(assertion.regexp, False) # When case = MockTestCase() assertion.run(config, 'http://host/uri', case, response) # Then self.assertEqual(case.assertIn.call_count, 1) call = case.assertIn.call_args args, kwargs = call self.assertEqual(args, (spec['header'], response.headers)) self.assertIn('msg', kwargs) self.assertFalse(case.assertRegexpMatches.called) self.assertFalse(case.assertEqual.called)
def test_body_yaml(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = { 'body': { 'format': 'yaml', 'lookup-var': False, 'value': {'param': ['value1', 'value2']}, }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'headers': {'Content-Type': 'application/yaml'}, 'data': textwrap.dedent( """\ param: - value1 - value2 """ ), } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_create_multipart_file(self): # Given config = Config.from_dict({'host': 'name.domain'}, self.test_filename) multipart_body = { 'field1': { 'filename': self.absolute_filename, }, 'field2': { 'filename': self.relative_filename, }, } spec = { 'body': { 'format': 'multipart', 'value': multipart_body, }, } loader = BodyTestParameter.from_dict(spec) # When with loader.load(config) as loaded: # Then self.assertIn('files', loaded) self.assertNotIn('headers', loaded) data = loaded['files'] self.assertIn('field1', data) self.assertIn('field2', data) fh1 = data['field1'] self.assertEqual(fh1.name, self.absolute_filename) fh2 = data['field2'] self.assertEqual(fh2.name, self.absolute_filename)
def test_load(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) expected = { 'params': { 'str': 'string value', 'int': 5, 'float': 5.2, 'bool': False, }, } spec = { 'queryparams': { 'str': 'string value', 'int': 5, 'float': 5.2, 'bool': False, }, } loader = QueryParamsTestParameter.from_dict(spec) # When with loader.load(config) as loaded: self.assertEqual(loaded, expected)
def test_no_expected_value(self): # Given config = Config.from_dict({'host': 'host'}, __file__) response = Mock() response.headers = { 'Content-Type': 'application/json' } spec = { 'name': 'header', 'header': 'Content-Type', } # When assertion = HeaderAssertion.from_dict(spec) # Then self.assertEqual(assertion.header, spec['header']) self.assertEqual(assertion.expected_value, None) self.assertEqual(assertion.regexp, False) # When case = MockTestCase() assertion.run(config, 'http://host/uri', case, response) # Then self.assertEqual(case.assertIn.call_count, 1) call = case.assertIn.call_args args, kwargs = call self.assertEqual(args, (spec['header'], response.headers)) self.assertIn('msg', kwargs) self.assertFalse(case.assertRegexpMatches.called) self.assertFalse(case.assertEqual.called)
def test_body_yaml(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = { 'body': { 'format': 'yaml', 'lookup-var': False, 'value': { 'param': ['value1', 'value2'] }, }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'headers': { 'Content-Type': 'application/yaml' }, 'data': textwrap.dedent("""\ param: - value1 - value2 """), } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_body_assertion_var_failed(self): # Given response_body = 'Unexpected value' expected = 'Expected var value' config = Config.from_dict( {'host': 'host', 'vars': {'some_var': expected}}, __file__, ) spec = { 'type': 'body', 'value': {'type': 'ref', 'var': 'some_var'}, } assertion = BodyAssertion.from_dict(spec) case = MockTestCase() response = Mock() response.text = response_body # When assertion.run(config, 'url', case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (response_body, expected)) self.assertIn('msg', kwargs)
def test_load(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = {'headers': {'Content-Type': 'application/json'}} parameter = HeadersTestParameter.from_dict(spec) # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, spec)
def test_load(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = {'method': 'POST'} parameter = MethodTestParameter.from_dict(spec) # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, spec)
def test_body_assertion_jq_filter(self): # Given config = Config.from_dict({'host': 'host'}, __file__) value = { 'object1': { 'dynamic': 1, 'static': 'Value1', }, 'object2': { 'dynamic': 2, 'static': 'Value2', }, } expected = value.copy() for key, obj in value.items(): # Deliberately not deterministic obj['dynamic'] = obj['dynamic'] + time.time() spec = { 'type': 'body', 'format': 'json', 'value': expected, 'lookup-var': False, # Filter to remove the non-deterministic value from # response and assertion value 'filter': 'with_entries(del(.value.dynamic))', } assertion = BodyAssertion.from_dict(spec) case = MockTestCase() response = Mock() response.json.return_value = value expected_call = { 'object1': { 'static': 'Value1', }, 'object2': { 'static': 'Value2', }, } # When assertion.run(config, 'url', case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (expected_call, expected_call)) self.assertIn('msg', kwargs)
def test_load_missing_variable(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = { 'headers': { 'Content-Type': { 'type': 'ref', 'var': 'none', }, }, } parameter = HeadersTestParameter.from_dict(spec) # When/Then with self.assertRaises(InvalidVariable): with parameter.load(config): pass
def test_valid_assertion(self): # Given config = Config.from_dict({'host': 'host'}, __file__) response = Mock() response.status_code = 200 case = MockTestCase() assertion = StatusCodeAssertion(200) # When assertion.run(config, 'http://host/uri', case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (200, 200)) self.assertIn('msg', kwargs)
def test_assert_expected_regexp_failure(self): # Given config = Config.from_dict({'host': 'host'}, __file__) content_type = 'text/github.v3+json' expected_content_type = 'application/.*?json' response = Mock() response.headers = { 'Content-Type': content_type, } spec = { 'name': 'header', 'header': 'Content-Type', 'regexp': expected_content_type, } # When assertion = HeaderAssertion.from_dict(spec) # Then self.assertEqual(assertion.header, spec['header']) self.assertEqual( assertion.expected_value, re.compile(expected_content_type)) self.assertEqual(assertion.regexp, True) # When case = MockTestCase() case.assertRegexpMatches.side_effect = AssertionError with self.assertRaises(AssertionError): assertion.run(config, 'http://host/uri', case, response) # Then self.assertEqual(case.assertIn.call_count, 1) call = case.assertIn.call_args args, kwargs = call self.assertEqual(args, (spec['header'], response.headers)) self.assertIn('msg', kwargs) self.assertEqual(case.assertRegexpMatches.call_count, 1) call = case.assertRegexpMatches.call_args args, kwargs = call self.assertEqual(args, (content_type, assertion.expected_value)) self.assertIn('msg', kwargs) self.assertFalse(case.assertEqual.called)
def test_body_format_none(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = { 'body': { 'format': 'none', 'lookup-var': False, 'value': 'plaintext', }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'data': 'plaintext', } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_assert_expected_regexp_failure(self): # Given config = Config.from_dict({'host': 'host'}, __file__) content_type = 'text/github.v3+json' expected_content_type = 'application/.*?json' response = Mock() response.headers = { 'Content-Type': content_type, } spec = { 'name': 'header', 'header': 'Content-Type', 'regexp': expected_content_type, } # When assertion = HeaderAssertion.from_dict(spec) # Then self.assertEqual(assertion.header, spec['header']) self.assertEqual(assertion.expected_value, re.compile(expected_content_type)) self.assertEqual(assertion.regexp, True) # When case = MockTestCase() case.assertRegexpMatches.side_effect = AssertionError with self.assertRaises(AssertionError): assertion.run(config, 'http://host/uri', case, response) # Then self.assertEqual(case.assertIn.call_count, 1) call = case.assertIn.call_args args, kwargs = call self.assertEqual(args, (spec['header'], response.headers)) self.assertIn('msg', kwargs) self.assertEqual(case.assertRegexpMatches.call_count, 1) call = case.assertRegexpMatches.call_args args, kwargs = call self.assertEqual(args, (content_type, assertion.expected_value)) self.assertIn('msg', kwargs) self.assertFalse(case.assertEqual.called)
def test_create_multipart_form_data(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) expected1 = 'some text' expected2 = '{}' multipart_body = { 'field1': { 'Content-Type': 'text/plain', 'value': expected1, }, 'field2': { 'Content-Type': 'application/json', 'value': expected2, }, } spec = { 'body': { 'format': 'multipart', 'value': multipart_body, }, } loader = BodyTestParameter.from_dict(spec) # When with loader.load(config) as loaded: # Then self.assertIn('files', loaded) self.assertNotIn('headers', loaded) data = loaded['files'] self.assertIn('field1', data) self.assertIn('field2', data) name, content, type_ = data['field1'] self.assertEqual(name, '') self.assertEqual(content.read().decode('utf-8'), expected1) self.assertEqual(type_, 'text/plain; charset=UTF-8') name, content, type_ = data['field2'] self.assertEqual(name, '') self.assertEqual(content.read().decode('utf-8'), expected2) self.assertEqual(type_, 'application/json; charset=UTF-8')
def test_body_json(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = { 'body': { 'format': 'json', 'lookup-var': False, 'value': {'param': ['value1', 'value2']}, }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'headers': {'Content-Type': 'application/json'}, 'data': '{"param": ["value1", "value2"]}', } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)
def test_body_assertion_plain(self): # Given config = Config.from_dict({'host': 'host'}, __file__) expected = 'I am a plaintext response' spec = { 'type': 'body', 'value': expected, } assertion = BodyAssertion.from_dict(spec) case = MockTestCase() response = Mock() response.text = expected # When assertion.run(config, 'url', case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (spec['value'], assertion.value)) self.assertIn('msg', kwargs)
def test_body_assertion_json(self): # Given config = Config.from_dict({'host': 'host'}, __file__) expected = {'expected': 'value'} spec = { 'type': 'body', 'format': 'json', 'value': expected, 'lookup-var': False, } assertion = BodyAssertion.from_dict(spec) case = MockTestCase() response = Mock() response.json.return_value = expected # When assertion.run(config, 'url', case, response) # Then self.assertEqual(case.assertEqual.call_count, 1) call = case.assertEqual.call_args args, kwargs = call self.assertEqual(args, (spec['value'], assertion.value)) self.assertIn('msg', kwargs)
def test_body_json(self): # Given config = Config.from_dict({'host': 'name.domain'}, __file__) spec = { 'body': { 'format': 'json', 'lookup-var': False, 'value': { 'param': ['value1', 'value2'] }, }, } parameter = BodyTestParameter.from_dict(spec) expected = { 'headers': { 'Content-Type': 'application/json' }, 'data': '{"param": ["value1", "value2"]}', } # When with parameter.load(config) as loaded: # Then self.assertEqual(loaded, expected)