Skip to content

Commit a7a6fa2

Browse files
authored
fix: resolve off by one error in session expiration (#647)
1 parent c8bc3be commit a7a6fa2

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sessions/SessionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class SessionManager {
126126
this.createSession();
127127
} else if (
128128
this.session.sessionId !== NIL_UUID &&
129-
new Date() > this.sessionExpiry
129+
new Date() >= this.sessionExpiry
130130
) {
131131
// The session has expired. Create a new one.
132132
this.createSession();

src/sessions/__tests__/SessionManager.test.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
DEFAULT_CONFIG,
2323
mockFetch
2424
} from '../../test-utils/test-utils';
25-
import { advanceTo } from 'jest-date-mock';
2625

2726
global.fetch = mockFetch;
2827
const NAVIGATION = 'navigation';
@@ -67,8 +66,14 @@ const defaultSessionManager = (config) => {
6766
);
6867
};
6968

69+
let navigatorCookieEnabled = true;
70+
Object.defineProperty(window.navigator, 'cookieEnabled', {
71+
configurable: true,
72+
get: () => navigatorCookieEnabled
73+
});
74+
7075
describe('SessionManager tests', () => {
71-
beforeEach(async () => {
76+
beforeEach(() => {
7277
window.performance.getEntriesByType = jest
7378
.fn()
7479
.mockImplementation((type) => {
@@ -78,21 +83,13 @@ describe('SessionManager tests', () => {
7883
});
7984

8085
// cookie enabled
81-
setNavigatorCookieEnabled(true);
82-
86+
navigatorCookieEnabled = true;
8387
removeCookie(SESSION_COOKIE_NAME, DEFAULT_CONFIG.cookieAttributes);
8488
removeCookie(USER_COOKIE_NAME, DEFAULT_CONFIG.cookieAttributes);
85-
89+
jest.useRealTimers(); // This avoids stack overflow to document.location.toString() in jest's mock browser environment
8690
mockRecord.mockClear();
8791
});
8892

89-
const setNavigatorCookieEnabled = (isEnabled: boolean) => {
90-
Object.defineProperty(window.navigator, 'cookieEnabled', {
91-
writable: true,
92-
value: isEnabled
93-
});
94-
};
95-
9693
test('When sessionId does not exist in cookie, then new sessionId is assigned', async () => {
9794
// Init
9895
const sessionManager = defaultSessionManager(DEFAULT_CONFIG);
@@ -196,7 +193,6 @@ describe('SessionManager tests', () => {
196193

197194
const sessionA = sessionManager.getSession();
198195
config.allowCookies = false;
199-
await new Promise((resolve) => setTimeout(resolve, 0));
200196
const sessionB = sessionManager.getSession();
201197

202198
// Assert
@@ -214,7 +210,6 @@ describe('SessionManager tests', () => {
214210

215211
const sessionA = sessionManager.getSession();
216212
config.allowCookies = true;
217-
await new Promise((resolve) => setTimeout(resolve, 0));
218213
const sessionB = sessionManager.getSession();
219214

220215
// Assert
@@ -248,7 +243,7 @@ describe('SessionManager tests', () => {
248243

249244
test('When cookie is disabled, then sessionId is assigned from sessionManager', async () => {
250245
// Init
251-
setNavigatorCookieEnabled(false);
246+
navigatorCookieEnabled = false;
252247
const sessionManager = defaultSessionManager({
253248
...DEFAULT_CONFIG,
254249
...{ allowCookies: true }
@@ -333,7 +328,7 @@ describe('SessionManager tests', () => {
333328

334329
test('When cookie is disabled, then userId is assigned from sessionManager', async () => {
335330
// Init
336-
setNavigatorCookieEnabled(false);
331+
navigatorCookieEnabled = false;
337332
const sessionManager = defaultSessionManager({
338333
...DEFAULT_CONFIG,
339334
...{ userIdRetentionDays: 90 }
@@ -387,7 +382,6 @@ describe('SessionManager tests', () => {
387382
});
388383

389384
const sessionOne = sessionManager.getSession();
390-
await new Promise((resolve) => setTimeout(resolve, 10));
391385
const sessionTwo = sessionManager.getSession();
392386

393387
// Assert

0 commit comments

Comments
 (0)