@@ -33,6 +33,7 @@ const actionsAuthorization = actionsAuthorizationMock.create();
3333const auditLogger = auditLoggerMock . create ( ) ;
3434
3535const kibanaVersion = 'v8.2.0' ;
36+ const createAPIKeyMock = jest . fn ( ) ;
3637const rulesClientParams : jest . Mocked < ConstructorOptions > = {
3738 taskManager,
3839 ruleTypeRegistry,
@@ -42,7 +43,7 @@ const rulesClientParams: jest.Mocked<ConstructorOptions> = {
4243 spaceId : 'default' ,
4344 namespace : 'default' ,
4445 getUserName : jest . fn ( ) ,
45- createAPIKey : jest . fn ( ) ,
46+ createAPIKey : createAPIKeyMock ,
4647 logger : loggingSystemMock . create ( ) . get ( ) ,
4748 encryptedSavedObjectsClient : encryptedSavedObjects ,
4849 getActionsClient : jest . fn ( ) ,
@@ -581,6 +582,99 @@ describe('bulkEdit()', () => {
581582 ) ;
582583 } ) ;
583584
585+ test ( 'should call bulkMarkApiKeysForInvalidation to invalidate unused keys if bulkUpdate failed' , async ( ) => {
586+ createAPIKeyMock . mockReturnValue ( { apiKeysEnabled : true , result : { api_key : '111' } } ) ;
587+ mockCreatePointInTimeFinderAsInternalUser ( {
588+ saved_objects : [
589+ {
590+ ...existingDecryptedRule ,
591+ attributes : { ...existingDecryptedRule . attributes , enabled : true } ,
592+ } ,
593+ ] ,
594+ } ) ;
595+
596+ unsecuredSavedObjectsClient . bulkUpdate . mockImplementation ( ( ) => {
597+ throw new Error ( 'Fail' ) ;
598+ } ) ;
599+
600+ await expect (
601+ rulesClient . bulkEdit ( {
602+ filter : 'alert.attributes.tags: "APM"' ,
603+ operations : [
604+ {
605+ field : 'tags' ,
606+ operation : 'add' ,
607+ value : [ 'test-1' ] ,
608+ } ,
609+ ] ,
610+ } )
611+ ) . rejects . toThrow ( 'Fail' ) ;
612+
613+ expect ( bulkMarkApiKeysForInvalidation ) . toHaveBeenCalledTimes ( 1 ) ;
614+ expect ( bulkMarkApiKeysForInvalidation ) . toHaveBeenCalledWith (
615+ { apiKeys : [ 'dW5kZWZpbmVkOjExMQ==' ] } ,
616+ expect . any ( Object ) ,
617+ expect . any ( Object )
618+ ) ;
619+ } ) ;
620+
621+ test ( 'should call bulkMarkApiKeysForInvalidation to invalidate unused keys if SO update failed' , async ( ) => {
622+ createAPIKeyMock . mockReturnValue ( { apiKeysEnabled : true , result : { api_key : '111' } } ) ;
623+ mockCreatePointInTimeFinderAsInternalUser ( {
624+ saved_objects : [
625+ {
626+ ...existingDecryptedRule ,
627+ attributes : { ...existingDecryptedRule . attributes , enabled : true } ,
628+ } ,
629+ ] ,
630+ } ) ;
631+
632+ unsecuredSavedObjectsClient . bulkUpdate . mockResolvedValue ( {
633+ saved_objects : [
634+ {
635+ id : '1' ,
636+ type : 'alert' ,
637+ attributes : {
638+ enabled : true ,
639+ tags : [ 'foo' ] ,
640+ alertTypeId : 'myType' ,
641+ schedule : { interval : '1m' } ,
642+ consumer : 'myApp' ,
643+ scheduledTaskId : 'task-123' ,
644+ params : { index : [ 'test-index-*' ] } ,
645+ throttle : null ,
646+ notifyWhen : null ,
647+ actions : [ ] ,
648+ } ,
649+ references : [ ] ,
650+ version : '123' ,
651+ error : {
652+ error : 'test failure' ,
653+ statusCode : 409 ,
654+ message : 'test failure' ,
655+ } ,
656+ } ,
657+ ] ,
658+ } ) ;
659+
660+ await rulesClient . bulkEdit ( {
661+ filter : 'alert.attributes.tags: "APM"' ,
662+ operations : [
663+ {
664+ field : 'tags' ,
665+ operation : 'add' ,
666+ value : [ 'test-1' ] ,
667+ } ,
668+ ] ,
669+ } ) ;
670+
671+ expect ( bulkMarkApiKeysForInvalidation ) . toHaveBeenCalledWith (
672+ { apiKeys : [ 'dW5kZWZpbmVkOjExMQ==' ] } ,
673+ expect . any ( Object ) ,
674+ expect . any ( Object )
675+ ) ;
676+ } ) ;
677+
584678 test ( 'should not call create apiKey if rule is disabled' , async ( ) => {
585679 await rulesClient . bulkEdit ( {
586680 filter : 'alert.attributes.tags: "APM"' ,
0 commit comments