-
-
Notifications
You must be signed in to change notification settings - Fork 349
/
Copy pathprovider.spec.js
118 lines (104 loc) · 3.96 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
118
const { Verifier } = require('@pact-foundation/pact');
const { versionFromGitTag } = require('absolute-version');
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, () => {
importData();
console.log('Animal Profile Service listening on http://localhost:8081');
});
// 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({
provider: 'e2e Provider Example',
logLevel: LOG_LEVEL,
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();
token = 'token';
return Promise.resolve(`Animals removed to the db`);
},
'Has some animals': () => {
token = 'token';
importData();
return Promise.resolve(`Animals added to the db`);
},
'Has an animal with ID 1': () => {
token = 'token';
importData();
return Promise.resolve(`Animals added to the db`);
},
'is not authenticated': () => {
token = '';
return Promise.resolve(`Invalid bearer token generated`);
},
'is authenticated': () => {
token = 'token';
return Promise.resolve({ description: `Bearer token generated` });
},
},
// Fetch pacts from broker
pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
// Fetch from broker with given tags
// consumerVersionTags: ['master', 'test', 'prod', 'feat/v3.0.0'],
// Tag provider version with given tags
providerVersionBranch: process.env.GIT_BRANCH || 'master',
// Find _all_ pacts that match the current provider branch
consumerVersionSelectors: [
{
matchingBranch: true,
},
{
mainBranch: true,
},
{
deployedOrReleased: true,
},
],
// Enables "pending pacts" feature
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-animal_profile_service.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,
// Your version numbers need to be unique for every different version of your provider
// see https://docs.pact.io/getting_started/versioning_in_the_pact_broker/ for details.
// If you use git tags, then you can use absolute-version as we do here.
providerVersion: versionFromGitTag(),
})
.verifyProvider()
.then((output) => {
console.log('Pact Verification Complete!');
console.log(output);
app.close();
});
});
});