-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthErrorHandler.test.ts
More file actions
61 lines (52 loc) · 2.61 KB
/
authErrorHandler.test.ts
File metadata and controls
61 lines (52 loc) · 2.61 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
import { describe, it, expect } from 'vitest';
import { isAuthenticationExpiredError, getAuthErrorMessage, shouldRedirectToLogin } from '../../utils/authErrorHandler';
describe('authErrorHandler', () => {
describe('isAuthenticationExpiredError', () => {
it('should return true for InteractionRequiredAuthError-like objects', () => {
const error = {
name: 'InteractionRequiredAuthError',
message: 'interaction_required: AADSTS160021: Application requested a user session which does not exist'
};
expect(isAuthenticationExpiredError(error)).toBe(true);
});
it('should return true for messages containing AADSTS160021', () => {
const error = new Error('interaction_required: AADSTS160021: Application requested a user session which does not exist');
expect(isAuthenticationExpiredError(error)).toBe(true);
});
it('should return true for session expired patterns', () => {
const error = new Error('user session which does not exist');
expect(isAuthenticationExpiredError(error)).toBe(true);
});
it('should return false for other errors', () => {
const error = new Error('Network error');
expect(isAuthenticationExpiredError(error)).toBe(false);
});
});
describe('getAuthErrorMessage', () => {
it('should return session expired message for authentication errors', () => {
const error = new Error('AADSTS160021: Application requested a user session which does not exist');
const message = getAuthErrorMessage(error);
expect(message).toBe('Your session has expired. Please sign out and sign in again to continue.');
});
it('should return permission message for 403 errors', () => {
const error = new Error('403 Forbidden');
const message = getAuthErrorMessage(error);
expect(message).toBe("You don't have permission to access this resource. Please check your permissions or contact your administrator.");
});
it('should return default auth error message for other errors', () => {
const error = new Error('Some other error');
const message = getAuthErrorMessage(error);
expect(message).toBe('Some other error');
});
});
describe('shouldRedirectToLogin', () => {
it('should return true for authentication expired errors', () => {
const error = new Error('AADSTS160021: Application requested a user session which does not exist');
expect(shouldRedirectToLogin(error)).toBe(true);
});
it('should return false for other errors', () => {
const error = new Error('Network error');
expect(shouldRedirectToLogin(error)).toBe(false);
});
});
});