-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresolveAuthSourceReadOnly.test.ts
More file actions
78 lines (67 loc) · 2.59 KB
/
Copy pathresolveAuthSourceReadOnly.test.ts
File metadata and controls
78 lines (67 loc) · 2.59 KB
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
import { afterEach, describe, expect, test } from "bun:test";
import { resolveAuthSourceReadOnly } from "../../../src/commands/auth";
import { DEFAULT_MNEMONIC } from "../../../src/utils/constants";
import { ENV } from "../../../src/cli/env";
import { ALICE_KEY_URI } from "../../_helpers/cliHelpers";
const CUSTOM_MNEMONIC =
"absorb oppose idea expire husband layer subject flip pause ahead daring stem";
const previousEnv = {
mnemonic: process.env[ENV.MNEMONIC],
keyUri: process.env[ENV.KEY_URI],
};
function clearAuthEnv(): void {
delete process.env[ENV.MNEMONIC];
delete process.env[ENV.KEY_URI];
}
function restoreAuthEnv(): void {
if (previousEnv.mnemonic === undefined) delete process.env[ENV.MNEMONIC];
else process.env[ENV.MNEMONIC] = previousEnv.mnemonic;
if (previousEnv.keyUri === undefined) delete process.env[ENV.KEY_URI];
else process.env[ENV.KEY_URI] = previousEnv.keyUri;
}
afterEach(() => {
restoreAuthEnv();
});
type EnvCase = {
label: string;
env: { mnemonic?: string; keyUri?: string };
expected: { source: string; isKeyUri: boolean; resolvedFrom: "env" | "default" };
};
const envCases: EnvCase[] = [
{
label: "no env falls back to DEFAULT_MNEMONIC",
env: {},
expected: { source: DEFAULT_MNEMONIC, isKeyUri: false, resolvedFrom: "default" },
},
{
label: "DOTNS_MNEMONIC is used when set",
env: { mnemonic: CUSTOM_MNEMONIC },
expected: { source: CUSTOM_MNEMONIC, isKeyUri: false, resolvedFrom: "env" },
},
{
label: "DOTNS_KEY_URI is used when mnemonic is absent",
env: { keyUri: ALICE_KEY_URI },
expected: { source: ALICE_KEY_URI, isKeyUri: true, resolvedFrom: "env" },
},
{
label: "DOTNS_MNEMONIC takes precedence over DOTNS_KEY_URI",
env: { mnemonic: CUSTOM_MNEMONIC, keyUri: ALICE_KEY_URI },
expected: { source: CUSTOM_MNEMONIC, isKeyUri: false, resolvedFrom: "env" },
},
{
label: "empty DOTNS_MNEMONIC falls through to default",
env: { mnemonic: "" },
expected: { source: DEFAULT_MNEMONIC, isKeyUri: false, resolvedFrom: "default" },
},
];
describe("resolveAuthSourceReadOnly honours environment variables (regression for #114)", () => {
test.each(envCases)("$label", async ({ env, expected }) => {
clearAuthEnv();
if (env.mnemonic !== undefined) process.env[ENV.MNEMONIC] = env.mnemonic;
if (env.keyUri !== undefined) process.env[ENV.KEY_URI] = env.keyUri;
const resolved = await resolveAuthSourceReadOnly();
expect(resolved.source).toBe(expected.source);
expect(resolved.isKeyUri).toBe(expected.isKeyUri);
expect(resolved.resolvedFrom).toBe(expected.resolvedFrom);
});
});