Skip to content

Commit fb6cd5c

Browse files
CorieWtaeold
andauthored
feat: add SecretManager mocking (#265)
* feat: add SecretManager mocking * chore: format * Respond to comments. * feat(tests): add secretmanager tests * chore: format --------- Co-authored-by: Daniel Lee <[email protected]>
1 parent c38afad commit fb6cd5c

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

spec/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ describe('index', () => {
6767

6868
import './lifecycle.spec';
6969
import './main.spec';
70+
import './secretmanager.spec';
7071
import './v2.spec';
7172
import './cloudevent/generate';
7273
import './app.spec';

spec/secretmanager.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { expect } from 'chai';
2+
import { mockSecretManager } from '../src/secretManager';
3+
4+
describe('mockSecretManager', () => {
5+
let originalEnv;
6+
7+
before(() => {
8+
// Capture the original environment variables
9+
originalEnv = { ...process.env };
10+
});
11+
12+
afterEach(() => {
13+
// Reset any mutations made by the test run
14+
process.env = { ...originalEnv };
15+
});
16+
17+
it('applies each key/value pair to process.env', () => {
18+
const conf = { FOO: 'bar', BAZ: 'qux' };
19+
20+
mockSecretManager(conf);
21+
22+
expect(process.env.FOO).to.equal('bar');
23+
expect(process.env.BAZ).to.equal('qux');
24+
});
25+
26+
it('overwrites an existing variable with the new value', () => {
27+
process.env.EXISTING = 'old';
28+
const conf = { EXISTING: 'new' };
29+
30+
mockSecretManager(conf);
31+
32+
expect(process.env.EXISTING).to.equal('new');
33+
});
34+
35+
it('supports non-string values (coerced to string)', () => {
36+
const conf: Record<string, string> = {
37+
NUM_VALUE: '123',
38+
BOOL_VALUE: 'true',
39+
};
40+
41+
mockSecretManager(conf);
42+
43+
expect(process.env.NUM_VALUE).to.equal('123');
44+
expect(process.env.BOOL_VALUE).to.equal('true');
45+
});
46+
});

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type HttpsFunctionOrCloudFunctionV1<T, U> = U extends HttpsFunction &
4545
? HttpsFunction & Runnable<T>
4646
: CloudFunctionV1<T>;
4747

48+
export { mockSecretManager } from './secretManager';
49+
4850
// Re-exporting V1 (to reduce breakage)
4951
export {
5052
ContextOptions,

src/secretManager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** Mock values returned by `functions.config()`. */
2+
export function mockSecretManager(conf: Record<string, string>) {
3+
for (const [key, value] of Object.entries(conf)) {
4+
process.env[key] = value;
5+
}
6+
}

0 commit comments

Comments
 (0)