import { RulesClient , ConstructorOptions } from '../rules_client' ; import { savedObjectsClientMock , loggingSystemMock } from '../../../../../../src/core/server/mocks' ; import { taskManagerMock } from '../../../../task_manager/server/mocks' ; import { ruleTypeRegistryMock } from '../../rule_type_registry.mock' ; import { alertingAuthorizationMock } from '../../authorization/alerting_authorization.mock' ; import { encryptedSavedObjectsMock } from '../../../../encrypted_saved_objects/server/mocks' ; import { actionsAuthorizationMock } from '../../../../actions/server/mocks' ; import { AlertingAuthorization , RegistryAlertTypeWithAuth , } from '../../authorization/alerting_authorization' ; import { ActionsAuthorization } from '../../../../actions/server' ; import { getBeforeSetup } from './lib' ; import { RecoveredActionGroup } from '../../../common' ; import { RegistryRuleType } from '../../rule_type_registry' ; const taskManager = taskManagerMock . createStart ( ) ; const ruleTypeRegistry = ruleTypeRegistryMock . create ( ) ; const unsecuredSavedObjectsClient = savedObjectsClientMock . create ( ) ; const encryptedSavedObjects = encryptedSavedObjectsMock . createClient ( ) ; const authorization = alertingAuthorizationMock . create ( ) ; const actionsAuthorization = actionsAuthorizationMock . create ( ) ; const kibanaVersion = 'v7.10.0' ; const rulesClientParams = { taskManager , ruleTypeRegistry , unsecuredSavedObjectsClient , authorization : authorization as unknown as AlertingAuthorization , actionsAuthorization : actionsAuthorization as unknown as ActionsAuthorization , spaceId : 'default' , namespace : 'default' , getUserName : jest . fn ( ) , createAPIKey : jest . fn ( ) , logger : loggingSystemMock . create ( ) . get ( ) , encryptedSavedObjectsClient : encryptedSavedObjects , getActionsClient : jest . fn ( ) , getEventLogClient : jest . fn ( ) , kibanaVersion , } ; beforeEach ( ( ) => { getBeforeSetup ( rulesClientParams , taskManager , ruleTypeRegistry ) ; } ) ; describe ( 'listAlertTypes' , ( ) => { let rulesClient ; const alertingAlertType = { actionGroups : [ ] , actionVariables : undefined , defaultActionGroupId : 'default' , minimumLicenseRequired : 'basic' , isExportable : true , recoveryActionGroup : RecoveredActionGroup , id : 'alertingAlertType' , name : 'alertingAlertType' , producer : 'alerts' , enabledInLicense : true , } ; const myAppAlertType = { actionGroups : [ ] , actionVariables : undefined , defaultActionGroupId : 'default' , minimumLicenseRequired : 'basic' , isExportable : true , recoveryActionGroup : RecoveredActionGroup , id : 'myAppAlertType' , name : 'myAppAlertType' , producer : 'myApp' , enabledInLicense : true , } ; const setOfAlertTypes = new Set ( [ myAppAlertType , alertingAlertType ] ) ; const authorizedConsumers = { alerts : { read : true , all : true } , myApp : { read : true , all : true } , myOtherApp : { read : true , all : true } , } ; beforeEach ( ( ) => { rulesClient = new RulesClient ( rulesClientParams ) ; } ) ; test ( 'should return a list of AlertTypes that exist in the registry' , async ( ) => { ruleTypeRegistry . list . mockReturnValue ( setOfAlertTypes ) ; authorization . filterByRuleTypeAuthorization . mockResolvedValue ( new Set < RegistryAlertTypeWithAuth > ( [ { ... myAppAlertType , authorizedConsumers } , { ... alertingAlertType , authorizedConsumers } , ] ) ) ; expect ( await rulesClient . listAlertTypes ( ) ) . toEqual ( new Set ( [ { ... myAppAlertType , authorizedConsumers } , { ... alertingAlertType , authorizedConsumers } , ] ) ) ; } ) ; describe ( 'authorization' , ( ) => { const listedTypes = new Set < RegistryRuleType > ( [ { actionGroups : [ ] , actionVariables : undefined , defaultActionGroupId : 'default' , minimumLicenseRequired : 'basic' , isExportable : true , recoveryActionGroup : RecoveredActionGroup , id : 'myType' , name : 'myType' , producer : 'myApp' , enabledInLicense : true , } , { id : 'myOtherType' , name : 'Test' , actionGroups : [ { id : 'default' , name : 'Default' } ] , defaultActionGroupId : 'default' , minimumLicenseRequired : 'basic' , isExportable : true , recoveryActionGroup : RecoveredActionGroup , producer : 'alerts' , enabledInLicense : true , } , ] ) ; beforeEach ( ( ) => { ruleTypeRegistry . list . mockReturnValue ( listedTypes ) ; } ) ; test ( 'should return a list of AlertTypes that exist in the registry only if the user is authorised to get them' , async ( ) => { const authorizedTypes = new Set < RegistryAlertTypeWithAuth > ( [ { id : 'myType' , name : 'Test' , actionGroups : [ { id : 'default' , name : 'Default' } ] , defaultActionGroupId : 'default' , minimumLicenseRequired : 'basic' , isExportable : true , recoveryActionGroup : RecoveredActionGroup , producer : 'alerts' , authorizedConsumers : { myApp : { read : true , all : true } , } , enabledInLicense : true , } , ] ) ; authorization . filterByRuleTypeAuthorization . mockResolvedValue ( authorizedTypes ) ; expect ( await rulesClient . listAlertTypes ( ) ) . toEqual ( authorizedTypes ) ; } ) ; } ) ; } ) ;