|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | +import { mkdtempSync, rmdirSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | + |
| 6 | +// Mock debug utilities |
| 7 | +vi.mock('../utils/debug.js', () => ({ |
| 8 | + logInfo: vi.fn(), |
| 9 | + logError: vi.fn(), |
| 10 | + logWarn: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +// Mock clack prompts |
| 14 | +vi.mock('../utils/clack.js', () => ({ |
| 15 | + default: { |
| 16 | + log: { |
| 17 | + success: vi.fn(), |
| 18 | + error: vi.fn(), |
| 19 | + info: vi.fn(), |
| 20 | + step: vi.fn(), |
| 21 | + }, |
| 22 | + spinner: vi.fn(() => ({ |
| 23 | + start: vi.fn(), |
| 24 | + stop: vi.fn(), |
| 25 | + })), |
| 26 | + isCancel: vi.fn(() => false), |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +// Mock staging API — we control it per test |
| 31 | +const mockFetchStagingCredentials = vi.fn(); |
| 32 | +vi.mock('../lib/staging-api.js', () => ({ |
| 33 | + fetchStagingCredentials: (...args: unknown[]) => mockFetchStagingCredentials(...args), |
| 34 | +})); |
| 35 | + |
| 36 | +let testDir: string; |
| 37 | + |
| 38 | +vi.mock('node:os', async (importOriginal) => { |
| 39 | + const original = await importOriginal<typeof import('node:os')>(); |
| 40 | + return { |
| 41 | + ...original, |
| 42 | + default: { |
| 43 | + ...original, |
| 44 | + homedir: () => testDir, |
| 45 | + }, |
| 46 | + homedir: () => testDir, |
| 47 | + }; |
| 48 | +}); |
| 49 | + |
| 50 | +const { getConfig, setInsecureConfigStorage, clearConfig } = await import('../lib/config-store.js'); |
| 51 | +const { provisionStagingEnvironment } = await import('./login.js'); |
| 52 | + |
| 53 | +describe('login', () => { |
| 54 | + beforeEach(() => { |
| 55 | + testDir = mkdtempSync(join(tmpdir(), 'login-test-')); |
| 56 | + setInsecureConfigStorage(true); |
| 57 | + vi.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + clearConfig(); |
| 62 | + try { |
| 63 | + rmdirSync(join(testDir, '.workos'), { recursive: true }); |
| 64 | + } catch {} |
| 65 | + try { |
| 66 | + rmdirSync(testDir); |
| 67 | + } catch {} |
| 68 | + }); |
| 69 | + |
| 70 | + describe('provisionStagingEnvironment', () => { |
| 71 | + it('creates a staging environment on success', async () => { |
| 72 | + mockFetchStagingCredentials.mockResolvedValueOnce({ |
| 73 | + clientId: 'client_staging_123', |
| 74 | + apiKey: 'sk_test_staging_abc', |
| 75 | + }); |
| 76 | + |
| 77 | + const result = await provisionStagingEnvironment('access_token_xyz'); |
| 78 | + |
| 79 | + expect(result).toBe(true); |
| 80 | + expect(mockFetchStagingCredentials).toHaveBeenCalledWith('access_token_xyz'); |
| 81 | + |
| 82 | + const config = getConfig(); |
| 83 | + expect(config).not.toBeNull(); |
| 84 | + expect(config?.environments['staging']).toEqual({ |
| 85 | + name: 'staging', |
| 86 | + type: 'sandbox', |
| 87 | + apiKey: 'sk_test_staging_abc', |
| 88 | + clientId: 'client_staging_123', |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('sets staging as active environment when no environments exist', async () => { |
| 93 | + mockFetchStagingCredentials.mockResolvedValueOnce({ |
| 94 | + clientId: 'client_123', |
| 95 | + apiKey: 'sk_test_abc', |
| 96 | + }); |
| 97 | + |
| 98 | + await provisionStagingEnvironment('token'); |
| 99 | + |
| 100 | + const config = getConfig(); |
| 101 | + expect(config?.activeEnvironment).toBe('staging'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('does not change active environment when one already exists', async () => { |
| 105 | + // Pre-create an environment |
| 106 | + const { saveConfig } = await import('../lib/config-store.js'); |
| 107 | + saveConfig({ |
| 108 | + activeEnvironment: 'production', |
| 109 | + environments: { |
| 110 | + production: { |
| 111 | + name: 'production', |
| 112 | + type: 'production', |
| 113 | + apiKey: 'sk_live_existing', |
| 114 | + }, |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + mockFetchStagingCredentials.mockResolvedValueOnce({ |
| 119 | + clientId: 'client_123', |
| 120 | + apiKey: 'sk_test_abc', |
| 121 | + }); |
| 122 | + |
| 123 | + await provisionStagingEnvironment('token'); |
| 124 | + |
| 125 | + const config = getConfig(); |
| 126 | + expect(config?.activeEnvironment).toBe('production'); |
| 127 | + expect(config?.environments['staging']).toBeDefined(); |
| 128 | + expect(config?.environments['production']).toBeDefined(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('updates existing staging environment if already present', async () => { |
| 132 | + const { saveConfig } = await import('../lib/config-store.js'); |
| 133 | + saveConfig({ |
| 134 | + activeEnvironment: 'staging', |
| 135 | + environments: { |
| 136 | + staging: { |
| 137 | + name: 'staging', |
| 138 | + type: 'sandbox', |
| 139 | + apiKey: 'sk_test_old', |
| 140 | + clientId: 'client_old', |
| 141 | + }, |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + mockFetchStagingCredentials.mockResolvedValueOnce({ |
| 146 | + clientId: 'client_new', |
| 147 | + apiKey: 'sk_test_new', |
| 148 | + }); |
| 149 | + |
| 150 | + const result = await provisionStagingEnvironment('token'); |
| 151 | + |
| 152 | + expect(result).toBe(true); |
| 153 | + const config = getConfig(); |
| 154 | + expect(config?.environments['staging']?.apiKey).toBe('sk_test_new'); |
| 155 | + expect(config?.environments['staging']?.clientId).toBe('client_new'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('returns false and does not throw on API 403 error', async () => { |
| 159 | + mockFetchStagingCredentials.mockRejectedValueOnce(new Error('Access denied')); |
| 160 | + |
| 161 | + const result = await provisionStagingEnvironment('token'); |
| 162 | + |
| 163 | + expect(result).toBe(false); |
| 164 | + const config = getConfig(); |
| 165 | + expect(config).toBeNull(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('returns false and does not throw on API 404 error', async () => { |
| 169 | + mockFetchStagingCredentials.mockRejectedValueOnce(new Error('No staging environment found')); |
| 170 | + |
| 171 | + const result = await provisionStagingEnvironment('token'); |
| 172 | + |
| 173 | + expect(result).toBe(false); |
| 174 | + }); |
| 175 | + |
| 176 | + it('returns false and does not throw on network error', async () => { |
| 177 | + mockFetchStagingCredentials.mockRejectedValueOnce(new Error('Network error')); |
| 178 | + |
| 179 | + const result = await provisionStagingEnvironment('token'); |
| 180 | + |
| 181 | + expect(result).toBe(false); |
| 182 | + }); |
| 183 | + |
| 184 | + it('returns false and does not throw on timeout', async () => { |
| 185 | + mockFetchStagingCredentials.mockRejectedValueOnce(new Error('Request timed out')); |
| 186 | + |
| 187 | + const result = await provisionStagingEnvironment('token'); |
| 188 | + |
| 189 | + expect(result).toBe(false); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
0 commit comments