def test_nested(self): (pact .given('a list of users exists') .upon_receiving('a query of all users') .with_request('get', '/users/', query={'limit': Term('\d+', '5')}) .will_respond_with(200, body={'results': EachLike({ 'username': Term('\w+', 'bob'), 'id': SomethingLike(123), 'groups': EachLike(123), 'meta': SomethingLike({ 'name': Term('.+', 'sample'), 'value': Term('.+', 'data') }) }, minimum=2)})) with pact: results = requests.get( 'http://localhost:1234/users/?limit=4') self.assertEqual(results.json(), { 'results': [ {'username': '******', 'id': 123, 'groups': [123], 'meta': {'name': 'sample', 'value': 'data'}}, {'username': '******', 'id': 123, 'groups': [123], 'meta': {'name': 'sample', 'value': 'data'}}]})
def test_sparse(self): (pact .given('the user `bob` exists') .upon_receiving('a request for the user object of `bob`') .with_request('get', Term('/users/[a-z]+', '/users/bob')) .will_respond_with(200, body={ 'username': SomethingLike('bob'), 'id': Term('\d+', '123')})) with pact: result = requests.get('http://localhost:1234/users/bob') self.assertEqual(result.json(), {'username': '******', 'id': '123'})
def test_get_user_non_admin(pact, client): expected = { 'name': 'UserA', 'id': Term(r'[a-f0-9]+', '1234567'), 'created_on': Term(r'\d+-\d+-\d+T\d+:\d+:\d+', '2016-12-15T20:16:01'), 'admin': False } (pact.given('UserA exists and is not an administrator').upon_receiving( 'a request for UserA').with_request( 'get', '/users/UserA').will_respond_with(200, body=Like(expected))) with pact: result = client.get_user('UserA')
def test_get_user_non_admin(pact, consumer): # Define the Matcher; the expected structure and content of the response expected = { "name": "UserA", "id": Format().uuid, "created_on": Term(r"\d+-\d+-\d+T\d+:\d+:\d+", "2016-12-15T20:16:01"), "ip_address": Format().ip_address, "admin": False, } # Define the expected behaviour of the Provider. This determines how the # Pact mock provider will behave. In this case, we expect a body which is # "Like" the structure defined above. This means the mock provider will # return the EXACT content where defined, e.g. UserA for name, and SOME # appropriate content e.g. for ip_address. (pact.given("UserA exists and is not an administrator").upon_receiving( "a request for UserA").with_request( "get", "/users/UserA").will_respond_with(200, body=Like(expected))) with pact: # Perform the actual request user = consumer.get_user("UserA") # In this case the mock Provider will have returned a valid response assert user.name == "UserA" # Make sure that all interactions defined occurred pact.verify()
def test_get_status(pact, client): #expected = { # 'name': 'UserA', # 'id': Term( # r'^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}\Z', # '00000000-0000-4000-a000-000000000000' # ), # 'created_on': Term( # r'\d+-\d+-\d+T\d+:\d+:\d+', # '2016-12-15T20:16:01' # ), # 'admin': False #} expected = { 'buildTime': Term( r'\d+-\d+-\d+T\d+:\d+:\d+Z', '2018-09-10T11:08:26Z' ), 'commit': Term( r'^[a-f0-9]+-dirty', '164762f67a3a7634fa4ee1e8bb55c458281803c7-dirty' ), 'startTime': Term( r'\d+-\d+-\d+T\d+:\d+:\d+Z', '2018-09-10T11:08:26Z' ) } (pact .given('status exists') .upon_receiving('a request for status') .with_request('get', '/status') .will_respond_with(200, body=Like(expected))) with pact: result = client.get_status('status')
def test_get_user_non_admin(pact, consumer): expected = { 'name': 'UserA', 'id': Format().uuid, 'created_on': Term(r'\d+-\d+-\d+T\d+:\d+:\d+', '2016-12-15T20:16:01'), 'ip_address': Format().ip_address, 'admin': False } (pact.given('UserA exists and is not an administrator').upon_receiving( 'a request for UserA').with_request( 'get', '/users/UserA').will_respond_with(200, body=Like(expected))) with pact: user = consumer.get_user('UserA') assert user.name == 'UserA'
def test_get_time(self): """Verify the expected response from the time server.""" expected = { 'currentTime': Term( '\d+-\d+-\d \d+:\d+:\d+', # noqa W605 '2016-12-15 20:16:01') } (pact.given('A request is made for the current time').upon_receiving( 'a request for current time').with_request( 'get', '/').will_respond_with(200, body=expected)) with pact: result = get_time() self.assertEqual(result, expected)
def test_get_user(self): expected = { 'username': '******', 'id': 123, 'groups': ['Editors'], 'last_modified': Term(r'\d+-\d+-\d+T\d+:\d+:\d+', '2016-12-15T20:16:01'), } (pact .given('UserA exists and is not an administrator') .upon_receiving('a request for UserA') .with_request('get', '/users/UserA') .will_respond_with(200, body=expected)) with pact: result = user('UserA') expected_result = expected.copy() expected_result['last_modified'] = expected_result['last_modified']._generate self.assertEqual(result, expected_result)
class ConsumerConfig(object): authenticate = { 'path': '/api/v1/auth/authenticate/', 'request_headers': { 'Content-Type': 'application/json' }, 'request_payload': { "username": "******", "password": "******" }, # this will validate the response has the UUID format (if the default value is not met) 'response_body_matcher': { "token": Term('[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}', 'edff372c-55de-11e7-907b-a6006ad3dba0') } } def __init__(self): pass
def test_get_build(self): true = True ts = Term('\d+-\d+-\d+T\d+:\d+:\d+.+', u'2017-09-17T19:43:12+03:00') expected = { u'name': u'#3455', u'completed': true, #boolean # u'timestamp': Term('\d+-\d+-\d+T\d+:\d+:\d+.+', u'2017-09-17T19:43:12+03:00'), u'info': { u'coverage': 30, u'apiversion': 0.1, u'swaggerlink': u'http://swagger', u'buildtime': 230 } } (pact.given('build 3455 exists').upon_receiving( 'a request for build 3455').with_request( 'get', '/builds/3455').will_respond_with(200, body=expected)) with pact: result = client.build(3455) self.assertEqual(result, expected)