diff --git a/packages/spacecat-shared-utils/src/helpers.js b/packages/spacecat-shared-utils/src/helpers.js new file mode 100644 index 000000000..51d558a78 --- /dev/null +++ b/packages/spacecat-shared-utils/src/helpers.js @@ -0,0 +1,37 @@ +/* + * Copyright 2023 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +import { isString } from './functions.js'; + +/** + * Resolves the name of the secret based on the function version. + * @param {Object} opts - The options object, not used in this implementation. + * @param {Object} ctx - The context object containing the function version. + * @param {string} defaultPath - The default path for the secret. + * @returns {string} - The resolved secret name. + */ +const resolveSecretsName = (opts, ctx, defaultPath) => { + const funcVersion = ctx?.func?.version; + + if (!isString(funcVersion)) { + throw new Error('Invalid context: func.version is required and must be a string'); + } + if (!isString(defaultPath)) { + throw new Error('Invalid defaultPath: must be a string'); + } + + return `${defaultPath}/${funcVersion}`; +}; + +export { + resolveSecretsName, +}; diff --git a/packages/spacecat-shared-utils/src/index.js b/packages/spacecat-shared-utils/src/index.js index 517615578..4c570913a 100644 --- a/packages/spacecat-shared-utils/src/index.js +++ b/packages/spacecat-shared-utils/src/index.js @@ -25,3 +25,5 @@ export { toBoolean, isValidUrl, } from './functions.js'; + +export { resolveSecretsName } from './helpers.js'; diff --git a/packages/spacecat-shared-utils/test/helpers.test.js b/packages/spacecat-shared-utils/test/helpers.test.js new file mode 100644 index 000000000..5828761b6 --- /dev/null +++ b/packages/spacecat-shared-utils/test/helpers.test.js @@ -0,0 +1,44 @@ +/* + * Copyright 2023 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ + +/* eslint-env mocha */ + +import { expect } from 'chai'; + +import { resolveSecretsName } from '../src/helpers.js'; + +describe('resolveSecretsName', () => { + it('resolves name correctly with valid inputs', () => { + const ctx = { func: { version: '1.0.0' } }; + const defaultPath = 'secretPath'; + expect(resolveSecretsName({}, ctx, defaultPath)).to.equal('secretPath/1.0.0'); + }); + + it('throws error when ctx is undefined', () => { + expect(() => resolveSecretsName({}, undefined, 'defaultPath')).to.throw('Invalid context: func.version is required and must be a string'); + }); + + it('throws error when ctx.func is undefined', () => { + const ctx = {}; + expect(() => resolveSecretsName({}, ctx, 'defaultPath')).to.throw('Invalid context: func.version is required and must be a string'); + }); + + it('throws error when ctx.func.version is not a string', () => { + const ctx = { func: { version: null } }; + expect(() => resolveSecretsName({}, ctx, 'defaultPath')).to.throw('Invalid context: func.version is required and must be a string'); + }); + + it('throws error when defaultPath is not a string', () => { + const ctx = { func: { version: '1.0.0' } }; + expect(() => resolveSecretsName({}, ctx, null)).to.throw('Invalid defaultPath: must be a string'); + }); +}); diff --git a/packages/spacecat-shared-utils/test/index.test.js b/packages/spacecat-shared-utils/test/index.test.js index 6b2231096..ef0eb59ae 100644 --- a/packages/spacecat-shared-utils/test/index.test.js +++ b/packages/spacecat-shared-utils/test/index.test.js @@ -30,6 +30,7 @@ describe('Index Exports', () => { 'isString', 'toBoolean', 'isValidUrl', + 'resolveSecretsName', ]; it('exports all expected functions', () => {