forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.test.ts
More file actions
166 lines (131 loc) · 5.55 KB
/
Copy pathsession.test.ts
File metadata and controls
166 lines (131 loc) · 5.55 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
import { webcrypto } from 'crypto';
import { TextDecoder, TextEncoder } from 'util';
import {
sessionManager,
encryptSessionData,
decryptSessionData,
getSessionItem,
setSessionItem,
removeSessionItem
} from '../session';
Object.defineProperty(globalThis, 'TextEncoder', { value: TextEncoder });
Object.defineProperty(globalThis, 'TextDecoder', { value: TextDecoder });
// Setup global crypto for Node environment
Object.defineProperty(globalThis, 'crypto', {
value: webcrypto,
configurable: true,
writable: true
});
if (typeof window !== 'undefined') {
Object.defineProperty(window, 'crypto', {
value: webcrypto,
configurable: true,
writable: true
});
}
describe('session encryption and management', () => {
beforeEach(() => {
localStorage.clear();
sessionStorage.clear();
sessionManager.clearCache();
sessionManager.stopMonitoring();
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
test('encryptSessionData and decryptSessionData encrypts and decrypts strings correctly', async () => {
const originalText = 'my-super-secret-stellar-key';
const encrypted = await encryptSessionData(originalText);
// Encrypted string should be valid JSON and contain ciphertext and iv
expect(encrypted).toContain('ciphertext');
expect(encrypted).toContain('iv');
expect(encrypted).not.toContain(originalText);
const decrypted = await decryptSessionData(encrypted);
expect(decrypted).toBe(originalText);
});
test('setSessionItem encrypts and getSessionItem decrypts correctly from localStorage', async () => {
const key = 'test_key';
const value = 'test_value';
await setSessionItem(key, value);
// Verify localStorage has encrypted value
const rawLocalValue = localStorage.getItem(key);
expect(rawLocalValue).not.toBeNull();
expect(rawLocalValue).toContain('ciphertext');
expect(rawLocalValue).not.toContain(value);
// Verify getSessionItem successfully decrypts it
const decrypted = await getSessionItem(key);
expect(decrypted).toBe(value);
});
test('getSessionItem falls back to plain text for unencrypted legacy values', async () => {
const key = 'legacy_key';
const value = 'legacy_plaintext_value';
// Store plaintext directly
localStorage.setItem(key, value);
// Verify getSessionItem returns the plaintext directly
const retrieved = await getSessionItem(key);
expect(retrieved).toBe(value);
});
test('removeSessionItem deletes key from localStorage', async () => {
const key = 'delete_key';
await setSessionItem(key, 'delete_me');
expect(localStorage.getItem(key)).not.toBeNull();
removeSessionItem(key);
expect(localStorage.getItem(key)).toBeNull();
});
test('SessionManager notifies subscribers on logout', async () => {
const logoutSpy = jest.fn();
const unsubscribe = sessionManager.subscribe(logoutSpy);
// Simulate idle timeout triggering notify
await sessionManager.startMonitoring();
// Directly run checkIdleTimeout with no last active timestamp
// It should initialize it rather than log out immediately
jest.advanceTimersByTime(10000);
// Now trigger notify manually to verify subscriber
(sessionManager as any).notifyLogout();
expect(logoutSpy).toHaveBeenCalledTimes(1);
unsubscribe();
});
test('SessionManager triggers logout after 15 minutes of inactivity', async () => {
const logoutSpy = jest.fn();
sessionManager.subscribe(logoutSpy);
// Start monitoring (initializes last active key)
await sessionManager.startMonitoring();
jest.advanceTimersByTime(100);
// Verify last active is initialized
const lastActiveInitial = await getSessionItem('vero_wallet_last_active');
expect(lastActiveInitial).not.toBeNull();
// Fast forward 14 minutes (under 15m limit)
await jest.advanceTimersByTimeAsync(14 * 60 * 1000);
await sessionManager.checkIdleTimeout();
expect(logoutSpy).not.toHaveBeenCalled();
// Fast forward 2 more minutes (exceeds 15m limit)
await jest.advanceTimersByTimeAsync(2 * 60 * 1000);
await sessionManager.checkIdleTimeout();
expect(logoutSpy).toHaveBeenCalled();
});
test('SessionManager activity reset updates last active timestamp', async () => {
await sessionManager.startMonitoring();
jest.advanceTimersByTime(100);
const initialTimestampStr = await getSessionItem('vero_wallet_last_active');
expect(initialTimestampStr).not.toBeNull();
const initialTimestamp = parseInt(initialTimestampStr!, 10);
// Throttle should prevent updating within 10 seconds
await jest.advanceTimersByTimeAsync(5000);
// Simulate activity event (scroll)
window.dispatchEvent(new Event('scroll'));
// Wait for the update promise if it is active (should be null since it's throttled)
const update1 = (sessionManager as any).activeUpdatePromise;
if (update1) await update1;
const check1TimestampStr = await getSessionItem('vero_wallet_last_active');
expect(parseInt(check1TimestampStr!, 10)).toBe(initialTimestamp);
// Advancing past 10s throttle limit and simulating activity
await jest.advanceTimersByTimeAsync(6000); // 11s total advanced
window.dispatchEvent(new Event('click'));
// Await any pending update promise to let it write to localStorage
const update2 = (sessionManager as any).activeUpdatePromise;
if (update2) await update2;
const check2TimestampStr = await getSessionItem('vero_wallet_last_active');
expect(parseInt(check2TimestampStr!, 10)).toBeGreaterThan(initialTimestamp);
});
});