-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathdeleteUserAttributes.test.ts
More file actions
114 lines (102 loc) · 3.75 KB
/
deleteUserAttributes.test.ts
File metadata and controls
114 lines (102 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { Amplify, fetchAuthSession } from '@aws-amplify/core';
import { decodeJWT } from '@aws-amplify/core/internals/utils';
import { AuthError } from '../../../src/errors/AuthError';
import { deleteUserAttributes } from '../../../src/providers/cognito';
import { DeleteUserAttributesException } from '../../../src/providers/cognito/types/errors';
import { createDeleteUserAttributesClient } from '../../../src/foundation/factories/serviceClients/cognitoIdentityProvider';
import { createCognitoUserPoolEndpointResolver } from '../../../src/providers/cognito/factories';
import { getMockError, mockAccessToken } from './testUtils/data';
import { setUpGetConfig } from './testUtils/setUpGetConfig';
jest.mock('@aws-amplify/core', () => ({
...(jest.createMockFromModule('@aws-amplify/core') as object),
Amplify: { getConfig: jest.fn(() => ({})), assertConfigured: jest.fn() },
}));
jest.mock('@aws-amplify/core/internals/utils', () => ({
...jest.requireActual('@aws-amplify/core/internals/utils'),
isBrowser: jest.fn(() => false),
}));
jest.mock(
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
);
jest.mock('../../../src/providers/cognito/factories');
describe('deleteUserAttributes', () => {
// assert mocks
const mockFetchAuthSession = fetchAuthSession as jest.Mock;
const mockDeleteUserAttributes = jest.fn();
const mockCreateDeleteUserAttributesClient = jest.mocked(
createDeleteUserAttributesClient,
);
const mockCreateCognitoUserPoolEndpointResolver = jest.mocked(
createCognitoUserPoolEndpointResolver,
);
beforeAll(() => {
setUpGetConfig(Amplify);
mockFetchAuthSession.mockResolvedValue({
tokens: { accessToken: decodeJWT(mockAccessToken) },
});
});
beforeEach(() => {
mockDeleteUserAttributes.mockResolvedValue({ $metadata: {} });
mockCreateDeleteUserAttributesClient.mockReturnValueOnce(
mockDeleteUserAttributes,
);
});
afterEach(() => {
mockDeleteUserAttributes.mockReset();
mockFetchAuthSession.mockClear();
mockCreateDeleteUserAttributesClient.mockClear();
});
it('should delete user attributes', async () => {
expect.assertions(2);
await deleteUserAttributes({
userAttributeKeys: ['given_name', 'address'],
});
expect(mockDeleteUserAttributes).toHaveBeenCalledWith(
expect.objectContaining({ region: 'us-west-2' }),
expect.objectContaining({
AccessToken: mockAccessToken,
UserAttributeNames: ['given_name', 'address'],
}),
);
expect(mockDeleteUserAttributes).toHaveBeenCalledTimes(1);
});
it('invokes mockCreateCognitoUserPoolEndpointResolver with expected endpointOverride', async () => {
const expectedUserPoolEndpoint = 'https://my-custom-endpoint.com';
jest.mocked(Amplify.getConfig).mockReturnValueOnce({
Auth: {
Cognito: {
userPoolClientId: '111111-aaaaa-42d8-891d-ee81a1549398',
userPoolId: 'us-west-2_zzzzz',
identityPoolId: 'us-west-2:xxxxxx',
userPoolEndpoint: expectedUserPoolEndpoint,
},
},
});
await deleteUserAttributes({
userAttributeKeys: ['given_name', 'address'],
});
expect(mockCreateCognitoUserPoolEndpointResolver).toHaveBeenCalledWith({
endpointOverride: expectedUserPoolEndpoint,
});
});
it('should throw an error when service returns an error response', async () => {
expect.assertions(2);
mockDeleteUserAttributes.mockImplementation(() => {
throw getMockError(
DeleteUserAttributesException.InvalidParameterException,
);
});
try {
await deleteUserAttributes({
userAttributeKeys: ['address', 'given_name'],
});
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(
DeleteUserAttributesException.InvalidParameterException,
);
}
});
});