From 8cdcd398f6a7d5639b363fc5026002a51c4ebac5 Mon Sep 17 00:00:00 2001 From: moritzraho Date: Thu, 21 Aug 2025 14:59:05 +0200 Subject: [PATCH 1/2] invalidate cache --- src/commands/app/deploy.js | 4 +-- src/commands/app/undeploy.js | 4 +-- src/lib/auth-helper.js | 19 +++++++++- test/commands/app/deploy.test.js | 2 ++ test/commands/app/undeploy.test.js | 2 ++ test/commands/lib/auth-helper.test.js | 51 ++++++++++++++++++++++++++- 6 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 84b400f9..29bba340 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -26,7 +26,7 @@ const { const rtLib = require('@adobe/aio-lib-runtime') const LogForwarding = require('../../lib/log-forwarding') const { sendAppAssetsDeployedAuditLog, sendAppDeployAuditLog } = require('../../lib/audit-logger') -const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper') +const { setRuntimeApiHostAndAuthHandler, getAccessToken, setCDNApiHostAndAuthHandler } = require('../../lib/auth-helper') const logActions = require('../../lib/log-actions') const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg' @@ -130,7 +130,7 @@ class Deploy extends BuildCommand { // - break into smaller pieces deploy, allowing to first deploy all actions then all web assets for (let i = 0; i < keys.length; ++i) { const k = keys[i] - const v = setRuntimeApiHostAndAuthHandler(values[i]) + const v = setCDNApiHostAndAuthHandler(setRuntimeApiHostAndAuthHandler(values[i])) await this.deploySingleConfig({ name: k, config: v, originalConfig: values[i], flags, spinner }) if (cliDetails?.accessToken && v.app.hasFrontend && flags['web-assets']) { diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index d1609b78..704b7324 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -20,7 +20,7 @@ const webLib = require('@adobe/aio-lib-web') const { runInProcess, buildExtensionPointPayloadWoMetadata } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') const { sendAppAssetsUndeployedAuditLog, sendAppUndeployAuditLog } = require('../../lib/audit-logger') -const { setRuntimeApiHostAndAuthHandler, getAccessToken } = require('../../lib/auth-helper') +const { setCDNApiHostAndAuthHandler, getAccessToken, setRuntimeApiHostAndAuthHandler } = require('../../lib/auth-helper') class Undeploy extends BaseCommand { async run () { @@ -80,7 +80,7 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] // TODO: remove this check once the deploy service is enabled by default - const v = setRuntimeApiHostAndAuthHandler(values[i]) + const v = setCDNApiHostAndAuthHandler(setRuntimeApiHostAndAuthHandler(values[i])) await this.undeployOneExt(k, v, flags, spinner) if (cliDetails?.accessToken) { diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index 23357fe2..598766c7 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -90,8 +90,25 @@ const setRuntimeApiHostAndAuthHandler = (_config) => { } } +const setCDNApiHostAndAuthHandler = (_config) => { + const env = getCliEnv() + let apiEndpoint = DEPLOY_SERVICE_ENDPOINTS[env] ?? DEPLOY_SERVICE_ENDPOINTS.prod + if (process.env.AIO_DEPLOY_SERVICE_URL) { + apiEndpoint = process.env.AIO_DEPLOY_SERVICE_URL + } + + const config = structuredClone(_config) + if (config.web) { + config.web.apihost = `${apiEndpoint}/cdn-api` + config.web.namespace = config.ow.namespace + config.web.auth_handler = bearerAuthHandler + } + return config // always return the config (in case there is no web config) +} + module.exports = { getAccessToken, bearerAuthHandler, - setRuntimeApiHostAndAuthHandler + setRuntimeApiHostAndAuthHandler, + setCDNApiHostAndAuthHandler } diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index c6b7ada3..db819349 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -192,6 +192,8 @@ beforeEach(() => { }) authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig) + // New deploy flow also applies CDN auth/host; make it a pass-through in tests + authHelper.setCDNApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig) authHelper.getAccessToken.mockImplementation(() => { return { accessToken: 'mocktoken', diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index 7bdebeca..d55ca9c2 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -95,6 +95,8 @@ beforeEach(() => { } }) authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) + // New undeploy flow also applies CDN auth/host; make it a pass-through in tests + authHelper.setCDNApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) jest.clearAllMocks() }) diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index 4580fc5a..e5b52bcd 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -1,4 +1,4 @@ -const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler } = require('../../../src/lib/auth-helper') +const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler, setCDNApiHostAndAuthHandler } = require('../../../src/lib/auth-helper') const { getToken, context } = require('@adobe/aio-lib-ims') const { CLI } = require('@adobe/aio-lib-ims/src/context') const { getCliEnv } = require('@adobe/aio-lib-env') @@ -143,3 +143,52 @@ describe('setRuntimeApiHostAndAuthHandler', () => { expect(result).not.toBeDefined() }) }) + +describe('setCDNApiHostAndAuthHandler', () => { + const DEPLOY_SERVICE_ENDPOINTS = { + prod: 'https://deploy-service.app-builder.adp.adobe.io', + stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io' + } + + beforeEach(() => { + jest.clearAllMocks() + delete process.env.AIO_DEPLOY_SERVICE_URL + }) + + test('should set web.apihost and web.auth_handler and preserve namespace from ow', () => { + const mockEnv = 'stage' + getCliEnv.mockReturnValue(mockEnv) + + const config = { web: {}, ow: { namespace: 'ns' } } + const result = setCDNApiHostAndAuthHandler(config) + + expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/cdn-api`) + expect(result.web.auth_handler).toBe(bearerAuthHandler) + expect(result.web.namespace).toBe('ns') + }) + + test('should be a no-op if config has no web key', () => { + const mockEnv = 'prod' + getCliEnv.mockReturnValue(mockEnv) + const input = { ow: { namespace: 'ns' }, other: {} } + const result = setCDNApiHostAndAuthHandler(input) + expect(result).toEqual(input) + }) + + test('should use custom deploy service URL from environment', () => { + const customUrl = 'https://custom-deploy-service.example.com' + process.env.AIO_DEPLOY_SERVICE_URL = customUrl + getCliEnv.mockReturnValue('prod') + + const config = { web: {}, ow: { namespace: 'ns' } } + const result = setCDNApiHostAndAuthHandler(config) + expect(result.web.apihost).toBe(`${customUrl}/cdn-api`) + }) + + test('falls back to prod endpoint for unknown env', () => { + getCliEnv.mockReturnValue('mystery-env') + const config = { web: {}, ow: { namespace: 'ns' } } + const result = setCDNApiHostAndAuthHandler(config) + expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/cdn-api`) + }) +}) From daf642604d4ecc7b4f5afa9b49af3c23f20d7d98 Mon Sep 17 00:00:00 2001 From: moritzraho Date: Tue, 9 Sep 2025 19:28:52 +0200 Subject: [PATCH 2/2] fix clone error with function (double clone) --- src/commands/app/config/get/log-forwarding.js | 4 +- .../app/config/get/log-forwarding/errors.js | 4 +- src/commands/app/config/set/log-forwarding.js | 4 +- src/commands/app/deploy.js | 6 +- src/commands/app/undeploy.js | 4 +- src/lib/auth-helper.js | 36 +++++------ .../app/config/set/log-forwarding.test.js | 10 ++-- test/commands/app/deploy.test.js | 4 +- test/commands/app/undeploy.test.js | 4 +- test/commands/lib/auth-helper.test.js | 59 ++++++++----------- 10 files changed, 59 insertions(+), 76 deletions(-) diff --git a/src/commands/app/config/get/log-forwarding.js b/src/commands/app/config/get/log-forwarding.js index 7962e458..c850b467 100644 --- a/src/commands/app/config/get/log-forwarding.js +++ b/src/commands/app/config/get/log-forwarding.js @@ -11,12 +11,12 @@ governing permissions and limitations under the License. const BaseCommand = require('../../../../BaseCommand') const LogForwarding = require('../../../../lib/log-forwarding') -const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper') +const { setAuthHandler } = require('../../../../lib/auth-helper') class LogForwardingCommand extends BaseCommand { async run () { let aioConfig = (await this.getFullConfig()).aio - aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig) + aioConfig = setAuthHandler(aioConfig) const lf = await LogForwarding.init(aioConfig) const localConfig = lf.getLocalConfig() diff --git a/src/commands/app/config/get/log-forwarding/errors.js b/src/commands/app/config/get/log-forwarding/errors.js index f35ab16d..5dcaa1af 100644 --- a/src/commands/app/config/get/log-forwarding/errors.js +++ b/src/commands/app/config/get/log-forwarding/errors.js @@ -12,7 +12,7 @@ governing permissions and limitations under the License. const BaseCommand = require('../../../../../BaseCommand') const rtLib = require('@adobe/aio-lib-runtime') const ora = require('ora') -const { setRuntimeApiHostAndAuthHandler } = require('../../../../../lib/auth-helper') +const { setAuthHandler } = require('../../../../../lib/auth-helper') class ErrorsCommand extends BaseCommand { async run () { @@ -32,7 +32,7 @@ class ErrorsCommand extends BaseCommand { async getLogForwarding () { let aioConfig = (await this.getFullConfig()).aio - aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig) + aioConfig = setAuthHandler(aioConfig) const runtimeConfig = aioConfig.runtime rtLib.utils.checkOpenWhiskCredentials({ ow: runtimeConfig }) diff --git a/src/commands/app/config/set/log-forwarding.js b/src/commands/app/config/set/log-forwarding.js index b64de221..9525ea0f 100644 --- a/src/commands/app/config/set/log-forwarding.js +++ b/src/commands/app/config/set/log-forwarding.js @@ -12,12 +12,12 @@ governing permissions and limitations under the License. const BaseCommand = require('../../../../BaseCommand') const LogForwarding = require('../../../../lib/log-forwarding') const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lf:set', { provider: 'debug' }) -const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper') +const { setAuthHandler } = require('../../../../lib/auth-helper') class LogForwardingCommand extends BaseCommand { async run () { let aioConfig = (await this.getFullConfig()).aio - aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig) + aioConfig = setAuthHandler(aioConfig) const lf = await LogForwarding.init(aioConfig) const destination = await this.promptDestination(lf.getSupportedDestinations()) diff --git a/src/commands/app/deploy.js b/src/commands/app/deploy.js index 29bba340..7ecf87c8 100644 --- a/src/commands/app/deploy.js +++ b/src/commands/app/deploy.js @@ -26,7 +26,7 @@ const { const rtLib = require('@adobe/aio-lib-runtime') const LogForwarding = require('../../lib/log-forwarding') const { sendAppAssetsDeployedAuditLog, sendAppDeployAuditLog } = require('../../lib/audit-logger') -const { setRuntimeApiHostAndAuthHandler, getAccessToken, setCDNApiHostAndAuthHandler } = require('../../lib/auth-helper') +const { getAccessToken, setAuthHandler } = require('../../lib/auth-helper') const logActions = require('../../lib/log-actions') const PRE_DEPLOY_EVENT_REG = 'pre-deploy-event-reg' @@ -88,7 +88,7 @@ class Deploy extends BuildCommand { if (aioConfig?.project?.workspace && flags['log-forwarding-update'] && flags.actions) { spinner.start('Updating log forwarding configuration') try { - const lfConfig = setRuntimeApiHostAndAuthHandler(aioConfig) + const lfConfig = setAuthHandler(aioConfig) const lf = await LogForwarding.init(lfConfig) if (lf.isLocalConfigChanged()) { @@ -130,7 +130,7 @@ class Deploy extends BuildCommand { // - break into smaller pieces deploy, allowing to first deploy all actions then all web assets for (let i = 0; i < keys.length; ++i) { const k = keys[i] - const v = setCDNApiHostAndAuthHandler(setRuntimeApiHostAndAuthHandler(values[i])) + const v = setAuthHandler(values[i]) await this.deploySingleConfig({ name: k, config: v, originalConfig: values[i], flags, spinner }) if (cliDetails?.accessToken && v.app.hasFrontend && flags['web-assets']) { diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 704b7324..8027f046 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -20,7 +20,7 @@ const webLib = require('@adobe/aio-lib-web') const { runInProcess, buildExtensionPointPayloadWoMetadata } = require('../../lib/app-helper') const rtLib = require('@adobe/aio-lib-runtime') const { sendAppAssetsUndeployedAuditLog, sendAppUndeployAuditLog } = require('../../lib/audit-logger') -const { setCDNApiHostAndAuthHandler, getAccessToken, setRuntimeApiHostAndAuthHandler } = require('../../lib/auth-helper') +const { getAccessToken, setAuthHandler } = require('../../lib/auth-helper') class Undeploy extends BaseCommand { async run () { @@ -80,7 +80,7 @@ class Undeploy extends BaseCommand { for (let i = 0; i < keys.length; ++i) { const k = keys[i] // TODO: remove this check once the deploy service is enabled by default - const v = setCDNApiHostAndAuthHandler(setRuntimeApiHostAndAuthHandler(values[i])) + const v = setAuthHandler(values[i]) await this.undeployOneExt(k, v, flags, spinner) if (cliDetails?.accessToken) { diff --git a/src/lib/auth-helper.js b/src/lib/auth-helper.js index 598766c7..1114d229 100644 --- a/src/lib/auth-helper.js +++ b/src/lib/auth-helper.js @@ -68,7 +68,11 @@ const bearerAuthHandler = { } } -const setRuntimeApiHostAndAuthHandler = (_config) => { +const setAuthHandler = (_config) => { + if (!_config || (!_config.runtime && !_config.ow && !_config.web)) { + return + } + const env = getCliEnv() let apiEndpoint = DEPLOY_SERVICE_ENDPOINTS[env] ?? DEPLOY_SERVICE_ENDPOINTS.prod if (process.env.AIO_DEPLOY_SERVICE_URL) { @@ -76,39 +80,31 @@ const setRuntimeApiHostAndAuthHandler = (_config) => { } const config = structuredClone(_config) + // config is .aio const aioConfig = (config && 'runtime' in config) ? config : null - if (aioConfig) { + if (config?.runtime) { aioConfig.runtime.apihost = `${apiEndpoint}/runtime` aioConfig.runtime.auth_handler = bearerAuthHandler return aioConfig } - const owConfig = (config && 'ow' in config) ? config : null - if (owConfig) { - owConfig.ow.apihost = `${apiEndpoint}/runtime` - owConfig.ow.auth_handler = bearerAuthHandler - return owConfig - } -} -const setCDNApiHostAndAuthHandler = (_config) => { - const env = getCliEnv() - let apiEndpoint = DEPLOY_SERVICE_ENDPOINTS[env] ?? DEPLOY_SERVICE_ENDPOINTS.prod - if (process.env.AIO_DEPLOY_SERVICE_URL) { - apiEndpoint = process.env.AIO_DEPLOY_SERVICE_URL + if (config?.ow) { + config.ow.apihost = `${apiEndpoint}/runtime` + config.ow.auth_handler = bearerAuthHandler } - const config = structuredClone(_config) - if (config.web) { + if (config?.web) { config.web.apihost = `${apiEndpoint}/cdn-api` - config.web.namespace = config.ow.namespace + config.web.namespace = config.ow?.namespace // for now we can only set the web.namespace if the ow.namespace is set config.web.auth_handler = bearerAuthHandler } - return config // always return the config (in case there is no web config) + + return config + // note we can't structuredClone the config from now on because the config contains functions } module.exports = { getAccessToken, bearerAuthHandler, - setRuntimeApiHostAndAuthHandler, - setCDNApiHostAndAuthHandler + setAuthHandler } diff --git a/test/commands/app/config/set/log-forwarding.test.js b/test/commands/app/config/set/log-forwarding.test.js index 6f43dac9..826e11c0 100644 --- a/test/commands/app/config/set/log-forwarding.test.js +++ b/test/commands/app/config/set/log-forwarding.test.js @@ -27,7 +27,7 @@ jest.mock('../../../../../src/lib/log-forwarding', () => { let command, lf beforeEach(async () => { - authHelper.setRuntimeApiHostAndAuthHandler.mockClear() + authHelper.setAuthHandler.mockClear() command = new TheCommand([]) command.appConfig = { @@ -51,7 +51,7 @@ beforeEach(async () => { getConfigFromJson: jest.fn() } LogForwarding.init.mockResolvedValue(lf) - authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) + authHelper.setAuthHandler.mockImplementation(aioConfig => aioConfig) }) test('set log forwarding destination and save local', async () => { @@ -92,10 +92,10 @@ test('set log forwarding destination and save local', async () => { expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input)) expect(localSetCall).toHaveBeenCalledTimes(1) expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings)) - expect(authHelper.setRuntimeApiHostAndAuthHandler).toHaveBeenCalledTimes(1) + expect(authHelper.setAuthHandler).toHaveBeenCalledTimes(1) }) -test('should invoke setRuntimeApiHostAndAuthHandler and set log forwarding destination', async () => { +test('should invoke setAuthHandler and set log forwarding destination', async () => { const destination = 'destination' const input = { field_one: 'val_one', @@ -133,7 +133,7 @@ test('should invoke setRuntimeApiHostAndAuthHandler and set log forwarding desti expect(setCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, input)) expect(localSetCall).toHaveBeenCalledTimes(1) expect(localSetCall).toHaveBeenCalledWith(new LogForwarding.LogForwardingConfig(destination, fullSanitizedSettings)) - expect(authHelper.setRuntimeApiHostAndAuthHandler).toHaveBeenCalledTimes(1) + expect(authHelper.setAuthHandler).toHaveBeenCalledTimes(1) }) test('set log forwarding destination and fail save local', async () => { diff --git a/test/commands/app/deploy.test.js b/test/commands/app/deploy.test.js index db819349..46d54459 100644 --- a/test/commands/app/deploy.test.js +++ b/test/commands/app/deploy.test.js @@ -191,9 +191,7 @@ beforeEach(() => { return entities }) - authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig) - // New deploy flow also applies CDN auth/host; make it a pass-through in tests - authHelper.setCDNApiHostAndAuthHandler.mockImplementation((aioConfig) => aioConfig) + authHelper.setAuthHandler.mockImplementation((aioConfig) => aioConfig) authHelper.getAccessToken.mockImplementation(() => { return { accessToken: 'mocktoken', diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index d55ca9c2..82048d48 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -94,9 +94,7 @@ beforeEach(() => { env: 'stage' } }) - authHelper.setRuntimeApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) - // New undeploy flow also applies CDN auth/host; make it a pass-through in tests - authHelper.setCDNApiHostAndAuthHandler.mockImplementation(aioConfig => aioConfig) + authHelper.setAuthHandler.mockImplementation(aioConfig => aioConfig) jest.clearAllMocks() }) diff --git a/test/commands/lib/auth-helper.test.js b/test/commands/lib/auth-helper.test.js index e5b52bcd..70cb98a1 100644 --- a/test/commands/lib/auth-helper.test.js +++ b/test/commands/lib/auth-helper.test.js @@ -1,4 +1,4 @@ -const { getAccessToken, bearerAuthHandler, setRuntimeApiHostAndAuthHandler, setCDNApiHostAndAuthHandler } = require('../../../src/lib/auth-helper') +const { getAccessToken, bearerAuthHandler, setAuthHandler } = require('../../../src/lib/auth-helper') const { getToken, context } = require('@adobe/aio-lib-ims') const { CLI } = require('@adobe/aio-lib-ims/src/context') const { getCliEnv } = require('@adobe/aio-lib-env') @@ -74,7 +74,7 @@ describe('bearerAuthHandler', () => { }) }) -describe('setRuntimeApiHostAndAuthHandler', () => { +describe('setAuthHandler', () => { const DEPLOY_SERVICE_ENDPOINTS = { prod: 'https://deploy-service.app-builder.adp.adobe.io', stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io' @@ -92,7 +92,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { getCliEnv.mockReturnValue(mockEnv) const config = { runtime: {} } - const result = setRuntimeApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`) expect(result.runtime.auth_handler).toBe(bearerAuthHandler) @@ -102,7 +102,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { getCliEnv.mockReturnValue(mockEnv) const config = { runtime: {} } - const result = setRuntimeApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.runtime.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/runtime`) expect(result.runtime.auth_handler).toBe(bearerAuthHandler) @@ -114,7 +114,7 @@ describe('setRuntimeApiHostAndAuthHandler', () => { getCliEnv.mockReturnValue(mockEnv) const config = { ow: {} } - const result = setRuntimeApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.ow.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/runtime`) expect(result.ow.auth_handler).toBe(bearerAuthHandler) @@ -127,68 +127,59 @@ describe('setRuntimeApiHostAndAuthHandler', () => { const customUrl = 'https://custom-deploy-service.example.com' process.env.AIO_DEPLOY_SERVICE_URL = customUrl const config = { runtime: {} } - const result = setRuntimeApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.runtime.apihost).toBe(`${customUrl}/runtime`) }) test('should return undefined when config is null or undefined', () => { - expect(setRuntimeApiHostAndAuthHandler(null)).not.toBeDefined() - expect(setRuntimeApiHostAndAuthHandler()).not.toBeDefined() + expect(setAuthHandler(null)).not.toBeDefined() + expect(setAuthHandler()).not.toBeDefined() }) - test('should return undefined when config has neither runtime nor ow', () => { + test('should return undefined when config has neither runtime nor ow nor web', () => { const config = { other: {} } - const result = setRuntimeApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result).not.toBeDefined() }) -}) - -describe('setCDNApiHostAndAuthHandler', () => { - const DEPLOY_SERVICE_ENDPOINTS = { - prod: 'https://deploy-service.app-builder.adp.adobe.io', - stage: 'https://deploy-service.stg.app-builder.corp.adp.adobe.io' - } - - beforeEach(() => { - jest.clearAllMocks() - delete process.env.AIO_DEPLOY_SERVICE_URL - }) test('should set web.apihost and web.auth_handler and preserve namespace from ow', () => { const mockEnv = 'stage' getCliEnv.mockReturnValue(mockEnv) const config = { web: {}, ow: { namespace: 'ns' } } - const result = setCDNApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS[mockEnv]}/cdn-api`) expect(result.web.auth_handler).toBe(bearerAuthHandler) expect(result.web.namespace).toBe('ns') }) - test('should be a no-op if config has no web key', () => { - const mockEnv = 'prod' - getCliEnv.mockReturnValue(mockEnv) - const input = { ow: { namespace: 'ns' }, other: {} } - const result = setCDNApiHostAndAuthHandler(input) - expect(result).toEqual(input) - }) - - test('should use custom deploy service URL from environment', () => { + test('should use custom deploy service URL from environment for web', () => { const customUrl = 'https://custom-deploy-service.example.com' process.env.AIO_DEPLOY_SERVICE_URL = customUrl getCliEnv.mockReturnValue('prod') const config = { web: {}, ow: { namespace: 'ns' } } - const result = setCDNApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.web.apihost).toBe(`${customUrl}/cdn-api`) + expect(result.web.auth_handler).toBe(bearerAuthHandler) + expect(result.web.namespace).toBe('ns') }) test('falls back to prod endpoint for unknown env', () => { getCliEnv.mockReturnValue('mystery-env') const config = { web: {}, ow: { namespace: 'ns' } } - const result = setCDNApiHostAndAuthHandler(config) + const result = setAuthHandler(config) expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/cdn-api`) + expect(result.web.auth_handler).toBe(bearerAuthHandler) + expect(result.web.namespace).toBe('ns') + }) + test('web.namespace=undefined if ow.namespace is not set', () => { + const config = { web: {} } + const result = setAuthHandler(config) + expect(result.web.namespace).toBe(undefined) + expect(result.web.apihost).toBe(`${DEPLOY_SERVICE_ENDPOINTS.prod}/cdn-api`) + expect(result.web.auth_handler).toBe(bearerAuthHandler) }) })