forked from pactflow/example-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product.pact.test.js
86 lines (78 loc) · 2.84 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
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] : [],
verbose: process.env.VERBOSE === 'true'
}
// For builds triggered by a 'contract content changed' webhook,
// just verify the changed pact. 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 `master` and `prod` pacts for this provider
const fetchPactsDynamicallyOpts = {
provider: "pactflow-example-provider",
//consumerVersionTag: ['master', 'prod'], //the old way of specifying which pacts to verify
consumerVersionSelectors: [{ tag: 'master', latest: true }, { deployed: true } ], // the new way of specifying which pacts to verify
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();
});
})
});