import expect from '@kbn/expect' ; import fetch from 'node-fetch' ; import { sha256 } from 'js-sha256' ; import { FtrProviderContext } from '../ftr_provider_context' ; import { CLOUD_USER_ID } from '../constants' ; export default function ( { getService , getPageObjects } ) { const browser = getService ( 'browser' ) ; const find = getService ( 'find' ) ; const PageObjects = getPageObjects ( [ 'common' ] ) ; const delay = ( ms ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ; describe ( 'Cloud FullStory integration' , function ( ) { before ( async ( ) => { await getService ( 'esSupertest' ) . post ( '/_security/role_mapping/saml1' ) . send ( { roles : [ 'superuser' ] , enabled : true , rules : { field : { 'realm.name' : 'saml1' } } } ) . expect ( 200 ) ; } ) ; it ( 'initializes FullStory' , async ( ) => { await PageObjects . common . navigateToApp ( 'home' ) ; await find . byCssSelector ( '[data-test-subj="userMenuButton"]' , 20000 ) ; expect ( await browser . execute ( ( ) => typeof window . FSKibana === 'function' ) ) . to . eql ( true ) ; } ) ; it ( 'records a FullStory session with the associated SAML user' , async ( ) => { let sessionUrl = null ; let attempts = 0 ; while ( sessionUrl === null && attempts < 30 ) { sessionUrl = await browser . execute ( ( ) => window . FSKibana . getCurrentSessionURL ( ) ) ; attempts ++ ; await delay ( 1000 ) ; } expect ( typeof sessionUrl ) . to . eql ( 'string' ) ; sessionUrl = sessionUrl ! . replace ( '%3A' , ':' ) ; const hashedUserId = sha256 ( CLOUD_USER_ID ) ; const fsSessions = await fetch ( `https://www.fullstory.com/api/v1/sessions?uid=${ hashedUserId }&limit=100` , { headers : { 'content-type' : 'application/json' , Authorization : `Basic ${ process . env . FULLSTORY_API_KEY }` , } , } ) . then ( ( r ) => r . json ( ) ) ; expect ( fsSessions . find ( ( s ) => s . FsUrl === sessionUrl ) ) . not . to . be ( undefined ) ; } ) ; } ) ; }