def test_v3(query_field): pact = Consumer("consumer").has_pact_with(Provider("provider"), version="3.0.0") pact.given([{ "name": "the condition exists", "params": {} }]).upon_receiving("a request").with_request( "GET", "/path", query=dict(fields=query_field)).will_respond_with(200, body="ok") result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": { "name": "consumer" }, "provider": { "name": "provider" }, "interactions": [{ "description": "a request", "providerStates": [{ "name": "the condition exists", "params": {} }], "request": dict(method="GET", path="/path", query=dict(fields=["first,second"])), "response": dict(status=200, body="ok"), }], "metadata": dict(pactSpecification=dict(version="3.0.0")), }
def test_like_v2(): pact = Consumer("consumer").has_pact_with(Provider("provider"), version="2.0.0") pact.given("the condition exists").upon_receiving("a request").with_request( "GET", "/path", query=Like("fields=first,second") ).will_respond_with(200, body="ok") result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": {"name": "consumer"}, "provider": {"name": "provider"}, "interactions": [ { "description": "a request", "providerState": "the condition exists", "request": dict( method="GET", path="/path", query="fields=first,second", matchingRules={"$.query": {"match": "type"}}, ), "response": dict(status=200, body="ok"), } ], "metadata": dict(pactSpecification=dict(version="2.0.0")), }
def test_v3(query_field): pact = Consumer('consumer').has_pact_with(Provider('provider'), version='3.0.0') pact.given([{'name': "the condition exists", 'params': {}}]).upon_receiving("a request") \ .with_request("GET", "/path", query=dict(fields=query_field)).will_respond_with(200, body='ok') result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': { 'name': 'consumer' }, 'provider': { 'name': 'provider' }, 'interactions': [{ 'description': 'a request', 'providerStates': [{ 'name': 'the condition exists', 'params': {} }], 'request': dict(method='GET', path='/path', query=dict(fields=['first,second'])), 'response': dict(status=200, body='ok'), }], 'metadata': dict(pactSpecification=dict(version='3.0.0')) }
def test_full_payload_v3(): pact = Consumer('consumer').has_pact_with(Provider('provider'), version='3.0.0') (pact .given([{"name": "User exists and is not an administrator", "params": {"username": "******"}}]) .upon_receiving('a request for UserA') .with_request('get', '/users/UserA', headers={'Accept': 'application/json'}, query=dict(term=['test'])) .will_respond_with(200, body={'username': '******'}, headers={'Content-Type': 'application/json'})) result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': {'name': 'consumer'}, 'provider': {'name': 'provider'}, 'interactions': [ { 'description': 'a request for UserA', 'providerStates': [{ "name": "User exists and is not an administrator", "params": {"username": "******"} }], 'request': dict(method='get', path='/users/UserA', headers={'Accept': 'application/json'}, query=dict(term=['test'])), 'response': dict(status=200, body={'username': '******'}, headers={'Content-Type': 'application/json'}) } ], 'metadata': dict(pactSpecification=dict(version='3.0.0')) }
def test_full_payload_v2(): pact = Consumer("consumer").has_pact_with(Provider("provider"), version="2.0.0") ( pact.given("UserA exists and is not an administrator") .upon_receiving("a request for UserA") .with_request( "get", "/users/UserA", headers={"Accept": "application/json"}, query="term=test" ) .will_respond_with( 200, body={"username": "******"}, headers={"Content-Type": "application/json"} ) ) result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": {"name": "consumer"}, "provider": {"name": "provider"}, "interactions": [ { "description": "a request for UserA", "providerState": "UserA exists and is not an administrator", "request": dict( method="get", path="/users/UserA", headers={"Accept": "application/json"}, query="term=test", ), "response": dict( status=200, body={"username": "******"}, headers={"Content-Type": "application/json"}, ), } ], "metadata": dict(pactSpecification=dict(version="2.0.0")), }
def test_like_v2(): pact = Consumer('consumer').has_pact_with(Provider('provider'), version='2.0.0') pact.given("the condition exists").upon_receiving("a request") \ .with_request("GET", "/path", query=Like("fields=first,second")).will_respond_with(200, body='ok') result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': { 'name': 'consumer' }, 'provider': { 'name': 'provider' }, 'interactions': [{ 'description': 'a request', 'providerState': 'the condition exists', 'request': dict(method='GET', path='/path', query='fields=first,second', matchingRules={'$.query': { 'match': 'type' }}), 'response': dict(status=200, body='ok'), }], 'metadata': dict(pactSpecification=dict(version='2.0.0')) }
def pact(): pact = Consumer('Consumer').has_pact_with(Provider('Provider'), host_name='localhost', port=1234, pact_dir='/usr/src/app/pact') pact.start_service() atexit.register(pact.stop_service) return pact
def test_mock_usage_fail_validation(): pact = Consumer('C').has_pact_with(Provider('P'), version='3.0.0') \ .given("g").upon_receiving("r").with_request("post", "/foo", body=Like({"a": "spam", "b": Equals("bee")})) \ .will_respond_with(200) with pytest.raises(AssertionError), pact: requests.post(pact.uri + '/foo', json={"a": "ham", "b": "wasp"})
def get_pact(self): if not self.__class__._pact: self.__class__._pact = Consumer(self.consumer_name).has_pact_with( Provider(self.provider_name), version="3.0.0", port=self.mock_server_port) return self.__class__._pact
def test_v2_not_allowed(): with pytest.raises(Includes.NotAllowed): Consumer("C").has_pact_with( Provider("P"), version="2.0.0").given("g").upon_receiving("r").with_request( "post", "/foo", body=Includes("bee", "been")).will_respond_with(200)
def test_mock_usage_pass_validation(): pact = Consumer('C').has_pact_with(Provider('P'), version='3.0.0') \ .given("g").upon_receiving("r").with_request("post", "/foo", body=Like({"a": "spam", "b": Includes("bee", 'been')})) \ .will_respond_with(200) with pact: requests.post(pact.uri + '/foo', json={"a": "ham", "b": "has bee in it"})
def test_mock_usage_pass_validation(): pact = (Consumer("C").has_pact_with( Provider("P"), version="3.0.0").given("g").upon_receiving("r").with_request( "post", "/foo", body=Like({ "a": "spam", "b": Equals("bee") })).will_respond_with(200)) with pact: requests.post(pact.uri + "/foo", json={"a": "ham", "b": "bee"})
def test_v3(): pact = Consumer("consumer").has_pact_with(Provider("provider"), version="3.0.0") pact.given([ dict(name="the condition exists", params={}), dict(name="the user exists", params=dict(username="******")), ]).upon_receiving("a request").with_request( "GET", "/path").will_respond_with(200, body="ok") result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": { "name": "consumer" }, "provider": { "name": "provider" }, "interactions": [{ "description": "a request", "providerStates": [ { "name": "the condition exists", "params": {} }, { "name": "the user exists", "params": { "username": "******" } }, ], "request": dict(method="GET", path="/path"), "response": dict(status=200, body="ok"), }], "metadata": dict(pactSpecification=dict(version="3.0.0")), }
def test_mock_usage_fail_validation(): pact = (Consumer("C").has_pact_with( Provider("P"), version="3.0.0").given("g").upon_receiving("r").with_request( "post", "/foo", body=Like({ "a": "spam", "b": Includes("bee", "been") })).will_respond_with(200)) with pytest.raises(AssertionError), pact: requests.post(pact.uri + "/foo", json={"a": "ham", "b": "wasp"})
def test_null(version): pact = Consumer("consumer").has_pact_with(Provider("provider"), version=version) pact.given(None).upon_receiving("a request").with_request( "GET", "/path").will_respond_with(200, body="ok") result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": { "name": "consumer" }, "provider": { "name": "provider" }, "interactions": [{ "description": "a request", "request": dict(method="GET", path="/path"), "response": dict(status=200, body="ok"), }], "metadata": dict(pactSpecification=dict(version=version)), }
def test_null(version): pact = Consumer('consumer').has_pact_with(Provider('provider'), version=version) pact.given(None).upon_receiving("a request").with_request("GET", "/path") \ .will_respond_with(200, body='ok') result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': { 'name': 'consumer' }, 'provider': { 'name': 'provider' }, 'interactions': [{ 'description': 'a request', 'request': dict(method='GET', path='/path'), 'response': dict(status=200, body='ok'), }], 'metadata': dict(pactSpecification=dict(version=version)) }
def test_v3(): pact = Consumer('consumer').has_pact_with(Provider('provider'), version='3.0.0') pact.given([ dict(name="the condition exists", params={}), dict(name="the user exists", params=dict(username='******')), ]).upon_receiving("a request").with_request( "GET", "/path").will_respond_with(200, body='ok') result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': { 'name': 'consumer' }, 'provider': { 'name': 'provider' }, 'interactions': [{ 'description': 'a request', 'providerStates': [{ 'name': 'the condition exists', 'params': {} }, { 'name': 'the user exists', 'params': { 'username': '******' } }], 'request': dict(method='GET', path='/path'), 'response': dict(status=200, body='ok'), }], 'metadata': dict(pactSpecification=dict(version='3.0.0')) }
def setUp(self): global pact pact = Consumer('UseCaseMetadataProject').has_pact_with( Provider('PortMetadata'), port=3000) self.app = create_app() self.client = self.app.test_client() self.user1 = User("MaxMustermann") self.service1 = OAuth2Service( servicename="TestService", implements=["metadata"], authorize_url="http://localhost/oauth/authorize", refresh_url="http://localhost/oauth/token", client_id="MNO", client_secret="UVW") self.token1 = OAuth2Token(self.user1, self.service1, "ABC", "X_ABC")
def test_eachlike(): pact = (Consumer("consumer").has_pact_with( Provider("provider"), version="2.0.0").given("the condition exists").upon_receiving( "a request").with_request("POST", "/path", body=EachLike(1)).will_respond_with( 200, body={"results": EachLike(1)})) result = pact.construct_pact(pact._interactions[0]) assert result == { "consumer": { "name": "consumer" }, "provider": { "name": "provider" }, "interactions": [{ "description": "a request", "providerState": "the condition exists", "request": dict( method="POST", path="/path", body=[1], matchingRules={"$.body": { "match": "type", "min": 1 }}, ), "response": dict( status=200, body={"results": [1]}, matchingRules={"$.body.results": { "match": "type", "min": 1 }}, ), }], "metadata": dict(pactSpecification=dict(version="2.0.0")), }
def test_eachlike(): pact = (Consumer('consumer').has_pact_with( Provider('provider'), version='2.0.0').given("the condition exists").upon_receiving( "a request").with_request("POST", "/path", body=EachLike(1)).will_respond_with( 200, body={"results": EachLike(1)})) result = pact.construct_pact(pact._interactions[0]) assert result == { 'consumer': { 'name': 'consumer' }, 'provider': { 'name': 'provider' }, 'interactions': [{ 'description': 'a request', 'providerState': 'the condition exists', 'request': dict(method='POST', path='/path', body=[1], matchingRules={'$.body': { 'match': 'type', 'min': 1 }}), 'response': dict(status=200, body={'results': [1]}, matchingRules={'$.body.results': { 'match': 'type', 'min': 1 }}), }], 'metadata': dict(pactSpecification=dict(version='2.0.0')) }
api_key = os.getenv("ZENODO_API_KEY", default=None) def create_app(): from src import bootstrap # creates a test client app = bootstrap(use_default_error=True, address="http://localhost:3000").app # propagate the exceptions to the test client app.config.update({"TESTING": True}) return app pact = Consumer("PortZenodo").has_pact_with(Provider("Zenodo"), port=3000) unittest.TestCase.maxDiff = None class TestPortZenodo(unittest.TestCase): def setUp(self): self.app = create_app() self.client = self.app.test_client() @classmethod def tearDownClass(cls): pass def tearDown(self): pass
from lib.Research import Research from pactman import Consumer, Provider import unittest pact = Consumer('UseCaseMetadataResearch').has_pact_with( Provider('PortMetadata'), port=3000) testing_address = "localhost:3000" class Test_Research(unittest.TestCase): def test_research_init_researchId(self): userId = 0 researchIndex = 0 researchId = 1 ports = [ # no entries [], # one entry [{ "port": "port-zenodo", "properties": [{ "portType": "metadata", "value": True }] }], # two entries [{ "port": "port-zenodo", "properties": [{
import atexit import unittest import requests from pactman import Consumer, Provider, Term, Like from pactman.verifier.pytest_plugin import pact_verifier pact = Consumer('Consumer').has_pact_with( Provider('Provider'), file_write_mode="override", use_mocking_server=False, pact_dir="C:\\Users\\abdulsha\\PycharmProjects\\pact-python-master\\Pacts") # pact.start_mocking() # atexit.register(pact.stop_mocking) class Generate_POC_PACTS(unittest.TestCase): def test_generate_pact_from_consumer_end(self): # Setting up Expected Response expected = {'skip': 0, 'limit': 150} pact.given('A Support User exists and is not an administrator' ).upon_receiving('a request for API support').with_request( method='GET', path='/api', ).will_respond_with(200, body=expected) # When a request is hit to Server, would it retrieve the expected data with pact: result = requests.get(pact.uri + '/api')
# creates a test client app = bootstrap( use_optimizer={ "compress": False, "minify": False }, use_default_error=True, testing=address, ).app # propagate the exceptions to the test client app.config.update({"TESTING": True}) return app pact = Consumer("UseCaseTokenStorage").has_pact_with( Provider("CentralServiceTokenStorage"), port=3000) class Test_TokenServiceServer(unittest.TestCase): def run(self, result=None): # this make pact as context in every test available. with pact as p: super(Test_TokenServiceServer, self).run(result) def setUp(self): self.app = create_app() self.client = self.app.test_client() def test_redirect(self): code = "XYZABC" user = User("user")
from lib.Storage import Storage from RDS import Token, OAuth2Token, User, LoginService, OAuth2Service, Util def create_app(): from src import bootstrap # creates a test client app = bootstrap(use_default_error=True, testing=True).app return app pact_host_port = 3000 pact_host_fqdn = f"http://localhost:{pact_host_port}" pact = Consumer("CentralServiceTokenStorage").has_pact_with( Provider("OAuth-Provider"), port=pact_host_port) class TestTokenStorageServer(unittest.TestCase): def setUp(self): self.app = create_app() self.client = self.app.test_client() self.empty_storage = Storage() self.success = {"success": True} self.user1 = User("Max Mustermann") self.user2 = User("Mimi Mimikri") self.user3 = User("Karla Kolumda")
import atexit import pytest import requests from pactman import Consumer, Provider #from pact import Consumer, Provider #pact = Consumer('buildReader').has_pact_with(Provider('bringon')) #pact.start_service() #atexit.register(pact.stop_service) pact = Consumer('Consumer').has_pact_with(Provider('Provider'), version='3.0.0') def test_interaction(): pact.given("some data exists").upon_receiving("a request") \ .with_request("get", "/", query={"foo": ["bar"]}).will_respond_with(200) with pact: requests.get(pact.uri, params={"foo": ["bar"]}) def test_server_is_up(): pact.given("The Server is online").upon_receiving("a request") \ .with_request("get", "/", headers={'Content-Type': 'application/json'}) \ .will_respond_with(200, body= 'Server Works!', headers={'Content-Type': 'application/json'}) with pact: r = requests.get(pact.uri, headers={'Content-Type': 'application/json'}) print (r.text)
import unittest from lib.ExporterService import ExporterService from pactman import Consumer, Provider def create_app(): from src import bootstrap # creates a test client app = bootstrap(use_default_error=True).app # propagate the exceptions to the test client app.config.update({"TESTING": True, "TESTSERVER": "http://localhost:3000"}) return app pact = Consumer('PortZenodo').has_pact_with(Provider('Zenodo'), port=3000) class Test_ExporterService(unittest.TestCase): def setUp(self): self.app = create_app() self.client = self.app.test_client() @unittest.skip def test_export_zenodo_from_owncloud_working(self): exporter = ExporterService( testing=True, testing_address=self.app.config.get("TESTSERVER")) # call to get file from owncloud pact.given('file and userid are valid').upon_receiving( 'the wanted file content').with_request(
from .test_service import Test_Service def create_app(): testing_address = "http://localhost:3000" from src import bootstrap # creates a test client app = bootstrap(use_default_error=True).app # propagate the exceptions to the test client app.config.update({"TESTING": testing_address}) return app pact = Consumer('ServiceExporter').has_pact_with(Provider('Zenodo'), port=3000) unittest.TestCase.maxDiff = None class TestServiceExporter(unittest.TestCase): def setUp(self): self.app = create_app() self.client = self.app.test_client() def test_files(self): userId = "admin" researchIndex = 1 userId = "admin" researchIndex = 1
def test_v2_not_allowed(): with pytest.raises(Equals.NotAllowed): Consumer('C').has_pact_with(Provider('P'), version='2.0.0') \ .given("g").upon_receiving("r").with_request("post", "/foo", body=Equals("bee")) \ .will_respond_with(200)
import unittest from lib.Service import Service from pactman import Consumer, Provider import json from RDS import FileTransferArchive, FileTransferMode pact = Consumer("ServiceExporter").has_pact_with(Provider("Zenodo"), port=3000) testingaddress = "http://localhost:3000" class Test_Service(unittest.TestCase): @staticmethod def requestStorageFolderGET(pact, folderpath: str, userId: str, files: list): pact.given( f"user {userId} has {len(files)} files in folderpath {folderpath}" ).upon_receiving( "the filepaths for files in folder with folderpath" ).with_request( "GET", f"/storage/folder" ).will_respond_with( 200, body={"files": files} ) @staticmethod def requestStorageFileGET( pact, filepath: str, userId: str, fileContent: str, code=200 ): pact.given(f"user {userId} has a file in filepath {filepath}").upon_receiving( "the filecontent of the given filepath" ).with_request("GET", f"/storage/file").will_respond_with( code, body=fileContent