Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/spacecat-shared-utils/src/helpers.js
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 2 additions & 0 deletions packages/spacecat-shared-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export {
toBoolean,
isValidUrl,
} from './functions.js';

export { resolveSecretsName } from './helpers.js';
44 changes: 44 additions & 0 deletions packages/spacecat-shared-utils/test/helpers.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
1 change: 1 addition & 0 deletions packages/spacecat-shared-utils/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('Index Exports', () => {
'isString',
'toBoolean',
'isValidUrl',
'resolveSecretsName',
];

it('exports all expected functions', () => {
Expand Down