@@ -395,9 +395,14 @@ describe('Fleet integrations', () => {
395
395
soClient = savedObjectsClientMock . create ( ) ;
396
396
esClient = elasticsearchServiceMock . createClusterClient ( ) . asInternalUser ;
397
397
endpointAppContextServiceMock = createMockEndpointAppContextService ( ) ;
398
+ jest . clearAllMocks ( ) ;
398
399
endpointAppContextServiceMock . getExceptionListsClient . mockReturnValue ( exceptionListClient ) ;
399
400
callback = getPackagePolicyPostCreateCallback ( endpointAppContextServiceMock ) ;
400
401
policyConfig = generator . generatePolicyPackagePolicy ( ) as PackagePolicy ;
402
+ // By default, simulate that the event filter list does not exist
403
+ ( exceptionListClient . getExceptionList as jest . Mock ) . mockResolvedValue ( null ) ;
404
+ ( exceptionListClient . createExceptionList as jest . Mock ) . mockResolvedValue ( { } ) ;
405
+ ( exceptionListClient . createExceptionListItem as jest . Mock ) . mockResolvedValue ( { } ) ;
401
406
} ) ;
402
407
403
408
it ( 'should create the Endpoint Event Filters List and add the correct Event Filters List Item attached to the policy given nonInteractiveSession parameter on integration config eventFilters' , async ( ) => {
@@ -419,6 +424,9 @@ describe('Fleet integrations', () => {
419
424
req
420
425
) ;
421
426
427
+ // Wait for all async code in createEventFilters to complete
428
+ await new Promise ( process . nextTick ) ;
429
+
422
430
expect ( exceptionListClient . createExceptionList ) . toHaveBeenCalledWith (
423
431
expect . objectContaining ( {
424
432
listId : ENDPOINT_ARTIFACT_LISTS . eventFilters . id ,
@@ -446,6 +454,55 @@ describe('Fleet integrations', () => {
446
454
) ;
447
455
} ) ;
448
456
457
+ it ( 'should NOT create the Event Filters List if it already exists, but should add the Event Filters List Item' , async ( ) => {
458
+ const integrationConfig = {
459
+ type : 'cloud' ,
460
+ eventFilters : {
461
+ nonInteractiveSession : true ,
462
+ } ,
463
+ } ;
464
+
465
+ policyConfig . inputs [ 0 ] ! . config ! . integration_config = {
466
+ value : integrationConfig ,
467
+ } ;
468
+
469
+ // Mock getExceptionList to return a non-null value (list already exists)
470
+ ( exceptionListClient . getExceptionList as jest . Mock ) . mockResolvedValue ( {
471
+ id : 'existing-list-id' ,
472
+ listId : ENDPOINT_ARTIFACT_LISTS . eventFilters . id ,
473
+ } ) ;
474
+
475
+ const postCreatedPolicyConfig = await callback (
476
+ policyConfig ,
477
+ soClient ,
478
+ esClient ,
479
+ requestContextMock . convertContext ( ctx ) ,
480
+ req
481
+ ) ;
482
+
483
+ // Should NOT attempt to create the list
484
+ expect ( exceptionListClient . createExceptionList ) . not . toHaveBeenCalled ( ) ;
485
+ // Should still create the event filter item
486
+ expect ( exceptionListClient . createExceptionListItem ) . toHaveBeenCalledWith (
487
+ expect . objectContaining ( {
488
+ listId : ENDPOINT_ARTIFACT_LISTS . eventFilters . id ,
489
+ tags : [ `policy:${ postCreatedPolicyConfig . id } ` ] ,
490
+ osTypes : [ 'linux' ] ,
491
+ entries : [
492
+ {
493
+ field : 'process.entry_leader.interactive' ,
494
+ operator : 'included' ,
495
+ type : 'match' ,
496
+ value : 'false' ,
497
+ } ,
498
+ ] ,
499
+ itemId : 'NEW_UUID' ,
500
+ namespaceType : 'agnostic' ,
501
+ meta : undefined ,
502
+ } )
503
+ ) ;
504
+ } ) ;
505
+
449
506
it ( 'should not call Event Filters List and Event Filters List Item if nonInteractiveSession parameter is not present on integration config eventFilters' , async ( ) => {
450
507
const integrationConfig = {
451
508
type : 'cloud' ,
0 commit comments