From e1ce698c554fc479d04a4ed48299b0a7f13f88ac Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 18 Nov 2024 10:42:47 -0500 Subject: [PATCH] [Fleet] Fix agent policy namespace validation (#200258) --- .../agent_policy_advanced_fields/index.tsx | 4 +-- .../services/spaces/space_settings.test.ts | 34 ++++++++++++++++++- .../server/services/spaces/space_settings.ts | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx index 0277184acabf..305148584f54 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/components/agent_policy_advanced_fields/index.tsx @@ -375,8 +375,8 @@ export const AgentPolicyAdvancedOptionsContent: React.FunctionComponent = > { }); }); }); + +describe('getSpaceSettings', () => { + function createSavedsClientMock(settingsAttributes?: any) { + const client = savedObjectsClientMock.create(); + + if (settingsAttributes) { + client.get.mockResolvedValue({ + attributes: settingsAttributes, + } as any); + } else { + client.get.mockRejectedValue( + SavedObjectsErrorHelpers.createGenericNotFoundError('Not found') + ); + } + + jest.mocked(appContextService.getInternalUserSOClientForSpaceId).mockReturnValue(client); + + return client; + } + it('should work with managedBy:null', async () => { + createSavedsClientMock({ + allowed_namespace_prefixes: ['test'], + managed_by: null, + }); + const res = await getSpaceSettings(); + + expect(res).toEqual({ + allowed_namespace_prefixes: ['test'], + managed_by: undefined, + }); + }); +}); diff --git a/x-pack/plugins/fleet/server/services/spaces/space_settings.ts b/x-pack/plugins/fleet/server/services/spaces/space_settings.ts index ece0291ff4f7..dff4df63b6a9 100644 --- a/x-pack/plugins/fleet/server/services/spaces/space_settings.ts +++ b/x-pack/plugins/fleet/server/services/spaces/space_settings.ts @@ -36,7 +36,7 @@ export async function getSpaceSettings(spaceId?: string) { return { allowed_namespace_prefixes: settings?.attributes?.allowed_namespace_prefixes ?? [], - managed_by: settings?.attributes?.managed_by, + managed_by: settings?.attributes?.managed_by ?? undefined, }; }