From a06af7aab4e33f44961e7eb6eaac080f13f06f08 Mon Sep 17 00:00:00 2001 From: Julia Bardi Date: Fri, 23 Feb 2024 09:16:17 +0100 Subject: [PATCH] added integration test --- .../server/routes/agent_policy/handlers.ts | 3 +-- .../server/services/agent_policy_create.ts | 4 +++ .../apis/agent_policy/agent_policy.ts | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/fleet/server/routes/agent_policy/handlers.ts b/x-pack/plugins/fleet/server/routes/agent_policy/handlers.ts index 259314c0a8c9e..857102fc7dd34 100644 --- a/x-pack/plugins/fleet/server/routes/agent_policy/handlers.ts +++ b/x-pack/plugins/fleet/server/routes/agent_policy/handlers.ts @@ -172,8 +172,7 @@ export const createAgentPolicyHandler: FleetRequestHandler< const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined; const withSysMonitoring = request.query.sys_monitoring ?? false; const monitoringEnabled = request.body.monitoring_enabled; - const force = request.body.force; - const { has_fleet_server: hasFleetServer, ...newPolicy } = request.body; + const { has_fleet_server: hasFleetServer, force, ...newPolicy } = request.body; const spaceId = fleetContext.spaceId; const authorizationHeader = HTTPAuthorizationHeader.parseFromRequest(request, user?.username); diff --git a/x-pack/plugins/fleet/server/services/agent_policy_create.ts b/x-pack/plugins/fleet/server/services/agent_policy_create.ts index 4ff80a4ee70bd..c830ab43d4110 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy_create.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy_create.ts @@ -54,6 +54,7 @@ async function createPackagePolicy( spaceId: string; user: AuthenticatedUser | undefined; authorizationHeader?: HTTPAuthorizationHeader | null; + force?: boolean; } ) { const newPackagePolicy = await packagePolicyService @@ -78,6 +79,7 @@ async function createPackagePolicy( user: options.user, bumpRevision: false, authorizationHeader: options.authorizationHeader, + force: options.force, }); } @@ -149,6 +151,7 @@ export async function createAgentPolicyWithPackages({ spaceId, user, authorizationHeader, + force, }); } @@ -158,6 +161,7 @@ export async function createAgentPolicyWithPackages({ spaceId, user, authorizationHeader, + force, }); } diff --git a/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts b/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts index b537f836acfe7..69ff7a03822b2 100644 --- a/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts +++ b/x-pack/test/fleet_api_integration/apis/agent_policy/agent_policy.ts @@ -232,6 +232,32 @@ export default function (providerContext: FtrProviderContext) { }); }); + it('should create .fleet-policies document with inputs', async () => { + const res = await supertest + .post(`/api/fleet/agent_policies?sys_monitoring=true`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'test-policy-with-system', + namespace: 'default', + force: true, // using force to bypass package verification error + }) + .expect(200); + + const policyDocRes = await es.search({ + index: '.fleet-policies', + query: { + term: { + policy_id: res.body.item.id, + }, + }, + }); + + expect(policyDocRes?.hits?.hits.length).to.eql(1); + const source = policyDocRes?.hits?.hits[0]?._source as any; + expect(source?.revision_idx).to.eql(1); + expect(source?.data?.inputs.length).to.eql(3); + }); + it('should return a 400 with an empty namespace', async () => { await supertest .post(`/api/fleet/agent_policies`)