Skip to content

Commit 043ae37

Browse files
committed
adding supplier contract
Signed-off-by: Tim Ireland <[email protected]>
1 parent 32c5a49 commit 043ae37

File tree

8 files changed

+436
-73
lines changed

8 files changed

+436
-73
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ node_modules
2222
/.pnp
2323
.pnp.js
2424

25+
/pact
26+
2527
# testing
2628
/coverage
2729

lambdas/backend-api/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
"test:unit": "jest",
77
"lint": "eslint .",
88
"lint:fix": "eslint . --fix",
9-
"typecheck": "tsc --noEmit"
9+
"typecheck": "tsc --noEmit",
10+
"pact:publish": "",
11+
"pact:publish1": "../../pact/bin/pactflow publish-provider-contract ../../infrastructure/terraform/modules/backend-api/spec.tmpl.json --provider=TemplateService --provider-app-version=1.0.0 --broker-base-url=https://nhs-england-notify.pactflow.io --content-type application/yaml --branch $(git rev-parse --abbrev-ref HEAD)",
12+
"generate-dependencies": "npx openapi-typescript-codegen --exportCore false --exportServices false --input ../../infrastructure/terraform/modules/backend-api/spec.tmpl.json --output ./src/types/generated"
1013
},
1114
"dependencies": {
1215
"@aws-sdk/client-cognito-identity-provider": "^3.699.0",
1316
"@aws-sdk/client-dynamodb": "^3.696.0",
1417
"@aws-sdk/client-ses": "^3.699.0",
1518
"@aws-sdk/lib-dynamodb": "^3.698.0",
19+
"@pact-foundation/pact": "13.1.4",
20+
"@pact-foundation/pact-cli": "16.0.1",
1621
"handlebars": "^4.7.8",
1722
"html-entities": "^2.5.2",
1823
"jsonwebtoken": "^9.0.2",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Verifier } from '@pact-foundation/pact';
2+
3+
describe('Pact Verification', () => {
4+
it('should validate the expectations of the consumer', async () => {
5+
const opts = {
6+
provider: 'TemplateService',
7+
consumerVersionSelectors: [
8+
{
9+
latest: true,
10+
},
11+
],
12+
providerBaseUrl: 'http://localhost:8080',
13+
pactBrokerUrl: process.env.PACT_BROKER_URL || 'http://127.0.0.1:8000',
14+
pactBrokerUsername: process.env.PACT_BROKER_USERNAME || 'pact_workshop',
15+
pactBrokerPassword: process.env.PACT_BROKER_PASSWORD || 'pact_workshop',
16+
publishVerificationResult: true,
17+
providerVersion: '1.0.0',
18+
};
19+
20+
const verifier = new Verifier(opts);
21+
22+
await verifier.verifyProvider();
23+
24+
throw error;
25+
});
26+
});

lambdas/backend-client/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"lint:fix": "eslint . --fix",
1010
"test:unit": "jest",
1111
"typecheck": "tsc --noEmit",
12+
"pact:publish": "pact-broker publish ./pacts --consumer-app-version=\"1.0.1\" --auto-detect-version-properties --broker-base-url=https://nhs-england-notify.pactflow.io",
1213
"generate-dependencies": "npx openapi-typescript-codegen --exportCore false --exportServices false --input ../../infrastructure/terraform/modules/backend-api/spec.tmpl.json --output ./src/types/generated"
1314
},
1415
"files": [
@@ -24,6 +25,8 @@
2425
"@swc/core": "^1.10.1",
2526
"@swc/jest": "^0.2.37",
2627
"@tsconfig/node20": "^20.1.4",
28+
"@pact-foundation/pact": "13.1.4",
29+
"@pact-foundation/pact-cli": "16.0.1",
2730
"axios-mock-adapter": "^2.1.0",
2831
"esbuild": "^0.24.0",
2932
"jest-mock-extended": "^4.0.0-beta1",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Contracts
2+
This folder comntains the consumer cretaed contracts (pacts) these are created from lambdas/backend-client/src/__tests__/api.pact.spec.ts
3+
4+
5+
6+
# Verifier
7+
8+
This runs against the api /lambdas/backend-api/src/__tests__/templates/api/api.pact.test.ts.
9+
10+
11+
# Bi-directional contract testing
12+
13+
This verifies that the consumer tests are comptibale with the provider OAS spec infrastructure/terraform/modules/backend-api/spec.tmpl.json
14+
15+
This bi-directional contract testing is only available as SAAS see https://pactflow.io/oss/
16+
17+
See https://docs.pactflow.io/docs/bi-directional-contract-testing

lambdas/backend-client/pacts/TemplateClient-TemplateService.json

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
"description": "get all templates",
88
"providerState": "templates exist",
99
"request": {
10+
"headers": {
11+
"Authorization": "Bearer 2019-01-14T11:34:18.045Z"
12+
},
13+
"matchingRules": {
14+
"$.header.Authorization": {
15+
"match": "type"
16+
}
17+
},
1018
"method": "GET",
1119
"path": "/v1/templates"
1220
},
@@ -37,6 +45,14 @@
3745
"description": "get template with ID 10",
3846
"providerState": "template with ID 10 exists",
3947
"request": {
48+
"headers": {
49+
"Authorization": "Bearer 2019-01-14T11:34:18.045Z"
50+
},
51+
"matchingRules": {
52+
"$.header.Authorization": {
53+
"match": "type"
54+
}
55+
},
4056
"method": "GET",
4157
"path": "/v1/template/10"
4258
},
@@ -64,7 +80,7 @@
6480
],
6581
"metadata": {
6682
"pact-js": {
67-
"version": "13.2.0"
83+
"version": "13.1.4"
6884
},
6985
"pactRust": {
7086
"ffi": "0.4.22",
@@ -77,4 +93,4 @@
7793
"provider": {
7894
"name": "TemplateService"
7995
}
80-
}
96+
}

lambdas/backend-client/src/__tests__/api.pact.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
MatchersV3,
66
SpecificationVersion,
77
} from '@pact-foundation/pact';
8+
89
import { TemplateClient } from 'nhs-notify-backend-client/src/template-api-client';
910
import { TemplateStatus, TemplateType } from 'nhs-notify-backend-client';
1011

@@ -33,7 +34,11 @@ describe('API Pact test', () => {
3334
withRequest: {
3435
method: 'GET',
3536
path: '/v1/templates',
37+
headers: {
38+
Authorization: like('Bearer 2019-01-14T11:34:18.045Z'),
39+
},
3640
},
41+
3742
willRespondWith: {
3843
status: 200,
3944
headers: {
@@ -78,6 +83,9 @@ describe('API Pact test', () => {
7883
withRequest: {
7984
method: 'GET',
8085
path: '/v1/template/10',
86+
headers: {
87+
Authorization: like('Bearer 2019-01-14T11:34:18.045Z'),
88+
},
8189
},
8290
willRespondWith: {
8391
status: 200,

0 commit comments

Comments
 (0)