@@ -23,41 +23,44 @@ describe('test authUtil model', () => {
2323 afterEach ( ( ) => {
2424 jest . clearAllMocks ( ) ;
2525 } ) ;
26-
27- describe ( 'test readVcapServices function' , ( ) => {
26+
27+ describe ( 'test readVcapServices function' , ( ) => {
28+ let originalEnv ;
29+ let mockLogger ;
30+ beforeEach ( ( ) => {
31+ originalEnv = cds . env ;
32+ mockLogger = { error : jest . fn ( ) , info : jest . fn ( ) } ;
33+ cds . log = jest . fn ( ( ) => mockLogger ) ;
34+ } ) ;
2835 afterEach ( ( ) => {
36+ cds . env = originalEnv ;
2937 jest . clearAllMocks ( ) ;
3038 } ) ;
3139
3240 it ( 'should return service credentials when found' , async ( ) => {
33-
34- const mockCredentials = { clientid : 'test-client' , clientsecret : 'test-secret' } ;
35- xsenv . serviceCredentials . mockReturnValue ( mockCredentials ) ;
36-
37- const mockReq = { success : jest . fn ( ) } ;
38- const result = await readVcapServices ( mockReq ) ;
39-
40- expect ( result ) . toEqual ( mockCredentials ) ;
41- expect ( xsenv . serviceCredentials ) . toHaveBeenCalledWith ( { tag : 'Print' } ) ;
41+ cds . env = { requires : { print : { credentials : { tag : 'Print' } } } } ;
42+ const req = { error : jest . fn ( ) } ;
43+ const result = await readVcapServices ( req ) ;
44+ expect ( result ) . toEqual ( { tag : 'Print' } ) ;
45+ expect ( req . error ) . not . toHaveBeenCalled ( ) ;
4246 } ) ;
4347
44- it ( 'should log an error and return undefined when service credentials are not found' , async ( ) => {
45- jest . resetModules ( ) ;
46- xsenv . serviceCredentials . mockImplementation ( ( ) => {
47- throw new Error ( 'Service not found' ) ;
48- } ) ;
49-
50- const mockReq = { error : jest . fn ( ) } ;
48+ it ( 'should log an error and return undefined when service credentials are not found in production' , async ( ) => {
49+ cds . env = { requires : { } } ;
5150 process . env . NODE_ENV = 'production' ;
52- let result = await readVcapServices ( mockReq ) ;
51+ const req = { error : jest . fn ( ) } ;
52+ const result = await readVcapServices ( req ) ;
5353 expect ( result ) . toBeUndefined ( ) ;
54- expect ( mockLogger . error ) . toHaveBeenCalledWith ( 'Print service not found' ) ;
55-
56- process . env . NODE_ENV = 'development' ;
57- result = await readVcapServices ( mockReq ) ;
58- expect ( result ) . toBeUndefined ( ) ;
59- expect ( mockReq . error ) . toHaveBeenCalled ( ) ;
54+ expect ( req . error ) . toHaveBeenCalledWith ( 500 , 'Print service not found' ) ;
6055 } ) ;
56+ it ( 'should log error and return undefined in non-production when credentials missing' , async ( ) => {
57+ cds . env = { requires : { } } ;
58+ process . env . NODE_ENV = 'development' ;
59+ const req = { error : jest . fn ( ) } ;
60+ const result = await readVcapServices ( req ) ;
61+ expect ( result ) . toBeUndefined ( ) ;
62+ expect ( req . error ) . not . toHaveBeenCalled ( ) ;
63+ } ) ;
6164 } ) ;
6265
6366 describe ( 'test getJwt function' , ( ) => {
0 commit comments