From c128fd5a1d553f612fad7686f3b8eb33ed573717 Mon Sep 17 00:00:00 2001 From: Joe Portner <5295965+jportner@users.noreply.github.com> Date: Thu, 21 Nov 2019 13:02:29 -0500 Subject: [PATCH] Prevent flashing lifespan warning Previously, the lifespan warning would flash on a user's screen every time they clicked a link, because that would result in an API call to get updated session info. Added a check to prevent that from happening. --- .../public/session/session_timeout.test.tsx | 25 ++++++++++++++----- .../public/session/session_timeout.tsx | 5 ++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/security/public/session/session_timeout.test.tsx b/x-pack/plugins/security/public/session/session_timeout.test.tsx index fee8309b9d8571..37de376e2144db 100644 --- a/x-pack/plugins/security/public/session/session_timeout.test.tsx +++ b/x-pack/plugins/security/public/session/session_timeout.test.tsx @@ -157,7 +157,7 @@ describe('Session Timeout', () => { test(`shows lifespan warning toast`, async () => { const sessionInfo = { now, - idleTimeoutExpiration: now + 2 * 60 * 1000, + idleTimeoutExpiration: null, lifespanExpiration: now + 2 * 60 * 1000, }; http.fetch.mockResolvedValue(sessionInfo); @@ -183,15 +183,28 @@ describe('Session Timeout', () => { jest.advanceTimersByTime(10 * 1000); expectIdleTimeoutWarningToast(notifications); - http.fetch.mockResolvedValue({ - now: now + 55 * 1000, - idleTimeoutExpiration: now + 55 * 1000 + 2 * 60 * 1000, - lifespanExpiration: null, - }); await sessionTimeout.extend('/foo'); expect(http.fetch).toHaveBeenCalledTimes(3); }); + test(`extend does not result in an HTTP call if a lifespan warning is shown`, async () => { + const sessionInfo = { + now, + idleTimeoutExpiration: null, + lifespanExpiration: now + 2 * 60 * 1000, + }; + http.fetch.mockResolvedValue(sessionInfo); + await sessionTimeout.init(); + + // we display the warning a minute before we expire the the session, which is 5 seconds before it actually expires + jest.advanceTimersByTime(55 * 1000); + expectLifespanWarningToast(notifications); + + expect(http.fetch).toHaveBeenCalledTimes(1); + await sessionTimeout.extend('/foo'); + expect(http.fetch).toHaveBeenCalledTimes(1); + }); + test(`extend hides displayed warning toast`, async () => { await sessionTimeout.init(); expect(http.fetch).toHaveBeenCalledTimes(1); diff --git a/x-pack/plugins/security/public/session/session_timeout.tsx b/x-pack/plugins/security/public/session/session_timeout.tsx index 8c891505605151..24abde3496b63e 100644 --- a/x-pack/plugins/security/public/session/session_timeout.tsx +++ b/x-pack/plugins/security/public/session/session_timeout.tsx @@ -84,8 +84,9 @@ export class SessionTimeout { return; } - if (this.warningToast) { - // the warning is currently showing and the user has clicked elsewhere on the page; + const { isMaximum } = this.getTimeout(); + if (this.warningToast && !isMaximum) { + // the idle timeout warning is currently showing and the user has clicked elsewhere on the page; // make a new call to get the latest session info return this.fetchSessionInfoAndResetTimers(); }