import expect from '@kbn/expect' ; import { FtrProviderContext } from '../services' ; import { LicensingPluginStart , LicenseType } from '../../../plugins/licensing/public' ; import '../../../../test/plugin_functional/plugins/core_provider_plugin/types' ; interface FeatureUsage { last_used ? : number ; license_level : LicenseType ; name : string ; } export default function ( ftrContext ) { const { getService , getPageObjects } = ftrContext ; const supertest = getService ( 'supertest' ) ; const browser = getService ( 'browser' ) ; const PageObjects = getPageObjects ( [ 'common' , 'security' ] ) ; const notifyFeatureUsage = async ( featureName , lastUsed ) => { await browser . executeAsync ( async ( feature , time , cb ) => { const { start } = window . _coreProvider ; const licensing = start . plugins . licensing ; await licensing . featureUsage . notifyUsage ( feature , time ) ; cb ( ) ; } , featureName , lastUsed ) ; } ; describe ( 'feature_usage API' , ( ) => { before ( async ( ) => { await PageObjects . security . login ( ) ; } ) ; it ( 'allows to register features to the server' , async ( ) => { const response = await supertest . get ( '/api/licensing/feature_usage' ) . expect ( 200 ) ; const features = response . body . features . map ( ( { name } ) => name ) ; expect ( features ) . to . contain ( 'test-client-A' ) ; expect ( features ) . to . contain ( 'test-client-B' ) ; } ) ; it ( 'allows to notify feature usage' , async ( ) => { const now = new Date ( ) ; await notifyFeatureUsage ( 'test-client-A' , now . getTime ( ) ) ; const response = await supertest . get ( '/api/licensing/feature_usage' ) . expect ( 200 ) ; const features = response . body . features as FeatureUsage [ ] ; expect ( features . find ( ( f ) => f . name === 'test-client-A' ) ?. last_used ) . to . be ( now . toISOString ( ) ) ; expect ( features . find ( ( f ) => f . name === 'test-client-B' ) ?. last_used ) . to . be ( null ) ; } ) ; } ) ; }