-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
129 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ REACT_APP_OIDC_AUDIENCES=kukkuu-api-test | |
REACT_APP_API_URI=https://kukkuu.api.test.hel.ninja/graphql | ||
REACT_APP_SENTRY_DSN=https://[email protected]/55 | ||
REACT_APP_IS_TEST_ENVIRONMENT=0 | ||
REACT_APP_IDLE_TIMEOUT_IN_MS=3600000 | ||
|
||
BROWSER_TESTS_UID= | ||
BROWSER_TESTS_PWD= | ||
|
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ REACT_APP_OIDC_SERVER_TYPE=KEYCLOAK | |
REACT_APP_API_URI=http://localhost:8081/graphql | ||
REACT_APP_SENTRY_DSN=https://[email protected]/55 | ||
REACT_APP_IS_TEST_ENVIRONMENT=0 | ||
REACT_APP_IDLE_TIMEOUT_IN_MS=3600000 | ||
|
||
BROWSER_TESTS_ENV_URL=http://localhost:3001 | ||
BROWSER_TESTS_JWT_SIGN_SECRET= | ||
|
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
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,30 @@ | ||
import React from 'react'; | ||
import { IdleTimerProvider } from 'react-idle-timer'; | ||
|
||
import authService from './authService'; | ||
import AppConfig from '../application/AppConfig'; | ||
|
||
type IdleTimerProps = { children: React.ReactNode }; | ||
|
||
function IdleTimer({ children }: IdleTimerProps) { | ||
const onIdle = () => { | ||
const isAuthenticated = authService.isAuthenticated(); | ||
if (isAuthenticated) { | ||
authService.logout(); | ||
} | ||
}; | ||
|
||
return ( | ||
<IdleTimerProvider | ||
timeout={AppConfig.userIdleTimeoutInMs || 3_600_000} | ||
onIdle={onIdle} | ||
name="kukkuu-admin-idle-timer" | ||
startOnMount | ||
crossTab | ||
> | ||
{children} | ||
</IdleTimerProvider> | ||
); | ||
} | ||
|
||
export default IdleTimer; |
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,62 @@ | ||
import { MessageChannel } from 'worker_threads'; | ||
|
||
import React from 'react'; | ||
import { | ||
render, | ||
cleanup, | ||
act, | ||
fireEvent, | ||
waitFor, | ||
} from '@testing-library/react'; | ||
|
||
import authService from '../../authentication/authService'; | ||
import IdleTimer from '../IdleTimer'; | ||
|
||
const originalEnv = process.env; | ||
|
||
beforeAll(() => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
global.MessageChannel = MessageChannel; | ||
process.env = { | ||
...originalEnv, | ||
REACT_APP_IDLE_TIMEOUT_IN_MS: '3600000', | ||
}; | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
cleanup(); | ||
process.env = originalEnv; | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
test('check idle timer has logged out after 60min and 1s', async () => { | ||
render(<IdleTimer />); | ||
const start = Date.now(); | ||
|
||
act(() => { | ||
jest.setSystemTime(start + 1000 * 60 * 60 + 1); | ||
fireEvent.focus(document); | ||
}); | ||
|
||
jest.spyOn(authService, 'isAuthenticated').mockReturnValueOnce(true); | ||
jest.spyOn(authService, 'logout'); | ||
await waitFor(() => { | ||
expect(authService.logout()).resolves.toEqual(1); | ||
}); | ||
}); | ||
|
||
test('check idle timer has not logged out after 50min', async () => { | ||
render(<IdleTimer />); | ||
const start = Date.now(); | ||
|
||
act(() => { | ||
jest.setSystemTime(start + 1000 * 60 * 50); | ||
fireEvent.focus(document); | ||
}); | ||
|
||
jest.spyOn(authService, 'isAuthenticated').mockReturnValueOnce(true); | ||
jest.spyOn(authService, 'logout'); | ||
expect(authService.logout()).resolves.toEqual(0); | ||
}); |
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