-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathconfig.spec.ts
467 lines (384 loc) · 16 KB
/
config.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import { expect } from "chai";
import * as sinon from "sinon";
import * as yaml from "yaml";
import * as path from "path";
import * as fsImport from "../fsutils";
import * as csmImport from "../gcp/secretManager";
import * as promptImport from "../prompt";
import * as dialogs from "./secrets/dialogs";
import * as config from "./config";
import { NodeType } from "yaml/dist/nodes/Node";
import { AppHostingYamlConfig, toEnvList } from "./yaml";
import { FirebaseError } from "../error";
describe("config", () => {
describe("discoverBackendRoot", () => {
let fs: sinon.SinonStubbedInstance<typeof fsImport>;
beforeEach(() => {
fs = sinon.stub(fsImport);
});
afterEach(() => {
sinon.verifyAndRestore();
});
it("finds apphosting.yaml at cwd", () => {
fs.listFiles.withArgs("/parent/cwd").returns(["apphosting.yaml"]);
expect(config.discoverBackendRoot("/parent/cwd")).equals("/parent/cwd");
});
it("finds apphosting.yaml in a parent directory", () => {
fs.listFiles.withArgs("/parent/cwd").returns(["random_file.txt"]);
fs.listFiles.withArgs("/parent").returns(["apphosting.yaml"]);
expect(config.discoverBackendRoot("/parent/cwd")).equals("/parent");
});
it("returns null if it finds firebase.json without finding apphosting.yaml", () => {
fs.listFiles.withArgs("/parent/cwd").returns([]);
fs.listFiles.withArgs("/parent").returns(["firebase.json"]);
expect(config.discoverBackendRoot("/parent/cwd")).equals(null);
});
it("returns if it reaches the fs root", () => {
fs.listFiles.withArgs("/parent/cwd").returns([]);
fs.listFiles.withArgs("/parent").returns(["random_file.txt"]);
fs.listFiles.withArgs("/").returns([]);
expect(config.discoverBackendRoot("/parent/cwd")).equals(null);
});
it("discovers backend root from any apphosting yaml file", () => {
fs.listFiles.withArgs("/parent/cwd").returns(["apphosting.staging.yaml"]);
expect(config.discoverBackendRoot("/parent/cwd")).equals("/parent/cwd");
});
});
describe("get/setEnv", () => {
it("sets new envs", () => {
const doc = new yaml.Document<NodeType<config.Config>>();
const env: config.Env = {
variable: "VARIABLE",
value: "value",
};
config.upsertEnv(doc, env);
const envAgain = config.findEnv(doc, env.variable);
expect(envAgain).deep.equals(env);
// Also check raw YAML:
const envs = doc.get("env") as yaml.YAMLSeq<config.Env>;
expect(envs.toJSON()).to.deep.equal([env]);
});
it("overwrites envs", () => {
const doc = new yaml.Document<NodeType<config.Config>>();
const env: config.Env = {
variable: "VARIABLE",
value: "value",
};
const newEnv: config.Env = {
variable: env.variable,
secret: "my-secret",
};
config.upsertEnv(doc, env);
config.upsertEnv(doc, newEnv);
expect(config.findEnv(doc, env.variable)).to.deep.equal(newEnv);
});
it("Preserves comments", () => {
const rawDoc = `
# Run config
runConfig:
# Reserve capacity
minInstances: 1
env:
# Publicly available
- variable: NEXT_PUBLIC_BUCKET
value: mybucket.appspot.com
`.trim();
const expectedAmendments = `
- variable: GOOGLE_API_KEY
secret: api-key
`;
const doc = yaml.parseDocument(rawDoc) as yaml.Document<NodeType<config.Config>>;
config.upsertEnv(doc, {
variable: "GOOGLE_API_KEY",
secret: "api-key",
});
expect(doc.toString()).to.equal(rawDoc + expectedAmendments);
});
});
describe("maybeAddSecretToYaml", () => {
let prompt: sinon.SinonStubbedInstance<typeof promptImport>;
let discoverBackendRoot: sinon.SinonStub;
let load: sinon.SinonStub;
let findEnv: sinon.SinonStub;
let upsertEnv: sinon.SinonStub;
let store: sinon.SinonStub;
let envVarForSecret: sinon.SinonStub;
beforeEach(() => {
prompt = sinon.stub(promptImport);
discoverBackendRoot = sinon.stub(config, "discoverBackendRoot");
load = sinon.stub(config, "load");
findEnv = sinon.stub(config, "findEnv");
upsertEnv = sinon.stub(config, "upsertEnv");
store = sinon.stub(config, "store");
envVarForSecret = sinon.stub(dialogs, "envVarForSecret");
});
afterEach(() => {
sinon.verifyAndRestore();
});
it("noops if the env already exists", async () => {
const doc = yaml.parseDocument("{}");
discoverBackendRoot.returns("CWD");
load.returns(doc);
findEnv.withArgs(doc, "SECRET").returns({ variable: "SECRET", secret: "SECRET" });
await config.maybeAddSecretToYaml("SECRET");
expect(discoverBackendRoot).to.have.been.called;
expect(load).to.have.been.calledWith("CWD/apphosting.yaml");
expect(prompt.confirm).to.not.have.been.called;
expect(prompt.promptOnce).to.not.have.been.called;
});
it("inserts into an existing doc", async () => {
const doc = yaml.parseDocument("{}");
discoverBackendRoot.returns("CWD");
load.withArgs(path.join("CWD", "apphosting.yaml")).returns(doc);
findEnv.withArgs(doc, "SECRET").returns(undefined);
prompt.confirm.resolves(true);
envVarForSecret.resolves("SECRET_VARIABLE");
await config.maybeAddSecretToYaml("SECRET");
expect(discoverBackendRoot).to.have.been.called;
expect(load).to.have.been.calledWith("CWD/apphosting.yaml");
expect(prompt.confirm).to.have.been.calledWithMatch({
message: "Would you like to add this secret to apphosting.yaml?",
default: true,
});
expect(envVarForSecret).to.have.been.calledWith("SECRET");
expect(upsertEnv).to.have.been.calledWithMatch(doc, {
variable: "SECRET_VARIABLE",
secret: "SECRET",
});
expect(store).to.have.been.calledWithMatch(path.join("CWD", "apphosting.yaml"), doc);
expect(prompt.promptOnce).to.not.have.been.called;
});
it("inserts into an new doc", async () => {
const doc = new yaml.Document();
discoverBackendRoot.returns(null);
findEnv.withArgs(doc, "SECRET").returns(undefined);
prompt.confirm.resolves(true);
prompt.promptOnce.resolves("CWD");
envVarForSecret.resolves("SECRET_VARIABLE");
await config.maybeAddSecretToYaml("SECRET");
expect(discoverBackendRoot).to.have.been.called;
expect(load).to.not.have.been.called;
expect(prompt.confirm).to.have.been.calledWithMatch({
message: "Would you like to add this secret to apphosting.yaml?",
default: true,
});
expect(prompt.promptOnce).to.have.been.calledWithMatch({
message:
"It looks like you don't have an apphosting.yaml yet. Where would you like to store it?",
default: process.cwd(),
});
expect(envVarForSecret).to.have.been.calledWith("SECRET");
expect(upsertEnv).to.have.been.calledWithMatch(doc, {
variable: "SECRET_VARIABLE",
secret: "SECRET",
});
expect(store).to.have.been.calledWithMatch(path.join("CWD", "apphosting.yaml"), doc);
});
});
describe("listAppHostingFilesInPath", () => {
let fs: sinon.SinonStubbedInstance<typeof fsImport>;
beforeEach(() => {
fs = sinon.stub(fsImport);
});
afterEach(() => {
sinon.verifyAndRestore();
});
it("only returns valid App Hosting YAML files", () => {
fs.listFiles
.withArgs("/parent/cwd")
.returns([
"test1.js",
"test2.js",
"apphosting.yaml",
"test4.js",
"apphosting.staging.yaml",
]);
const apphostingYamls = config.listAppHostingFilesInPath("/parent/cwd");
expect(apphostingYamls).to.deep.equal([
"/parent/cwd/apphosting.yaml",
"/parent/cwd/apphosting.staging.yaml",
]);
});
});
describe("maybeGenerateEmulatorsYaml", () => {
let discoverBackendRoot: sinon.SinonStub;
let overrideChosenEnv: sinon.SinonStub;
let loadFromFile: sinon.SinonStub;
let store: sinon.SinonStub;
let fs: sinon.SinonStubbedInstance<typeof fsImport>;
let prompt: sinon.SinonStubbedInstance<typeof promptImport>;
const existingYaml = AppHostingYamlConfig.empty();
existingYaml.env = {
VAR: { value: "value" },
API_KEY: { secret: "api-key" },
API_KEY2: { secret: "api-key2" },
};
beforeEach(() => {
discoverBackendRoot = sinon.stub(config, "discoverBackendRoot");
overrideChosenEnv = sinon.stub(config, "overrideChosenEnv");
store = sinon.stub(config, "store");
loadFromFile = sinon.stub(AppHostingYamlConfig, "loadFromFile");
fs = sinon.stub(fsImport);
prompt = sinon.stub(promptImport);
});
afterEach(() => {
sinon.verifyAndRestore();
});
it("noops if emulators.yaml already exists", async () => {
discoverBackendRoot.withArgs("/project").returns("/project");
fs.fileExistsSync.withArgs(`/project/${config.APPHOSTING_EMULATORS_YAML_FILE}`).returns(true);
await config.maybeGenerateEmulatorYaml("projectId", "/project");
expect(prompt.confirm).to.not.have.been.called;
expect(store).to.not.have.been.called;
});
// This allows us to prompt to give devs access to prod keys
it("returns existing config even if the user does not create apphosting.emulator.yaml", async () => {
discoverBackendRoot.withArgs("/project").returns("/project");
fs.fileExistsSync
.withArgs(`/project/${config.APPHOSTING_EMULATORS_YAML_FILE}`)
.returns(false);
// Do not create emulator file
prompt.confirm.resolves(false);
loadFromFile.resolves(existingYaml);
await expect(
config.maybeGenerateEmulatorYaml("projectId", "/project"),
).to.eventually.deep.equal(toEnvList(existingYaml.env));
});
it("returns overwritten config", async () => {
discoverBackendRoot.withArgs("/project").returns("/project");
fs.fileExistsSync
.withArgs(`/project/${config.APPHOSTING_EMULATORS_YAML_FILE}`)
.returns(false);
loadFromFile.resolves(existingYaml);
// Create emulator file
prompt.confirm.resolves(true);
overrideChosenEnv.resolves({
API_KEY2: { secret: "test-api-key2" },
});
store.resolves();
await expect(
config.maybeGenerateEmulatorYaml("projectId", "/project"),
).to.eventually.deep.equal([
{ variable: "VAR", value: "value" },
{ variable: "API_KEY", secret: "api-key" },
{ variable: "API_KEY2", secret: "test-api-key2" },
]);
expect(overrideChosenEnv.firstCall.args[1]).to.deep.equal({
VAR: { value: "value" },
API_KEY: { secret: "api-key" },
API_KEY2: { secret: "api-key2" },
});
expect(store).to.have.been.called;
const emulatorYaml = store.firstCall.args[1] as yaml.Document;
expect(emulatorYaml.toJSON()).to.deep.equal({
env: [{ variable: "API_KEY2", secret: "test-api-key2" }],
});
});
});
describe("overrideChosenEnv", () => {
let csm: sinon.SinonStubbedInstance<typeof csmImport>;
let prompt: sinon.SinonStubbedInstance<typeof promptImport>;
beforeEach(() => {
csm = sinon.stub(csmImport);
prompt = sinon.stub(promptImport);
});
afterEach(() => {
sinon.verifyAndRestore();
});
it("noops with no envs", async () => {
await expect(config.overrideChosenEnv(undefined, {})).to.eventually.deep.equal({});
expect(promptImport.promptOnce).to.not.have.been.called;
expect(csmImport.getSecret).to.not.have.been.called;
});
it("noops with no selected envs", async () => {
const originalEnv: Record<string, Omit<config.Env, "variable">> = {
VARIABLE: { value: "value" },
API_KEY: { secret: "api-key" },
};
prompt.promptOnce.onFirstCall().resolves([]);
await expect(config.overrideChosenEnv(undefined, originalEnv)).to.eventually.deep.equal({});
expect(prompt.promptOnce).to.have.been.calledOnce;
expect(csm.secretExists).to.not.have.been.called;
});
it("can override plaintext values", async () => {
const originalEnv: Record<string, config.Env> = {
VARIABLE: { variable: "VARIABLE", value: "value" },
VARIABLE2: { variable: "VARIABLE2", value: "value2" },
};
prompt.promptOnce.onFirstCall().resolves(["VARIABLE2"]);
prompt.promptOnce.onSecondCall().resolves("new-value2");
await expect(config.overrideChosenEnv(undefined, originalEnv)).to.eventually.deep.equal({
VARIABLE2: { variable: "VARIABLE2", value: "new-value2" },
});
expect(prompt.promptOnce).to.have.been.calledTwice;
expect(csmImport.secretExists).to.not.have.been.called;
});
it("throws when trying to overwrite secrets without knowing the project", async () => {
const originalEnv: Record<string, config.Env> = {
API_KEY: { variable: "API_KEY", secret: "api-key" },
};
prompt.promptOnce.onFirstCall().resolves(["API_KEY"]);
await expect(config.overrideChosenEnv(undefined, originalEnv)).to.be.rejectedWith(
FirebaseError,
/Need a project ID to overwrite a secret./,
);
});
it("can create new secrets", async () => {
const originalEnv: Record<string, config.Env> = {
API_KEY: { variable: "API_KEY", secret: "api-key" },
};
prompt.promptOnce.onFirstCall().resolves(["API_KEY"]);
prompt.promptOnce.onSecondCall().resolves("test-api-key");
csm.secretExists.withArgs("project", "test-api-key").resolves(false);
prompt.promptOnce.onThirdCall().resolves("plaintext secret value");
await expect(config.overrideChosenEnv("project", originalEnv)).to.eventually.deep.equal({
API_KEY: { variable: "API_KEY", secret: "test-api-key" },
});
expect(prompt.promptOnce).to.have.been.calledThrice;
expect(csm.secretExists).to.have.been.calledOnce;
expect(csm.createSecret).to.have.been.calledOnce;
expect(csm.addVersion).to.have.been.calledOnce;
expect(csm.addVersion.getCall(0).args[2]).to.equal("plaintext secret value");
});
it("can create new secrets after warning about reuse", async () => {
const originalEnv: Record<string, config.Env> = {
API_KEY: { variable: "API_KEY", secret: "api-key" },
};
prompt.promptOnce.onCall(0).resolves(["API_KEY"]);
prompt.promptOnce.onCall(1).resolves("test-api-key");
csm.secretExists.withArgs("project", "test-api-key").resolves(true);
prompt.promptOnce.onCall(2).resolves("pick-new");
prompt.promptOnce.onCall(3).resolves("test-api-key2");
prompt.promptOnce.onCall(4).resolves("plaintext secret value");
await expect(config.overrideChosenEnv("project", originalEnv)).to.eventually.deep.equal({
API_KEY: { variable: "API_KEY", secret: "test-api-key2" },
});
expect(prompt.promptOnce.callCount).to.equal(5);
expect(csm.secretExists).to.have.been.calledTwice;
expect(csm.createSecret).to.have.been.calledOnce;
expect(csm.addVersion).to.have.been.calledOnce;
expect(csm.addVersion.getCall(0).args[2]).to.equal("plaintext secret value");
});
it("can reuse secrets", async () => {
const originalEnv: Record<string, config.Env> = {
API_KEY: { variable: "API_KEY", secret: "api-key" },
};
prompt.promptOnce.onFirstCall().resolves(["API_KEY"]);
prompt.promptOnce.onSecondCall().resolves("test-api-key");
csm.secretExists.withArgs("project", "test-api-key").resolves(true);
prompt.promptOnce.onThirdCall().resolves("reuse");
await expect(config.overrideChosenEnv("project", originalEnv)).to.eventually.deep.equal({
API_KEY: { variable: "API_KEY", secret: "test-api-key" },
});
expect(prompt.promptOnce).to.have.been.calledThrice;
expect(csm.secretExists).to.have.been.calledOnce;
expect(csm.createSecret).to.not.have.been.called;
expect(csm.addVersion).to.not.have.been.called;
});
it("suggests test key names", () => {
expect(config.suggestedTestKeyName("GOOGLE_GENAI_API_KEY")).to.equal(
"test-google-genai-api-key",
);
});
});
});