@@ -93,6 +93,16 @@ test('createAPIKey() returns { created: false } when security is disabled', asyn
9393 expect ( createAPIKeyResult ) . toEqual ( { created : false } ) ;
9494} ) ;
9595
96+ test ( 'createAPIKey() returns { created: false } when security is enabled but ES security is disabled' , async ( ) => {
97+ const factory = new AlertsClientFactory ( alertsClientFactoryParams ) ;
98+ factory . create ( KibanaRequest . from ( fakeRequest ) , fakeRequest ) ;
99+ const constructorCall = jest . requireMock ( '../alerts_client' ) . AlertsClient . mock . calls [ 0 ] [ 0 ] ;
100+
101+ securityPluginSetup . authc . createAPIKey . mockResolvedValueOnce ( null ) ;
102+ const createAPIKeyResult = await constructorCall . createAPIKey ( ) ;
103+ expect ( createAPIKeyResult ) . toEqual ( { created : false } ) ;
104+ } ) ;
105+
96106test ( 'createAPIKey() returns an API key when security is enabled' , async ( ) => {
97107 const factory = new AlertsClientFactory ( {
98108 ...alertsClientFactoryParams ,
@@ -105,3 +115,17 @@ test('createAPIKey() returns an API key when security is enabled', async () => {
105115 const createAPIKeyResult = await constructorCall . createAPIKey ( ) ;
106116 expect ( createAPIKeyResult ) . toEqual ( { created : true , result : { api_key : '123' , id : 'abc' } } ) ;
107117} ) ;
118+
119+ test ( 'createAPIKey() throws when security plugin createAPIKey throws an error' , async ( ) => {
120+ const factory = new AlertsClientFactory ( {
121+ ...alertsClientFactoryParams ,
122+ securityPluginSetup : securityPluginSetup as any ,
123+ } ) ;
124+ factory . create ( KibanaRequest . from ( fakeRequest ) , fakeRequest ) ;
125+ const constructorCall = jest . requireMock ( '../alerts_client' ) . AlertsClient . mock . calls [ 0 ] [ 0 ] ;
126+
127+ securityPluginSetup . authc . createAPIKey . mockRejectedValueOnce ( new Error ( 'TLS disabled' ) ) ;
128+ await expect ( constructorCall . createAPIKey ( ) ) . rejects . toThrowErrorMatchingInlineSnapshot (
129+ `"TLS disabled"`
130+ ) ;
131+ } ) ;
0 commit comments