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_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_user_non_admin(broker, pact, consumer): expected = { 'name': 'UserA', 'id': Format().uuid, 'created_on': Format().timestamp, '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' assert user.created_on == datetime.strptime('2000-2-1T12:30:00', '%Y-%m-%dT%H:%M:%S')
def test_get_existing_location(pact, consumer): expected = { 'id': Format().uuid, 'zip_code': '90210', 'country': 'United States', 'place_name': 'Beverly Hills', 'state': 'California', 'active': True } (pact.given('Location exists in database').upon_receiving( 'a request for 90210').with_request('get', '/90210').will_respond_with( 200, body=Like(expected))) with pact: location = consumer.get_location('90210') assert location.place_name == 'Beverly Hills'
"""pact test for user service client""" import logging import os from datetime import datetime import pytest from pact import Consumer, Like, Provider, Format from src.consumer import UserConsumer log = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) print(Format().__dict__) PACT_BROKER_URL = "http://localhost" PACT_FILE = "userserviceclient-userservice.json" PACT_BROKER_USERNAME = "******" PACT_BROKER_PASSWORD = "******" PACT_MOCK_HOST = 'localhost' PACT_MOCK_PORT = 1234 PACT_DIR = os.path.dirname(os.path.realpath(__file__)) @pytest.fixture def consumer(): return UserConsumer('http://{host}:{port}'.format(host=PACT_MOCK_HOST, port=PACT_MOCK_PORT))