@@ -1436,97 +1436,120 @@ describe('find()', () => {
14361436} ) ;
14371437
14381438describe ( 'delete()' , ( ) => {
1439- test ( 'successfully removes an alert' , async ( ) => {
1440- const alertsClient = new AlertsClient ( alertsClientParams ) ;
1441- encryptedSavedObjects . getDecryptedAsInternalUser . mockResolvedValueOnce ( {
1442- id : '1' ,
1443- type : 'alert' ,
1444- attributes : {
1445- alertTypeId : '123' ,
1446- schedule : { interval : '10s' } ,
1447- params : {
1448- bar : true ,
1449- } ,
1450- scheduledTaskId : 'task-123' ,
1451- actions : [
1452- {
1453- group : 'default' ,
1454- actionRef : 'action_0' ,
1455- params : {
1456- foo : true ,
1457- } ,
1458- } ,
1459- ] ,
1439+ let alertsClient : AlertsClient ;
1440+ const existingAlert = {
1441+ id : '1' ,
1442+ type : 'alert' ,
1443+ attributes : {
1444+ alertTypeId : '123' ,
1445+ schedule : { interval : '10s' } ,
1446+ params : {
1447+ bar : true ,
14601448 } ,
1461- references : [
1449+ scheduledTaskId : 'task-123' ,
1450+ actions : [
14621451 {
1463- name : 'action_0' ,
1464- type : 'action' ,
1465- id : '1' ,
1452+ group : 'default' ,
1453+ actionRef : 'action_0' ,
1454+ params : {
1455+ foo : true ,
1456+ } ,
14661457 } ,
14671458 ] ,
1468- } ) ;
1469- savedObjectsClient . delete . mockResolvedValueOnce ( {
1459+ } ,
1460+ references : [
1461+ {
1462+ name : 'action_0' ,
1463+ type : 'action' ,
1464+ id : '1' ,
1465+ } ,
1466+ ] ,
1467+ } ;
1468+
1469+ beforeEach ( ( ) => {
1470+ alertsClient = new AlertsClient ( alertsClientParams ) ;
1471+ savedObjectsClient . get . mockResolvedValue ( existingAlert ) ;
1472+ savedObjectsClient . delete . mockResolvedValue ( {
14701473 success : true ,
14711474 } ) ;
1475+ encryptedSavedObjects . getDecryptedAsInternalUser . mockResolvedValue ( {
1476+ ...existingAlert ,
1477+ attributes : {
1478+ ...existingAlert . attributes ,
1479+ apiKey : Buffer . from ( '123:abc' ) . toString ( 'base64' ) ,
1480+ } ,
1481+ } ) ;
1482+ } ) ;
1483+
1484+ test ( 'successfully removes an alert' , async ( ) => {
14721485 const result = await alertsClient . delete ( { id : '1' } ) ;
14731486 expect ( result ) . toEqual ( { success : true } ) ;
1474- expect ( savedObjectsClient . delete ) . toHaveBeenCalledTimes ( 1 ) ;
1475- expect ( savedObjectsClient . delete . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
1476- Array [
1477- "alert",
1478- "1",
1479- ]
1480- ` ) ;
1481- expect ( taskManager . remove ) . toHaveBeenCalledTimes ( 1 ) ;
1482- expect ( taskManager . remove . mock . calls [ 0 ] ) . toMatchInlineSnapshot ( `
1483- Array [
1484- "task-123",
1485- ]
1486- ` ) ;
1487+ expect ( savedObjectsClient . delete ) . toHaveBeenCalledWith ( 'alert' , '1' ) ;
1488+ expect ( taskManager . remove ) . toHaveBeenCalledWith ( 'task-123' ) ;
1489+ expect ( alertsClientParams . invalidateAPIKey ) . toHaveBeenCalledWith ( { id : '123' } ) ;
14871490 } ) ;
14881491
1489- test ( 'swallows error when invalidate API key throws' , async ( ) => {
1490- const alertsClient = new AlertsClient ( alertsClientParams ) ;
1491- alertsClientParams . invalidateAPIKey . mockRejectedValueOnce ( new Error ( 'Fail' ) ) ;
1492- encryptedSavedObjects . getDecryptedAsInternalUser . mockResolvedValueOnce ( {
1493- id : '1' ,
1494- type : 'alert' ,
1492+ test ( `doesn't remove a task when scheduledTaskId is null` , async ( ) => {
1493+ savedObjectsClient . get . mockResolvedValue ( {
1494+ ...existingAlert ,
14951495 attributes : {
1496- alertTypeId : '123' ,
1497- schedule : { interval : '10s' } ,
1498- params : {
1499- bar : true ,
1500- } ,
1501- apiKey : Buffer . from ( '123:abc' ) . toString ( 'base64' ) ,
1502- scheduledTaskId : 'task-123' ,
1503- actions : [
1504- {
1505- group : 'default' ,
1506- actionRef : 'action_0' ,
1507- params : {
1508- foo : true ,
1509- } ,
1510- } ,
1511- ] ,
1496+ ...existingAlert . attributes ,
1497+ scheduledTaskId : null ,
15121498 } ,
1513- references : [
1514- {
1515- name : 'action_0' ,
1516- type : 'action' ,
1517- id : '1' ,
1518- } ,
1519- ] ,
15201499 } ) ;
1521- savedObjectsClient . delete . mockResolvedValueOnce ( {
1522- success : true ,
1500+
1501+ await alertsClient . delete ( { id : '1' } ) ;
1502+ expect ( taskManager . remove ) . not . toHaveBeenCalled ( ) ;
1503+ } ) ;
1504+
1505+ test ( `doesn't invalidate API key when apiKey is null` , async ( ) => {
1506+ encryptedSavedObjects . getDecryptedAsInternalUser . mockResolvedValue ( {
1507+ ...existingAlert ,
1508+ attributes : {
1509+ ...existingAlert . attributes ,
1510+ apiKey : null ,
1511+ } ,
15231512 } ) ;
15241513
15251514 await alertsClient . delete ( { id : '1' } ) ;
1515+ expect ( alertsClientParams . invalidateAPIKey ) . not . toHaveBeenCalled ( ) ;
1516+ } ) ;
1517+
1518+ test ( 'swallows error when invalidate API key throws' , async ( ) => {
1519+ alertsClientParams . invalidateAPIKey . mockRejectedValueOnce ( new Error ( 'Fail' ) ) ;
1520+
1521+ await alertsClient . delete ( { id : '1' } ) ;
1522+ expect ( alertsClientParams . invalidateAPIKey ) . toHaveBeenCalledWith ( { id : '123' } ) ;
15261523 expect ( alertsClientParams . logger . error ) . toHaveBeenCalledWith (
15271524 'Failed to invalidate API Key: Fail'
15281525 ) ;
15291526 } ) ;
1527+
1528+ test ( 'swallows error when getDecryptedAsInternalUser throws an error' , async ( ) => {
1529+ encryptedSavedObjects . getDecryptedAsInternalUser . mockRejectedValue ( new Error ( 'Fail' ) ) ;
1530+
1531+ await alertsClient . delete ( { id : '1' } ) ;
1532+ expect ( alertsClientParams . invalidateAPIKey ) . not . toHaveBeenCalled ( ) ;
1533+ expect ( alertsClientParams . logger . error ) . toHaveBeenCalledWith (
1534+ 'delete(): Failed to load API key to invalidate on alert 1: Fail'
1535+ ) ;
1536+ } ) ;
1537+
1538+ test ( 'throws error when savedObjectsClient.get throws an error' , async ( ) => {
1539+ savedObjectsClient . get . mockRejectedValue ( new Error ( 'SOC Fail' ) ) ;
1540+
1541+ await expect ( alertsClient . delete ( { id : '1' } ) ) . rejects . toThrowErrorMatchingInlineSnapshot (
1542+ `"SOC Fail"`
1543+ ) ;
1544+ } ) ;
1545+
1546+ test ( 'throws error when taskManager.remove throws an error' , async ( ) => {
1547+ taskManager . remove . mockRejectedValue ( new Error ( 'TM Fail' ) ) ;
1548+
1549+ await expect ( alertsClient . delete ( { id : '1' } ) ) . rejects . toThrowErrorMatchingInlineSnapshot (
1550+ `"TM Fail"`
1551+ ) ;
1552+ } ) ;
15301553} ) ;
15311554
15321555describe ( 'update()' , ( ) => {
0 commit comments