|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import * as sinon from 'sinon' |
| 8 | +import assert from 'assert' |
| 9 | +import { initialize, SagemakerCookie } from '../../auth/activation' |
| 10 | +import { LoginManager } from '../../auth/deprecated/loginManager' |
| 11 | +import * as extensionUtilities from '../../shared/extensionUtilities' |
| 12 | +import * as authUtils from '../../auth/utils' |
| 13 | +import * as errors from '../../shared/errors' |
| 14 | +import * as credentials from '../../auth/providers/credentials' |
| 15 | + |
| 16 | +describe('auth/activation', function () { |
| 17 | + let sandbox: sinon.SinonSandbox |
| 18 | + let mockLoginManager: LoginManager |
| 19 | + let executeCommandStub: sinon.SinonStub |
| 20 | + let isAmazonQStub: sinon.SinonStub |
| 21 | + let isSageMakerStub: sinon.SinonStub |
| 22 | + let initializeCredentialsProviderManagerStub: sinon.SinonStub |
| 23 | + let getErrorMsgStub: sinon.SinonStub |
| 24 | + let fromStringStub: sinon.SinonStub |
| 25 | + let mockLogger: any |
| 26 | + let onDidChangeActiveConnectionEvent: sinon.SinonStub |
| 27 | + |
| 28 | + beforeEach(function () { |
| 29 | + sandbox = sinon.createSandbox() |
| 30 | + |
| 31 | + // Create mocks |
| 32 | + mockLoginManager = { |
| 33 | + login: sandbox.stub(), |
| 34 | + logout: sandbox.stub(), |
| 35 | + } as any |
| 36 | + |
| 37 | + mockLogger = { |
| 38 | + warn: sandbox.stub(), |
| 39 | + info: sandbox.stub(), |
| 40 | + error: sandbox.stub(), |
| 41 | + debug: sandbox.stub(), |
| 42 | + } |
| 43 | + |
| 44 | + onDidChangeActiveConnectionEvent = sandbox.stub() |
| 45 | + |
| 46 | + // Stub external dependencies |
| 47 | + executeCommandStub = sandbox.stub(vscode.commands, 'executeCommand') |
| 48 | + isAmazonQStub = sandbox.stub(extensionUtilities, 'isAmazonQ') |
| 49 | + isSageMakerStub = sandbox.stub(extensionUtilities, 'isSageMaker') |
| 50 | + initializeCredentialsProviderManagerStub = sandbox.stub(authUtils, 'initializeCredentialsProviderManager') |
| 51 | + getErrorMsgStub = sandbox.stub(errors, 'getErrorMsg') |
| 52 | + fromStringStub = sandbox.stub(credentials, 'fromString') |
| 53 | + }) |
| 54 | + |
| 55 | + afterEach(function () { |
| 56 | + sandbox.restore() |
| 57 | + }) |
| 58 | + |
| 59 | + describe('initialize', function () { |
| 60 | + it('should not execute sagemaker.parseCookies when not in AmazonQ and SageMaker environment', async function () { |
| 61 | + isAmazonQStub.returns(false) |
| 62 | + isSageMakerStub.returns(false) |
| 63 | + |
| 64 | + await initialize(mockLoginManager) |
| 65 | + |
| 66 | + assert.ok(!executeCommandStub.called) |
| 67 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 68 | + }) |
| 69 | + |
| 70 | + it('should not execute sagemaker.parseCookies when only in AmazonQ environment', async function () { |
| 71 | + isAmazonQStub.returns(true) |
| 72 | + isSageMakerStub.returns(false) |
| 73 | + |
| 74 | + await initialize(mockLoginManager) |
| 75 | + |
| 76 | + assert.ok(!executeCommandStub.called) |
| 77 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should not execute sagemaker.parseCookies when only in SageMaker environment', async function () { |
| 81 | + isAmazonQStub.returns(false) |
| 82 | + isSageMakerStub.returns(true) |
| 83 | + |
| 84 | + await initialize(mockLoginManager) |
| 85 | + |
| 86 | + assert.ok(!executeCommandStub.called) |
| 87 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should execute sagemaker.parseCookies when in both AmazonQ and SageMaker environment', async function () { |
| 91 | + isAmazonQStub.returns(true) |
| 92 | + isSageMakerStub.returns(true) |
| 93 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Sso' } as SagemakerCookie) |
| 94 | + |
| 95 | + await initialize(mockLoginManager) |
| 96 | + |
| 97 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 98 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 99 | + }) |
| 100 | + |
| 101 | + it('should initialize credentials provider manager when authMode is not Sso', async function () { |
| 102 | + isAmazonQStub.returns(true) |
| 103 | + isSageMakerStub.returns(true) |
| 104 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Iam' } as SagemakerCookie) |
| 105 | + |
| 106 | + await initialize(mockLoginManager) |
| 107 | + |
| 108 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 109 | + assert.ok(initializeCredentialsProviderManagerStub.calledOnce) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should initialize credentials provider manager when authMode is undefined', async function () { |
| 113 | + isAmazonQStub.returns(true) |
| 114 | + isSageMakerStub.returns(true) |
| 115 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({} as SagemakerCookie) |
| 116 | + |
| 117 | + await initialize(mockLoginManager) |
| 118 | + |
| 119 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 120 | + assert.ok(initializeCredentialsProviderManagerStub.calledOnce) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should warn and not throw when sagemaker.parseCookies command is not found', async function () { |
| 124 | + isAmazonQStub.returns(true) |
| 125 | + isSageMakerStub.returns(true) |
| 126 | + const error = new Error("command 'sagemaker.parseCookies' not found") |
| 127 | + executeCommandStub.withArgs('sagemaker.parseCookies').rejects(error) |
| 128 | + getErrorMsgStub.returns("command 'sagemaker.parseCookies' not found") |
| 129 | + |
| 130 | + await initialize(mockLoginManager) |
| 131 | + |
| 132 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 133 | + assert.ok(getErrorMsgStub.calledOnceWith(error)) |
| 134 | + assert.ok( |
| 135 | + mockLogger.warn.calledOnceWith( |
| 136 | + 'Failed to execute command "sagemaker.parseCookies": Error: command \'sagemaker.parseCookies\' not found' |
| 137 | + ) |
| 138 | + ) |
| 139 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 140 | + }) |
| 141 | + |
| 142 | + it('should throw when sagemaker.parseCookies fails with non-command-not-found error', async function () { |
| 143 | + isAmazonQStub.returns(true) |
| 144 | + isSageMakerStub.returns(true) |
| 145 | + const error = new Error('Some other error') |
| 146 | + executeCommandStub.withArgs('sagemaker.parseCookies').rejects(error) |
| 147 | + getErrorMsgStub.returns('Some other error') |
| 148 | + |
| 149 | + await assert.rejects(initialize(mockLoginManager), /Some other error/) |
| 150 | + |
| 151 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 152 | + assert.ok(getErrorMsgStub.calledOnceWith(error)) |
| 153 | + assert.ok(!mockLogger.warn.called) |
| 154 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 155 | + }) |
| 156 | + |
| 157 | + it('should handle the complete flow for AmazonQ + SageMaker with Iam authMode', async function () { |
| 158 | + isAmazonQStub.returns(true) |
| 159 | + isSageMakerStub.returns(true) |
| 160 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Iam' } as SagemakerCookie) |
| 161 | + |
| 162 | + let eventCallback: (conn: any) => Promise<void> = async () => {} |
| 163 | + onDidChangeActiveConnectionEvent.callsFake((callback: any) => { |
| 164 | + eventCallback = callback |
| 165 | + return { dispose: sandbox.stub() } |
| 166 | + }) |
| 167 | + |
| 168 | + const mockConnection = { |
| 169 | + type: 'iam', |
| 170 | + state: 'valid', |
| 171 | + id: 'test-connection-id', |
| 172 | + } |
| 173 | + const mockProviderId = { credentialSource: 'test', credentialTypeId: 'test' } |
| 174 | + fromStringStub.withArgs('test-connection-id').returns(mockProviderId) |
| 175 | + ;(mockLoginManager.login as sinon.SinonStub).resolves(true) |
| 176 | + |
| 177 | + await initialize(mockLoginManager) |
| 178 | + |
| 179 | + // Verify SageMaker initialization |
| 180 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 181 | + assert.ok(initializeCredentialsProviderManagerStub.calledOnce) |
| 182 | + |
| 183 | + // Verify event listener is set up |
| 184 | + assert.ok(onDidChangeActiveConnectionEvent.calledOnce) |
| 185 | + |
| 186 | + // Test event handler works correctly |
| 187 | + await eventCallback(mockConnection) |
| 188 | + assert.ok( |
| 189 | + (mockLoginManager.login as sinon.SinonStub).calledOnceWith({ |
| 190 | + passive: true, |
| 191 | + providerId: mockProviderId, |
| 192 | + }) |
| 193 | + ) |
| 194 | + }) |
| 195 | + }) |
| 196 | +}) |
0 commit comments