-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi-key.spec.ts
More file actions
159 lines (139 loc) · 5.03 KB
/
api-key.spec.ts
File metadata and controls
159 lines (139 loc) · 5.03 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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { mkdtempSync, rmdirSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
// Mock debug utilities
vi.mock('../utils/debug.js', () => ({
logWarn: vi.fn(),
}));
// Mock exitWithError — must throw to halt execution like process.exit
class ExitError extends Error {
code: string;
constructor(error: { code: string; message: string }) {
super(error.message);
this.code = error.code;
}
}
const mockExitWithError = vi.fn((error: { code: string; message: string }) => {
throw new ExitError(error);
});
vi.mock('../utils/output.js', () => ({
exitWithError: (...args: unknown[]) => mockExitWithError(...(args as [{ code: string; message: string }])),
}));
let testDir: string;
// Mock os.homedir for config-store
vi.mock('node:os', async (importOriginal) => {
const original = await importOriginal<typeof import('node:os')>();
return {
...original,
default: {
...original,
homedir: () => testDir,
},
homedir: () => testDir,
};
});
const { saveConfig, setInsecureConfigStorage, clearConfig } = await import('./config-store.js');
const { resolveApiKey, resolveOptionalApiKey, resolveApiBaseUrl } = await import('./api-key.js');
describe('api-key', () => {
const originalEnv = process.env;
beforeEach(() => {
testDir = mkdtempSync(join(tmpdir(), 'api-key-test-'));
setInsecureConfigStorage(true);
process.env = { ...originalEnv };
delete process.env.WORKOS_API_KEY;
});
afterEach(() => {
clearConfig();
process.env = originalEnv;
try {
rmdirSync(join(testDir, '.workos'), { recursive: true });
} catch {}
try {
rmdirSync(testDir);
} catch {}
});
describe('resolveApiKey', () => {
it('returns --api-key flag over env var and stored key', () => {
process.env.WORKOS_API_KEY = 'sk_env_var';
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_stored' } },
});
expect(resolveApiKey({ apiKey: 'sk_flag' })).toBe('sk_flag');
});
it('returns WORKOS_API_KEY env var when no flag provided', () => {
process.env.WORKOS_API_KEY = 'sk_env_var';
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_stored' } },
});
expect(resolveApiKey()).toBe('sk_env_var');
});
it('returns active environment API key when no env var or flag', () => {
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_stored' } },
});
expect(resolveApiKey()).toBe('sk_stored');
});
it('exits with error when no API key available', () => {
expect(() => resolveApiKey()).toThrow(ExitError);
expect(mockExitWithError).toHaveBeenCalledWith(expect.objectContaining({ code: 'no_api_key' }));
});
it('exits with error when config exists but no active environment', () => {
saveConfig({ environments: {} });
expect(() => resolveApiKey()).toThrow(ExitError);
});
it('ignores empty string env var', () => {
process.env.WORKOS_API_KEY = '';
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_stored' } },
});
expect(resolveApiKey()).toBe('sk_stored');
});
it('ignores empty string flag', () => {
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_stored' } },
});
expect(resolveApiKey({ apiKey: '' })).toBe('sk_stored');
});
});
describe('resolveOptionalApiKey', () => {
it('returns undefined when no API key is available', () => {
mockExitWithError.mockClear();
expect(resolveOptionalApiKey()).toBeUndefined();
expect(mockExitWithError).not.toHaveBeenCalled();
});
it('returns configured API key when available', () => {
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_stored' } },
});
expect(resolveOptionalApiKey()).toBe('sk_stored');
});
});
describe('resolveApiBaseUrl', () => {
it('returns default URL when no config', () => {
expect(resolveApiBaseUrl()).toBe('https://api.workos.com');
});
it('returns default URL when active env has no endpoint', () => {
saveConfig({
activeEnvironment: 'prod',
environments: { prod: { name: 'prod', type: 'production', apiKey: 'sk_test' } },
});
expect(resolveApiBaseUrl()).toBe('https://api.workos.com');
});
it('returns custom endpoint from active environment', () => {
saveConfig({
activeEnvironment: 'local',
environments: {
local: { name: 'local', type: 'sandbox', apiKey: 'sk_test', endpoint: 'http://localhost:8001' },
},
});
expect(resolveApiBaseUrl()).toBe('http://localhost:8001');
});
});
});