Skip to content

feat: add SecretManager mocking #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('index', () => {

import './lifecycle.spec';
import './main.spec';
import './secretmanager.spec';
import './v2.spec';
import './cloudevent/generate';
import './app.spec';
Expand Down
46 changes: 46 additions & 0 deletions spec/secretmanager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect } from 'chai';
import { mockSecretManager } from '../src/secretManager';

describe('mockSecretManager', () => {
let originalEnv;

before(() => {
// Capture the original environment variables
originalEnv = { ...process.env };
});

afterEach(() => {
// Reset any mutations made by the test run
process.env = { ...originalEnv };
});

it('applies each key/value pair to process.env', () => {
const conf = { FOO: 'bar', BAZ: 'qux' };

mockSecretManager(conf);

expect(process.env.FOO).to.equal('bar');
expect(process.env.BAZ).to.equal('qux');
});

it('overwrites an existing variable with the new value', () => {
process.env.EXISTING = 'old';
const conf = { EXISTING: 'new' };

mockSecretManager(conf);

expect(process.env.EXISTING).to.equal('new');
});

it('supports non-string values (coerced to string)', () => {
const conf: Record<string, string> = {
NUM_VALUE: '123',
BOOL_VALUE: 'true',
};

mockSecretManager(conf);

expect(process.env.NUM_VALUE).to.equal('123');
expect(process.env.BOOL_VALUE).to.equal('true');
});
});
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type HttpsFunctionOrCloudFunctionV1<T, U> = U extends HttpsFunction &
? HttpsFunction & Runnable<T>
: CloudFunctionV1<T>;

export { mockSecretManager } from './secretManager';

// Re-exporting V1 (to reduce breakage)
export {
ContextOptions,
Expand Down
6 changes: 6 additions & 0 deletions src/secretManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Mock values returned by `functions.config()`. */
export function mockSecretManager(conf: Record<string, string>) {
for (const [key, value] of Object.entries(conf)) {
process.env[key] = value;
}
}