import { loggingSystemMock , savedObjectsClientMock } from 'src/core/server/mocks' ; import { getSavedObjectType } from '@kbn/securitysolution-list-utils' ; import { EXCEPTION_LIST_NAMESPACE , EXCEPTION_LIST_NAMESPACE_AGNOSTIC , } from '@kbn/securitysolution-list-constants' ; import type { SavedObjectsUpdateResponse } from 'kibana/server' ; import { getFoundExceptionListSchemaMock } from '../../../common/schemas/response/found_exception_list_schema.mock' ; import { getFoundExceptionListItemSchemaMock } from '../../../common/schemas/response/found_exception_list_item_schema.mock' ; import { getExceptionListItemSchemaMock } from '../../../common/schemas/response/exception_list_item_schema.mock' ; import { getDetectionsExceptionListSchemaMock , getExceptionListSchemaMock , getTrustedAppsListSchemaMock , } from '../../../common/schemas/response/exception_list_schema.mock' ; import { ExtensionPointStorage , ExtensionPointStorageClientInterface } from '../extension_points' ; import type { ExceptionListSoSchema } from '../../schemas/saved_objects' ; import { DATE_NOW , ID , _VERSION } from '../../../common/constants.mock' ; import type { SavedObject } from '../../../../../../src/core/types' ; import { ExceptionListClient } from './exception_list_client' ; import type { CreateExceptionListItemOptions , UpdateExceptionListItemOptions , } from './exception_list_client_types' ; const isExceptionsListSavedObjectType = ( type ) : boolean => type === EXCEPTION_LIST_NAMESPACE || type === EXCEPTION_LIST_NAMESPACE_AGNOSTIC ; export class ExceptionListClientMock extends ExceptionListClient { public getExceptionList = jest . fn ( ) . mockResolvedValue ( getExceptionListSchemaMock ( ) ) ; public getExceptionListItem = jest . fn ( ) . mockResolvedValue ( getExceptionListItemSchemaMock ( ) ) ; public createExceptionList = jest . fn ( ) . mockResolvedValue ( getExceptionListSchemaMock ( ) ) ; public updateExceptionList = jest . fn ( ) . mockResolvedValue ( getExceptionListSchemaMock ( ) ) ; public deleteExceptionList = jest . fn ( ) . mockResolvedValue ( getExceptionListSchemaMock ( ) ) ; public createExceptionListItem = jest . fn ( ) . mockResolvedValue ( getExceptionListItemSchemaMock ( ) ) ; public updateExceptionListItem = jest . fn ( ) . mockResolvedValue ( getExceptionListItemSchemaMock ( ) ) ; public deleteExceptionListItem = jest . fn ( ) . mockResolvedValue ( getExceptionListItemSchemaMock ( ) ) ; public findExceptionListItem = jest . fn ( ) . mockResolvedValue ( getFoundExceptionListItemSchemaMock ( ) ) ; public findExceptionList = jest . fn ( ) . mockResolvedValue ( getFoundExceptionListSchemaMock ( ) ) ; public createTrustedAppsList = jest . fn ( ) . mockResolvedValue ( getTrustedAppsListSchemaMock ( ) ) ; public createEndpointList = jest . fn ( ) . mockResolvedValue ( getExceptionListSchemaMock ( ) ) ; public exportExceptionListAndItems = jest . fn ( ) . mockResolvedValue ( { exportData : `${ JSON . stringify ( getDetectionsExceptionListSchemaMock ( ) ) }\n${ JSON . stringify ( getExceptionListItemSchemaMock ( { list_id : 'exception_list_id' } ) ) }` , exportDetails : { exported_exception_list_count : 1 , exported_exception_list_item_count : 1 , missing_exception_list_item_count : 0 , missing_exception_list_items : [ ] , missing_exception_lists : [ ] , missing_exception_lists_count : 0 , } , } ) ; } export const getExceptionListClientMock = ( savedObject ? , serverExtensionsClient = new ExtensionPointStorage ( loggingSystemMock . createLogger ( ) ) . getClient ( ) ) : ExceptionListClient => { const mock = new ExceptionListClientMock ( { savedObjectsClient : savedObject ? savedObject : savedObjectsClientMock . create ( ) , serverExtensionsClient , user : 'elastic' , } ) ; return mock ; } ; export const getCreateExceptionListItemOptionsMock = ( ) : CreateExceptionListItemOptions => { const { comments , description , entries , item_id : itemId , list_id : listId , meta , name , namespace_type : namespaceType , os_types : osTypes , tags , type , } = getExceptionListItemSchemaMock ( ) ; return { comments , description , entries , itemId , listId , meta , name , namespaceType , osTypes , tags , type , } ; } ; export const getUpdateExceptionListItemOptionsMock = ( ) : UpdateExceptionListItemOptions => { const { comments , entries , itemId , namespaceType , name , osTypes , description , meta , tags , type } = getCreateExceptionListItemOptionsMock ( ) ; return { _version : undefined , comments , description , entries , id : ID , itemId , meta , name , namespaceType , osTypes , tags , type , } ; } ; export const getExceptionListSoSchemaMock = ( overrides = { } ) : ExceptionListSoSchema => { const { comments , created_at , created_by , description , entries , item_id , list_id , meta , name , os_types , tags , tie_breaker_id , type , updated_by , } = getExceptionListItemSchemaMock ( ) ; const soSchema = { comments , created_at , created_by , description , entries , immutable : undefined , item_id , list_id , list_type : 'item' , meta , name , os_types , tags , tie_breaker_id , type , updated_by , version : undefined , ... overrides , } ; return soSchema ; } ; param attributesOverrides param savedObjectOverrides export const getExceptionListItemSavedObject = ( attributesOverrides = { } , savedObjectOverrides = { } ) : SavedObject < ExceptionListSoSchema > => { return { attributes : getExceptionListSoSchemaMock ( attributesOverrides ) , coreMigrationVersion : undefined , error : undefined , id : ID , migrationVersion : undefined , namespaces : undefined , originId : undefined , references : [ ] , type : getSavedObjectType ( { namespaceType : 'agnostic' } ) , updated_at : DATE_NOW , version : _VERSION , ... savedObjectOverrides , } ; } ; param soClient export const getExceptionListSavedObjectClientMock = ( soClient = savedObjectsClientMock . create ( ) ) : ReturnType < typeof savedObjectsClientMock . create > => { const origCreateMock = soClient . create . getMockImplementation ( ) ; soClient . create . mockImplementation ( async ( ... args ) => { const [ type , attributes ] = args ; if ( isExceptionsListSavedObjectType ( type ) ) { return getExceptionListItemSavedObject ( attributes as ExceptionListSoSchema , { type } ) ; } if ( origCreateMock ) { return origCreateMock ( ... args ) ; } return undefined as unknown as SavedObject ; } ) ; const origUpdateMock = soClient . update . getMockImplementation ( ) ; soClient . update . mockImplementation ( async ( ... args ) => { const [ type , id , attributes , { version } = { version : undefined } ] = args ; if ( isExceptionsListSavedObjectType ( type ) ) { return getExceptionListItemSavedObject ( attributes as ExceptionListSoSchema , { id , type , version : version ?? _VERSION , } ) ; } if ( origUpdateMock ) { return origUpdateMock ( ... args ) ; } return undefined as unknown as SavedObjectsUpdateResponse ; } ) ; const origGetMock = soClient . get . getMockImplementation ( ) ; soClient . get . mockImplementation ( async ( ... args ) => { const [ type , id ] = args ; if ( isExceptionsListSavedObjectType ( type ) ) { return getExceptionListItemSavedObject ( { } , { id } ) ; } if ( origGetMock ) { return origGetMock ( ... args ) ; } return undefined as unknown as SavedObject ; } ) ; return soClient ; } ;