|
5 | 5 | import * as vscode from 'vscode'
|
6 | 6 | import assert from 'assert'
|
7 | 7 | import { FakeExtensionContext } from '../fakeExtensionContext'
|
8 |
| -import { ExtensionUse } from '../../auth/utils' |
| 8 | +import { BuilderIdKind, ExtensionUse, SsoKind, hasBuilderId, hasIamCredentials, hasSso } from '../../auth/utils' |
| 9 | +import { Connection, SsoConnection, codecatalystScopes, codewhispererScopes } from '../../auth/connection' |
| 10 | +import { builderIdConnection, iamConnection, ssoConnection } from './testUtil' |
9 | 11 |
|
10 | 12 | describe('ExtensionUse.isFirstUse()', function () {
|
11 | 13 | let fakeState: vscode.Memento
|
@@ -62,3 +64,104 @@ describe('ExtensionUse.isFirstUse()', function () {
|
62 | 64 | return new ExtensionUse()
|
63 | 65 | }
|
64 | 66 | })
|
| 67 | + |
| 68 | +type SsoTestCase = { kind: SsoKind; connections: Connection[]; expected: boolean } |
| 69 | +type BuilderIdTestCase = { kind: BuilderIdKind; connections: Connection[]; expected: boolean } |
| 70 | + |
| 71 | +describe('connection exists funcs', function () { |
| 72 | + const cwIdcConnection: SsoConnection = { ...ssoConnection, scopes: codewhispererScopes, label: 'codeWhispererSso' } |
| 73 | + const cwBuilderIdConnection: SsoConnection = { |
| 74 | + ...builderIdConnection, |
| 75 | + scopes: codewhispererScopes, |
| 76 | + label: 'codeWhispererBuilderId', |
| 77 | + } |
| 78 | + const ccBuilderIdConnection: SsoConnection = { |
| 79 | + ...builderIdConnection, |
| 80 | + scopes: codecatalystScopes, |
| 81 | + label: 'codeCatalystBuilderId', |
| 82 | + } |
| 83 | + const ssoConnections: Connection[] = [ |
| 84 | + ssoConnection, |
| 85 | + builderIdConnection, |
| 86 | + cwIdcConnection, |
| 87 | + cwBuilderIdConnection, |
| 88 | + ccBuilderIdConnection, |
| 89 | + ] |
| 90 | + const allConnections = [iamConnection, ...ssoConnections] |
| 91 | + |
| 92 | + describe('ssoExists()', function () { |
| 93 | + const anyCases: SsoTestCase[] = [ |
| 94 | + { connections: [ssoConnection], expected: true }, |
| 95 | + { connections: allConnections, expected: true }, |
| 96 | + { connections: [], expected: false }, |
| 97 | + { connections: [iamConnection], expected: false }, |
| 98 | + ].map(c => { |
| 99 | + return { ...c, kind: 'any' } |
| 100 | + }) |
| 101 | + const cwIdcCases: SsoTestCase[] = [ |
| 102 | + { connections: [cwIdcConnection], expected: true }, |
| 103 | + { connections: allConnections, expected: true }, |
| 104 | + { connections: [], expected: false }, |
| 105 | + { connections: allConnections.filter(c => c !== cwIdcConnection), expected: false }, |
| 106 | + ].map(c => { |
| 107 | + return { ...c, kind: 'codewhisperer' } |
| 108 | + }) |
| 109 | + const allCases = [...anyCases, ...cwIdcCases] |
| 110 | + |
| 111 | + allCases.forEach(args => { |
| 112 | + it(`ssoExists() returns '${args.expected}' when kind '${args.kind}' given [${args.connections |
| 113 | + .map(c => c.label) |
| 114 | + .join(', ')}]`, async function () { |
| 115 | + assert.strictEqual(await hasSso(args.kind, async () => args.connections), args.expected) |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('builderIdExists()', function () { |
| 121 | + const cwBuilderIdCases: BuilderIdTestCase[] = [ |
| 122 | + { connections: [cwBuilderIdConnection], expected: true }, |
| 123 | + { connections: allConnections, expected: true }, |
| 124 | + { connections: [], expected: false }, |
| 125 | + { connections: allConnections.filter(c => c !== cwBuilderIdConnection), expected: false }, |
| 126 | + ].map(c => { |
| 127 | + return { ...c, kind: 'codewhisperer' } |
| 128 | + }) |
| 129 | + |
| 130 | + const ccBuilderIdCases: BuilderIdTestCase[] = [ |
| 131 | + {connections: [ccBuilderIdConnection], expected: true}, |
| 132 | + {connections: allConnections, expected: true}, |
| 133 | + {connections: [], expected: false}, |
| 134 | + {connections: allConnections.filter(c => c !== ccBuilderIdConnection), expected: false}, |
| 135 | + ].map(c => { return {...c, kind: 'codecatalyst'}}) |
| 136 | + |
| 137 | + const allCases = [...cwBuilderIdCases, ...ccBuilderIdCases] |
| 138 | + |
| 139 | + allCases.forEach(args => { |
| 140 | + it(`builderIdExists() returns '${args.expected}' when kind '${args.kind}' given [${args.connections |
| 141 | + .map(c => c.label) |
| 142 | + .join(', ')}]`, async function () { |
| 143 | + assert.strictEqual(await hasBuilderId(args.kind, async () => args.connections), args.expected) |
| 144 | + }) |
| 145 | + }) |
| 146 | + }) |
| 147 | + |
| 148 | + describe('credentialExists()', function () { |
| 149 | + const cases: [Connection[], boolean][] = [ |
| 150 | + [[iamConnection], true], |
| 151 | + [allConnections, true], |
| 152 | + [[], false], |
| 153 | + [allConnections.filter(c => c !== iamConnection), false], |
| 154 | + ] |
| 155 | + |
| 156 | + cases.forEach(args => { |
| 157 | + it(`credentialExists() returns '${args[1]}' given [${args[0] |
| 158 | + .map(c => c.label) |
| 159 | + .join(', ')}]`, async function () { |
| 160 | + const connections = args[0] |
| 161 | + const expected = args[1] |
| 162 | + |
| 163 | + assert.strictEqual(await hasIamCredentials(async () => connections), expected) |
| 164 | + }) |
| 165 | + }) |
| 166 | + }) |
| 167 | +}) |
0 commit comments