|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as sinon from 'sinon' |
| 7 | +import assert from 'assert' |
| 8 | +import { tryRegister } from '../testUtil' |
| 9 | +import { |
| 10 | + amazonQScopes, |
| 11 | + AuthUtil, |
| 12 | + baseCustomization, |
| 13 | + Customization, |
| 14 | + FeatureConfigProvider, |
| 15 | + getSelectedCustomization, |
| 16 | + refreshStatusBar, |
| 17 | + setSelectedCustomization, |
| 18 | +} from '../../codewhisperer' |
| 19 | +import { FeatureContext, globals } from '../../shared' |
| 20 | +import { resetCodeWhispererGlobalVariables } from '../codewhisperer/testUtil' |
| 21 | +import { createSsoProfile, createTestAuth } from '../credentials/testUtil' |
| 22 | +import { SsoConnection } from '../../auth' |
| 23 | + |
| 24 | +const enterpriseSsoStartUrl = 'https://enterprise.awsapps.com/start' |
| 25 | + |
| 26 | +describe('CodeWhisperer-customizationUtils', function () { |
| 27 | + let auth: ReturnType<typeof createTestAuth> |
| 28 | + let ssoConn: SsoConnection |
| 29 | + let featureCustomization: FeatureContext |
| 30 | + |
| 31 | + before(async function () { |
| 32 | + createTestAuth(globals.globalState) |
| 33 | + tryRegister(refreshStatusBar) |
| 34 | + }) |
| 35 | + |
| 36 | + beforeEach(async function () { |
| 37 | + auth = createTestAuth(globals.globalState) |
| 38 | + ssoConn = await auth.createInvalidSsoConnection( |
| 39 | + createSsoProfile({ startUrl: enterpriseSsoStartUrl, scopes: amazonQScopes }) |
| 40 | + ) |
| 41 | + featureCustomization = { |
| 42 | + name: 'featureCustomizationName', |
| 43 | + value: { |
| 44 | + stringValue: 'featureCustomizationArn', |
| 45 | + }, |
| 46 | + variation: 'featureCustomizationName', |
| 47 | + } |
| 48 | + sinon.stub(FeatureConfigProvider, 'getFeature').returns(featureCustomization) |
| 49 | + |
| 50 | + sinon.stub(AuthUtil.instance, 'isConnectionExpired').returns(false) |
| 51 | + sinon.stub(AuthUtil.instance, 'isConnected').returns(true) |
| 52 | + sinon.stub(AuthUtil.instance, 'isCustomizationFeatureEnabled').value(true) |
| 53 | + sinon.stub(AuthUtil.instance, 'conn').value(ssoConn) |
| 54 | + |
| 55 | + await resetCodeWhispererGlobalVariables() |
| 56 | + }) |
| 57 | + |
| 58 | + afterEach(function () { |
| 59 | + sinon.restore() |
| 60 | + }) |
| 61 | + |
| 62 | + it('Returns baseCustomization when not SSO', async function () { |
| 63 | + sinon.stub(AuthUtil.instance, 'isValidEnterpriseSsoInUse').returns(false) |
| 64 | + const customization = getSelectedCustomization() |
| 65 | + |
| 66 | + assert.strictEqual(customization.name, baseCustomization.name) |
| 67 | + }) |
| 68 | + |
| 69 | + it('Returns selectedCustomization when customization manually selected', async function () { |
| 70 | + sinon.stub(AuthUtil.instance, 'isValidEnterpriseSsoInUse').returns(true) |
| 71 | + |
| 72 | + const selectedCustomization: Customization = { |
| 73 | + arn: 'selectedCustomizationArn', |
| 74 | + name: 'selectedCustomizationName', |
| 75 | + description: 'selectedCustomizationDescription', |
| 76 | + } |
| 77 | + |
| 78 | + await setSelectedCustomization(selectedCustomization) |
| 79 | + |
| 80 | + const actualCustomization = getSelectedCustomization() |
| 81 | + |
| 82 | + assert.strictEqual(actualCustomization.name, selectedCustomization.name) |
| 83 | + }) |
| 84 | + |
| 85 | + it('Returns AB customization', async function () { |
| 86 | + sinon.stub(AuthUtil.instance, 'isValidEnterpriseSsoInUse').returns(true) |
| 87 | + |
| 88 | + await setSelectedCustomization(baseCustomization) |
| 89 | + |
| 90 | + const returnedCustomization = getSelectedCustomization() |
| 91 | + |
| 92 | + assert.strictEqual(returnedCustomization.name, featureCustomization.name) |
| 93 | + }) |
| 94 | +}) |
0 commit comments