-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathlistCurrentUsers.test.ts
More file actions
170 lines (138 loc) · 4.96 KB
/
Copy pathlistCurrentUsers.test.ts
File metadata and controls
170 lines (138 loc) · 4.96 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { Amplify } from '@aws-amplify/core';
import { listCurrentUsers } from '../../../src/providers/cognito/apis/listCurrentUsers';
import { cognitoUserPoolsTokenProvider } from '../../../src/providers/cognito/tokenProvider';
const userPoolClientId = '111111-aaaaa-42d8-891d-ee81a1549398';
const authConfig = {
Cognito: {
userPoolClientId,
userPoolId: 'us-west-2_zzzzz',
},
};
cognitoUserPoolsTokenProvider.setAuthConfig(authConfig);
Amplify.configure({
Auth: authConfig,
});
const { authTokenStore } = cognitoUserPoolsTokenProvider;
const mockKeyValueStorage = {
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
clear: jest.fn(),
};
describe('listCurrentUsers', () => {
let getAuthUserListSpy: jest.SpyInstance;
let getStoredIdTokenSpy: jest.SpyInstance;
let loadTokensSpy: jest.SpyInstance;
beforeEach(() => {
jest
.spyOn(authTokenStore, 'getKeyValueStorage')
.mockReturnValue(mockKeyValueStorage);
getAuthUserListSpy = jest.spyOn(authTokenStore, 'getAuthUserList');
getStoredIdTokenSpy = jest.spyOn(authTokenStore, 'getStoredIdToken');
loadTokensSpy = jest.spyOn(authTokenStore, 'loadTokens');
});
afterEach(() => {
jest.restoreAllMocks();
mockKeyValueStorage.getItem.mockReset();
});
it('returns AuthUser[] resolved from stored id tokens in roster order', async () => {
getAuthUserListSpy.mockResolvedValue(['alice', 'bob']);
getStoredIdTokenSpy.mockImplementation((username: string) =>
Promise.resolve({
payload: { 'cognito:username': username, sub: `${username}-sub` },
toString: () => `${username}-idToken`,
}),
);
const result = await listCurrentUsers();
expect(result).toEqual([
{ username: 'alice', userId: 'alice-sub' },
{ username: 'bob', userId: 'bob-sub' },
]);
});
it('drops roster entries whose stored tokens cannot be resolved', async () => {
getAuthUserListSpy.mockResolvedValue(['alice', 'ghost', 'bob']);
getStoredIdTokenSpy.mockImplementation((username: string) => {
if (username === 'ghost') return Promise.resolve(undefined);
return Promise.resolve({
payload: { 'cognito:username': username, sub: `${username}-sub` },
toString: () => `${username}-idToken`,
});
});
const result = await listCurrentUsers();
expect(result).toEqual([
{ username: 'alice', userId: 'alice-sub' },
{ username: 'bob', userId: 'bob-sub' },
]);
});
it('drops roster entries whose stored id token lacks a `sub` claim', async () => {
getAuthUserListSpy.mockResolvedValue(['alice', 'nosub', 'bob']);
getStoredIdTokenSpy.mockImplementation((username: string) => {
if (username === 'nosub') {
return Promise.resolve({
payload: { 'cognito:username': username },
toString: () => `${username}-idToken`,
});
}
return Promise.resolve({
payload: { 'cognito:username': username, sub: `${username}-sub` },
toString: () => `${username}-idToken`,
});
});
const result = await listCurrentUsers();
expect(result).toEqual([
{ username: 'alice', userId: 'alice-sub' },
{ username: 'bob', userId: 'bob-sub' },
]);
});
it('does not trigger a token refresh', async () => {
getAuthUserListSpy.mockResolvedValue(['alice']);
getStoredIdTokenSpy.mockResolvedValue({
payload: { 'cognito:username': 'alice', sub: 'alice-sub' },
toString: () => 'alice-idToken',
});
await listCurrentUsers();
// identities are read directly from stored tokens; loadTokens (which can
// drive a refresh) must not be invoked.
expect(loadTokensSpy).not.toHaveBeenCalled();
});
it('includes signInDetails when stored signInDetails key is present', async () => {
getAuthUserListSpy.mockResolvedValue(['alice']);
getStoredIdTokenSpy.mockResolvedValue({
payload: { 'cognito:username': 'alice', sub: 'alice-sub' },
toString: () => 'alice-idToken',
});
const signInDetails = {
loginId: 'alice@example.com',
authFlowType: 'USER_SRP_AUTH',
};
mockKeyValueStorage.getItem.mockImplementation((key: string) => {
if (key.endsWith('.signInDetails')) {
return Promise.resolve(JSON.stringify(signInDetails));
}
return Promise.resolve(null);
});
const result = await listCurrentUsers();
expect(result).toEqual([
{
username: 'alice',
userId: 'alice-sub',
signInDetails,
},
]);
});
it('still returns the user when signInDetails read fails', async () => {
getAuthUserListSpy.mockResolvedValue(['alice']);
getStoredIdTokenSpy.mockResolvedValue({
payload: { 'cognito:username': 'alice', sub: 'alice-sub' },
toString: () => 'alice-idToken',
});
// signInDetails key returns invalid JSON — parse will throw, but the
// implementation currently only reads it conditionally so it may return
// null. Either way the user should still be resolvable.
mockKeyValueStorage.getItem.mockResolvedValue(null);
const result = await listCurrentUsers();
expect(result).toEqual([{ username: 'alice', userId: 'alice-sub' }]);
});
});