import expect from '@kbn/expect' ; import { FtrProviderContext } from '../../../api_integration/ftr_provider_context' ; import { skipIfNoDockerRegistry } from '../../helpers' ; import { GetInfoResponse , Installed } from '../../../../plugins/fleet/common' ; import { setupFleetAndAgents } from '../agents/services' ; export default function ( providerContext ) { const { getService } = providerContext ; const supertest = getService ( 'supertest' ) ; const supertestWithoutAuth = getService ( 'supertestWithoutAuth' ) ; const log = getService ( 'log' ) ; const es = getService ( 'es' ) ; describe ( 'setup api' , async ( ) => { skipIfNoDockerRegistry ( providerContext ) ; setupFleetAndAgents ( providerContext ) ; describe ( 'setup performs upgrades' , async ( ) => { const oldEndpointVersion = '0.13.0' ; beforeEach ( async ( ) => { const url = '/api/fleet/epm/packages/endpoint' ; await supertest . delete ( url ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { force : true } ) . expect ( 200 ) ; await supertest . post ( `${ url }-${ oldEndpointVersion }` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { force : true } ) . expect ( 200 ) ; } ) ; it ( 'upgrades the endpoint package from 0.13.0 to the latest version available' , async function ( ) { let { body } = await supertest . get ( `/api/fleet/epm/packages/endpoint/${ oldEndpointVersion }` ) . expect ( 200 ) ; const latestEndpointVersion = body . item . latestVersion ; log . info ( `Endpoint package latest version: ${ latestEndpointVersion }` ) ; expect ( latestEndpointVersion ) . not . eql ( oldEndpointVersion ) ; await supertest . post ( `/api/fleet/setup` ) . set ( 'kbn-xsrf' , 'xxxx' ) . expect ( 200 ) ; ( { body } = await supertest . get ( `/api/fleet/epm/packages/endpoint/${ latestEndpointVersion }` ) . expect ( 200 ) ) ; expect ( body . item ) . to . have . property ( 'savedObject' ) ; expect ( ( body . item as Installed ) . savedObject . attributes . install_version ) . to . eql ( latestEndpointVersion ) ; } ) ; } ) ; it ( 'allows elastic/fleet-server user to call required APIs' , async ( ) => { const { token , } = await es . security . createServiceToken ( { namespace : 'elastic' , service : 'fleet-server' , } ) ; await supertestWithoutAuth . post ( '/api/fleet/setup' ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) ; await supertestWithoutAuth . post ( '/api/fleet/agents/setup' ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) ; await supertestWithoutAuth . get ( '/api/fleet/agent_policies' ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) ; const response = await supertestWithoutAuth . get ( '/api/fleet/enrollment_api_keys' ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) ; const enrollmentApiKeyId = response . body . items [ 0 ] . id ; await supertestWithoutAuth . get ( `/api/fleet/enrollment_api_keys/${ enrollmentApiKeyId }` ) . set ( 'Authorization' , `Bearer ${ token . value }` ) . set ( 'kbn-xsrf' , 'xxx' ) . expect ( 200 ) ; } ) ; } ) ; }