Skip to content

Commit 57163eb

Browse files
committed
Add support for multiple requests in testAuth function
Some services might have more then one request to auth with the server for the same shield. This commit adds support for those requests in testAuth using the new multipleRequests option.
1 parent 8c38355 commit 57163eb

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

services/test-helpers.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ function fakeJwtToken() {
228228
* @param {object} options.authOverride - Override class auth params.
229229
* @param {object} options.configOverride - Override the config for this test.
230230
* @param {boolean} options.ignoreOpenApiExample - For classes without OpenApi example ignore for usage of override examples only
231+
* @param {boolean} options.multipleRequests - For classes that require multiple requests to complete the test.
231232
* @throws {TypeError} - Throws a TypeError if the input `serviceClass` is not an instance of BaseService,
232233
* or if `serviceClass` is missing authorizedOrigins.
233234
*
@@ -242,8 +243,6 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
242243
)
243244
}
244245

245-
cleanUpNockAfterEach()
246-
247246
const {
248247
contentType,
249248
apiHeaderKey = 'x-api-key',
@@ -255,6 +254,7 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
255254
authOverride,
256255
configOverride,
257256
ignoreOpenApiExample = false,
257+
multipleRequests = false,
258258
} = options
259259
if (contentType && typeof contentType !== 'string') {
260260
throw new TypeError('Invalid contentType: Must be a String.')
@@ -278,6 +278,13 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
278278
if (ignoreOpenApiExample && typeof ignoreOpenApiExample !== 'boolean') {
279279
throw new TypeError('Invalid ignoreOpenApiExample: Must be an Object.')
280280
}
281+
if (multipleRequests && typeof multipleRequests !== 'boolean') {
282+
throw new TypeError('Invalid multipleRequests: Must be an Object.')
283+
}
284+
285+
if (!multipleRequests) {
286+
cleanUpNockAfterEach()
287+
}
281288

282289
const auth = { ...serviceClass.auth, ...authOverride }
283290
const fakeUser = auth.userKey
@@ -321,6 +328,9 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
321328
const scopeArr = []
322329
authOrigins.forEach(authOrigin => {
323330
const scope = nock(authOrigin)
331+
if (multipleRequests) {
332+
scope.persist()
333+
}
324334
scopeArr.push(scope)
325335
switch (authMethod) {
326336
case 'BasicAuth':
@@ -400,6 +410,15 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
400410
),
401411
).to.not.have.property('isError')
402412

413+
// cleapup persistance if we have multiple requests
414+
if (multipleRequests) {
415+
scopeArr.forEach(scope => scope.persist(false))
416+
nock.restore()
417+
nock.cleanAll()
418+
nock.enableNetConnect()
419+
nock.activate()
420+
}
421+
403422
// if we get 'Mocks not yet satisfied' we have redundent authOrigins or we are missing a critical request
404423
scopeArr.forEach(scope => scope.done())
405424
}

0 commit comments

Comments
 (0)