forked from gov-suite/governed-service-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_test.ts
68 lines (61 loc) · 2.39 KB
/
vault_test.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
import * as ta from "https://deno.land/[email protected]/testing/asserts.ts";
import * as mod from "./vault.ts";
Deno.test("env var with default", async () => {
const envVars = new mod.EnvironmentVault(
{ commonNamespace: "PREFIX_", secretsNamespace: "PREFIX_SECRET_" },
);
const serverHost = envVars.defineEnvVar(
"SERVER_HOST",
{ defaultValue: "test.domain.com" },
);
ta.assert(serverHost.baseName, "SERVER_HOST");
ta.assert(serverHost.qualifiedName, "PREFIX_SERVER_HOST");
ta.assertStrictEquals(serverHost.value(), "test.domain.com");
Deno.env.set("PREFIX_SERVER_HOST", "updated.domain.com");
ta.assert(serverHost.value(), "updated.domain.com");
});
Deno.test("env var undefined without default", async () => {
let encounteredUndefinedAttr: mod.VaultAttr | undefined;
const envVars = new mod.EnvironmentVault(
{ commonNamespace: "PREFIX_", secretsNamespace: "PREFIX_SECRET_" },
{
onUndefined: (attr: mod.VaultAttr) => {
encounteredUndefinedAttr = attr;
},
},
);
const serverUser = envVars.defineEnvVar("SERVER_USER");
ta.assert(serverUser.baseName, "SERVER_USER");
ta.assert(serverUser.qualifiedName, "PREFIX_SERVER_USER");
ta.assertStrictEquals(serverUser.value(), undefined);
ta.assertStrictEquals(serverUser, encounteredUndefinedAttr);
Deno.env.set("PREFIX_SERVER_USER", "shah");
ta.assert(serverUser.value(), "shah");
});
Deno.test("env var undefined secret without default", async () => {
let encounteredDuplicateAttr: mod.VaultAttr | undefined;
const envVars = new mod.EnvironmentVault(
{ commonNamespace: "PREFIX_", secretsNamespace: "PREFIX_SECRET_" },
{
onDuplicateDefn: (
newAttr: mod.VaultAttr,
existingAttr: mod.VaultAttr,
): mod.VaultAttr => {
encounteredDuplicateAttr = existingAttr;
return existingAttr;
},
},
);
const serverPassword = envVars.defineEnvVar(
"SERVER_PASSWD",
{ isSecret: true },
);
ta.assert(serverPassword.baseName, "SERVER_PASSWD");
ta.assert(serverPassword.qualifiedName, "PREFIX_SECRET_SERVER_PASSWD");
ta.assertStrictEquals(serverPassword.value(), undefined);
Deno.env.set("PREFIX_SECRET_SERVER_PASSWD", "*****");
ta.assert(serverPassword.value(), "*****");
// try to define it twice to see if we catch it
envVars.defineEnvVar("SERVER_PASSWD", { isSecret: true });
ta.assertStrictEquals(serverPassword, encounteredDuplicateAttr);
});