-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathqDeveloperProfiles.test.ts
162 lines (137 loc) · 6.14 KB
/
qDeveloperProfiles.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import * as assert from 'assert'
import { StubbedInstance, stubInterface } from 'ts-sinon'
import { CodeWhispererServiceToken } from '../codeWhispererService'
import { SsoConnectionType } from '../utils'
import {
AWSInitializationOptions,
CancellationTokenSource,
Logging,
} from '@aws/language-server-runtimes/server-interface'
import {
AmazonQDeveloperProfile,
getListAllAvailableProfilesHandler,
ListAllAvailableProfilesHandler,
signalsAWSQDeveloperProfilesEnabled,
} from './qDeveloperProfiles'
import { DEFAULT_AWS_Q_ENDPOINT_URL, DEFAULT_AWS_Q_REGION } from '../../shared/constants'
const SOME_Q_DEVELOPER_PROFILE_ARN = 'some-random-q-developer-profile-arn'
const SOME_Q_DEVELOPER_PROFILE_NAME = 'some-random-q-developer-profile-name'
const SOME_Q_ENDPOINT_URL = 'some-random-q-endpoint'
const SOME_AWS_Q_ENDPOINT = new Map([[DEFAULT_AWS_Q_REGION, DEFAULT_AWS_Q_ENDPOINT_URL]])
const SOME_AWS_Q_ENDPOINTS = new Map([
[DEFAULT_AWS_Q_REGION, DEFAULT_AWS_Q_ENDPOINT_URL],
['eu-central-1', SOME_Q_ENDPOINT_URL],
])
const EXPECTED_DEVELOPER_PROFILES_LIST: AmazonQDeveloperProfile[] = Array.from(SOME_AWS_Q_ENDPOINTS.keys(), region => ({
arn: SOME_Q_DEVELOPER_PROFILE_ARN,
name: SOME_Q_DEVELOPER_PROFILE_NAME,
identityDetails: {
region,
},
}))
const UNHAPPY_SSO_CONNECTION_TYPES: SsoConnectionType[] = ['builderId', 'none']
describe('ListAllAvailableProfiles Handler', () => {
let logging: StubbedInstance<Logging>
let codeWhispererService: StubbedInstance<CodeWhispererServiceToken>
let handler: ListAllAvailableProfilesHandler
let tokenSource: CancellationTokenSource
const listAvailableProfilesResponse = {
profiles: [
{
arn: SOME_Q_DEVELOPER_PROFILE_ARN,
profileName: SOME_Q_DEVELOPER_PROFILE_NAME,
},
],
$response: {} as any,
}
const listAvailableProfilesResponseWithNextToken = {
...listAvailableProfilesResponse,
nextToken: 'some-random-next-token',
}
beforeEach(() => {
logging = stubInterface<Logging>()
codeWhispererService = stubInterface<CodeWhispererServiceToken>()
codeWhispererService.listAvailableProfiles.resolves(listAvailableProfilesResponse)
handler = getListAllAvailableProfilesHandler(() => codeWhispererService)
tokenSource = new CancellationTokenSource()
})
it('should aggregrate profiles retrieved from different regions', async () => {
const profiles = await handler({
connectionType: 'identityCenter',
logging,
endpoints: SOME_AWS_Q_ENDPOINTS,
token: tokenSource.token,
})
assert.strictEqual(
codeWhispererService.listAvailableProfiles.callCount,
EXPECTED_DEVELOPER_PROFILES_LIST.length
)
assert.deepStrictEqual(profiles, EXPECTED_DEVELOPER_PROFILES_LIST)
})
UNHAPPY_SSO_CONNECTION_TYPES.forEach((connectionType: SsoConnectionType) => {
it(`should return an empty list when connection type equals: ${connectionType}`, async () => {
const profiles = await handler({
connectionType,
logging,
token: tokenSource.token,
})
assert.deepStrictEqual(profiles, [])
})
})
describe('Pagination', () => {
const MAX_EXPECTED_PAGES = 10
it('should paginate if nextToken is defined', async () => {
const EXPECTED_CALLS = 3
codeWhispererService.listAvailableProfiles
.onFirstCall()
.resolves(listAvailableProfilesResponseWithNextToken)
codeWhispererService.listAvailableProfiles
.onSecondCall()
.resolves(listAvailableProfilesResponseWithNextToken)
codeWhispererService.listAvailableProfiles.onThirdCall().resolves(listAvailableProfilesResponse)
const profiles = await handler({
connectionType: 'identityCenter',
logging,
endpoints: SOME_AWS_Q_ENDPOINT,
token: tokenSource.token,
})
assert.strictEqual(codeWhispererService.listAvailableProfiles.callCount, EXPECTED_CALLS)
assert.deepStrictEqual(profiles, Array(EXPECTED_CALLS).fill(EXPECTED_DEVELOPER_PROFILES_LIST[0]))
})
it(`should retrieve at most ${MAX_EXPECTED_PAGES} pages`, async () => {
codeWhispererService.listAvailableProfiles.resolves(listAvailableProfilesResponseWithNextToken)
const profiles = await handler({
connectionType: 'identityCenter',
logging,
endpoints: SOME_AWS_Q_ENDPOINT,
token: tokenSource.token,
})
assert.strictEqual(codeWhispererService.listAvailableProfiles.callCount, MAX_EXPECTED_PAGES)
assert.deepStrictEqual(profiles, Array(MAX_EXPECTED_PAGES).fill(EXPECTED_DEVELOPER_PROFILES_LIST[0]))
})
})
})
describe('signalsAWSQDeveloperProfilesEnabled', () => {
const makeQCapability = (value?: any) => {
return value !== undefined ? { developerProfiles: value } : {}
}
const makeInitOptions = (value?: any): AWSInitializationOptions => {
return { awsClientCapabilities: { q: makeQCapability(value) } }
}
const TEST_CASES: { input: AWSInitializationOptions; expected: boolean }[] = [
{ input: {}, expected: false },
{ input: { awsClientCapabilities: {} }, expected: false },
{ input: makeInitOptions(), expected: false },
{ input: makeInitOptions([]), expected: false },
{ input: makeInitOptions({}), expected: false },
{ input: makeInitOptions(42), expected: false },
{ input: makeInitOptions('some-string'), expected: false },
{ input: makeInitOptions(false), expected: false },
{ input: makeInitOptions(true), expected: true },
]
TEST_CASES.forEach(testCase => {
it(`should return: ${testCase.expected} when passed: ${JSON.stringify(testCase.input)}`, () => {
assert.strictEqual(signalsAWSQDeveloperProfilesEnabled(testCase.input), testCase.expected)
})
})
})