import { FtrProviderContext } from '../../../api_integration/ftr_provider_context' ; import { skipIfNoDockerRegistry } from '../../helpers' ; export default function ( providerContext ) { const { getService } = providerContext ; const supertest = getService ( 'supertest' ) ; const dockerServers = getService ( 'dockerServers' ) ; const server = dockerServers . get ( 'registry' ) ; describe ( 'Package Policy - update' , async function ( ) { skipIfNoDockerRegistry ( providerContext ) ; let agentPolicyId ; let managedAgentPolicyId ; let packagePolicyId ; let packagePolicyId2 ; before ( async ( ) => { await getService ( 'esArchiver' ) . load ( 'x-pack/test/functional/es_archives/empty_kibana' ) ; await getService ( 'esArchiver' ) . load ( 'x-pack/test/functional/es_archives/fleet/empty_fleet_server' ) ; } ) ; before ( async function ( ) { if ( ! server . enabled ) { return ; } const { body : agentPolicyResponse } = await supertest . post ( `/api/fleet/agent_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'Test policy' , namespace : 'default' , } ) ; agentPolicyId = agentPolicyResponse . item . id ; const { body : managedAgentPolicyResponse } = await supertest . post ( `/api/fleet/agent_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'Test hosted agent policy' , namespace : 'default' , is_managed : true , } ) ; const managedExists = managedAgentPolicyResponse . statusCode === 409 ; if ( managedExists ) { const errorRegex = /^agent policy \'(?<id>[\w,\-]+)\' already exists/i ; const result = errorRegex . exec ( managedAgentPolicyResponse . message ) ; if ( result ?. groups ?. id ) { managedAgentPolicyId = result . groups . id ; } } else { managedAgentPolicyId = managedAgentPolicyResponse . item . id ; } const { body : packagePolicyResponse } = await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'default' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) ; packagePolicyId = packagePolicyResponse . item . id ; const { body : packagePolicyResponse2 } = await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-2' , description : '' , namespace : 'default' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) ; packagePolicyId2 = packagePolicyResponse2 . item . id ; } ) ; after ( async function ( ) { await supertest . post ( `/api/fleet/agent_policies/delete` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { agentPolicyId } ) ; } ) ; after ( async ( ) => { await getService ( 'esArchiver' ) . unload ( 'x-pack/test/functional/es_archives/fleet/empty_fleet_server' ) ; await getService ( 'esArchiver' ) . unload ( 'x-pack/test/functional/es_archives/empty_kibana' ) ; } ) ; it ( 'should work with valid values on "regular" policies' , async function ( ) { await supertest . put ( `/api/fleet/package_policies/${ packagePolicyId }` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'updated_namespace' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) ; } ) ; it ( 'should work with valid values on hosted policies' , async function ( ) { await supertest . put ( `/api/fleet/package_policies/${ packagePolicyId }` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'updated_namespace' , policy_id : managedAgentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) ; } ) ; it ( 'should return a 400 if there is another package policy with the same name' , async function ( ) { await supertest . put ( `/api/fleet/package_policies/${ packagePolicyId2 }` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'updated_namespace' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 400 ) ; } ) ; it ( 'should return a 400 if there is another package policy with the same name on a different policy' , async function ( ) { const { body : agentPolicyResponse } = await supertest . post ( `/api/fleet/agent_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'Test policy 2' , namespace : 'default' , } ) ; const otherAgentPolicyId = agentPolicyResponse . item . id ; await supertest . put ( `/api/fleet/package_policies/${ packagePolicyId2 }` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'updated_namespace' , policy_id : otherAgentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 400 ) ; } ) ; it ( 'should work with frozen input vars' , async function ( ) { await supertest . put ( `/api/fleet/package_policies/${ packagePolicyId }` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'updated_namespace' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ { enabled : true , type : 'test-input' , streams : [ ] , vars : { frozen_var : { type : 'text' , value : 'abc' , frozen : true , } , unfrozen_var : { type : 'text' , value : 'def' , } , } , } , ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) ; } ) ; } ) ; }