Skip to content

Commit e09e63f

Browse files
committed
Fix unit test errors
1 parent 63daa08 commit e09e63f

File tree

2 files changed

+80
-20
lines changed

2 files changed

+80
-20
lines changed

packages/core/src/auth/auth2.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,13 @@ export class LanguageClientAuth {
139139
* Decrypts an object
140140
*/
141141
private async decrypt<T>(request: string): Promise<T> {
142-
const result = await jose.compactDecrypt(request, this.encryptionKey)
143-
return JSON.parse(new TextDecoder().decode(result.plaintext)) as T
142+
try {
143+
const result = await jose.compactDecrypt(request, this.encryptionKey)
144+
return JSON.parse(new TextDecoder().decode(result.plaintext)) as T
145+
} catch (e) {
146+
getLogger().error(`Failed to decrypt: ${request}`)
147+
return request as T
148+
}
144149
}
145150

146151
async getSsoToken(

packages/core/src/test/credentials/auth2.test.ts

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as sinon from 'sinon'
77
import * as vscode from 'vscode'
8+
import * as jose from 'jose'
89
import { LanguageClientAuth, SsoLogin, IamLogin } from '../../auth/auth2'
910
import { LanguageClient } from 'vscode-languageclient'
1011
import {
@@ -40,7 +41,7 @@ const tokenId = 'test-token'
4041
describe('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)
@@ -95,12 +114,30 @@ describe('LanguageClientAuth', () => {
95114

96115
sinon.assert.calledOnce(client.sendRequest)
97116
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-
})
117+
sinon.assert.match(
118+
requestParams,
119+
await encrypt({
120+
profile: {
121+
kinds: [ProfileKind.SsoTokenProfile],
122+
name: profileName,
123+
settings: {
124+
aws_access_key_id: '',
125+
aws_secret_access_key: '',
126+
role_arn: '',
127+
region: region,
128+
sso_session: profileName,
129+
},
130+
},
131+
ssoSession: {
132+
name: profileName,
133+
settings: {
134+
sso_region: region,
135+
sso_start_url: startUrl,
136+
sso_registration_scopes: ['scope1'],
137+
},
138+
},
139+
})
140+
)
104141
})
105142

106143
it('sends correct IAM profile update parameters', async () => {
@@ -112,17 +149,20 @@ describe('LanguageClientAuth', () => {
112149

113150
sinon.assert.calledOnce(client.sendRequest)
114151
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-
})
152+
sinon.assert.match(
153+
requestParams,
154+
await encrypt({
155+
kinds: [ProfileKind.IamCredentialProcessProfile],
156+
name: profileName,
157+
settings: {
158+
aws_access_key_id: 'myAccessKey',
159+
aws_secret_access_key: 'mySecretKey',
160+
aws_session_token: 'mySessionToken',
161+
role_arn: '',
162+
source_profile: '',
163+
},
164+
})
165+
)
126166
})
127167
})
128168

@@ -213,6 +253,21 @@ describe('LanguageClientAuth', () => {
213253

214254
describe('getIamCredential', () => {
215255
it('sends correct request parameters', async () => {
256+
client.sendRequest.resolves({
257+
credential: {
258+
id: 'my-id',
259+
kinds: [],
260+
credentials: {
261+
accessKeyId: 'my-access-key',
262+
secretAccessKey: 'my-secret-key',
263+
sessionToken: 'my-session-token',
264+
},
265+
},
266+
updateCredentialsParams: {
267+
data: 'my-data',
268+
},
269+
} satisfies GetIamCredentialResult)
270+
216271
await auth.getIamCredential(profileName, true)
217272

218273
sinon.assert.calledOnce(client.sendRequest)

0 commit comments

Comments
 (0)