import Boom from '@hapi/boom' ; import { LegacyOpenSearchErrorHelpers } from './errors' ; describe ( 'OpenSearchErrorHelpers' , ( ) => { describe ( 'NotAuthorized error' , ( ) => { describe ( 'decorateNotAuthorizedError' , ( ) => { it ( 'returns original object' , ( ) => { const error = new Error ( ) ; expect ( LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( error ) ) . toBe ( error ) ; } ) ; it ( 'makes the error identifiable as a NotAuthorized error' , ( ) => { const error = new Error ( ) ; expect ( LegacyOpenSearchErrorHelpers . isNotAuthorizedError ( error ) ) . toBe ( false ) ; LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( error ) ; expect ( LegacyOpenSearchErrorHelpers . isNotAuthorizedError ( error ) ) . toBe ( true ) ; } ) ; it ( 'adds boom properties' , ( ) => { const error = LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( new Error ( ) ) ; expect ( typeof error . output ) . toBe ( 'object' ) ; expect ( error . output . statusCode ) . toBe ( 401 ) ; } ) ; it ( 'preserves boom properties of input' , ( ) => { const error = Boom . notFound ( ) ; LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( error ) ; expect ( error . output . statusCode ) . toBe ( 404 ) ; } ) ; describe ( 'error.output' , ( ) => { it ( 'defaults to message of error' , ( ) => { const error = LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( new Error ( 'foobar' ) ) ; expect ( error . output . payload ) . toHaveProperty ( 'message' , 'foobar' ) ; } ) ; it ( 'prefixes message with passed reason' , ( ) => { const error = LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( new Error ( 'foobar' ) , 'biz' ) ; expect ( error . output . payload ) . toHaveProperty ( 'message' , 'biz: foobar' ) ; } ) ; it ( 'sets statusCode to 401' , ( ) => { const error = LegacyOpenSearchErrorHelpers . decorateNotAuthorizedError ( new Error ( 'foo' ) ) ; expect ( error . output ) . toHaveProperty ( 'statusCode' , 401 ) ; } ) ; } ) ; } ) ; } ) ; } ) ;