import { parse as parseCookie , Cookie } from 'tough-cookie' ; import expect from '@kbn/expect' ; import { FtrProviderContext } from '../../ftr_provider_context' ; export default function ( { getService } ) { const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; const config = getService ( 'config' ) ; const kibanaServerConfig = config . get ( 'servers.kibana' ) ; const validUsername = kibanaServerConfig . username ; const validPassword = kibanaServerConfig . password ; describe ( 'Session' , ( ) => { let sessionCookie ; const saveCookie = async ( response ) => { sessionCookie = parseCookie ( response . headers [ 'set-cookie' ] [ 0 ] ) ! ; return response ; } ; const getSessionInfo = async ( ) => supertestWithoutAuth . get ( '/internal/security/session' ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'kbn-system-request' , 'true' ) . set ( 'Cookie' , sessionCookie . cookieString ( ) ) . send ( ) . expect ( 200 ) ; const extendSession = async ( ) => supertestWithoutAuth . post ( '/internal/security/session' ) . set ( 'kbn-xsrf' , 'xxx' ) . set ( 'Cookie' , sessionCookie . cookieString ( ) ) . send ( ) . expect ( 302 ) . then ( saveCookie ) ; beforeEach ( async ( ) => { await supertestWithoutAuth . post ( '/internal/security/login' ) . set ( 'kbn-xsrf' , 'xxx' ) . send ( { providerType : 'basic' , providerName : 'basic1' , currentURL : '/' , params : { username : validUsername , password : validPassword } , } ) . expect ( 200 ) . then ( saveCookie ) ; } ) ; describe ( 'GET /internal/security/session' , ( ) => { it ( 'should return current session information' , async ( ) => { const { body } = await getSessionInfo ( ) ; expect ( body . expiresInMs ) . to . be . a ( 'number' ) ; expect ( body . canBeExtended ) . to . be ( true ) ; expect ( body . provider ) . to . eql ( { type : 'basic' , name : 'basic1' } ) ; } ) ; it ( 'should not extend the session' , async ( ) => { const { body } = await getSessionInfo ( ) ; const { body : body2 } = await getSessionInfo ( ) ; expect ( body2 . expiresInMs ) . to . be . lessThan ( body . expiresInMs ) ; } ) ; } ) ; describe ( 'POST /internal/security/session' , ( ) => { it ( 'should redirect to GET' , async ( ) => { const response = await extendSession ( ) ; expect ( response . headers . location ) . to . be ( '/internal/security/session' ) ; } ) ; it ( 'should extend the session' , async ( ) => { const { body } = await getSessionInfo ( ) ; await extendSession ( ) ; const { body : body2 } = await getSessionInfo ( ) ; expect ( body2 . expiresInMs ) . not . to . be . lessThan ( body . expiresInMs ) ; } ) ; } ) ; } ) ; }