@@ -10,7 +10,6 @@ jest.mock('./api_keys');
1010jest . mock ( './authenticator' ) ;
1111
1212import Boom from 'boom' ;
13- import { errors } from 'elasticsearch' ;
1413import { first } from 'rxjs/operators' ;
1514
1615import {
@@ -27,7 +26,6 @@ import {
2726 AuthToolkit ,
2827 IClusterClient ,
2928 CoreSetup ,
30- ElasticsearchErrorHelpers ,
3129 KibanaRequest ,
3230 LoggerFactory ,
3331 ScopedClusterClient ,
@@ -289,67 +287,66 @@ describe('setupAuthentication()', () => {
289287 } ) ;
290288
291289 describe ( 'getCurrentUser()' , ( ) => {
292- let getCurrentUser : ( r : KibanaRequest ) => Promise < AuthenticatedUser | null > ;
290+ let getCurrentUser : ( r : KibanaRequest ) => AuthenticatedUser | null ;
293291 beforeEach ( async ( ) => {
294292 getCurrentUser = ( await setupAuthentication ( mockSetupAuthenticationParams ) ) . getCurrentUser ;
295293 } ) ;
296294
297- it ( 'returns `null` if Security is disabled' , async ( ) => {
295+ it ( 'returns `null` if Security is disabled' , ( ) => {
298296 mockSetupAuthenticationParams . license . isEnabled . mockReturnValue ( false ) ;
299297
300- await expect ( getCurrentUser ( httpServerMock . createKibanaRequest ( ) ) ) . resolves . toBe ( null ) ;
298+ expect ( getCurrentUser ( httpServerMock . createKibanaRequest ( ) ) ) . toBe ( null ) ;
301299 } ) ;
302300
303- it ( 'fails if `authenticate` call fails' , async ( ) => {
304- const failureReason = new Error ( 'Something went wrong' ) ;
305- mockScopedClusterClient . callAsCurrentUser . mockRejectedValue ( failureReason ) ;
301+ it ( 'returns user from the auth state.' , ( ) => {
302+ const mockUser = mockAuthenticatedUser ( ) ;
306303
307- await expect ( getCurrentUser ( httpServerMock . createKibanaRequest ( ) ) ) . rejects . toBe (
308- failureReason
309- ) ;
304+ const mockAuthGet = mockSetupAuthenticationParams . http . auth . get as jest . Mock ;
305+ mockAuthGet . mockReturnValue ( { state : mockUser } ) ;
306+
307+ const mockRequest = httpServerMock . createKibanaRequest ( ) ;
308+ expect ( getCurrentUser ( mockRequest ) ) . toBe ( mockUser ) ;
309+ expect ( mockAuthGet ) . toHaveBeenCalledTimes ( 1 ) ;
310+ expect ( mockAuthGet ) . toHaveBeenCalledWith ( mockRequest ) ;
310311 } ) ;
311312
312- it ( 'returns result of `authenticate` call .' , async ( ) => {
313- const mockUser = mockAuthenticatedUser ( ) ;
314- mockScopedClusterClient . callAsCurrentUser . mockResolvedValue ( mockUser ) ;
313+ it ( 'returns null if auth state is not available .' , ( ) => {
314+ const mockAuthGet = mockSetupAuthenticationParams . http . auth . get as jest . Mock ;
315+ mockAuthGet . mockReturnValue ( { } ) ;
315316
316- await expect ( getCurrentUser ( httpServerMock . createKibanaRequest ( ) ) ) . resolves . toBe ( mockUser ) ;
317+ const mockRequest = httpServerMock . createKibanaRequest ( ) ;
318+ expect ( getCurrentUser ( mockRequest ) ) . toBeNull ( ) ;
319+ expect ( mockAuthGet ) . toHaveBeenCalledTimes ( 1 ) ;
320+ expect ( mockAuthGet ) . toHaveBeenCalledWith ( mockRequest ) ;
317321 } ) ;
318322 } ) ;
319323
320324 describe ( 'isAuthenticated()' , ( ) => {
321- let isAuthenticated : ( r : KibanaRequest ) => Promise < boolean > ;
325+ let isAuthenticated : ( r : KibanaRequest ) => boolean ;
322326 beforeEach ( async ( ) => {
323327 isAuthenticated = ( await setupAuthentication ( mockSetupAuthenticationParams ) ) . isAuthenticated ;
324328 } ) ;
325329
326- it ( 'returns `true` if Security is disabled' , async ( ) => {
327- mockSetupAuthenticationParams . license . isEnabled . mockReturnValue ( false ) ;
328-
329- await expect ( isAuthenticated ( httpServerMock . createKibanaRequest ( ) ) ) . resolves . toBe ( true ) ;
330- } ) ;
330+ it ( 'returns `true` if request is authenticated' , ( ) => {
331+ const mockIsAuthenticated = mockSetupAuthenticationParams . http . auth
332+ . isAuthenticated as jest . Mock ;
333+ mockIsAuthenticated . mockReturnValue ( true ) ;
331334
332- it ( 'returns `true` if `authenticate` succeeds.' , async ( ) => {
333- const mockUser = mockAuthenticatedUser ( ) ;
334- mockScopedClusterClient . callAsCurrentUser . mockResolvedValue ( mockUser ) ;
335-
336- await expect ( isAuthenticated ( httpServerMock . createKibanaRequest ( ) ) ) . resolves . toBe ( true ) ;
337- } ) ;
338-
339- it ( 'returns `false` if `authenticate` fails with 401.' , async ( ) => {
340- const failureReason = ElasticsearchErrorHelpers . decorateNotAuthorizedError ( new Error ( ) ) ;
341- mockScopedClusterClient . callAsCurrentUser . mockRejectedValue ( failureReason ) ;
342-
343- await expect ( isAuthenticated ( httpServerMock . createKibanaRequest ( ) ) ) . resolves . toBe ( false ) ;
335+ const mockRequest = httpServerMock . createKibanaRequest ( ) ;
336+ expect ( isAuthenticated ( mockRequest ) ) . toBe ( true ) ;
337+ expect ( mockIsAuthenticated ) . toHaveBeenCalledTimes ( 1 ) ;
338+ expect ( mockIsAuthenticated ) . toHaveBeenCalledWith ( mockRequest ) ;
344339 } ) ;
345340
346- it ( 'fails if `authenticate` call fails with unknown reason' , async ( ) => {
347- const failureReason = new errors . BadRequest ( ) ;
348- mockScopedClusterClient . callAsCurrentUser . mockRejectedValue ( failureReason ) ;
341+ it ( 'returns `false` if request is not authenticated' , ( ) => {
342+ const mockIsAuthenticated = mockSetupAuthenticationParams . http . auth
343+ . isAuthenticated as jest . Mock ;
344+ mockIsAuthenticated . mockReturnValue ( false ) ;
349345
350- await expect ( isAuthenticated ( httpServerMock . createKibanaRequest ( ) ) ) . rejects . toBe (
351- failureReason
352- ) ;
346+ const mockRequest = httpServerMock . createKibanaRequest ( ) ;
347+ expect ( isAuthenticated ( mockRequest ) ) . toBe ( false ) ;
348+ expect ( mockIsAuthenticated ) . toHaveBeenCalledTimes ( 1 ) ;
349+ expect ( mockIsAuthenticated ) . toHaveBeenCalledWith ( mockRequest ) ;
353350 } ) ;
354351 } ) ;
355352
0 commit comments