This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
/
keychain.spec.js
367 lines (321 loc) · 14.8 KB
/
keychain.spec.js
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import keychain from 'react-native-keychain';
import keychainWrapper, {
storeSeedInKeychain,
storeTwoFactorAuthKeyInKeychain,
getTwoFactorAuthKeyFromKeychain,
getPasswordFromKeychain,
hasDuplicateAccountName,
hasDuplicateSeed,
getSeed,
updateAccountNameInKeychain,
deleteFromKeychain,
} from '../../utils/keychain';
jest.mock('react-native-keychain', () => ({
setGenericPassword: jest.fn(() => Promise.resolve({})),
getGenericPassword: jest.fn(() => Promise.resolve({ username: '', password: '', service: 'bundleId' })),
resetGenericPassword: jest.fn(() => Promise.resolve({})),
}));
describe('Testing keychain util', () => {
describe('#get', () => {
afterEach(() => {
keychain.getGenericPassword.mockClear();
});
it('should return an object with keys password, data and service', () => {
return keychainWrapper.get().then((data) => {
expect(Object.keys(data)).toEqual(['password', 'data', 'service']);
});
});
it('should call getGenericPassword method on keychain', () => {
return keychainWrapper.get().then(() => {
expect(keychain.getGenericPassword).toHaveBeenCalled();
});
});
});
describe('#set', () => {
afterEach(() => {
keychain.setGenericPassword.mockClear();
});
it('should call setGenericPassword method on keychain', () => {
return keychainWrapper.set('foo', 'baz').then(() => {
expect(keychain.setGenericPassword).toHaveBeenCalledTimes(1);
});
});
});
describe('#clear', () => {
afterEach(() => {
keychain.resetGenericPassword.mockClear();
});
it('should call resetGenericPassword method on keychain', () => {
return keychainWrapper.clear().then(() => {
expect(keychain.resetGenericPassword).toHaveBeenCalled();
});
});
});
describe('#storeSeedInKeychain', () => {
afterEach(() => {
keychain.setGenericPassword.mockClear();
keychain.getGenericPassword.mockClear();
});
describe('when data prop is empty', () => {
it('should call setGenericPassword with first argument and info consisting of seed and name', () => {
return storeSeedInKeychain('foo', 'SEED', 'account').then(() => {
expect(keychain.setGenericPassword).toHaveBeenCalledWith(
'foo',
JSON.stringify({
accounts: [{ seed: 'SEED', name: 'account' }],
shared: { twoFactorAuthKey: null },
}),
);
});
});
});
describe('when data prop is not empty', () => {
it('should call setGenericPassword with first argument and accounts array updated with info consisting of seed and name', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: '',
password: {
accounts: [{ seed: 'FOO', name: 'ACCOUNT_ONE' }],
shared: { twoFactorAuthKey: null },
},
service: 'bundleId',
}),
);
return storeSeedInKeychain('foo', 'SEED', 'new account').then(() => {
expect(keychain.setGenericPassword).toHaveBeenCalledWith(
'foo',
JSON.stringify({
accounts: [{ seed: 'FOO', name: 'ACCOUNT_ONE' }, { seed: 'SEED', name: 'new account' }],
shared: { twoFactorAuthKey: null },
}),
);
});
});
});
});
describe('#storeTwoFactorAuthKeyInKeychain', () => {
afterEach(() => {
keychain.setGenericPassword.mockClear();
keychain.getGenericPassword.mockClear();
});
describe('when key passed as an argument is not a string', () => {
it('should throw with error "Invalid two factor authentication key."', () => {
return storeTwoFactorAuthKeyInKeychain(null).catch((err) => {
expect(err.message).toEqual('Invalid two factor authentication key.');
});
});
});
describe('when password prop from getGenericPassword is empty', () => {
it('should throw with error "Cannot store two factor authentication key. No account information found in keychain."', () => {
return storeTwoFactorAuthKeyInKeychain('foo').catch((err) => {
expect(err.message).toEqual(
'Cannot store two factor authentication key. No account information found in keychain.',
);
});
});
});
describe('when data prop from getGenericPassword is empty', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: 'foo', // password -> username
password: '', // data -> password
service: 'bundleId',
}),
);
it('should throw with error "Cannot store two factor authentication key. No account information found in keychain."', () => {
return storeTwoFactorAuthKeyInKeychain('foo').catch((err) => {
expect(err.message).toEqual(
'Cannot store two factor authentication key. No account information found in keychain.',
);
});
});
});
describe('when data and password props are not empty and key is a string', () => {
it('should assign twoFactorAuthKey to argument', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: 'baz',
password: {
accounts: [{ seed: 'FOO', name: 'ACCOUNT_ONE' }],
shared: { twoFactorAuthKey: null },
},
service: 'bundleId',
}),
);
return storeTwoFactorAuthKeyInKeychain('foo').then(() => {
expect(keychain.setGenericPassword).toHaveBeenCalledWith(
'baz',
JSON.stringify({
accounts: [{ seed: 'FOO', name: 'ACCOUNT_ONE' }],
shared: { twoFactorAuthKey: 'foo' },
}),
);
});
});
});
});
describe('#getTwoFactorAuthKeyFromKeychain', () => {
afterEach(() => {
keychain.getGenericPassword.mockClear();
});
it('should return twoFactorAuthKey prop', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: 'baz',
password: {
accounts: [{ seed: 'FOO', name: 'ACCOUNT_ONE' }],
shared: { twoFactorAuthKey: 'my super secret key' },
},
service: 'bundleId',
}),
);
return getTwoFactorAuthKeyFromKeychain().then((key) => expect(key).toEqual('my super secret key'));
});
});
describe('#getPasswordFromKeychain', () => {
afterEach(() => {
keychain.getGenericPassword.mockClear();
});
it('should call getPasswordFromKeychain once', () => {
return getPasswordFromKeychain().then(() => expect(keychain.getGenericPassword).toHaveBeenCalledTimes(1));
});
it('should return password prop from keychain object', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: 'barcelona', // username is transformed to password -> keychain.get()
password: '',
service: '',
}),
);
return getPasswordFromKeychain().then((password) => expect(password).toEqual('barcelona'));
});
});
describe('#hasDuplicateAccountName', () => {
it('should return false if name passed in second argument is not present in any account array object', () => {
const data = JSON.stringify({ accounts: [{ seed: 'SEED', name: 'bar' }], shared: {} });
expect(hasDuplicateAccountName(data, 'foo')).toEqual(false);
expect(hasDuplicateAccountName(data, 'baz')).toEqual(false);
});
it('should return true if name passed in second argument is present in any account array object', () => {
const data = JSON.stringify({
accounts: [{ seed: 'SEED', name: 'bar' }, { seed: 'SEEDTWO', name: 'foo' }],
shared: {},
});
expect(hasDuplicateAccountName(data, 'bar')).toEqual(true);
expect(hasDuplicateAccountName(data, 'foo')).toEqual(true);
});
});
describe('#hasDuplicateSeed', () => {
it('should return false if seed passed in second argument is not present in any account array object', () => {
const data = JSON.stringify({ accounts: [{ seed: 'SEED', name: 'bar' }], shared: {} });
expect(hasDuplicateSeed(data, 'SEEDTWO')).toEqual(false);
expect(hasDuplicateSeed(data, 'SEEDTHREE')).toEqual(false);
});
it('should return true if seed passed in second argument is present in any account array object', () => {
const data = JSON.stringify({
accounts: [{ seed: 'SEED', name: 'bar' }, { seed: 'SEEDTWO', name: 'foo' }],
shared: {},
});
expect(hasDuplicateSeed(data, 'SEED')).toEqual(true);
expect(hasDuplicateSeed(data, 'SEEDTWO')).toEqual(true);
});
});
describe('#getSeed', () => {
it('should return an empty string if index does not exist in accounts array prop passed as first argument', () => {
const data = JSON.stringify({ accounts: [{ seed: 'SEED', name: 'bar' }], shared: {} });
[1, -1, 0.5, null, undefined].forEach((item) => expect(getSeed(data, item)).toEqual(''));
});
it('should return seed prop for item at specified index if index exists is accounts array', () => {
const data = JSON.stringify({
accounts: [{ seed: 'DANGLE', name: 'bar' }, { seed: '9999', name: 'foo' }],
shared: {},
});
expect(getSeed(data, 0)).toEqual('DANGLE');
expect(getSeed(data, 1)).toEqual('9999');
});
});
describe('#updateAccountNameInKeychain', () => {
afterEach(() => {
keychain.setGenericPassword.mockClear();
keychain.getGenericPassword.mockClear();
});
describe('when data prop fetched for keychain is empty', () => {
it('should throw with error "Something went wrong while updating account name."', () => {
return updateAccountNameInKeychain().catch((err) => {
expect(err.message).toEqual('Something went wrong while updating account name.');
});
});
});
describe('when data prop fetched for keychain is not empty', () => {
it('should call setGenericPassword method on keychain with updated account name', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: 'baz',
password: {
accounts: [
{ seed: 'FOO', name: 'ACCOUNT_ONE' },
{ seed: 'BAR', name: 'ACCOUNT_TWO' },
{ seed: '9999', name: 'ACCOUNT_THREE' },
],
shared: { twoFactorAuthKey: null },
},
service: 'bundleId',
}),
);
return updateAccountNameInKeychain(0, 'NEW ACCOUNT NAME', 'baz').then(() => {
expect(keychain.setGenericPassword).toHaveBeenCalledWith(
'baz',
JSON.stringify({
accounts: [
{ seed: 'FOO', name: 'NEW ACCOUNT NAME' },
{ seed: 'BAR', name: 'ACCOUNT_TWO' },
{ seed: '9999', name: 'ACCOUNT_THREE' },
],
shared: { twoFactorAuthKey: null },
}),
);
});
});
});
});
describe('#deleteFromKeychain', () => {
afterEach(() => {
keychain.setGenericPassword.mockClear();
keychain.getGenericPassword.mockClear();
});
describe('when data prop fetched for keychain is empty', () => {
it('should throw with error "Something went wrong while deleting from keychain."', () => {
return deleteFromKeychain().catch((err) => {
expect(err.message).toEqual('Something went wrong while deleting from keychain.');
});
});
});
describe('when data prop fetched for keychain is not empty', () => {
it('should call setGenericPassword method on keychain with filtered accounts', () => {
keychain.getGenericPassword.mockImplementation(() =>
Promise.resolve({
username: 'baz',
password: {
accounts: [
{ seed: 'FOO', name: 'ACCOUNT_ONE' },
{ seed: 'BAR', name: 'ACCOUNT_TWO' },
{ seed: '9999', name: 'ACCOUNT_THREE' },
],
shared: { twoFactorAuthKey: null },
},
service: 'bundleId',
}),
);
return deleteFromKeychain(0, 'baz').then(() => {
expect(keychain.setGenericPassword).toHaveBeenCalledWith(
'baz',
JSON.stringify({
accounts: [{ seed: 'BAR', name: 'ACCOUNT_TWO' }, { seed: '9999', name: 'ACCOUNT_THREE' }],
shared: { twoFactorAuthKey: null },
}),
);
});
});
});
});
});