Repository navigation
Expand file tree
/
Copy pathPasskeyTest.ts
More file actions
241 lines (192 loc) · 11.7 KB
/
Copy pathPasskeyTest.ts
File metadata and controls
241 lines (192 loc) · 11.7 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import {beforeAll, beforeEach, describe, expect, it} from '@jest/globals';
import {addLocalPasskeyCredential, deleteLocalPasskeyCredentials, getPasskeyOnyxKey, reconcileLocalPasskeysWithBackend} from '@libs/actions/Passkey';
import type {BackendPasskeyCredential} from '@libs/actions/Passkey';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {LocalPasskeyCredentialsEntry, PasskeyCredential} from '@src/types/onyx';
import Onyx from 'react-native-onyx';
import getOnyxValue from '../utils/getOnyxValue';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
describe('actions/Passkey', () => {
const userId = '123';
beforeAll(() => {
Onyx.init({keys: ONYXKEYS});
});
beforeEach(() => {
return Onyx.clear().then(waitForBatchedUpdates);
});
describe('getPasskeyOnyxKey', () => {
it('should return correct key format', () => {
// Given a userId
// When generating an Onyx key
const key = getPasskeyOnyxKey(userId);
// Then it should return the PASSKEY_CREDENTIALS collection prefix followed by the userId
expect(key).toBe(`${ONYXKEYS.COLLECTION.PASSKEY_CREDENTIALS}${userId}`);
});
});
describe('addLocalPasskeyCredential', () => {
const credential: PasskeyCredential = {id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]};
it('should throw error when userId is empty', () => {
// Given an empty userId
// When adding a credential
// Then it should throw an error
expect(() => addLocalPasskeyCredential({userId: '', credential, existingCredentials: null})).toThrow();
});
it('should create new entry when existingEntry is null', async () => {
// Given no existing passkey entry in Onyx
// When adding a new credential
addLocalPasskeyCredential({userId, credential, existingCredentials: null});
await waitForBatchedUpdates();
// Then the credential should be stored as the only entry in Onyx
const value = await getOnyxValue(getPasskeyOnyxKey(userId));
expect(value).toEqual([credential]);
});
it('should add credential to existing entry', async () => {
// Given an existing passkey entry with one credential
const existingCredentials: LocalPasskeyCredentialsEntry = [{id: 'existing', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}];
// When adding a new credential
addLocalPasskeyCredential({userId, credential, existingCredentials});
await waitForBatchedUpdates();
// Then both credentials should be stored in Onyx
const value = await getOnyxValue(getPasskeyOnyxKey(userId));
expect(value).toEqual([{id: 'existing', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}, credential]);
});
it('should store credential with aaguid field', async () => {
// Given a credential with an AAGUID
const credentialWithAAGUID: PasskeyCredential = {
id: 'cred-aaguid',
type: CONST.PASSKEY_CREDENTIAL_TYPE,
transports: [CONST.PASSKEY_TRANSPORT.INTERNAL],
aaguid: 'fbfc3007-154e-4ecc-8c0b-6e020557d7bd',
};
// When adding it to Onyx
addLocalPasskeyCredential({userId, credential: credentialWithAAGUID, existingCredentials: null});
await waitForBatchedUpdates();
// Then the AAGUID should be preserved in Onyx storage
const value = await getOnyxValue(getPasskeyOnyxKey(userId));
expect(value).toEqual([credentialWithAAGUID]);
});
it('should throw error when credential with same id already exists', () => {
// Given an existing entry that already contains a credential with the same id
const existingCredentials: LocalPasskeyCredentialsEntry = [{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}];
// When adding a credential with a duplicate id
// Then it should throw an error
expect(() => addLocalPasskeyCredential({userId, credential, existingCredentials})).toThrow();
});
});
describe('deleteLocalPasskeyCredentials', () => {
it('should throw error when userId is empty', () => {
// Given an empty userId
// When deleting credentials
// Then it should throw an error
expect(() => deleteLocalPasskeyCredentials('')).toThrow();
});
it('should delete existing passkey entry from Onyx', async () => {
// Given a passkey entry stored in Onyx
const entry: LocalPasskeyCredentialsEntry = [{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}];
await Onyx.set(`${ONYXKEYS.COLLECTION.PASSKEY_CREDENTIALS}${userId}`, entry);
await waitForBatchedUpdates();
// When deleting credentials for the user
deleteLocalPasskeyCredentials(userId);
await waitForBatchedUpdates();
// Then the entry should be an empty array
const value = await getOnyxValue(`${ONYXKEYS.COLLECTION.PASSKEY_CREDENTIALS}${userId}`);
expect(value).toEqual([]);
});
it('should handle deletion of non-existent entry gracefully', async () => {
// Given no existing passkey entry in Onyx
// When deleting credentials for the user
deleteLocalPasskeyCredentials(userId);
await waitForBatchedUpdates();
// Then an empty array should be set
const value = await getOnyxValue(`${ONYXKEYS.COLLECTION.PASSKEY_CREDENTIALS}${userId}`);
expect(value).toEqual([]);
});
});
describe('reconcileLocalPasskeysWithBackend', () => {
it('should throw error when userId is empty', () => {
// Given an empty userId
// When reconciling credentials
// Then it should throw an error
expect(() => reconcileLocalPasskeysWithBackend({userId: '', backendCredentials: [], localCredentials: null})).toThrow();
});
it('should return empty array when localEntry is null', () => {
// Given a null local entry
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials: [], localCredentials: null});
// Then it should return an empty array
expect(result).toEqual([]);
});
it('should return empty array when localEntry has no credentials', () => {
// Given a local entry with no credentials
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials: [], localCredentials: []});
// Then it should return an empty array
expect(result).toEqual([]);
});
it('should return all matched credentials when all exist on backend', () => {
// Given local credentials that all exist on the backend
const localCredentials: PasskeyCredential[] = [
{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]},
{id: 'cred-2', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.HYBRID]},
];
const backendCredentials: BackendPasskeyCredential[] = [
{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE},
{id: 'cred-2', type: CONST.PASSKEY_CREDENTIAL_TYPE},
];
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials, localCredentials});
// Then all local credentials should be returned
expect(result).toEqual(localCredentials);
});
it('should remove local credentials not on backend and update Onyx', async () => {
// Given two local credentials where only one exists on the backend
const localCredentials: PasskeyCredential[] = [
{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]},
{id: 'cred-2', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.HYBRID]},
];
const backendCredentials: BackendPasskeyCredential[] = [{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE}];
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials, localCredentials});
await waitForBatchedUpdates();
// Then only the matching credential should be returned and stored in Onyx
expect(result).toEqual([{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}]);
const value = await getOnyxValue(getPasskeyOnyxKey(userId));
expect(value).toEqual([{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}]);
});
it('should set empty credentials when no credentials match backend', async () => {
// Given a local credential that does not exist on the backend
const localCredentials: PasskeyCredential[] = [{id: 'old-cred', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL]}];
const backendCredentials: BackendPasskeyCredential[] = [{id: 'new-cred', type: CONST.PASSKEY_CREDENTIAL_TYPE}];
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials, localCredentials});
await waitForBatchedUpdates();
// Then an empty array should be returned and Onyx should be empty
expect(result).toEqual([]);
const value = await getOnyxValue(getPasskeyOnyxKey(userId));
expect(value).toEqual([]);
});
it('should preserve aaguid from local credentials', () => {
// Given a local credential with an AAGUID and a backend credential without it
const localCredentials: PasskeyCredential[] = [
{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL], aaguid: 'fbfc3007-154e-4ecc-8c0b-6e020557d7bd'},
];
const backendCredentials: BackendPasskeyCredential[] = [{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE}];
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials, localCredentials});
// Then the local AAGUID should be preserved in the result
expect(result).toEqual(localCredentials);
});
it('should preserve transports from local credentials', () => {
// Given a local credential with multiple transports and a backend credential without transports
const localCredentials: PasskeyCredential[] = [
{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE, transports: [CONST.PASSKEY_TRANSPORT.INTERNAL, CONST.PASSKEY_TRANSPORT.HYBRID]},
];
const backendCredentials: BackendPasskeyCredential[] = [{id: 'cred-1', type: CONST.PASSKEY_CREDENTIAL_TYPE}];
// When reconciling with backend
const result = reconcileLocalPasskeysWithBackend({userId, backendCredentials, localCredentials});
// Then the local transports should be preserved in the result
expect(result).toEqual(localCredentials);
});
});
});