-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathforgetDevice.test.ts
More file actions
185 lines (168 loc) · 6.86 KB
/
forgetDevice.test.ts
File metadata and controls
185 lines (168 loc) · 6.86 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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 { DEVICE_METADATA_NOT_FOUND_EXCEPTION } from '../../../src/errors/constants';
import { forgetDevice } from '../../../src/providers/cognito';
import { ForgetDeviceException } from '../../../src/providers/cognito/types/errors';
import { tokenOrchestrator } from '../../../src/providers/cognito/tokenProvider';
import { createForgetDeviceClient } 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/providers/cognito/tokenProvider');
jest.mock(
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
);
jest.mock('../../../src/providers/cognito/factories');
describe('fetchMFAPreference', () => {
const mockDeviceMetadata = {
deviceKey: 'deviceKey',
deviceGroupKey: 'deviceGroupKey',
randomPassword: 'randomPassword',
};
// assert mocks
const mockFetchAuthSession = fetchAuthSession as jest.Mock;
const mockForgetDevice = jest.fn();
const mockCreateForgetDeviceClient = jest.mocked(createForgetDeviceClient);
const mockClearDeviceMetadata =
tokenOrchestrator.clearDeviceMetadata as jest.Mock;
const mockGetDeviceMetadata =
tokenOrchestrator.getDeviceMetadata as jest.Mock;
const mockCreateCognitoUserPoolEndpointResolver = jest.mocked(
createCognitoUserPoolEndpointResolver,
);
beforeAll(() => {
setUpGetConfig(Amplify);
mockFetchAuthSession.mockResolvedValue({
tokens: { accessToken: decodeJWT(mockAccessToken) },
});
});
beforeEach(() => {
mockForgetDevice.mockResolvedValue({ $metadata: {} });
mockGetDeviceMetadata.mockResolvedValue(mockDeviceMetadata);
mockCreateForgetDeviceClient.mockReturnValueOnce(mockForgetDevice);
});
afterEach(() => {
mockForgetDevice.mockReset();
mockGetDeviceMetadata.mockReset();
mockFetchAuthSession.mockClear();
mockClearDeviceMetadata.mockClear();
mockCreateForgetDeviceClient.mockClear();
});
it(`should forget 'external device' 'with' inputParams when tokenStore deviceMetadata 'present'`, async () => {
expect.assertions(3);
await forgetDevice({ device: { id: 'externalDeviceKey' } });
expect(mockForgetDevice).toHaveBeenCalledWith(
expect.objectContaining({ region: 'us-west-2' }),
expect.objectContaining({
AccessToken: mockAccessToken,
DeviceKey: 'externalDeviceKey',
}),
);
expect(mockForgetDevice).toHaveBeenCalledTimes(1);
expect(mockClearDeviceMetadata).not.toHaveBeenCalled();
});
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 forgetDevice({ device: { id: 'externalDeviceKey' } });
expect(mockCreateCognitoUserPoolEndpointResolver).toHaveBeenCalledWith({
endpointOverride: expectedUserPoolEndpoint,
});
});
it(`should forget 'current device' 'with' inputParams when tokenStore deviceMetadata 'present'`, async () => {
expect.assertions(3);
await forgetDevice({ device: { id: mockDeviceMetadata.deviceKey } });
expect(mockForgetDevice).toHaveBeenCalledWith(
expect.objectContaining({ region: 'us-west-2' }),
expect.objectContaining({
AccessToken: mockAccessToken,
DeviceKey: mockDeviceMetadata.deviceKey,
}),
);
expect(mockForgetDevice).toHaveBeenCalledTimes(1);
expect(mockClearDeviceMetadata).toHaveBeenCalled();
});
it(`should forget 'current device' 'without' inputParams when tokenStore deviceMetadata 'present'`, async () => {
expect.assertions(3);
await forgetDevice();
expect(mockForgetDevice).toHaveBeenCalledWith(
expect.objectContaining({ region: 'us-west-2' }),
expect.objectContaining({
AccessToken: mockAccessToken,
DeviceKey: mockDeviceMetadata.deviceKey,
}),
);
expect(mockForgetDevice).toHaveBeenCalledTimes(1);
expect(mockClearDeviceMetadata).toHaveBeenCalled();
});
it(`should forget 'external device' 'with' inputParams when tokenStore deviceMetadata 'not present'`, async () => {
mockGetDeviceMetadata.mockResolvedValue(null);
await forgetDevice({ device: { id: 'externalDeviceKey' } });
expect(mockForgetDevice).toHaveBeenCalledWith(
expect.objectContaining({ region: 'us-west-2' }),
expect.objectContaining({
AccessToken: mockAccessToken,
DeviceKey: 'externalDeviceKey',
}),
);
expect(mockForgetDevice).toHaveBeenCalledTimes(1);
expect(mockClearDeviceMetadata).not.toHaveBeenCalled();
});
it(`should forget 'current device' 'with' inputParams when tokenStore deviceMetadata 'not present'`, async () => {
mockGetDeviceMetadata.mockResolvedValue(null);
expect.assertions(3);
await forgetDevice({ device: { id: mockDeviceMetadata.deviceKey } });
expect(mockForgetDevice).toHaveBeenCalledWith(
expect.objectContaining({ region: 'us-west-2' }),
expect.objectContaining({
AccessToken: mockAccessToken,
DeviceKey: mockDeviceMetadata.deviceKey,
}),
);
expect(mockForgetDevice).toHaveBeenCalledTimes(1);
expect(mockClearDeviceMetadata).not.toHaveBeenCalled();
});
it(`should throw and error when forget 'current device' 'without' inputParams when tokenStore deviceMetadata 'not present'`, async () => {
mockGetDeviceMetadata.mockResolvedValue(null);
expect.assertions(2);
try {
await forgetDevice();
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(DEVICE_METADATA_NOT_FOUND_EXCEPTION);
}
});
it('should throw an error when service returns an error response', async () => {
mockGetDeviceMetadata.mockResolvedValue(null);
expect.assertions(2);
mockForgetDevice.mockImplementation(() => {
throw getMockError(ForgetDeviceException.InvalidParameterException);
});
try {
await forgetDevice({ device: { id: mockDeviceMetadata.deviceKey } });
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(ForgetDeviceException.InvalidParameterException);
}
});
});