44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7- import { coreMock } from 'src/core/public/mocks' ;
87import { SessionExpired } from './session_expired' ;
98
10- describe ( 'Session Expiration ' , ( ) => {
9+ describe ( '#logout ' , ( ) => {
1110 const mockGetItem = jest . fn ( ) . mockReturnValue ( null ) ;
11+ const CURRENT_URL = '/foo/bar?baz=quz#quuz' ;
12+ const LOGOUT_URL = '/logout' ;
13+ const TENANT = '/some-basepath' ;
14+
15+ let newUrlPromise : Promise < string > ;
1216
1317 beforeAll ( ( ) => {
1418 Object . defineProperty ( window , 'sessionStorage' , {
@@ -19,69 +23,42 @@ describe('Session Expiration', () => {
1923 } ) ;
2024 } ) ;
2125
26+ beforeEach ( ( ) => {
27+ window . history . pushState ( { } , '' , CURRENT_URL ) ;
28+ mockGetItem . mockReset ( ) ;
29+ newUrlPromise = new Promise < string > ( resolve => {
30+ jest . spyOn ( window . location , 'assign' ) . mockImplementation ( url => {
31+ resolve ( url ) ;
32+ } ) ;
33+ } ) ;
34+ } ) ;
35+
2236 afterAll ( ( ) => {
2337 delete ( window as any ) . sessionStorage ;
2438 } ) ;
2539
26- describe ( 'logout' , ( ) => {
27- const mockCurrentUrl = ( url : string ) => window . history . pushState ( { } , '' , url ) ;
28- const tenant = '' ;
29-
30- it ( 'redirects user to "/logout" when there is no basePath' , async ( ) => {
31- const { basePath } = coreMock . createSetup ( ) . http ;
32- mockCurrentUrl ( '/foo/bar?baz=quz#quuz' ) ;
33- const sessionExpired = new SessionExpired ( basePath , tenant ) ;
34- const newUrlPromise = new Promise < string > ( resolve => {
35- jest . spyOn ( window . location , 'assign' ) . mockImplementation ( url => {
36- resolve ( url ) ;
37- } ) ;
38- } ) ;
39-
40- sessionExpired . logout ( ) ;
40+ it ( `redirects user to the logout URL with 'msg' and 'next' parameters` , async ( ) => {
41+ const sessionExpired = new SessionExpired ( LOGOUT_URL , TENANT ) ;
42+ sessionExpired . logout ( ) ;
4143
42- const url = await newUrlPromise ;
43- expect ( url ) . toBe (
44- `/logout?next=${ encodeURIComponent ( '/foo/bar?baz=quz#quuz' ) } &msg=SESSION_EXPIRED`
45- ) ;
46- } ) ;
47-
48- it ( 'adds a provider parameter when an auth provider is saved in sessionStorage' , async ( ) => {
49- const { basePath } = coreMock . createSetup ( ) . http ;
50- mockCurrentUrl ( '/foo/bar?baz=quz#quuz' ) ;
51- const sessionExpired = new SessionExpired ( basePath , tenant ) ;
52- const newUrlPromise = new Promise < string > ( resolve => {
53- jest . spyOn ( window . location , 'assign' ) . mockImplementation ( url => {
54- resolve ( url ) ;
55- } ) ;
56- } ) ;
57- mockGetItem . mockReturnValueOnce ( 'basic' ) ;
58-
59- sessionExpired . logout ( ) ;
44+ const next = `&next=${ encodeURIComponent ( CURRENT_URL ) } ` ;
45+ await expect ( newUrlPromise ) . resolves . toBe ( `${ LOGOUT_URL } ?msg=SESSION_EXPIRED${ next } ` ) ;
46+ } ) ;
6047
61- const url = await newUrlPromise ;
62- expect ( url ) . toBe (
63- `/logout?next=${ encodeURIComponent (
64- '/foo/bar?baz=quz#quuz'
65- ) } &msg=SESSION_EXPIRED&provider=basic`
66- ) ;
67- } ) ;
48+ it ( `adds 'provider' parameter when sessionStorage contains the provider name for this tenant` , async ( ) => {
49+ const providerName = 'basic' ;
50+ mockGetItem . mockReturnValueOnce ( providerName ) ;
6851
69- it ( 'redirects user to "/${basePath}/logout" and removes basePath from next parameter when there is a basePath' , async ( ) => {
70- const { basePath } = coreMock . createSetup ( { basePath : '/foo' } ) . http ;
71- mockCurrentUrl ( '/foo/bar?baz=quz#quuz' ) ;
72- const sessionExpired = new SessionExpired ( basePath , tenant ) ;
73- const newUrlPromise = new Promise < string > ( resolve => {
74- jest . spyOn ( window . location , 'assign' ) . mockImplementation ( url => {
75- resolve ( url ) ;
76- } ) ;
77- } ) ;
52+ const sessionExpired = new SessionExpired ( LOGOUT_URL , TENANT ) ;
53+ sessionExpired . logout ( ) ;
7854
79- sessionExpired . logout ( ) ;
55+ expect ( mockGetItem ) . toHaveBeenCalledTimes ( 1 ) ;
56+ expect ( mockGetItem ) . toHaveBeenCalledWith ( `${ TENANT } /session_provider` ) ;
8057
81- const url = await newUrlPromise ;
82- expect ( url ) . toBe (
83- `/foo/logout?next= ${ encodeURIComponent ( '/bar?baz=quz#quuz' ) } &msg=SESSION_EXPIRED`
84- ) ;
85- } ) ;
58+ const next = `&next= ${ encodeURIComponent ( CURRENT_URL ) } ` ;
59+ const provider = `&provider= ${ providerName } ` ;
60+ await expect ( newUrlPromise ) . resolves . toBe (
61+ ` ${ LOGOUT_URL } ?msg=SESSION_EXPIRED ${ next } ${ provider } `
62+ ) ;
8663 } ) ;
8764} ) ;
0 commit comments