|
| 1 | +// Copyright (C) 2025 by Posit Software, PBC. |
| 2 | + |
| 3 | +import { describe, expect, test, vi, beforeEach } from "vitest"; |
| 4 | +import { window, env } from "vscode"; |
| 5 | +import { ConnectAuthTokenActivator } from "./ConnectAuthTokenActivator"; |
| 6 | +import { useApi } from "src/api"; |
| 7 | + |
| 8 | +// Mock dependencies |
| 9 | +vi.mock("vscode", () => ({ |
| 10 | + window: { |
| 11 | + createOutputChannel: vi.fn(() => ({ |
| 12 | + appendLine: vi.fn(), |
| 13 | + })), |
| 14 | + showInformationMessage: vi.fn(), |
| 15 | + showErrorMessage: vi.fn(), |
| 16 | + }, |
| 17 | + Uri: { |
| 18 | + parse: vi.fn(), |
| 19 | + }, |
| 20 | + env: { |
| 21 | + openExternal: vi.fn(), |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("src/api", () => ({ |
| 26 | + useApi: vi.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +vi.mock("src/utils/progress", () => ({ |
| 30 | + showProgress: vi.fn((_, __, callback) => callback()), |
| 31 | +})); |
| 32 | + |
| 33 | +vi.mock("src/utils/errors", () => ({ |
| 34 | + getMessageFromError: vi.fn((error) => error?.message || "Unknown error"), |
| 35 | +})); |
| 36 | + |
| 37 | +// Type guards for mocked functions |
| 38 | +const mockUseApi = vi.mocked(useApi); |
| 39 | +const mockShowInformationMessage = vi.mocked(window.showInformationMessage); |
| 40 | +const mockShowErrorMessage = vi.mocked(window.showErrorMessage); |
| 41 | +const mockOpenExternal = vi.mocked(env.openExternal); |
| 42 | + |
| 43 | +describe("ConnectAuthTokenActivator", () => { |
| 44 | + let activator: ConnectAuthTokenActivator; |
| 45 | + let mockApi: { |
| 46 | + credentials: { |
| 47 | + generateToken: ReturnType<typeof vi.fn>; |
| 48 | + verifyToken: ReturnType<typeof vi.fn>; |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + vi.clearAllMocks(); |
| 54 | + |
| 55 | + mockApi = { |
| 56 | + credentials: { |
| 57 | + generateToken: vi.fn(), |
| 58 | + verifyToken: vi.fn(), |
| 59 | + }, |
| 60 | + }; |
| 61 | + mockUseApi.mockResolvedValue( |
| 62 | + mockApi as unknown as Awaited<ReturnType<typeof useApi>>, |
| 63 | + ); |
| 64 | + |
| 65 | + activator = new ConnectAuthTokenActivator( |
| 66 | + "https://connect.example.com", |
| 67 | + "test-view-id", |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + test("constructor initializes properties correctly", () => { |
| 72 | + expect(activator).toBeDefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + test("initialize() calls useApi and sets up the API client", async () => { |
| 76 | + await activator.initialize(); |
| 77 | + expect(mockUseApi).toHaveBeenCalledOnce(); |
| 78 | + }); |
| 79 | + |
| 80 | + test("activateToken() completes the full token authentication flow", async () => { |
| 81 | + // Setup mocks |
| 82 | + mockApi.credentials.generateToken.mockResolvedValue({ |
| 83 | + data: { |
| 84 | + token: "test-token-123", |
| 85 | + claimUrl: "https://connect.example.com/claim/123", |
| 86 | + privateKey: "test-private-key-123", |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + mockApi.credentials.verifyToken.mockResolvedValue({ |
| 91 | + status: 200, |
| 92 | + data: { username: "testuser" }, |
| 93 | + }); |
| 94 | + |
| 95 | + // Initialize and run |
| 96 | + await activator.initialize(); |
| 97 | + const result = await activator.activateToken(); |
| 98 | + |
| 99 | + // Verify the flow |
| 100 | + expect(mockApi.credentials.generateToken).toHaveBeenCalledWith( |
| 101 | + "https://connect.example.com", |
| 102 | + ); |
| 103 | + expect(mockOpenExternal).toHaveBeenCalledWith( |
| 104 | + expect.objectContaining({}), // Uri.parse result |
| 105 | + ); |
| 106 | + expect(mockApi.credentials.verifyToken).toHaveBeenCalledWith( |
| 107 | + "https://connect.example.com", |
| 108 | + "test-token-123", |
| 109 | + "test-private-key-123", |
| 110 | + ); |
| 111 | + expect(mockShowInformationMessage).toHaveBeenCalledWith( |
| 112 | + "Successfully authenticated as testuser", |
| 113 | + ); |
| 114 | + |
| 115 | + // Verify result |
| 116 | + expect(result).toEqual({ |
| 117 | + token: "test-token-123", |
| 118 | + privateKey: "test-private-key-123", |
| 119 | + userName: "testuser", |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + test("activateToken() handles token verification polling with retries", async () => { |
| 124 | + // Setup mocks |
| 125 | + mockApi.credentials.generateToken.mockResolvedValue({ |
| 126 | + data: { |
| 127 | + token: "test-token-123", |
| 128 | + claimUrl: "https://connect.example.com/claim/123", |
| 129 | + privateKey: "test-private-key-123", |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + // Mock verification to fail a few times, then succeed |
| 134 | + mockApi.credentials.verifyToken |
| 135 | + .mockRejectedValueOnce(new Error("401 Unauthorized")) |
| 136 | + .mockRejectedValueOnce(new Error("401 Unauthorized")) |
| 137 | + .mockResolvedValueOnce({ |
| 138 | + status: 200, |
| 139 | + data: { username: "testuser" }, |
| 140 | + }); |
| 141 | + |
| 142 | + // Initialize and run |
| 143 | + await activator.initialize(); |
| 144 | + const result = await activator.activateToken(); |
| 145 | + |
| 146 | + // Verify multiple verification attempts |
| 147 | + expect(mockApi.credentials.verifyToken).toHaveBeenCalledTimes(3); |
| 148 | + expect(result.userName).toBe("testuser"); |
| 149 | + }); |
| 150 | + |
| 151 | + test("activateToken() throws error when not initialized", async () => { |
| 152 | + // Don't call initialize() |
| 153 | + await expect(activator.activateToken()).rejects.toThrow( |
| 154 | + "ConnectAuthTokenActivator must be initialized before use", |
| 155 | + ); |
| 156 | + }); |
| 157 | + |
| 158 | + test("activateToken() handles token generation failure", async () => { |
| 159 | + mockApi.credentials.generateToken.mockRejectedValue( |
| 160 | + new Error("Failed to generate token"), |
| 161 | + ); |
| 162 | + |
| 163 | + await activator.initialize(); |
| 164 | + |
| 165 | + await expect(activator.activateToken()).rejects.toThrow( |
| 166 | + "Failed to generate token", |
| 167 | + ); |
| 168 | + expect(mockShowErrorMessage).toHaveBeenCalledWith( |
| 169 | + "Failed to complete token authentication: Failed to generate token", |
| 170 | + ); |
| 171 | + }); |
| 172 | + |
| 173 | + test("activateToken() handles timeout when token claim takes too long", async () => { |
| 174 | + // Setup mocks |
| 175 | + mockApi.credentials.generateToken.mockResolvedValue({ |
| 176 | + data: { |
| 177 | + token: "test-token-123", |
| 178 | + claimUrl: "https://connect.example.com/claim/123", |
| 179 | + privateKey: "test-private-key-123", |
| 180 | + }, |
| 181 | + }); |
| 182 | + |
| 183 | + // Mock verification to always fail (simulating timeout) |
| 184 | + mockApi.credentials.verifyToken.mockRejectedValue( |
| 185 | + new Error("401 Unauthorized"), |
| 186 | + ); |
| 187 | + |
| 188 | + // Create activator with reduced maxAttempts for faster testing |
| 189 | + const fastActivator = new ConnectAuthTokenActivator( |
| 190 | + "https://connect.example.com", |
| 191 | + "test-view-id", |
| 192 | + 2, // maxAttempts |
| 193 | + ); |
| 194 | + await fastActivator.initialize(); |
| 195 | + |
| 196 | + await expect(fastActivator.activateToken()).rejects.toThrow( |
| 197 | + "Token claim process timed out or was cancelled", |
| 198 | + ); |
| 199 | + }); |
| 200 | +}); |
0 commit comments