@@ -7,41 +7,36 @@ import * as sinon from 'sinon'
77import assert , { fail } from 'assert'
88import { AuthUtil , RegionProfile , RegionProfileManager , defaultServiceConfig } from 'aws-core-vscode/codewhisperer'
99import { globals } from 'aws-core-vscode/shared'
10- import { createTestAuth } from 'aws-core-vscode/test'
11- import { SsoConnection } from 'aws-core-vscode/auth'
10+ import { builderIdStartUrl } from 'aws-core-vscode/auth'
1211
1312const enterpriseSsoStartUrl = 'https://enterprise.awsapps.com/start'
13+ const region = 'us-east-1'
1414
1515describe ( 'RegionProfileManager' , function ( ) {
16- let sut : RegionProfileManager
17- let auth : ReturnType < typeof createTestAuth >
18- let authUtil : AuthUtil
16+ let regionProfileManager : RegionProfileManager
1917
2018 const profileFoo : RegionProfile = {
2119 name : 'foo' ,
22- region : 'us-east-1' ,
20+ region,
2321 arn : 'foo arn' ,
2422 description : 'foo description' ,
2523 }
2624
2725 async function setupConnection ( type : 'builderId' | 'idc' ) {
2826 if ( type === 'builderId' ) {
29- await authUtil . connectToAwsBuilderId ( )
30- const conn = authUtil . conn
31- assert . strictEqual ( conn ?. type , 'sso' )
32- assert . strictEqual ( conn . label , 'AWS Builder ID' )
27+ await AuthUtil . instance . login ( builderIdStartUrl , region )
28+ assert . ok ( AuthUtil . instance . isSsoSession ( ) )
29+ assert . ok ( AuthUtil . instance . isBuilderIdConnection ( ) )
3330 } else if ( type === 'idc' ) {
34- await authUtil . connectToEnterpriseSso ( enterpriseSsoStartUrl , 'us-east-1' )
35- const conn = authUtil . conn
36- assert . strictEqual ( conn ?. type , 'sso' )
37- assert . strictEqual ( conn . label , 'IAM Identity Center (enterprise)' )
31+ await AuthUtil . instance . login ( enterpriseSsoStartUrl , region )
32+ assert . ok ( AuthUtil . instance . isSsoSession ( ) )
33+ assert . ok ( AuthUtil . instance . isIdcConnection ( ) )
3834 }
3935 }
4036
4137 beforeEach ( function ( ) {
42- auth = createTestAuth ( globals . globalState )
43- authUtil = new AuthUtil ( auth )
44- sut = new RegionProfileManager ( ( ) => authUtil . conn )
38+ regionProfileManager = new RegionProfileManager ( AuthUtil . instance )
39+ // const authUtilStub = sinon.stub(AuthUtil.instance, 'isIdcConnection').returns(isSso)
4540 } )
4641
4742 afterEach ( function ( ) {
@@ -65,9 +60,9 @@ describe('RegionProfileManager', function () {
6560 const mockClient = {
6661 listAvailableProfiles : listProfilesStub ,
6762 }
68- const createClientStub = sinon . stub ( sut , 'createQClient' ) . resolves ( mockClient )
63+ const createClientStub = sinon . stub ( regionProfileManager , 'createQClient' ) . resolves ( mockClient )
6964
70- const r = await sut . listRegionProfile ( )
65+ const r = await regionProfileManager . listRegionProfiles ( )
7166
7267 assert . strictEqual ( r . length , 2 )
7368 assert . deepStrictEqual ( r , [
@@ -93,41 +88,40 @@ describe('RegionProfileManager', function () {
9388 describe ( 'switch and get profile' , function ( ) {
9489 it ( 'should switch if connection is IdC' , async function ( ) {
9590 await setupConnection ( 'idc' )
96- await sut . switchRegionProfile ( profileFoo , 'user' )
97- assert . deepStrictEqual ( sut . activeRegionProfile , profileFoo )
91+ await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
92+ assert . deepStrictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
9893 } )
9994
10095 it ( 'should do nothing and return undefined if connection is builder id' , async function ( ) {
10196 await setupConnection ( 'builderId' )
102- await sut . switchRegionProfile ( profileFoo , 'user' )
103- assert . deepStrictEqual ( sut . activeRegionProfile , undefined )
97+ await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
98+ assert . deepStrictEqual ( regionProfileManager . activeRegionProfile , undefined )
10499 } )
105100 } )
106101
107102 describe ( `client config` , function ( ) {
108103 it ( `no valid credential should throw` , async function ( ) {
109- assert . ok ( authUtil . conn === undefined )
104+ assert . ok ( ! AuthUtil . instance . isConnected ( ) )
110105
111106 assert . throws ( ( ) => {
112- sut . clientConfig
107+ regionProfileManager . clientConfig
113108 } , / t r y i n g t o g e t c l i e n t c o n f i g u r a t i o n w i t h o u t c r e d e n t i a l / )
114109 } )
115110
116111 it ( `builder id should always use default profile IAD` , async function ( ) {
117112 await setupConnection ( 'builderId' )
118- await sut . switchRegionProfile ( profileFoo , 'user' )
119- assert . deepStrictEqual ( sut . activeRegionProfile , undefined )
120- const conn = authUtil . conn
121- if ( ! conn ) {
113+ await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
114+ assert . deepStrictEqual ( regionProfileManager . activeRegionProfile , undefined )
115+ if ( ! AuthUtil . instance . isConnected ( ) ) {
122116 fail ( 'connection should not be undefined' )
123117 }
124118
125- assert . deepStrictEqual ( sut . clientConfig , defaultServiceConfig )
119+ assert . deepStrictEqual ( regionProfileManager . clientConfig , defaultServiceConfig )
126120 } )
127121
128122 it ( `idc should return correct endpoint corresponding to profile region` , async function ( ) {
129123 await setupConnection ( 'idc' )
130- await sut . switchRegionProfile (
124+ await regionProfileManager . switchRegionProfile (
131125 {
132126 name : 'foo' ,
133127 region : 'eu-central-1' ,
@@ -136,16 +130,16 @@ describe('RegionProfileManager', function () {
136130 } ,
137131 'user'
138132 )
139- assert . ok ( sut . activeRegionProfile )
140- assert . deepStrictEqual ( sut . clientConfig , {
133+ assert . ok ( regionProfileManager . activeRegionProfile )
134+ assert . deepStrictEqual ( regionProfileManager . clientConfig , {
141135 region : 'eu-central-1' ,
142136 endpoint : 'https://q.eu-central-1.amazonaws.com/' ,
143137 } )
144138 } )
145139
146140 it ( `idc should throw if corresponding endpoint is not defined` , async function ( ) {
147141 await setupConnection ( 'idc' )
148- await sut . switchRegionProfile (
142+ await regionProfileManager . switchRegionProfile (
149143 {
150144 name : 'foo' ,
151145 region : 'unknown region' ,
@@ -156,74 +150,71 @@ describe('RegionProfileManager', function () {
156150 )
157151
158152 assert . throws ( ( ) => {
159- sut . clientConfig
153+ regionProfileManager . clientConfig
160154 } , / Q c l i e n t c o n f i g u r a t i o n e r r o r , e n d p o i n t n o t f o u n d f o r r e g i o n * / )
161155 } )
162156 } )
163157
164158 describe ( 'persistence' , function ( ) {
165159 it ( 'persistSelectedRegionProfile' , async function ( ) {
166160 await setupConnection ( 'idc' )
167- await sut . switchRegionProfile ( profileFoo , 'user' )
168- assert . deepStrictEqual ( sut . activeRegionProfile , profileFoo )
169- const conn = authUtil . conn
170- if ( ! conn ) {
161+ await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
162+ assert . deepStrictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
163+ if ( ! AuthUtil . instance . isConnected ( ) ) {
171164 fail ( 'connection should not be undefined' )
172165 }
173166
174- await sut . persistSelectRegionProfile ( )
167+ await regionProfileManager . persistSelectRegionProfile ( )
175168
176169 const state = globals . globalState . tryGet < { [ label : string ] : RegionProfile } > (
177170 'aws.amazonq.regionProfiles' ,
178171 Object ,
179172 { }
180173 )
181174
182- assert . strictEqual ( state [ conn . id ] , profileFoo )
175+ assert . strictEqual ( state [ AuthUtil . instance . profileName ] , profileFoo )
183176 } )
184177
185178 it ( `restoreRegionProfile` , async function ( ) {
186- sinon . stub ( sut , 'listRegionProfile ' ) . resolves ( [ profileFoo ] )
179+ sinon . stub ( regionProfileManager , 'listRegionProfiles ' ) . resolves ( [ profileFoo ] )
187180 await setupConnection ( 'idc' )
188- const conn = authUtil . conn
189- if ( ! conn ) {
181+ if ( ! AuthUtil . instance . isConnected ( ) ) {
190182 fail ( 'connection should not be undefined' )
191183 }
192184
193185 const state = { } as any
194- state [ conn . id ] = profileFoo
186+ state [ AuthUtil . instance . profileName ] = profileFoo
195187
196188 await globals . globalState . update ( 'aws.amazonq.regionProfiles' , state )
197189
198- await sut . restoreRegionProfile ( conn )
190+ await regionProfileManager . restoreRegionProfile ( )
199191
200- assert . strictEqual ( sut . activeRegionProfile , profileFoo )
192+ assert . strictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
201193 } )
202194 } )
203195
204196 describe ( 'invalidate' , function ( ) {
205197 it ( 'should reset activeProfile and global state' , async function ( ) {
206198 // setup
207199 await setupConnection ( 'idc' )
208- await sut . switchRegionProfile ( profileFoo , 'user' )
209- assert . deepStrictEqual ( sut . activeRegionProfile , profileFoo )
210- const conn = authUtil . conn
211- if ( ! conn ) {
200+ await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
201+ assert . deepStrictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
202+ if ( ! AuthUtil . instance . isConnected ( ) ) {
212203 fail ( 'connection should not be undefined' )
213204 }
214- await sut . persistSelectRegionProfile ( )
205+ await regionProfileManager . persistSelectRegionProfile ( )
215206 const state = globals . globalState . tryGet < { [ label : string ] : RegionProfile } > (
216207 'aws.amazonq.regionProfiles' ,
217208 Object ,
218209 { }
219210 )
220- assert . strictEqual ( state [ conn . id ] , profileFoo )
211+ assert . strictEqual ( state [ AuthUtil . instance . profileName ] , profileFoo )
221212
222213 // subject to test
223- await sut . invalidateProfile ( profileFoo . arn )
214+ await regionProfileManager . invalidateProfile ( profileFoo . arn )
224215
225216 // assertion
226- assert . strictEqual ( sut . activeRegionProfile , undefined )
217+ assert . strictEqual ( regionProfileManager . activeRegionProfile , undefined )
227218 const actualGlobalState = globals . globalState . tryGet < { [ label : string ] : RegionProfile } > (
228219 'aws.amazonq.regionProfiles' ,
229220 Object ,
@@ -236,11 +227,10 @@ describe('RegionProfileManager', function () {
236227 describe ( 'createQClient' , function ( ) {
237228 it ( `should configure the endpoint and region correspondingly` , async function ( ) {
238229 await setupConnection ( 'idc' )
239- await sut . switchRegionProfile ( profileFoo , 'user' )
240- assert . deepStrictEqual ( sut . activeRegionProfile , profileFoo )
241- const conn = authUtil . conn as SsoConnection
230+ await regionProfileManager . switchRegionProfile ( profileFoo , 'user' )
231+ assert . deepStrictEqual ( regionProfileManager . activeRegionProfile , profileFoo )
242232
243- const client = await sut . createQClient ( 'eu-central-1' , 'https://amazon.com/' , conn )
233+ const client = await regionProfileManager . createQClient ( 'eu-central-1' , 'https://amazon.com/' )
244234
245235 assert . deepStrictEqual ( client . config . region , 'eu-central-1' )
246236 assert . deepStrictEqual ( client . endpoint . href , 'https://amazon.com/' )
0 commit comments