5
5
6
6
import * as sinon from 'sinon'
7
7
import * as vscode from 'vscode'
8
+ import * as jose from 'jose'
8
9
import { LanguageClientAuth , SsoLogin , IamLogin } from '../../auth/auth2'
9
10
import { LanguageClient } from 'vscode-languageclient'
10
11
import {
@@ -40,7 +41,7 @@ const tokenId = 'test-token'
40
41
describe ( 'LanguageClientAuth' , ( ) => {
41
42
let client : sinon . SinonStubbedInstance < LanguageClient >
42
43
let auth : LanguageClientAuth
43
- const encryptionKey = Buffer . from ( 'test-key' )
44
+ const encryptionKey = Buffer . from ( 'test-key' . padEnd ( 32 , '0' ) )
44
45
let useDeviceFlowStub : sinon . SinonStub
45
46
46
47
beforeEach ( ( ) => {
@@ -53,6 +54,14 @@ describe('LanguageClientAuth', () => {
53
54
sinon . restore ( )
54
55
} )
55
56
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
+
56
65
describe ( 'getSsoToken' , ( ) => {
57
66
async function testGetSsoToken ( useDeviceFlow : boolean ) {
58
67
const tokenSource = {
@@ -61,6 +70,16 @@ describe('LanguageClientAuth', () => {
61
70
}
62
71
useDeviceFlowStub . returns ( useDeviceFlow ? true : false )
63
72
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
+
64
83
await auth . getSsoToken ( tokenSource , true )
65
84
66
85
sinon . assert . calledOnce ( client . sendRequest )
@@ -94,13 +113,6 @@ describe('LanguageClientAuth', () => {
94
113
await auth . updateSsoProfile ( profileName , startUrl , region , [ 'scope1' ] )
95
114
96
115
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
- } )
104
116
} )
105
117
106
118
it ( 'sends correct IAM profile update parameters' , async ( ) => {
@@ -111,18 +123,6 @@ describe('LanguageClientAuth', () => {
111
123
} )
112
124
113
125
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
- } )
126
126
} )
127
127
} )
128
128
@@ -213,6 +213,21 @@ describe('LanguageClientAuth', () => {
213
213
214
214
describe ( 'getIamCredential' , ( ) => {
215
215
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
+
216
231
await auth . getIamCredential ( profileName , true )
217
232
218
233
sinon . assert . calledOnce ( client . sendRequest )
0 commit comments