diff --git a/pact/verifier.py b/pact/verifier.py index c7936c174..a9642aee8 100644 --- a/pact/verifier.py +++ b/pact/verifier.py @@ -1,4 +1,6 @@ """Classes and methods to verify Contracts.""" +import json + from pact.verify_wrapper import VerifyWrapper, path_exists, expand_directories class Verifier(object): @@ -103,6 +105,9 @@ def extract_params(self, **kwargs): verbose = kwargs.get('verbose', False) publish_version = kwargs.get('publish_version', None) + raw_consumer_selectors = kwargs.get('consumer_version_selectors', []) + consumer_selectors = self._build_consumer_selectors(raw_consumer_selectors) + options = { 'log_dir': log_dir, 'log_level': log_level, @@ -114,9 +119,19 @@ def extract_params(self, **kwargs): 'provider_states_setup_url': states_setup_url, 'verbose': verbose, 'publish_version': publish_version, + 'consumer_selectors': consumer_selectors } return self.filter_empty_options(**options) + def _build_consumer_selectors(self, consumer_selectors): + """ + Build the consumer_selectors list. + + Turn each dict in the consumer_selectors list into a string with a + json object, as expected by VerifyWrapper. + """ + return [json.dumps(selector) for selector in consumer_selectors] + def filter_empty_options(self, **kwargs): """Filter out empty options.""" kwargs = dict(filter(lambda item: item[1] is not None, kwargs.items())) diff --git a/tests/test_verifier.py b/tests/test_verifier.py index 2781de601..0639ab3cd 100644 --- a/tests/test_verifier.py +++ b/tests/test_verifier.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + from unittest import TestCase import unittest from mock import patch @@ -42,6 +44,35 @@ def test_verifier_with_provider_and_files(self, mock_path_exists, mock_wrapper): enable_pending=False, include_wip_pacts_since=None) + @patch("pact.verify_wrapper.VerifyWrapper.call_verify") + @patch('pact.verifier.path_exists', return_value=True) + def test_verifier_with_provider_and_files_passes_consumer_selctors(self, mock_path_exists, mock_wrapper): + mock_wrapper.return_value = (True, 'some logs') + + output, _ = self.verifier.verify_pacts( + 'path/to/pact1', + 'path/to/pact2', + headers=['header1', 'header2'], + consumer_version_selectors=[ + # Using OrderedDict for the sake of testing + OrderedDict([("tag", "main"), ("latest", True)]), + OrderedDict([("tag", "test"), ("latest", False)]), + ] + ) + + assertVerifyCalled(mock_wrapper, + 'path/to/pact1', + 'path/to/pact2', + provider='test_provider', + custom_provider_headers=['header1', 'header2'], + provider_base_url='http://localhost:8888', + log_level='INFO', + verbose=False, + enable_pending=False, + include_wip_pacts_since=None, + consumer_selectors=['{"tag": "main", "latest": true}', + '{"tag": "test", "latest": false}']) + def test_validate_on_publish_results(self): self.assertRaises(Exception, self.verifier.verify_pacts, 'path/to/pact1', publish=True) @@ -140,6 +171,35 @@ def test_verifier_with_broker(self, mock_wrapper): enable_pending=False, include_wip_pacts_since=None) + @patch("pact.verify_wrapper.VerifyWrapper.call_verify") + def test_verifier_with_broker_passes_consumer_selctors(self, mock_wrapper): + + mock_wrapper.return_value = (True, 'some value') + + output, _ = self.verifier.verify_with_broker( + consumer_version_selectors=[ + # Using OrderedDict for the sake of testing + OrderedDict([("tag", "main"), ("latest", True)]), + OrderedDict([("tag", "test"), ("latest", False)]), + ], + **self.default_opts + ) + + self.assertTrue(output) + assertVerifyCalled(mock_wrapper, + provider='test_provider', + provider_base_url='http://localhost:8888', + broker_password=self.broker_password, + broker_username=self.broker_username, + broker_token='token', + broker_url=self.broker_url, + log_level='INFO', + verbose=False, + enable_pending=False, + include_wip_pacts_since=None, + consumer_selectors=['{"tag": "main", "latest": true}', + '{"tag": "test", "latest": false}']) + @patch("pact.verify_wrapper.VerifyWrapper.call_verify") @patch('pact.verifier.path_exists', return_value=True) def test_publish_on_success(self, mock_path_exists, mock_wrapper):