Skip to content

Commit

Permalink
Add configOverride to testAuth
Browse files Browse the repository at this point in the history
Some tests might require non default config for cases where these are set.
This allows creating tests for those cases.
  • Loading branch information
jNullj committed Feb 24, 2024
1 parent f156762 commit cd6c65b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions services/test-helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _ from 'lodash'
import dayjs from 'dayjs'
import { expect } from 'chai'
import nock from 'nock'
Expand Down Expand Up @@ -156,6 +157,7 @@ function generateFakeConfig(
*
* @param {BaseService} serviceClass The service class to find the authorized origins.
* @param {object} authOverride Return result with overrid params.
* @param {object} configOverride - Override the config.
* @throws {TypeError} - Throws a TypeError if the input `serviceClass` is not an instance of BaseService.
* @returns {string} First auth origin found.
*
Expand All @@ -164,7 +166,7 @@ function generateFakeConfig(
* getServiceClassAuthOrigin(Obs)
* // outputs 'https://api.opensuse.org'
*/
function getServiceClassAuthOrigin(serviceClass, authOverride) {
function getServiceClassAuthOrigin(serviceClass, authOverride, configOverride) {
if (
!serviceClass ||
!serviceClass.prototype ||
Expand All @@ -178,7 +180,8 @@ function getServiceClassAuthOrigin(serviceClass, authOverride) {
if (auth.authorizedOrigins) {
return serviceClass.auth.authorizedOrigins
} else {
return [config.public.services[auth.serviceKey].authorizedOrigins]
const mergedConfig = _.merge(runnerConfig, configOverride)
return [mergedConfig.public.services[auth.serviceKey].authorizedOrigins]
}
}

Expand Down Expand Up @@ -212,6 +215,7 @@ function fakeJwtToken() {
* @param {string} options.jwtLoginEndpoint - jwtAuth Login endpoint.
* @param {object} options.exampleOverride - Override example params in test.
* @param {object} options.authOverride - Override class auth params.
* @param {object} options.configOverride - Override the config for this test.
* @throws {TypeError} - Throws a TypeError if the input `serviceClass` is not an instance of BaseService,
* or if `serviceClass` is missing authorizedOrigins.
*
Expand All @@ -237,6 +241,7 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
jwtLoginEndpoint,
exampleOverride = {},
authOverride,
configOverride,
} = options
if (contentType && typeof contentType !== 'string') {
throw new TypeError('Invalid contentType: Must be a String.')
Expand All @@ -254,11 +259,18 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
if (authOverride && typeof authOverride !== 'object') {
throw new TypeError('Invalid authOverride: Must be an Object.')
}
if (configOverride && typeof configOverride !== 'object') {
throw new TypeError('Invalid configOverride: Must be an Object.')
}

const auth = { ...serviceClass.auth, ...authOverride }
const fakeUser = auth.userKey ? 'fake-user' : undefined
const fakeSecret = 'fake-secret'
const authOrigins = getServiceClassAuthOrigin(serviceClass, authOverride)
const authOrigins = getServiceClassAuthOrigin(
serviceClass,
authOverride,
configOverride,
)
const config = generateFakeConfig(
serviceClass,
fakeSecret,
Expand Down Expand Up @@ -344,10 +356,14 @@ async function testAuth(serviceClass, authMethod, dummyResponse, options = {}) {
})

expect(
await serviceClass.invoke(defaultContext, config, {
...exampleInvokeParams,
...exampleOverride,
}),
await serviceClass.invoke(
defaultContext,
_.merge(config, configOverride),
{
...exampleInvokeParams,
...exampleOverride,
},
),
).to.not.have.property('isError')

// if we get 'Mocks not yet satisfied' we have redundent authOrigins or we are missing a critical request
Expand Down

0 comments on commit cd6c65b

Please sign in to comment.