-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeychain.test.ts
More file actions
192 lines (166 loc) · 5.49 KB
/
Copy pathkeychain.test.ts
File metadata and controls
192 lines (166 loc) · 5.49 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
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
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
accountKey,
deleteTokens,
getTokens,
saveTokens,
setBackendForTesting,
type KeychainBackend,
type TokenBundle,
} from "./keychain.js";
function fakeBackend(): KeychainBackend & { store: Map<string, string> } {
const store = new Map<string, string>();
return {
store,
get: (account) => store.get(account) ?? null,
set: (account, secret) => {
store.set(account, secret);
},
delete: (account) => store.delete(account),
};
}
const prod = { name: "prod", instanceUrl: "https://auth.example.com" };
const bundle: TokenBundle = {
accessToken: "access-abc",
refreshToken: "refresh-xyz",
accessTokenExpiresAt: 1000,
refreshTokenExpiresAt: 2000,
};
let backend: ReturnType<typeof fakeBackend>;
beforeEach(() => {
backend = fakeBackend();
setBackendForTesting(backend);
delete process.env.SEAMLESS_REFRESH_TOKEN;
});
afterEach(() => {
setBackendForTesting(null);
delete process.env.SEAMLESS_REFRESH_TOKEN;
});
describe("accountKey", () => {
it("scopes by profile name and instance URL", () => {
expect(accountKey(prod)).toBe("prod::https://auth.example.com");
expect(accountKey({ name: "prod", instanceUrl: "https://other.example.com" })).not.toBe(
accountKey(prod),
);
});
});
describe("saveTokens / getTokens", () => {
it("round-trips a token bundle", async () => {
await saveTokens(prod, bundle);
expect(await getTokens(prod)).toEqual(bundle);
});
it("keeps tokens for same-named profiles on different instances separate", async () => {
const staging = { name: "prod", instanceUrl: "https://staging.example.com" };
await saveTokens(prod, bundle);
await saveTokens(staging, { accessToken: "a2", refreshToken: "r2" });
expect((await getTokens(prod))?.refreshToken).toBe("refresh-xyz");
expect((await getTokens(staging))?.refreshToken).toBe("r2");
expect(backend.store.size).toBe(2);
});
it("returns null when nothing is stored", async () => {
expect(await getTokens(prod)).toBeNull();
});
it("returns null on corrupt stored data", async () => {
backend.set(accountKey(prod), "{ not json");
expect(await getTokens(prod)).toBeNull();
});
});
describe("SEAMLESS_REFRESH_TOKEN fallback", () => {
it("returns the env refresh token without touching the keychain", async () => {
process.env.SEAMLESS_REFRESH_TOKEN = "ci-refresh";
setBackendForTesting({
get: () => {
throw new Error("keychain should not be read when env is set");
},
set: () => {},
delete: () => false,
});
expect(await getTokens(prod)).toEqual({
accessToken: "",
refreshToken: "ci-refresh",
});
});
});
describe("deleteTokens", () => {
it("clears a profile's entry", async () => {
await saveTokens(prod, bundle);
expect(await deleteTokens(prod)).toBe(true);
expect(await getTokens(prod)).toBeNull();
});
it("reports false when there was nothing to delete", async () => {
expect(await deleteTokens(prod)).toBe(false);
});
});
describe("loadBackend (real backend, mocked @napi-rs/keyring)", () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
vi.doUnmock("@napi-rs/keyring");
vi.resetModules();
});
it("wraps a failure to load the native module in a KeychainUnavailableError", async () => {
vi.doMock("@napi-rs/keyring", () => {
throw new Error("native module not found for this platform");
});
const mod = await import("./keychain.js");
await expect(mod.saveTokens(prod, bundle)).rejects.toBeInstanceOf(
mod.KeychainUnavailableError,
);
});
it("gets, sets, and deletes through the real Entry-backed backend", async () => {
const store = new Map<string, string>();
vi.doMock("@napi-rs/keyring", () => ({
Entry: class {
account: string;
constructor(_service: string, account: string) {
this.account = account;
}
getPassword() {
const value = store.get(this.account);
if (value === undefined) throw new Error("no matching entry found");
return value;
}
setPassword(secret: string) {
store.set(this.account, secret);
}
deletePassword() {
if (!store.has(this.account)) throw new Error("entry not found");
store.delete(this.account);
return true;
}
},
}));
const mod = await import("./keychain.js");
expect(await mod.getTokens(prod)).toBeNull();
await mod.saveTokens(prod, bundle);
expect(await mod.getTokens(prod)).toEqual(bundle);
expect(await mod.deleteTokens(prod)).toBe(true);
expect(await mod.deleteTokens(prod)).toBe(false);
});
it("wraps unexpected keychain errors in a KeychainUnavailableError", async () => {
vi.doMock("@napi-rs/keyring", () => ({
Entry: class {
getPassword(): string {
throw new Error("permission denied");
}
setPassword(): void {
throw new Error("permission denied");
}
deletePassword(): boolean {
throw new Error("permission denied");
}
},
}));
const mod = await import("./keychain.js");
await expect(mod.getTokens(prod)).rejects.toBeInstanceOf(
mod.KeychainUnavailableError,
);
await expect(mod.saveTokens(prod, bundle)).rejects.toBeInstanceOf(
mod.KeychainUnavailableError,
);
await expect(mod.deleteTokens(prod)).rejects.toBeInstanceOf(
mod.KeychainUnavailableError,
);
});
});