diff --git a/x-pack/plugins/observability/public/hooks/use_alert_permission.test.tsx b/x-pack/plugins/observability/public/hooks/use_alert_permission.test.ts similarity index 100% rename from x-pack/plugins/observability/public/hooks/use_alert_permission.test.tsx rename to x-pack/plugins/observability/public/hooks/use_alert_permission.test.ts diff --git a/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.test.ts b/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.test.ts new file mode 100644 index 0000000000000..6bb8b476b4d01 --- /dev/null +++ b/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.test.ts @@ -0,0 +1,79 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { renderHook } from '@testing-library/react-hooks'; +import { applicationServiceMock } from 'src/core/public/mocks'; +import { casesFeatureId } from '../../common'; +import { useGetUserCasesPermissions } from './use_get_user_cases_permissions'; +import { kibanaStartMock } from '../utils/kibana_react.mock'; + +let mockUseKibanaReturnValue = kibanaStartMock.startContract(); + +jest.mock('../utils/kibana_react', () => ({ + __esModule: true, + useKibana: jest.fn(() => mockUseKibanaReturnValue), +})); + +describe('useGetUserCasesPermissions', function () { + it('returns expected permissions when capabilities entry exists', () => { + mockUseKibanaReturnValue = { + ...mockUseKibanaReturnValue, + services: { + ...mockUseKibanaReturnValue.services, + application: { + ...mockUseKibanaReturnValue.services.application, + capabilities: { + ...applicationServiceMock.createStartContract().capabilities, + [casesFeatureId]: { crud_cases: false, read_cases: true }, + }, + }, + }, + }; + const { result } = renderHook(() => useGetUserCasesPermissions(), {}); + expect(result.current?.read).toBe(true); + expect(result.current?.crud).toBe(false); + }); + + it('returns false when capabilities entry permissions are missing', () => { + mockUseKibanaReturnValue = { + ...mockUseKibanaReturnValue, + services: { + ...mockUseKibanaReturnValue.services, + application: { + ...mockUseKibanaReturnValue.services.application, + capabilities: { + ...applicationServiceMock.createStartContract().capabilities, + [casesFeatureId]: { + /* intentionally empty */ + }, + }, + }, + }, + }; + const { result } = renderHook(() => useGetUserCasesPermissions(), {}); + expect(result.current?.read).toBe(false); + expect(result.current?.crud).toBe(false); + }); + + it('returns false when capabilities entry is missing entirely', () => { + mockUseKibanaReturnValue = { + ...mockUseKibanaReturnValue, + services: { + ...mockUseKibanaReturnValue.services, + application: { + ...mockUseKibanaReturnValue.services.application, + capabilities: { + ...applicationServiceMock.createStartContract().capabilities, + }, + }, + }, + }; + const { result } = renderHook(() => useGetUserCasesPermissions(), {}); + expect(result.current?.read).toBe(false); + expect(result.current?.crud).toBe(false); + }); +}); diff --git a/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.tsx b/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.tsx index 452fd8006fbf6..2827f89626794 100644 --- a/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.tsx +++ b/x-pack/plugins/observability/public/hooks/use_get_user_cases_permissions.tsx @@ -20,11 +20,11 @@ export function useGetUserCasesPermissions() { useEffect(() => { const capabilitiesCanUserCRUD: boolean = - typeof uiCapabilities[casesFeatureId].crud_cases === 'boolean' + typeof uiCapabilities[casesFeatureId]?.crud_cases === 'boolean' ? (uiCapabilities[casesFeatureId].crud_cases as boolean) : false; const capabilitiesCanUserRead: boolean = - typeof uiCapabilities[casesFeatureId].read_cases === 'boolean' + typeof uiCapabilities[casesFeatureId]?.read_cases === 'boolean' ? (uiCapabilities[casesFeatureId].read_cases as boolean) : false; setCasesPermissions({ diff --git a/x-pack/plugins/observability/public/observability_public_plugins_start.mock.ts b/x-pack/plugins/observability/public/observability_public_plugins_start.mock.ts new file mode 100644 index 0000000000000..4d6d312bcf199 --- /dev/null +++ b/x-pack/plugins/observability/public/observability_public_plugins_start.mock.ts @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +const casesUiStartMock = { + createStart() { + return { + getCases: jest.fn(), + getAllCasesSelectorModal: jest.fn(), + getCreateCaseFlyout: jest.fn(), + getRecentCases: jest.fn(), + }; + }, +}; + +const embeddableStartMock = { + createStart() { + return { + getEmbeddableFactory: jest.fn(), + getEmbeddableFactories: jest.fn(), + EmbeddablePanel: jest.fn(), + getStateTransfer: jest.fn(), + getAttributeService: jest.fn(), + telemetry: null, + inject: jest.fn(), + extract: jest.fn(), + getAllMigrations: jest.fn(), + }; + }, +}; + +export const observabilityPublicPluginsStartMock = { + createStart() { + return { + cases: casesUiStartMock.createStart(), + embeddable: embeddableStartMock.createStart(), + triggersActionsUi: null, + data: null, + lens: null, + discover: null, + }; + }, +}; diff --git a/x-pack/plugins/observability/public/utils/kibana_react.mock.ts b/x-pack/plugins/observability/public/utils/kibana_react.mock.ts new file mode 100644 index 0000000000000..5cb00169abfb9 --- /dev/null +++ b/x-pack/plugins/observability/public/utils/kibana_react.mock.ts @@ -0,0 +1,23 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { coreMock, notificationServiceMock, overlayServiceMock } from 'src/core/public/mocks'; +import { observabilityPublicPluginsStartMock } from '../observability_public_plugins_start.mock'; + +export const kibanaStartMock = { + startContract() { + return { + notifications: notificationServiceMock.createStartContract(), + overlays: overlayServiceMock.createStartContract(), + services: { + ...coreMock.createStart(), + ...observabilityPublicPluginsStartMock.createStart(), + storage: coreMock.createStorage(), + }, + }; + }, +};