-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
provider.spec.js
117 lines (106 loc) · 3.78 KB
/
provider.spec.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const { Verifier } = require('@pact-foundation/pact');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const { server, importData, animalRepository } = require('../provider.js');
const path = require('path');
const LOG_LEVEL = process.env.LOG_LEVEL || 'INFO';
const app = server.listen(8081, () => {
console.log('Animal Profile Service listening on http://localhost:8081');
});
const pactBroker = 'https://testdemo.pactflow.io';
// Verify that the provider meets all consumer expectations
describe('Pact Verification', () => {
it('validates the expectations of Matching Service', () => {
let token = 'INVALID TOKEN';
return new Verifier({
logLevel: LOG_LEVEL,
provider: 'Animal Profile Service V3',
providerBaseUrl: 'http://localhost:8081',
requestFilter: (req, res, next) => {
console.log(
'Middleware invoked before provider API - injecting Authorization token'
);
req.headers['MY_SPECIAL_HEADER'] = 'my special value';
// e.g. ADD Bearer token
req.headers['authorization'] = `Bearer ${token}`;
next();
},
stateHandlers: {
'Has no animals': () => {
animalRepository.clear();
return Promise.resolve({
description: `Animals removed from the db`,
});
},
'Has some animals': () => {
importData();
return Promise.resolve({
description: `Animals added to the db`,
count: animalRepository.count(),
});
},
'Has an animal with ID': (parameters) => {
importData();
animalRepository.first().id = parameters.id;
return Promise.resolve({
description: `Animal with ID ${parameters.id} added to the db`,
id: parameters.id,
});
},
'is not authenticated': () => {
token = '';
return Promise.resolve({
description: `Invalid bearer token generated`,
});
},
'is authenticated': () => {
token = 'token';
return Promise.resolve({ description: `Bearer token generated` });
},
},
// Fetch pacts from broker
pactBrokerUrl: pactBroker,
// Fetch from broker with given tags
providerVersionTags: ['master'],
providerVersionBranch: process.env.GIT_BRANCH || 'master',
// Find _all_ pacts that match the current provider branch
consumerVersionSelectors: [
{
matchingBranch: true,
},
{
mainBranch: true,
},
{
deployedOrReleased: true,
},
],
enablePending: true,
// Specific Remote pacts (doesn't need to be a broker)
// pactUrls: ['https://test.pactflow.io/pacts/provider/Animal%20Profile%20Service/consumer/Matching%20Service/latest'],
// Local pacts
// pactUrls: [
// path.resolve(
// process.cwd(),
// './pacts/Matching Service V3-Animal Profile Service V3.json'
// ),
// ],
// If you're using the open source Pact Broker, use the username/password option as per below
// pactBrokerUsername: process.env.PACT_BROKER_USERNAME
// pactBrokerPassword: process.env.PACT_BROKER_PASSWORD
//
// if you're using a PactFlow broker, you must authenticate using the bearer token option
// You can obtain the token from https://<your broker>.pactflow.io/settings/api-tokens
pactBrokerToken: process.env.PACT_BROKER_TOKEN,
publishVerificationResult: true,
providerVersion: '1.0.0',
})
.verifyProvider()
.then((output) => {
console.log('Pact Verification Complete!');
console.log('Result:', output);
app.close();
});
});
});