Skip to content

Commit 588b55d

Browse files
authored
fix(consumer): ensure a description is provided for all interactions (#278)
1 parent 02643d4 commit 588b55d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

examples/broker/docker-compose.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3'
1+
version: '3.9'
22

33
services:
44
# A PostgreSQL database for the Broker to store Pacts and verification results
@@ -32,6 +32,13 @@ services:
3232
PACT_BROKER_DATABASE_NAME: postgres
3333
PACT_BROKER_BASIC_AUTH_USERNAME: pactbroker
3434
PACT_BROKER_BASIC_AUTH_PASSWORD: pactbroker
35+
# The Pact Broker provides a healthcheck endpoint which we will use to wait
36+
# for it to become available before starting up
37+
healthcheck:
38+
test: [ "CMD", "wget", "-q", "--tries=1", "--spider", "http://pactbroker:pactbroker@localhost:9292/diagnostic/status/heartbeat" ]
39+
interval: 1s
40+
timeout: 2s
41+
retries: 5
3542

3643
# An NGINX reverse proxy in front of the Broker on port 8443, to be able to
3744
# terminate with SSL
@@ -44,3 +51,7 @@ services:
4451
- ./ssl:/etc/nginx/ssl
4552
ports:
4653
- "8443:443"
54+
restart: always
55+
depends_on:
56+
broker_app:
57+
condition: service_healthy

pact/pact.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from .broker import Broker
1414
from .constants import MOCK_SERVICE_PATH
1515
from .matchers import from_term
16+
from .verify_wrapper import PactException
1617

1718

1819
class Pact(Broker):
@@ -165,6 +166,12 @@ def given(self, provider_state):
165166
def setup(self):
166167
"""Configure the Mock Service to ready it for a test."""
167168
try:
169+
# First, check that the interactions are all complete
170+
for interaction in self._interactions:
171+
missing_fields = [f for f in self.MANDATORY_FIELDS if f not in interaction]
172+
if missing_fields:
173+
raise PactException(f"Interaction incomplete, missing field(s): {', '.join(missing_fields)}")
174+
168175
interactions_uri = f"{self.uri}/interactions"
169176
resp = requests.delete(
170177
interactions_uri, headers=self.HEADERS, verify=False

tests/test_pact.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pact.constants import MOCK_SERVICE_PATH
1212
from pact.pact import Pact, FromTerms, Request, Response
1313
from pact import pact as pact
14+
from pact.verify_wrapper import PactException
1415

1516

1617
class PactTestCase(TestCase):
@@ -559,6 +560,25 @@ def test_context_raises_error(self):
559560
self.assertFalse(self.mock_verify.called)
560561

561562

563+
class PactContextManagerSetupTestCase(PactTestCase):
564+
def test_definition_without_description(self):
565+
# Description (populated from "given") is listed in the MANDATORY_FIELDS.
566+
# Make sure if it isn't there that an exception is raised
567+
pact = Pact(self.consumer, self.provider)
568+
(pact.given("A request without a description")
569+
.with_request('GET', '/path')
570+
.will_respond_with(200, body='success'))
571+
572+
self.assertEqual(len(pact._interactions), 1)
573+
574+
self.assertTrue('description' not in pact._interactions[0])
575+
576+
# By using "with", __enter__ will call the setup method that will verify if this is present
577+
with self.assertRaises(PactException):
578+
with pact:
579+
pact.verify()
580+
581+
562582
class FromTermsTestCase(TestCase):
563583
def test_json(self):
564584
with self.assertRaises(NotImplementedError):

0 commit comments

Comments
 (0)