-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathNativeCredentialsManager.errors.spec.ts
More file actions
257 lines (239 loc) · 7.95 KB
/
NativeCredentialsManager.errors.spec.ts
File metadata and controls
257 lines (239 loc) · 7.95 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import { NativeCredentialsManager } from '../NativeCredentialsManager';
import { INativeBridge } from '../../bridge';
import { CredentialsManagerError } from '../../../../core/models';
// Mock the native bridge
const mockBridge: jest.Mocked<INativeBridge> = {
getCredentials: jest.fn(),
saveCredentials: jest.fn(),
clearCredentials: jest.fn(),
// Stub other methods
hasValidInstance: jest.fn(),
initialize: jest.fn(),
getBundleIdentifier: jest.fn(),
authorize: jest.fn(),
clearSession: jest.fn(),
hasValidCredentials: jest.fn(),
cancelWebAuth: jest.fn(),
resumeWebAuth: jest.fn(),
};
describe('Common Error Handling - NativeCredentialsManager', () => {
let manager: NativeCredentialsManager;
beforeEach(() => {
jest.clearAllMocks();
manager = new NativeCredentialsManager(mockBridge);
});
describe('Core Error Types', () => {
const coreErrorTestCases = [
{
code: 'INVALID_CREDENTIALS',
message: 'Invalid credentials provided.',
expectedType: 'INVALID_CREDENTIALS',
method: 'getCredentials',
},
{
code: 'NO_CREDENTIALS',
message: 'No credentials stored.',
expectedType: 'NO_CREDENTIALS',
method: 'getCredentials',
},
{
code: 'NO_REFRESH_TOKEN',
message: 'No refresh token available.',
expectedType: 'NO_REFRESH_TOKEN',
method: 'getCredentials',
},
{
code: 'RENEW_FAILED',
message: 'Refresh token renewal failed.',
expectedType: 'RENEW_FAILED',
method: 'getCredentials',
},
{
code: 'STORE_FAILED',
message: 'Failed to store credentials.',
expectedType: 'STORE_FAILED',
method: 'saveCredentials',
args: [{}],
},
{
code: 'REVOKE_FAILED',
message: 'Failed to revoke credentials.',
expectedType: 'REVOKE_FAILED',
method: 'clearCredentials',
},
{
code: 'LARGE_MIN_TTL',
message:
'The minTTL requested is greater than the lifetime of the renewed access token. Request a lower minTTL or increase the "Token Expiration" value in the settings page of your Auth0 API.',
expectedType: 'LARGE_MIN_TTL',
method: 'getCredentials',
},
{
code: 'CREDENTIAL_MANAGER_ERROR',
message: 'General credentials manager error occurred.',
expectedType: 'CREDENTIAL_MANAGER_ERROR',
method: 'getCredentials',
},
{
code: 'BIOMETRICS_FAILED',
message: 'Biometric authentication failed.',
expectedType: 'BIOMETRICS_FAILED',
method: 'getCredentials',
},
{
code: 'NO_NETWORK',
message: 'No network connection available.',
expectedType: 'NO_NETWORK',
method: 'getCredentials',
},
{
code: 'API_ERROR',
message: 'API request failed.',
expectedType: 'API_ERROR',
method: 'getCredentials',
},
];
coreErrorTestCases.forEach(
({ code, message, expectedType, method, args = [] }) => {
it(`should handle ${code} error`, async () => {
const nativeError = { code, message };
(
mockBridge[method as keyof typeof mockBridge] as jest.Mock
).mockRejectedValue(nativeError);
await expect((manager as any)[method](...args)).rejects.toThrow(
CredentialsManagerError
);
try {
await (manager as any)[method](...args);
} catch (e) {
const err = e as CredentialsManagerError;
expect(err.type).toBe(expectedType);
expect(err.message).toBe(message);
}
});
}
);
});
describe('Special Error Cases', () => {
const specialErrorTestCases = [
{
code: 'INCOMPATIBLE_DEVICE',
message: 'Device is incompatible.',
expectedType: 'INCOMPATIBLE_DEVICE',
},
{
code: 'CRYPTO_EXCEPTION',
message: 'Cryptographic exception occurred.',
expectedType: 'CRYPTO_EXCEPTION',
},
{
code: 'UNKNOWN_PLATFORM_ERROR',
message: 'An unknown platform error occurred.',
expectedType: 'UNKNOWN_ERROR',
},
];
specialErrorTestCases.forEach(({ code, message, expectedType }) => {
it(`should handle ${code} error${
expectedType === 'UNKNOWN_ERROR' ? ' with UNKNOWN_ERROR type' : ''
}`, async () => {
const nativeError = { code, message };
mockBridge.getCredentials.mockRejectedValue(nativeError);
await expect(manager.getCredentials()).rejects.toThrow(
CredentialsManagerError
);
try {
await manager.getCredentials();
} catch (e) {
const err = e as CredentialsManagerError;
expect(err.type).toBe(expectedType);
expect(err.message).toBe(message);
}
});
});
});
describe('DPoP Credential State Errors', () => {
const dpopErrorTestCases = [
{
code: 'DPOP_KEY_MISSING',
message:
'The stored credentials are DPoP-bound but the DPoP key pair is no longer available in the keystore.',
expectedType: 'DPOP_KEY_MISSING',
},
{
code: 'DPOP_NOT_CONFIGURED',
message:
'The stored credentials are DPoP-bound but the client was not configured with DPoP.',
expectedType: 'DPOP_NOT_CONFIGURED',
},
{
code: 'DPOP_KEY_MISMATCH',
message:
'The stored credentials are DPoP-bound but the current DPoP key pair does not match the one used when credentials were saved.',
expectedType: 'DPOP_KEY_MISMATCH',
},
];
dpopErrorTestCases.forEach(({ code, message, expectedType }) => {
it(`should handle ${code} error`, async () => {
const nativeError = { code, message };
mockBridge.getCredentials.mockRejectedValue(nativeError);
await expect(manager.getCredentials()).rejects.toThrow(
CredentialsManagerError
);
try {
await manager.getCredentials();
} catch (e) {
const err = e as CredentialsManagerError;
expect(err.type).toBe(expectedType);
expect(err.message).toBe(message);
}
});
});
});
describe('Android Biometric Error Mappings', () => {
const biometricErrorTestCases = [
'BIOMETRIC_NO_ACTIVITY',
'BIOMETRIC_ERROR_STATUS_UNKNOWN',
'BIOMETRIC_ERROR_UNSUPPORTED',
'BIOMETRIC_ERROR_HW_UNAVAILABLE',
'BIOMETRIC_ERROR_NONE_ENROLLED',
'BIOMETRIC_ERROR_NO_HARDWARE',
'BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED',
'BIOMETRIC_AUTHENTICATION_CHECK_FAILED',
'BIOMETRIC_ERROR_DEVICE_CREDENTIAL_NOT_AVAILABLE',
'BIOMETRIC_ERROR_STRONG_AND_DEVICE_CREDENTIAL_NOT_AVAILABLE',
'BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL',
'BIOMETRIC_ERROR_NEGATIVE_BUTTON',
'BIOMETRIC_ERROR_HW_NOT_PRESENT',
'BIOMETRIC_ERROR_NO_BIOMETRICS',
'BIOMETRIC_ERROR_USER_CANCELED',
'BIOMETRIC_ERROR_LOCKOUT_PERMANENT',
'BIOMETRIC_ERROR_VENDOR',
'BIOMETRIC_ERROR_LOCKOUT',
'BIOMETRIC_ERROR_CANCELED',
'BIOMETRIC_ERROR_NO_SPACE',
'BIOMETRIC_ERROR_TIMEOUT',
'BIOMETRIC_ERROR_UNABLE_TO_PROCESS',
'BIOMETRICS_INVALID_USER',
'BIOMETRIC_AUTHENTICATION_FAILED',
];
biometricErrorTestCases.forEach((errorCode) => {
it(`should map ${errorCode} to BIOMETRICS_FAILED`, async () => {
const nativeError = {
code: errorCode,
message: `Biometric error: ${errorCode}`,
};
mockBridge.getCredentials.mockRejectedValue(nativeError);
await expect(manager.getCredentials()).rejects.toThrow(
CredentialsManagerError
);
try {
await manager.getCredentials();
} catch (e) {
const err = e as CredentialsManagerError;
expect(err.type).toBe('BIOMETRICS_FAILED');
expect(err.message).toBe(`Biometric error: ${errorCode}`);
}
});
});
});
});