-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
Copy pathconsumer.spec.js
269 lines (246 loc) · 7.92 KB
/
consumer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
const path = require('path');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const expect = chai.expect;
const { Pact, Matchers } = require('@pact-foundation/pact');
const LOG_LEVEL = process.env.LOG_LEVEL || 'TRACE';
chai.use(chaiAsPromised);
describe('Pact', () => {
const provider = new Pact({
consumer: 'e2e Consumer Example',
provider: 'e2e Provider Example',
// port: 1234, // You can set the port explicitly here or dynamically (see setup() below)
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
logLevel: LOG_LEVEL,
spec: 2,
});
// Alias flexible matchers for simplicity
const { eachLike, like, term, iso8601DateTimeWithMillis } = Matchers;
// Animal we want to match :)
const suitor = {
id: 2,
available_from: '2017-12-04T14:47:18.582Z',
first_name: 'Nanny',
animal: 'goat',
last_name: 'Doe',
age: 27,
gender: 'F',
location: {
description: 'Werribee Zoo',
country: 'Australia',
post_code: 3000,
},
eligibility: {
available: true,
previously_married: true,
},
interests: ['walks in the garden/meadow', 'parkour'],
};
const MIN_ANIMALS = 2;
// Define animal payload, with flexible matchers
//
// This makes the test much more resilient to changes in actual data.
// Here we specify the 'shape' of the object that we care about.
// It is also import here to not put in expectations for parts of the
// API we don't care about
const animalBodyExpectation = {
id: like(1),
available_from: iso8601DateTimeWithMillis(),
first_name: like('Billy'),
last_name: like('Goat'),
animal: like('goat'),
age: like(21),
gender: term({
matcher: 'F|M',
generate: 'M',
}),
location: {
description: like('Melbourne Zoo'),
country: like('Australia'),
post_code: like(3000),
},
eligibility: {
available: like(true),
previously_married: like(false),
},
interests: eachLike('walks in the garden/meadow'),
};
// Define animal list payload, reusing existing object matcher
const animalListExpectation = eachLike(animalBodyExpectation, {
min: MIN_ANIMALS,
});
// Setup a Mock Server before unit tests run.
// This server acts as a Test Double for the real Provider API.
// We then call addInteraction() for each test to configure the Mock Service
// to act like the Provider
// It also sets up expectations for what requests are to come, and will fail
// if the calls are not seen.
before(() =>
provider.setup().then((opts) => {
// Get a dynamic port from the runtime
process.env.API_HOST = `http://127.0.0.1:${opts.port}`;
})
);
// After each individual test (one or more interactions)
// we validate that the correct request came through.
// This ensures what we _expect_ from the provider, is actually
// what we've asked for (and is what gets captured in the contract)
afterEach(() => provider.verify());
// Configure and import consumer API
// Note that we update the API endpoint to point at the Mock Service
const {
createMateForDates,
suggestion,
getAnimalById,
} = require('../consumer');
// Verify service client works as expected.
//
// Note that we don't call the consumer API endpoints directly, but
// use unit-style tests that test the collaborating function behaviour -
// we want to test the function that is calling the external service.
describe('when a call to list all animals from the Animal Service is made', () => {
describe('and the user is not authenticated', () => {
before(() =>
provider.addInteraction({
state: 'is not authenticated',
uponReceiving: 'a request for all animals',
withRequest: {
method: 'GET',
path: '/animals/available',
},
willRespondWith: {
status: 401,
},
})
);
it('returns a 401 unauthorized', () => {
return expect(suggestion(suitor)).to.eventually.be.rejectedWith(
'Unauthorized'
);
});
});
describe('and the user is authenticated', () => {
describe('and there are animals in the database', () => {
before(() =>
provider.addInteraction({
state: 'Has some animals',
uponReceiving: 'a request for all animals',
withRequest: {
method: 'GET',
path: '/animals/available',
headers: {
Authorization: like('Bearer token'),
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: animalListExpectation,
},
})
);
it('returns a list of animals', (done) => {
const suggestedMates = suggestion(suitor);
expect(suggestedMates).to.eventually.have.deep.property(
'suggestions[0].score',
94
);
expect(suggestedMates)
.to.eventually.have.property('suggestions')
.with.lengthOf(MIN_ANIMALS)
.notify(done);
});
});
});
});
describe('when a call to the Animal Service is made to retrieve a single animal by ID', () => {
describe('and there is an animal in the DB with ID 1', () => {
before(() =>
provider.addInteraction({
state: 'Has an animal with ID 1',
uponReceiving: 'a request for an animal with ID 1',
withRequest: {
method: 'GET',
path: term({ generate: '/animals/1', matcher: '/animals/[0-9]+' }),
headers: {
Authorization: like('Bearer token'),
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: animalBodyExpectation,
},
})
);
it('returns the animal', (done) => {
const suggestedMates = getAnimalById(11);
expect(suggestedMates)
.to.eventually.have.deep.property('id', 1)
.notify(done);
});
});
describe('and there no animals in the database', () => {
before(() =>
provider.addInteraction({
state: 'Has no animals',
uponReceiving: 'a request for an animal with ID 100',
withRequest: {
method: 'GET',
path: '/animals/100',
headers: {
Authorization: like('Bearer token'),
},
},
willRespondWith: {
status: 404,
},
})
);
it('returns a 404', (done) => {
// uncomment below to test a failed verify
// const suggestedMates = getAnimalById(123)
const suggestedMates = getAnimalById(100);
expect(suggestedMates).to.eventually.be.a('null').notify(done);
});
});
});
describe('when a call to the Animal Service is made to create a new mate', () => {
before(() =>
provider.addInteraction({
uponReceiving: 'a request to create a new mate',
state: 'is authenticated',
withRequest: {
method: 'POST',
path: '/animals',
body: like(suitor),
headers: {
'Content-Type': 'application/json; charset=utf-8',
Authorization: like('Bearer token'),
},
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
body: like(suitor),
},
})
);
it('creates a new mate', (done) => {
expect(createMateForDates(suitor)).to.eventually.be.fulfilled.notify(
done
);
});
});
// Write pact files
after(() => {
return provider.finalize();
});
});