55
66import * as sinon from 'sinon'
77import * as vscode from 'vscode'
8+ import * as jose from 'jose'
89import { LanguageClientAuth , SsoLogin , IamLogin } from '../../auth/auth2'
910import { LanguageClient } from 'vscode-languageclient'
1011import {
@@ -40,7 +41,7 @@ const tokenId = 'test-token'
4041describe ( 'LanguageClientAuth' , ( ) => {
4142 let client : sinon . SinonStubbedInstance < LanguageClient >
4243 let auth : LanguageClientAuth
43- const encryptionKey = Buffer . from ( 'test-key' )
44+ const encryptionKey = Buffer . from ( 'test-key' . padEnd ( 32 , '0' ) )
4445 let useDeviceFlowStub : sinon . SinonStub
4546
4647 beforeEach ( ( ) => {
@@ -53,6 +54,14 @@ describe('LanguageClientAuth', () => {
5354 sinon . restore ( )
5455 } )
5556
57+ async function encrypt < T > ( request : T ) : Promise < string > {
58+ const payload = new TextEncoder ( ) . encode ( JSON . stringify ( request ) )
59+ const encrypted = await new jose . CompactEncrypt ( payload )
60+ . setProtectedHeader ( { alg : 'dir' , enc : 'A256GCM' } )
61+ . encrypt ( encryptionKey )
62+ return encrypted
63+ }
64+
5665 describe ( 'getSsoToken' , ( ) => {
5766 async function testGetSsoToken ( useDeviceFlow : boolean ) {
5867 const tokenSource = {
@@ -61,6 +70,16 @@ describe('LanguageClientAuth', () => {
6170 }
6271 useDeviceFlowStub . returns ( useDeviceFlow ? true : false )
6372
73+ client . sendRequest . resolves ( {
74+ ssoToken : {
75+ id : 'my-id' ,
76+ accessToken : 'my-access-token' ,
77+ } ,
78+ updateCredentialsParams : {
79+ data : 'my-data' ,
80+ } ,
81+ } satisfies GetSsoTokenResult )
82+
6483 await auth . getSsoToken ( tokenSource , true )
6584
6685 sinon . assert . calledOnce ( client . sendRequest )
@@ -94,13 +113,6 @@ describe('LanguageClientAuth', () => {
94113 await auth . updateSsoProfile ( profileName , startUrl , region , [ 'scope1' ] )
95114
96115 sinon . assert . calledOnce ( client . sendRequest )
97- const requestParams = client . sendRequest . firstCall . args [ 1 ]
98- sinon . assert . match ( requestParams . profile , {
99- name : profileName ,
100- } )
101- sinon . assert . match ( requestParams . ssoSession . settings , {
102- sso_region : region ,
103- } )
104116 } )
105117
106118 it ( 'sends correct IAM profile update parameters' , async ( ) => {
@@ -111,18 +123,6 @@ describe('LanguageClientAuth', () => {
111123 } )
112124
113125 sinon . assert . calledOnce ( client . sendRequest )
114- const requestParams = client . sendRequest . firstCall . args [ 1 ]
115- sinon . assert . match ( requestParams . profile , {
116- name : profileName ,
117- kinds : [ ProfileKind . IamCredentialsProfile ] ,
118- } )
119- sinon . assert . match ( requestParams . profile . settings , {
120- aws_access_key_id : 'myAccessKey' ,
121- aws_secret_access_key : 'mySecretKey' ,
122- aws_session_token : 'mySessionToken' ,
123- role_arn : '' ,
124- source_profile : '' ,
125- } )
126126 } )
127127 } )
128128
@@ -213,6 +213,21 @@ describe('LanguageClientAuth', () => {
213213
214214 describe ( 'getIamCredential' , ( ) => {
215215 it ( 'sends correct request parameters' , async ( ) => {
216+ client . sendRequest . resolves ( {
217+ credential : {
218+ id : 'my-id' ,
219+ kinds : [ ] ,
220+ credentials : {
221+ accessKeyId : 'my-access-key' ,
222+ secretAccessKey : 'my-secret-key' ,
223+ sessionToken : 'my-session-token' ,
224+ } ,
225+ } ,
226+ updateCredentialsParams : {
227+ data : 'my-data' ,
228+ } ,
229+ } satisfies GetIamCredentialResult )
230+
216231 await auth . getIamCredential ( profileName , true )
217232
218233 sinon . assert . calledOnce ( client . sendRequest )
0 commit comments