|
1 | 1 | /* eslint-disable @typescript-eslint/naming-convention */ |
2 | 2 |
|
| 3 | +import assert from "assert"; |
3 | 4 | import {Disposable} from "vscode"; |
| 5 | +import {anything, instance, mock, reset, verify, when} from "ts-mockito"; |
| 6 | +import {WorkspaceClient} from "@databricks/sdk-experimental"; |
| 7 | +import {ConnectionManager} from "./ConnectionManager"; |
| 8 | +import {ConfigModel} from "./models/ConfigModel"; |
| 9 | +import {CliWrapper} from "../cli/CliWrapper"; |
| 10 | +import {WorkspaceFolderManager} from "../vscode-objs/WorkspaceFolderManager"; |
| 11 | +import {CustomWhenContext} from "../vscode-objs/CustomWhenContext"; |
| 12 | +import {Telemetry} from "../telemetry"; |
| 13 | +import {AuthProvider} from "./auth/AuthProvider"; |
4 | 14 |
|
5 | 15 | describe(__filename, () => { |
6 | 16 | let disposables: Array<Disposable>; |
7 | 17 |
|
| 18 | + let mockCli: CliWrapper; |
| 19 | + let mockConfigModel: ConfigModel; |
| 20 | + let mockWorkspaceFolderManager: WorkspaceFolderManager; |
| 21 | + let mockCustomWhenContext: CustomWhenContext; |
| 22 | + let mockAuthProvider: AuthProvider; |
| 23 | + let mockWorkspaceClient: WorkspaceClient; |
| 24 | + |
| 25 | + function buildConnectionManager(): ConnectionManager { |
| 26 | + return new ConnectionManager( |
| 27 | + instance(mockCli), |
| 28 | + instance(mockConfigModel), |
| 29 | + instance(mockWorkspaceFolderManager), |
| 30 | + instance(mockCustomWhenContext), |
| 31 | + new Telemetry() |
| 32 | + ); |
| 33 | + } |
| 34 | + |
8 | 35 | beforeEach(() => { |
9 | 36 | disposables = []; |
| 37 | + mockCli = mock(CliWrapper); |
| 38 | + mockConfigModel = mock(ConfigModel); |
| 39 | + mockWorkspaceFolderManager = mock(WorkspaceFolderManager); |
| 40 | + mockCustomWhenContext = mock(CustomWhenContext); |
| 41 | + mockAuthProvider = mock<AuthProvider>(); |
| 42 | + mockWorkspaceClient = mock(WorkspaceClient); |
| 43 | + |
| 44 | + // DatabricksWorkspace.load() reads the org id from a header on the |
| 45 | + // currentUser.me() response and (best-effort) the workspace conf. |
| 46 | + when(mockWorkspaceClient.currentUser).thenReturn({ |
| 47 | + me: async () => |
| 48 | + ({ |
| 49 | + "userName": "test@databricks.com", |
| 50 | + "x-databricks-org-id": "1234", |
| 51 | + }) as any, |
| 52 | + } as any); |
| 53 | + when(mockWorkspaceClient.apiClient).thenReturn(undefined as any); |
| 54 | + when(mockAuthProvider.getWorkspaceClient()).thenResolve( |
| 55 | + instance(mockWorkspaceClient) |
| 56 | + ); |
| 57 | + when(mockAuthProvider.host).thenReturn( |
| 58 | + new URL("https://test.databricks.com") |
| 59 | + ); |
10 | 60 | }); |
11 | 61 |
|
12 | 62 | afterEach(() => { |
13 | 63 | disposables.forEach((d) => d.dispose()); |
| 64 | + reset(mockConfigModel); |
| 65 | + }); |
| 66 | + |
| 67 | + it("connectFromEnvironment connects using the injected auth provider", async () => { |
| 68 | + const cm = buildConnectionManager(); |
| 69 | + disposables.push(cm); |
| 70 | + |
| 71 | + await cm.connectFromEnvironment(instance(mockAuthProvider)); |
| 72 | + |
| 73 | + assert.equal(cm.state, "CONNECTED"); |
| 74 | + assert.ok(cm.workspaceClient); |
| 75 | + assert.ok(cm.databricksWorkspace); |
| 76 | + assert.equal( |
| 77 | + cm.databricksWorkspace?.host.toString(), |
| 78 | + "https://test.databricks.com/" |
| 79 | + ); |
| 80 | + verify(mockCustomWhenContext.setLoggedIn(true)).atLeast(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it("connectFromEnvironment does not touch the config model (no bundle coupling)", async () => { |
| 84 | + const cm = buildConnectionManager(); |
| 85 | + disposables.push(cm); |
| 86 | + |
| 87 | + await cm.connectFromEnvironment(instance(mockAuthProvider)); |
| 88 | + |
| 89 | + verify(mockConfigModel.set(anything(), anything())).never(); |
| 90 | + verify(mockConfigModel.setAuthProvider(anything())).never(); |
14 | 91 | }); |
15 | 92 |
|
16 | | - // TODO |
17 | | - // login |
18 | | - // logout |
19 | | - // configure |
20 | | - // attach cluster |
21 | | - // detach cluster |
22 | | - // attach workspace |
23 | | - // detach workspace |
| 93 | + it("connectFromEnvironment disconnects and rethrows on failure", async () => { |
| 94 | + when(mockAuthProvider.getWorkspaceClient()).thenReject( |
| 95 | + new Error("no credentials") |
| 96 | + ); |
| 97 | + const cm = buildConnectionManager(); |
| 98 | + disposables.push(cm); |
| 99 | + |
| 100 | + await assert.rejects( |
| 101 | + () => cm.connectFromEnvironment(instance(mockAuthProvider)), |
| 102 | + /no credentials/ |
| 103 | + ); |
| 104 | + |
| 105 | + assert.equal(cm.state, "DISCONNECTED"); |
| 106 | + assert.equal(cm.workspaceClient, undefined); |
| 107 | + assert.equal(cm.databricksWorkspace, undefined); |
| 108 | + verify(mockCustomWhenContext.setLoggedIn(false)).atLeast(1); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("connectFromEnvironment without an injected auth provider", () => { |
| 112 | + // These exercise the production credential path (new Config with an |
| 113 | + // EnvironmentLoader, PAT-only enforcement) which is skipped when a test |
| 114 | + // injects an AuthProvider. We only cover the fail-fast branches here: |
| 115 | + // the successful connect builds a real WorkspaceClient and calls |
| 116 | + // currentUser.me() against the host, which would hit the network - that |
| 117 | + // path is already covered by the injected-AuthProvider tests above. We |
| 118 | + // drive these purely through env vars and restore the environment |
| 119 | + // afterwards. |
| 120 | + let savedEnv: NodeJS.ProcessEnv; |
| 121 | + |
| 122 | + beforeEach(() => { |
| 123 | + savedEnv = process.env; |
| 124 | + process.env = {...savedEnv}; |
| 125 | + // Clear anything a local ~/.databrickscfg-style env would set so the |
| 126 | + // EnvironmentLoader only sees what each test injects. |
| 127 | + delete process.env.DATABRICKS_HOST; |
| 128 | + delete process.env.DATABRICKS_TOKEN; |
| 129 | + delete process.env.DATABRICKS_CONFIG_PROFILE; |
| 130 | + }); |
| 131 | + |
| 132 | + afterEach(() => { |
| 133 | + process.env = savedEnv; |
| 134 | + }); |
| 135 | + |
| 136 | + it("fails fast when no host is present in the environment", async () => { |
| 137 | + process.env.DATABRICKS_TOKEN = "dapi1234567890"; |
| 138 | + const cm = buildConnectionManager(); |
| 139 | + disposables.push(cm); |
| 140 | + |
| 141 | + await assert.rejects( |
| 142 | + () => cm.connectFromEnvironment(), |
| 143 | + /No Databricks host found in the environment/ |
| 144 | + ); |
| 145 | + assert.equal(cm.state, "DISCONNECTED"); |
| 146 | + }); |
| 147 | + |
| 148 | + it("fails fast when a host but no token is present", async () => { |
| 149 | + process.env.DATABRICKS_HOST = "https://test.databricks.com"; |
| 150 | + const cm = buildConnectionManager(); |
| 151 | + disposables.push(cm); |
| 152 | + |
| 153 | + await assert.rejects( |
| 154 | + () => cm.connectFromEnvironment(), |
| 155 | + /No Databricks token found in the environment/ |
| 156 | + ); |
| 157 | + assert.equal(cm.state, "DISCONNECTED"); |
| 158 | + }); |
| 159 | + }); |
24 | 160 | }); |
0 commit comments