From fa7817a69c0cbc6796fd6b48582320f07e331dc5 Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Thu, 16 Jan 2025 12:37:04 -0300 Subject: [PATCH] test(oauth): UT ensures new token is bubbled up Updated UT to ensure `AuthInfo.refreshFn` always bubbles up the new, refreshed access token instead of the old one. --- test/unit/org/authInfo.test.ts | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/test/unit/org/authInfo.test.ts b/test/unit/org/authInfo.test.ts index dbd3ce709..3697c87d7 100644 --- a/test/unit/org/authInfo.test.ts +++ b/test/unit/org/authInfo.test.ts @@ -1096,14 +1096,25 @@ describe('AuthInfo', () => { describe('refreshFn', () => { it('should call init() and save()', async () => { + const refreshedToken = '123456789abc'; + const context = { getUsername: () => '', - getFields: (decrypt = false) => ({ - loginUrl: testOrg.loginUrl, - clientId: testOrg.clientId, - privateKey: 'authInfoTest/jwt/server.key', - accessToken: decrypt ? testOrg.accessToken : testOrg.encryptedAccessToken, - }), + getFields: $$.SANDBOX.stub() + .onFirstCall() + .callsFake((decrypt = false) => ({ + loginUrl: testOrg.loginUrl, + clientId: testOrg.clientId, + privateKey: 'authInfoTest/jwt/server.key', + accessToken: decrypt ? testOrg.accessToken : testOrg.encryptedAccessToken, + })) + .onSecondCall() + .callsFake((decrypt = false) => ({ + loginUrl: testOrg.loginUrl, + clientId: testOrg.clientId, + privateKey: 'authInfoTest/jwt/server.key', + accessToken: decrypt ? refreshedToken : testOrg.encryptedAccessToken, + })), initAuthOptions: $$.SANDBOX.stub(), save: $$.SANDBOX.stub(), logger: $$.TEST_LOGGER, @@ -1119,15 +1130,18 @@ describe('AuthInfo', () => { expect(context.initAuthOptions.called, 'Should have called AuthInfo.initAuthOptions() during refreshFn()').to.be .true; const expectedInitArgs = { - loginUrl: context.getFields().loginUrl, - clientId: context.getFields().clientId, - privateKey: context.getFields().privateKey, + loginUrl: testOrg.loginUrl, + clientId: testOrg.clientId, + privateKey: testOrg.privateKey, accessToken: testOrg.accessToken, }; expect(context.initAuthOptions.firstCall.args[0]).to.deep.equal(expectedInitArgs); expect(context.save.called, 'Should have called AuthInfo.save() during refreshFn()').to.be.true; expect(testCallback.called, 'Should have called the callback passed to refreshFn()').to.be.true; - expect(testCallback.firstCall.args[1]).to.equal(testOrg.accessToken); + expect( + testCallback.firstCall.args[1], + 'Should have passed the new access token to the refreshFn callback' + ).to.equal(refreshedToken); }); it('should path.resolve jwtkeyfilepath', async () => {