|
| 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 | + |
| 15 | +describe('auth/activation', function () { |
| 16 | + let sandbox: sinon.SinonSandbox |
| 17 | + let mockLoginManager: LoginManager |
| 18 | + let executeCommandStub: sinon.SinonStub |
| 19 | + let isAmazonQStub: sinon.SinonStub |
| 20 | + let isSageMakerStub: sinon.SinonStub |
| 21 | + let initializeCredentialsProviderManagerStub: sinon.SinonStub |
| 22 | + let getErrorMsgStub: sinon.SinonStub |
| 23 | + let mockLogger: any |
| 24 | + |
| 25 | + beforeEach(function () { |
| 26 | + sandbox = sinon.createSandbox() |
| 27 | + |
| 28 | + // Create mocks |
| 29 | + mockLoginManager = { |
| 30 | + login: sandbox.stub(), |
| 31 | + logout: sandbox.stub(), |
| 32 | + } as any |
| 33 | + |
| 34 | + mockLogger = { |
| 35 | + warn: sandbox.stub(), |
| 36 | + info: sandbox.stub(), |
| 37 | + error: sandbox.stub(), |
| 38 | + debug: sandbox.stub(), |
| 39 | + } |
| 40 | + |
| 41 | + // Stub external dependencies |
| 42 | + executeCommandStub = sandbox.stub(vscode.commands, 'executeCommand') |
| 43 | + isAmazonQStub = sandbox.stub(extensionUtilities, 'isAmazonQ') |
| 44 | + isSageMakerStub = sandbox.stub(extensionUtilities, 'isSageMaker') |
| 45 | + initializeCredentialsProviderManagerStub = sandbox.stub(authUtils, 'initializeCredentialsProviderManager') |
| 46 | + getErrorMsgStub = sandbox.stub(errors, 'getErrorMsg') |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(function () { |
| 50 | + sandbox.restore() |
| 51 | + }) |
| 52 | + |
| 53 | + describe('initialize', function () { |
| 54 | + it('should not execute sagemaker.parseCookies when not in AmazonQ and SageMaker environment', async function () { |
| 55 | + isAmazonQStub.returns(false) |
| 56 | + isSageMakerStub.returns(false) |
| 57 | + |
| 58 | + await initialize(mockLoginManager) |
| 59 | + |
| 60 | + assert.ok(!executeCommandStub.called) |
| 61 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should not execute sagemaker.parseCookies when only in AmazonQ environment', async function () { |
| 65 | + isAmazonQStub.returns(true) |
| 66 | + isSageMakerStub.returns(false) |
| 67 | + |
| 68 | + await initialize(mockLoginManager) |
| 69 | + |
| 70 | + assert.ok(!executeCommandStub.called) |
| 71 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should not execute sagemaker.parseCookies when only in SageMaker environment', async function () { |
| 75 | + isAmazonQStub.returns(false) |
| 76 | + isSageMakerStub.returns(true) |
| 77 | + |
| 78 | + await initialize(mockLoginManager) |
| 79 | + |
| 80 | + assert.ok(!executeCommandStub.called) |
| 81 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should execute sagemaker.parseCookies when in both AmazonQ and SageMaker environment', async function () { |
| 85 | + isAmazonQStub.returns(true) |
| 86 | + isSageMakerStub.returns(true) |
| 87 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Sso' } as SagemakerCookie) |
| 88 | + |
| 89 | + await initialize(mockLoginManager) |
| 90 | + |
| 91 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 92 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 93 | + }) |
| 94 | + |
| 95 | + it('should initialize credentials provider manager when authMode is not Sso', async function () { |
| 96 | + isAmazonQStub.returns(true) |
| 97 | + isSageMakerStub.returns(true) |
| 98 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({ authMode: 'Iam' } as SagemakerCookie) |
| 99 | + |
| 100 | + await initialize(mockLoginManager) |
| 101 | + |
| 102 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 103 | + assert.ok(initializeCredentialsProviderManagerStub.calledOnce) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should initialize credentials provider manager when authMode is undefined', async function () { |
| 107 | + isAmazonQStub.returns(true) |
| 108 | + isSageMakerStub.returns(true) |
| 109 | + executeCommandStub.withArgs('sagemaker.parseCookies').resolves({} as SagemakerCookie) |
| 110 | + |
| 111 | + await initialize(mockLoginManager) |
| 112 | + |
| 113 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 114 | + assert.ok(initializeCredentialsProviderManagerStub.calledOnce) |
| 115 | + }) |
| 116 | + |
| 117 | + it('should warn and not throw when sagemaker.parseCookies command is not found', async function () { |
| 118 | + isAmazonQStub.returns(true) |
| 119 | + isSageMakerStub.returns(true) |
| 120 | + const error = new Error("command 'sagemaker.parseCookies' not found") |
| 121 | + executeCommandStub.withArgs('sagemaker.parseCookies').rejects(error) |
| 122 | + getErrorMsgStub.returns("command 'sagemaker.parseCookies' not found") |
| 123 | + |
| 124 | + await initialize(mockLoginManager) |
| 125 | + |
| 126 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 127 | + assert.ok(getErrorMsgStub.calledOnceWith(error)) |
| 128 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 129 | + }) |
| 130 | + |
| 131 | + it('should throw when sagemaker.parseCookies fails with non-command-not-found error', async function () { |
| 132 | + isAmazonQStub.returns(true) |
| 133 | + isSageMakerStub.returns(true) |
| 134 | + const error = new Error('Some other error') |
| 135 | + executeCommandStub.withArgs('sagemaker.parseCookies').rejects(error) |
| 136 | + getErrorMsgStub.returns('Some other error') |
| 137 | + |
| 138 | + await assert.rejects(initialize(mockLoginManager), /Some other error/) |
| 139 | + |
| 140 | + assert.ok(executeCommandStub.calledOnceWith('sagemaker.parseCookies')) |
| 141 | + assert.ok(getErrorMsgStub.calledOnceWith(error)) |
| 142 | + assert.ok(!mockLogger.warn.called) |
| 143 | + assert.ok(!initializeCredentialsProviderManagerStub.called) |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments