Skip to content

Commit

Permalink
Merge branch 'next' into next-storage-list
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinkumar6 authored Aug 10, 2023
2 parents 62b1b3f + 68c86c5 commit 3ac7550
Show file tree
Hide file tree
Showing 38 changed files with 3,053 additions and 743 deletions.
89 changes: 89 additions & 0 deletions packages/auth/__tests__/providers/cognito/refreshToken.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { decodeJWT } from '@aws-amplify/core';
import { fetchTransferHandler } from '@aws-amplify/core/internals/aws-client-utils';
import { mockJsonResponse, mockRequestId } from './testUtils/data';
import { CognitoUserPoolTokenRefresher } from '../../../src/providers/cognito/apis/tokenRefresher';
import { CognitoAuthTokens } from '../../../src/providers/cognito/tokenProvider/types';
jest.mock('@aws-amplify/core/lib/clients/handlers/fetch');

describe('refresh token tests', () => {
test('Default Cognito Token Refresh Handler', async () => {
const succeedResponse = {
status: 200,
headers: {
'x-amzn-requestid': mockRequestId,
},
body: {
AuthenticationResult: {
AccessToken:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MTAyOTMxMzB9.YzDpgJsrB3z-ZU1XxMcXSQsMbgCzwH_e-_76rnfehh0',
ExpiresIn: 3600,
IdToken:
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MTAyOTMxMzB9.YzDpgJsrB3z-ZU1XxMcXSQsMbgCzwH_e-_76rnfehh0',
TokenType: 'Bearer',
},
ChallengeParameters: {},
$metadata: {
attempts: 1,
httpStatusCode: 200,
requestId: mockRequestId,
},
},
};
const expectedOutput: CognitoAuthTokens = {
accessToken: decodeJWT(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MTAyOTMxMzB9.YzDpgJsrB3z-ZU1XxMcXSQsMbgCzwH_e-_76rnfehh0'
),
idToken: decodeJWT(
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE3MTAyOTMxMzB9.YzDpgJsrB3z-ZU1XxMcXSQsMbgCzwH_e-_76rnfehh0'
),
metadata: {
refreshToken: 'refreshtoken',
},
clockDrift: 0,
};
const expectedRequest = {
url: new URL('https://cognito-idp.us-east-1.amazonaws.com/'),
method: 'POST',
headers: expect.objectContaining({
'cache-control': 'no-store',
'content-type': 'application/x-amz-json-1.1',
'x-amz-target': 'AWSCognitoIdentityProviderService.InitiateAuth',
'x-amz-user-agent': 'aws-amplify/6.0.0 framework/0',
}),
body: JSON.stringify({
ClientId: 'aaaaaaaaaaaa',
AuthFlow: 'REFRESH_TOKEN_AUTH',
AuthParameters: {
REFRESH_TOKEN: 'refreshtoken',
},
}),
};

(fetchTransferHandler as jest.Mock).mockResolvedValue(
mockJsonResponse(succeedResponse)
);
const response = await CognitoUserPoolTokenRefresher({
tokens: {
accessToken: {
payload: {},
},
clockDrift: 0,
metadata: {
refreshToken: 'refreshtoken',
},
},
authConfig: {
userPoolId: 'us-east-1_aaaaaaa',
userPoolWebClientId: 'aaaaaaaaaaaa',
},
});

expect(response.accessToken.toString()).toEqual(
expectedOutput.accessToken.toString()
);
expect(fetchTransferHandler).toBeCalledWith(
expectedRequest,
expect.anything()
);
});
});
49 changes: 49 additions & 0 deletions packages/auth/__tests__/providers/cognito/testUtils/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { HttpResponse } from '@aws-amplify/core/src/clients/types';

// Common
const region = 'us-east-1';

export const mockJsonResponse = ({
status,
headers,
body,
}: {
status: number;
headers: Record<string, string>;
body: any;
}): HttpResponse => {
const responseBody = {
json: async () => body,
blob: async () => fail('blob() should not be called'),
text: async () => fail('text() should not be called'),
} as HttpResponse['body'];
return {
statusCode: status,
headers,
body: responseBody,
};
};

export const mockRequestId = 'ff1ca798-b930-4b81-9ef3-c02e770188af';

export const mockTokens = {
SecretKey: 'secret-access-key',
SessionToken: 'session-token',
Expiration: 1442877512.0,
AccessKeyId: 'access-key-id',
};

export const mockFailureResponse = {
status: 403,
headers: {
'x-amzn-requestid': mockRequestId,
'x-amzn-errortype': 'ForbiddenException',
},
body: {
__type: 'ForbiddenException',
message: `Forbidden`,
},
};
Loading

0 comments on commit 3ac7550

Please sign in to comment.