forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next' into next-storage-list
- Loading branch information
Showing
38 changed files
with
3,053 additions
and
743 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages/auth/__tests__/providers/cognito/refreshToken.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
packages/auth/__tests__/providers/cognito/testUtils/data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
}, | ||
}; |
Oops, something went wrong.