-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1237 from US-Trustee-Program/CAMS-513-session-deb…
…ugging CAMS-513 Added session check hook to App.tsx
- Loading branch information
Showing
12 changed files
with
154 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import OktaGateway from './okta-gateway'; | |
import { CamsJwtHeader } from '../../../../../common/src/cams/jwt'; | ||
import * as AuthorizationConfiguration from '../../../configs/authorization-configuration'; | ||
import { AuthorizationConfig } from '../../types/authorization'; | ||
import { nowInSeconds } from '../../../../../common/src/date-helper'; | ||
|
||
describe('Okta gateway tests', () => { | ||
const gateway = OktaGateway; | ||
|
@@ -63,7 +64,7 @@ describe('Okta gateway tests', () => { | |
sub: '[email protected]', | ||
aud: 'api://default', | ||
iat: 0, | ||
exp: Math.floor(Date.now() / 1000) + 600, | ||
exp: nowInSeconds() + 600, | ||
AD_Groups: ['groupD'], | ||
ad_groups: ['groupA', 'groupB'], | ||
groups: ['groupB', 'groupC'], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ import { | |
import { CamsSession } from '../session'; | ||
import { CamsJwtClaims } from '../jwt'; | ||
import { Pagination } from '../../api/pagination'; | ||
import { getIsoDate, getTodaysIsoDate, sortDates } from '../../date-helper'; | ||
import { getIsoDate, getTodaysIsoDate, nowInSeconds, sortDates } from '../../date-helper'; | ||
import { CamsRole } from '../roles'; | ||
import { MOCKED_USTP_OFFICES_ARRAY } from '../offices'; | ||
import { REGION_02_GROUP_NY } from './mock-user'; | ||
|
@@ -654,15 +654,15 @@ function getManhattanTrialAttorneySession(): CamsSession { | |
} | ||
|
||
function getJwt(claims: Partial<CamsJwtClaims> = {}): string { | ||
const SECONDS_SINCE_EPOCH = Math.floor(Date.now() / 1000); | ||
const NOW = nowInSeconds(); | ||
const ONE_HOUR = 3600; | ||
const salt = Math.floor(Math.random() * 10); | ||
|
||
const payload: CamsJwtClaims = { | ||
iss: 'http://fake.issuer.com/oauth2/default', | ||
sub: '[email protected]', | ||
aud: 'fakeApi', | ||
exp: SECONDS_SINCE_EPOCH + ONE_HOUR + salt, | ||
exp: NOW + ONE_HOUR + salt, | ||
groups: [], | ||
...claims, | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import LocalStorage from '@/lib/utils/local-storage'; | ||
import { LOGOUT_PATH } from '@/login/login-library'; | ||
import { checkForSessionEnd, initializeSessionEndLogout } from '@/login/session-end-logout'; | ||
import { CamsSession } from '@common/cams/session'; | ||
import MockData from '@common/cams/test-utilities/mock-data'; | ||
import { nowInSeconds } from '@common/date-helper'; | ||
|
||
describe('Session End Logout tests', () => { | ||
const host = 'camshost'; | ||
const protocol = 'http:'; | ||
const assign = vi.fn(); | ||
|
||
const mockLocation: Location = { | ||
assign, | ||
host, | ||
protocol, | ||
hash: '', | ||
hostname: '', | ||
href: '', | ||
origin: '', | ||
pathname: '', | ||
port: '', | ||
search: '', | ||
reload: vi.fn(), | ||
replace: vi.fn(), | ||
ancestorOrigins: { | ||
length: 0, | ||
item: vi.fn(), | ||
contains: vi.fn(), | ||
[Symbol.iterator]: vi.fn(), | ||
}, | ||
} as const; | ||
|
||
const logoutUri = protocol + '//' + host + LOGOUT_PATH; | ||
|
||
beforeEach(() => { | ||
window.location = { ...mockLocation }; | ||
}); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
test('should redirect if session doesnt exist', () => { | ||
vi.spyOn(LocalStorage, 'getSession').mockReturnValue(null); | ||
checkForSessionEnd(); | ||
expect(assign).toHaveBeenCalledWith(logoutUri); | ||
}); | ||
|
||
test('should redirect if session is expired', () => { | ||
const oneSecondAgo = nowInSeconds() - 1000; | ||
const session: CamsSession = { | ||
user: MockData.getCamsUser(), | ||
accessToken: MockData.getJwt(), | ||
provider: 'mock', | ||
issuer: '', | ||
expires: oneSecondAgo, | ||
}; | ||
vi.spyOn(LocalStorage, 'getSession').mockReturnValue(session); | ||
checkForSessionEnd(); | ||
expect(assign).toHaveBeenCalledWith(logoutUri); | ||
}); | ||
|
||
test('should not redirect if session is not expired', () => { | ||
const tenSecondsFromNow = nowInSeconds() + 10000; | ||
const session: CamsSession = { | ||
user: MockData.getCamsUser(), | ||
accessToken: MockData.getJwt(), | ||
provider: 'mock', | ||
issuer: '', | ||
expires: tenSecondsFromNow, | ||
}; | ||
vi.spyOn(LocalStorage, 'getSession').mockReturnValue(session); | ||
checkForSessionEnd(); | ||
expect(assign).not.toHaveBeenCalledWith(logoutUri); | ||
}); | ||
|
||
test('should call setInterval correctly', () => { | ||
const tenSecondsFromNow = nowInSeconds() + 10000; | ||
const session: CamsSession = { | ||
user: MockData.getCamsUser(), | ||
accessToken: MockData.getJwt(), | ||
provider: 'mock', | ||
issuer: '', | ||
expires: tenSecondsFromNow, | ||
}; | ||
const setIntervalSpy = vi.spyOn(global, 'setInterval'); | ||
|
||
initializeSessionEndLogout(session); | ||
const milliseconds = 10000000; | ||
expect(setIntervalSpy.mock.calls[0][1]).toBeGreaterThan(milliseconds - 5); | ||
expect(setIntervalSpy.mock.calls[0][1]).toBeLessThan(milliseconds + 5); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import LocalStorage from '@/lib/utils/local-storage'; | ||
import { LOGOUT_PATH } from './login-library'; | ||
import { redirectTo } from '@/lib/hooks/UseCamsNavigator'; | ||
import { CamsSession } from '@common/cams/session'; | ||
import { nowInSeconds } from '@common/date-helper'; | ||
|
||
export function checkForSessionEnd() { | ||
const session = LocalStorage.getSession(); | ||
if (!session || session.expires <= nowInSeconds()) { | ||
const { host, protocol } = window.location; | ||
const logoutUri = protocol + '//' + host + LOGOUT_PATH; | ||
redirectTo(logoutUri); | ||
} | ||
} | ||
|
||
export function initializeSessionEndLogout(session: CamsSession) { | ||
setInterval(checkForSessionEnd, Math.floor(session.expires - nowInSeconds()) * 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters