Skip to content

Commit

Permalink
chore(auth): update tokenOrchestrator unit test exp token (aws-amplif…
Browse files Browse the repository at this point in the history
…y#13117)

Co-authored-by: Ashwin Kumar <ashwsrir@amazon.com>
  • Loading branch information
ashwinkumar6 and Ashwin Kumar authored Mar 14, 2024
1 parent 79e32a9 commit 5d4ccd6
Showing 1 changed file with 63 additions and 12 deletions.
75 changes: 63 additions & 12 deletions packages/auth/__tests__/providers/cognito/tokenOrchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { TokenOrchestrator } from '../../../src/providers/cognito';
import { Hub, ResourcesConfig } from '@aws-amplify/core';
import { authAPITestParams } from './testUtils/authApiTestParams';
import { AMPLIFY_SYMBOL } from '@aws-amplify/core/internals/utils';
jest.mock('@aws-amplify/core', () => ({
...jest.requireActual('@aws-amplify/core'),
Expand Down Expand Up @@ -34,6 +33,62 @@ const validAuthConfig: ResourcesConfig = {
},
};

const currentDate = new Date();

const expiredDate = new Date();
expiredDate.setDate(currentDate.getDate() - 5);
const expiredDateInSeconds = Math.floor(expiredDate.getTime() / 1000);

const futureDate = new Date();
futureDate.setDate(currentDate.getDate() + 5);
const futureDateInSeconds = Math.floor(futureDate.getTime() / 1000);

const expiredAuthTokens = {
idToken: {
payload: {
sub: '1234567890',
name: 'John Doe',
iat: 1516239022,
exp: expiredDateInSeconds,
},
},
accessToken: {
payload: {
sub: '1234567890',
name: 'John Doe',
iat: 1516239022,
exp: expiredDateInSeconds,
},
},
accessTokenExpAt: expiredDate,
clockDrift: undefined,
metadata: undefined,
};

const validAuthTokens = {
idToken: {
payload: {
sub: '1234567890',
name: 'John Doe the second',
iat: 1516239022,
iss: 'https://test.com',
exp: futureDateInSeconds,
},
},
accessToken: {
payload: {
sub: '1234567890',
name: 'John Doe the second',
iat: 1516239022,
iss: 'https://test.com',
exp: futureDateInSeconds,
},
},
accessTokenExpAt: futureDate,
clockDrift: undefined,
metadata: undefined,
};

describe('TokenOrchestrator', () => {
const tokenOrchestrator = new TokenOrchestrator();
describe('Happy Path Cases:', () => {
Expand All @@ -44,26 +99,22 @@ describe('TokenOrchestrator', () => {
mockAuthTokenStore.getLastAuthUser.mockResolvedValue('test-username');
});
it('Should get tokens', async () => {
mockAuthTokenStore.loadTokens.mockResolvedValue(
authAPITestParams.ValidAuthTokens,
);
mockAuthTokenStore.loadTokens.mockResolvedValue(validAuthTokens);

const tokensRes = await tokenOrchestrator.getTokens();
expect(tokensRes).toEqual({
accessToken: authAPITestParams.ValidAuthTokens.accessToken,
idToken: authAPITestParams.ValidAuthTokens.idToken,
accessToken: validAuthTokens.accessToken,
idToken: validAuthTokens.idToken,
signInDetails: undefined,
});
});
it('Should call tokenRefresher and return valid tokens', async () => {
mockAuthTokenStore.loadTokens.mockResolvedValue(
authAPITestParams.ExpiredAuthTokens,
);
mockTokenRefresher.mockResolvedValue(authAPITestParams.ValidAuthTokens);
mockAuthTokenStore.loadTokens.mockResolvedValue(expiredAuthTokens);
mockTokenRefresher.mockResolvedValue(validAuthTokens);
const tokensRes = await tokenOrchestrator.getTokens();
expect(tokensRes).toEqual({
accessToken: authAPITestParams.ValidAuthTokens.accessToken,
idToken: authAPITestParams.ValidAuthTokens.idToken,
accessToken: validAuthTokens.accessToken,
idToken: validAuthTokens.idToken,
signInDetails: undefined,
});
expect(Hub.dispatch).toHaveBeenCalledWith(
Expand Down

0 comments on commit 5d4ccd6

Please sign in to comment.