import expect from '@kbn/expect' ; import { FtrProviderContext } from '../../../api_integration/ftr_provider_context' ; import { skipIfNoDockerRegistry } from '../../helpers' ; export default function ( providerContext ) { const { getService } = providerContext ; const supertest = getService ( 'supertest' ) ; describe ( 'Package Policy - create' , async function ( ) { skipIfNoDockerRegistry ( providerContext ) ; let agentPolicyId ; 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' ) ; } ) ; after ( async ( ) => { await getService ( 'esArchiver' ) . unload ( 'x-pack/test/functional/es_archives/empty_kibana' ) ; await getService ( 'esArchiver' ) . unload ( 'x-pack/test/functional/es_archives/fleet/empty_fleet_server' ) ; } ) ; before ( async function ( ) { const { body : agentPolicyResponse } = await supertest . post ( `/api/fleet/agent_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'Test policy' , namespace : 'default' , } ) ; agentPolicyId = agentPolicyResponse . item . id ; } ) ; after ( async function ( ) { await supertest . post ( `/api/fleet/agent_policies/delete` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { agentPolicyId } ) ; } ) ; it ( 'can only add to hosted agent policies using the force parameter' , async function ( ) { const { body : { item : hostedPolicy } , } = await supertest . post ( `/api/fleet/agent_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : `Hosted policy from ${ Date . now ( ) }` , namespace : 'default' , is_managed : true , } ) ; const { body : responseWithoutForce } = await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest' , description : '' , namespace : 'default' , policy_id : hostedPolicy . id , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 400 ) ; expect ( responseWithoutForce . statusCode ) . to . be ( 400 ) ; expect ( responseWithoutForce . message ) . to . contain ( 'Cannot update integrations of hosted agent policy' ) ; const { body : responseWithForce } = await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { force : true , name : 'filetest-1' , description : '' , namespace : 'default' , policy_id : hostedPolicy . id , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 200 ) ; expect ( responseWithForce . item . name ) . to . eql ( 'filetest-1' ) ; await supertest . post ( `/api/fleet/agent_policies/delete` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { agentPolicyId : hostedPolicy . id , } ) ; } ) ; it ( 'should work with valid values' , async function ( ) { 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' , } , } ) . expect ( 200 ) ; } ) ; it ( 'should return a 400 with an empty namespace' , async function ( ) { await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , 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 with an invalid namespace' , async function ( ) { await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'InvalidNamespace' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 400 ) ; await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'filetest-1' , description : '' , namespace : 'testlength😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 400 ) ; } ) ; it ( 'should not allow multiple limited packages on the same agent policy' , async function ( ) { await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'endpoint-1' , description : '' , namespace : 'default' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'endpoint' , title : 'Endpoint' , version : '1.3.0-dev.0' , } , } ) . expect ( 200 ) ; await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'endpoint-2' , description : '' , namespace : 'default' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'endpoint' , title : 'Endpoint' , version : '1.3.0-dev.0' , } , } ) . expect ( 400 ) ; } ) ; it ( 'should return a 400 if there is another package policy with the same name' , async function ( ) { await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'same-name-test-1' , description : '' , namespace : 'default' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 200 ) ; await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'same-name-test-1' , description : '' , namespace : 'default' , 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 a 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 . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'same-name-test-2' , description : '' , namespace : 'default' , policy_id : otherAgentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 200 ) ; await supertest . post ( `/api/fleet/package_policies` ) . set ( 'kbn-xsrf' , 'xxxx' ) . send ( { name : 'same-name-test-2' , description : '' , namespace : 'default' , policy_id : agentPolicyId , enabled : true , output_id : '' , inputs : [ ] , package : { name : 'filetest' , title : 'For File Tests' , version : '0.1.0' , } , } ) . expect ( 400 ) ; } ) ; } ) ; }