-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(auth): custom
userPoolEndpoint
cannot be applied on the server-…
…side (#13739) * chore(auth): add the service client factories into foundation * refactor(auth): use service clients in foundation * chore(auth): refactor existing unit test suites * chore(auth): add unit tests for newly added modules * chore(auth): add missing license header * chore(repo): update sdk type extractio destination for the auth package * chore: resolve most of the comments from cshfang * chore: resolve most of the comments from cshfang cont. * chore: resolve most of the comments from cshfang cont. 2 * chore: flatten out the /core from foundation * chore: update outdate test file name to match the src changes
- Loading branch information
Showing
117 changed files
with
2,775 additions
and
987 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/auth/__tests__/foundation/cognitoUserPoolEndpointResolver.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,16 @@ | ||
import { AmplifyUrl } from '@aws-amplify/core/internals/utils'; | ||
|
||
import { cognitoUserPoolEndpointResolver } from '../../src/foundation/cognitoUserPoolEndpointResolver'; | ||
import { COGNITO_IDP_SERVICE_NAME } from '../../src/foundation/constants'; | ||
|
||
describe('cognitoUserPoolEndpointResolver', () => { | ||
it('should return the Cognito User Pool endpoint', () => { | ||
const region = 'us-west-2'; | ||
const { url } = cognitoUserPoolEndpointResolver({ region }); | ||
|
||
expect(url instanceof AmplifyUrl).toBe(true); | ||
expect(url.toString()).toEqual( | ||
`https://${COGNITO_IDP_SERVICE_NAME}.us-west-2.amazonaws.com/`, | ||
); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
.../auth/__tests__/foundation/factories/serviceClients/cognitoIdentityProvider/index.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,41 @@ | ||
import { composeServiceApi } from '@aws-amplify/core/internals/aws-client-utils/composers'; | ||
|
||
import * as serviceClients from '../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider'; | ||
import { DEFAULT_SERVICE_CLIENT_API_CONFIG } from '../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/constants'; | ||
|
||
import { mockServiceClientAPIConfig } from './testUtils/data'; | ||
|
||
jest.mock('@aws-amplify/core/internals/aws-client-utils/composers', () => ({ | ||
...jest.requireActual( | ||
'@aws-amplify/core/internals/aws-client-utils/composers', | ||
), | ||
composeServiceApi: jest.fn(), | ||
})); | ||
|
||
export const mockComposeServiceApi = jest.mocked(composeServiceApi); | ||
|
||
describe('service clients', () => { | ||
const serviceClientFactories = Object.keys(serviceClients); | ||
|
||
afterEach(() => { | ||
mockComposeServiceApi.mockClear(); | ||
}); | ||
|
||
test.each(serviceClientFactories)( | ||
'factory `%s` should invoke composeServiceApi with expected parameters', | ||
serviceClientFactory => { | ||
// eslint-disable-next-line import/namespace | ||
serviceClients[serviceClientFactory](mockServiceClientAPIConfig); | ||
|
||
expect(mockComposeServiceApi).toHaveBeenCalledWith( | ||
expect.any(Function), | ||
expect.any(Function), | ||
expect.any(Function), | ||
expect.objectContaining({ | ||
...DEFAULT_SERVICE_CLIENT_API_CONFIG, | ||
...mockServiceClientAPIConfig, | ||
}), | ||
); | ||
}, | ||
); | ||
}); |
43 changes: 43 additions & 0 deletions
43
...viceClients/cognitoIdentityProvider/shared/handler/cognitoUserPoolTransferHandler.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,43 @@ | ||
import { unauthenticatedHandler } from '@aws-amplify/core/internals/aws-client-utils'; | ||
import { composeTransferHandler } from '@aws-amplify/core/internals/aws-client-utils/composers'; | ||
|
||
import { cognitoUserPoolTransferHandler } from '../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/handler'; | ||
|
||
jest.mock('@aws-amplify/core/internals/aws-client-utils/composers'); | ||
jest.mock('@aws-amplify/core/internals/aws-client-utils'); | ||
|
||
const mockComposeTransferHandler = jest.mocked(composeTransferHandler); | ||
const mockUnauthenticatedHandler = jest.mocked(unauthenticatedHandler); | ||
|
||
describe('cognitoUserPoolTransferHandler', () => { | ||
beforeAll(() => { | ||
// need to make sure cognitoUserPoolTransferHandler is imported and used in | ||
// the scope of the test | ||
const _ = cognitoUserPoolTransferHandler; | ||
}); | ||
|
||
it('adds the disableCacheMiddlewareFactory at module loading', () => { | ||
expect(mockComposeTransferHandler).toHaveBeenCalledTimes(1); | ||
|
||
const [core, middleware] = mockComposeTransferHandler.mock.calls[0]; | ||
|
||
expect(core).toStrictEqual(mockUnauthenticatedHandler); | ||
expect(middleware).toHaveLength(1); | ||
|
||
const disableCacheMiddlewareFactory = middleware[0] as any; | ||
const disableCacheMiddlewarePendingNext = disableCacheMiddlewareFactory(); | ||
|
||
const mockNext = jest.fn(); | ||
const disableCacheMiddleware = disableCacheMiddlewarePendingNext(mockNext); | ||
const mockRequest = { | ||
headers: {}, | ||
}; | ||
|
||
disableCacheMiddleware(mockRequest); | ||
|
||
expect(mockNext).toHaveBeenCalledWith(mockRequest); | ||
expect(mockRequest.headers).toEqual({ | ||
'cache-control': 'no-store', | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
...rviceClients/cognitoIdentityProvider/shared/serde/createEmptyResponseDeserializer.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,55 @@ | ||
import { | ||
HttpResponse, | ||
parseJsonError, | ||
} from '@aws-amplify/core/internals/aws-client-utils'; | ||
|
||
import { createEmptyResponseDeserializer } from '../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createEmptyResponseDeserializer'; | ||
import { AuthError } from '../../../../../../../src/errors/AuthError'; | ||
|
||
jest.mock('@aws-amplify/core/internals/aws-client-utils'); | ||
|
||
const mockParseJsonError = jest.mocked(parseJsonError); | ||
|
||
describe('createEmptyResponseDeserializer created response deserializer', () => { | ||
const deserializer = createEmptyResponseDeserializer(); | ||
|
||
it('returns undefined for 2xx status code', async () => { | ||
const response: HttpResponse = { | ||
statusCode: 200, | ||
body: { | ||
json: () => Promise.resolve({}), | ||
blob: () => Promise.resolve(new Blob()), | ||
text: () => Promise.resolve(''), | ||
}, | ||
headers: {}, | ||
}; | ||
const output = await deserializer(response); | ||
|
||
expect(output).toBeUndefined(); | ||
}); | ||
|
||
it('throws AuthError for 4xx status code', async () => { | ||
const expectedErrorName = 'TestError'; | ||
const expectedErrorMessage = 'TestErrorMessage'; | ||
const expectedError = new Error(expectedErrorMessage); | ||
expectedError.name = expectedErrorName; | ||
|
||
mockParseJsonError.mockReturnValueOnce(expectedError as any); | ||
const response: HttpResponse = { | ||
statusCode: 400, | ||
body: { | ||
json: () => Promise.resolve({}), | ||
blob: () => Promise.resolve(new Blob()), | ||
text: () => Promise.resolve(''), | ||
}, | ||
headers: {}, | ||
}; | ||
|
||
expect(deserializer(response as any)).rejects.toThrow( | ||
new AuthError({ | ||
name: expectedErrorName, | ||
message: expectedErrorMessage, | ||
}), | ||
); | ||
}); | ||
}); |
59 changes: 59 additions & 0 deletions
59
...es/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer.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,59 @@ | ||
import { | ||
HttpResponse, | ||
parseJsonBody, | ||
parseJsonError, | ||
} from '@aws-amplify/core/internals/aws-client-utils'; | ||
|
||
import { createUserPoolDeserializer } from '../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolDeserializer'; | ||
import { AuthError } from '../../../../../../../src/errors/AuthError'; | ||
|
||
jest.mock('@aws-amplify/core/internals/aws-client-utils'); | ||
|
||
const mockParseJsonBody = jest.mocked(parseJsonBody); | ||
const mockParseJsonError = jest.mocked(parseJsonError); | ||
|
||
describe('buildUserPoolDeserializer created response deserializer', () => { | ||
const deserializer = createUserPoolDeserializer(); | ||
|
||
it('returns body for 2xx status code', async () => { | ||
const expectedBody = { test: 'test' }; | ||
mockParseJsonBody.mockResolvedValueOnce(expectedBody); | ||
const response: HttpResponse = { | ||
statusCode: 200, | ||
body: { | ||
json: () => Promise.resolve({}), | ||
blob: () => Promise.resolve(new Blob()), | ||
text: () => Promise.resolve(''), | ||
}, | ||
headers: {}, | ||
}; | ||
const output = await deserializer(response); | ||
|
||
expect(output).toStrictEqual(expectedBody); | ||
}); | ||
|
||
it('throws AuthError for 4xx status code', async () => { | ||
const expectedErrorName = 'TestError'; | ||
const expectedErrorMessage = 'TestErrorMessage'; | ||
const expectedError = new Error(expectedErrorMessage); | ||
expectedError.name = expectedErrorName; | ||
|
||
mockParseJsonError.mockReturnValueOnce(expectedError as any); | ||
const response: HttpResponse = { | ||
statusCode: 400, | ||
body: { | ||
json: () => Promise.resolve({}), | ||
blob: () => Promise.resolve(new Blob()), | ||
text: () => Promise.resolve(''), | ||
}, | ||
headers: {}, | ||
}; | ||
|
||
expect(deserializer(response as any)).rejects.toThrow( | ||
new AuthError({ | ||
name: expectedErrorName, | ||
message: expectedErrorMessage, | ||
}), | ||
); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
...ries/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolSerializer.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,27 @@ | ||
import { AmplifyUrl } from '@aws-amplify/core/internals/utils'; | ||
|
||
import { createUserPoolSerializer } from '../../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/shared/serde/createUserPoolSerializer'; | ||
|
||
describe('createUserPoolSerializer created request serializer', () => { | ||
test.each(['SignUp', 'InitiateAuth', 'RevokeToken'] as const)( | ||
`it serializes requests from operation %s`, | ||
operation => { | ||
const testInput = { testBody: 'testBody' }; | ||
const testEndpoint = { | ||
url: new AmplifyUrl('http://test.com'), | ||
}; | ||
const serializer = createUserPoolSerializer(operation); | ||
const result = serializer(testInput, testEndpoint); | ||
|
||
expect(result).toEqual({ | ||
method: 'POST', | ||
url: testEndpoint.url, | ||
headers: { | ||
'content-type': 'application/x-amz-json-1.1', | ||
'x-amz-target': `AWSCognitoIdentityProviderService.${operation}`, | ||
}, | ||
body: JSON.stringify(testInput), | ||
}); | ||
}, | ||
); | ||
}); |
7 changes: 7 additions & 0 deletions
7
...h/__tests__/foundation/factories/serviceClients/cognitoIdentityProvider/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,7 @@ | ||
import { ServiceClientFactoryInput } from '../../../../../../src/foundation/factories/serviceClients/cognitoIdentityProvider/types'; | ||
|
||
export const mockServiceClientAPIConfig: ServiceClientFactoryInput = { | ||
endpointResolver: jest.fn() as jest.MockedFunction< | ||
ServiceClientFactoryInput['endpointResolver'] | ||
>, | ||
}; |
47 changes: 47 additions & 0 deletions
47
packages/auth/__tests__/foundation/parsers/regionParsers.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,47 @@ | ||
import { AuthError } from '../../../src/errors/AuthError'; | ||
import { | ||
getRegionFromIdentityPoolId, | ||
getRegionFromUserPoolId, | ||
} from '../../../src/foundation/parsers/regionParsers'; | ||
|
||
describe('getRegionFromIdentityPoolId()', () => { | ||
it('returns the region from the identity pool id', () => { | ||
const identityPoolId = 'us-west-2:12345678-1234-1234-1234-123456789012'; | ||
const region = getRegionFromIdentityPoolId(identityPoolId); | ||
expect(region).toEqual('us-west-2'); | ||
}); | ||
|
||
test.each([undefined, 'invalid-id-123'])( | ||
`throws an error when the identity pool id is invalid as %p`, | ||
identityPoolId => { | ||
expect(() => getRegionFromIdentityPoolId(identityPoolId)).toThrow( | ||
new AuthError({ | ||
name: 'InvalidIdentityPoolIdException', | ||
message: 'Invalid identity pool id provided.', | ||
recoverySuggestion: | ||
'Make sure a valid identityPoolId is given in the config.', | ||
}), | ||
); | ||
}, | ||
); | ||
}); | ||
|
||
describe('getRegionFromUserPoolId()', () => { | ||
it('should return the region from the user pool id', () => { | ||
const userPoolId = 'us-west-2_12345678'; | ||
const region = getRegionFromUserPoolId(userPoolId); | ||
expect(region).toEqual('us-west-2'); | ||
}); | ||
|
||
test.each([undefined, 'invalid-id-123'])( | ||
`throws an error when the user pool id is invalid as %p`, | ||
userPoolId => { | ||
expect(() => getRegionFromUserPoolId(userPoolId)).toThrow( | ||
new AuthError({ | ||
name: 'InvalidUserPoolId', | ||
message: 'Invalid user pool id provided.', | ||
}), | ||
); | ||
}, | ||
); | ||
}); |
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
Oops, something went wrong.