This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathproduct.pact.test.js
95 lines (87 loc) · 3.51 KB
/
product.pact.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require("dotenv").config();
const { Verifier } = require("@pact-foundation/pact");
const controller = require("./product.controller");
const Product = require("./product");
// Setup provider server to verify
const app = require("express")();
const authMiddleware = require("../middleware/auth.middleware");
app.use(authMiddleware);
app.use(require("./product.routes"));
const server = app.listen("8080");
describe("Pact Verification", () => {
it("validates the expectations of ProductService", () => {
const baseOpts = {
logLevel: "INFO",
providerBaseUrl: "http://localhost:8080",
providerVersion: process.env.GIT_COMMIT,
providerVersionTags: process.env.GIT_BRANCH ? [process.env.GIT_BRANCH] : [], // the old way of publishing verification results with the tag property
// providerVersionBranch: process.env.GIT_BRANCH
// ? process.env.GIT_BRANCH
// : "", // the recommended way of publishing verification results with the branch property
verbose: process.env.VERBOSE === "true",
};
// For builds triggered by a 'contract_content_changed' just verify the changed pact.
// https://docs.pact.io/pact_broker/webhooks#the-contract-content-changed-event
// The URL will bave been passed in from the webhook to the CI job.
const pactChangedOpts = {
pactUrls: [process.env.PACT_URL],
};
// For 'normal' provider builds, fetch the the latest version from the main branch of each consumer, as specified by
// the consumer's selectors branch property and all the currently deployed of each consumer.
// https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors
const fetchPactsDynamicallyOpts = {
provider: "pactflow-example-provider-legacy",
consumerVersionSelectors: [{ tag: 'master', latest: true }, { deployed: true } ], // the way of specifying which pacts to verify if using tags
// consumerVersionSelectors: [
// { mainBranch: true },
// { deployed: true },
// ], // the new way of specifying which pacts to verify if using branches (recommended)
pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
enablePending: false,
includeWipPactsSince: undefined,
};
const stateHandlers = {
"products exists": () => {
controller.repository.products = new Map([
["10", new Product("10", "CREDIT_CARD", "28 Degrees", "v1")],
]);
},
"products exist": () => {
controller.repository.products = new Map([
["10", new Product("10", "CREDIT_CARD", "28 Degrees", "v1")],
]);
},
"a product with ID 10 exists": () => {
controller.repository.products = new Map([
["10", new Product("10", "CREDIT_CARD", "28 Degrees", "v1")],
]);
},
"a product with ID 11 does not exist": () => {
controller.repository.products = new Map();
},
};
const requestFilter = (req, res, next) => {
if (!req.headers["authorization"]) {
next();
return;
}
req.headers["authorization"] = `Bearer ${new Date().toISOString()}`;
next();
};
const opts = {
...baseOpts,
...(process.env.PACT_URL ? pactChangedOpts : fetchPactsDynamicallyOpts),
stateHandlers: stateHandlers,
requestFilter: requestFilter,
};
return new Verifier(opts)
.verifyProvider()
.then((output) => {
console.log("Pact Verification Complete!");
console.log(output);
})
.finally(() => {
server.close();
});
});
});