11const cds = require ( '@sap/cds' )
22
3- const { POST , GET } = cds . test ( ) . in ( __dirname )
3+ const { axios, POST , GET } = cds . test ( ) . in ( __dirname )
4+
5+ // do not throw for 4xx responses
6+ axios . defaults . validateStatus = ( ) => true
47
58cds . env . requires [ 'audit-log' ] = {
69 kind : 'audit-log-to-console' ,
@@ -22,6 +25,7 @@ describe('AuditLogService API', () => {
2225 }
2326
2427 const ALICE = { username : 'alice' , password : 'password' }
28+ const BOB = { username : 'bob' , password : 'password' }
2529
2630 beforeAll ( ( ) => {
2731 __log = global . console . log
@@ -135,4 +139,43 @@ describe('AuditLogService API', () => {
135139 } )
136140 } )
137141 } )
142+
143+ describe ( 'custom log 403' , ( ) => {
144+ test ( 'early reject' , async ( ) => {
145+ const response = await GET ( '/api/Books' , { auth : BOB } )
146+ expect ( response ) . toMatchObject ( { status : 403 } )
147+ expect ( _logs . length ) . toBe ( 1 )
148+ expect ( _logs ) . toContainMatchObject ( { user : 'bob' , ip : '::1' } )
149+ } )
150+
151+ test ( 'late reject' , async ( ) => {
152+ const response = await GET ( '/api/Books' , { auth : ALICE } )
153+ expect ( response ) . toMatchObject ( { status : 403 } )
154+ expect ( _logs . length ) . toBe ( 1 )
155+ expect ( _logs ) . toContainMatchObject ( { user : 'alice' , ip : '::1' } )
156+ } )
157+
158+ test ( 'early reject in batch' , async ( ) => {
159+ const response = await POST (
160+ '/api/$batch' ,
161+ { requests : [ { method : 'GET' , url : '/Books' , id : 'r1' } ] } ,
162+ { auth : BOB }
163+ )
164+ expect ( response ) . toMatchObject ( { status : 403 } )
165+ expect ( _logs . length ) . toBeGreaterThan ( 0 ) //> coding in ./srv/server.js results in 2 logs on @sap/cds^7
166+ expect ( _logs ) . toContainMatchObject ( { user : 'bob' , ip : '::1' } )
167+ } )
168+
169+ test ( 'late reject in batch' , async ( ) => {
170+ const response = await POST (
171+ '/api/$batch' ,
172+ { requests : [ { method : 'GET' , url : '/Books' , id : 'r1' } ] } ,
173+ { auth : ALICE }
174+ )
175+ expect ( response ) . toMatchObject ( { status : 200 } )
176+ expect ( response . data . responses [ 0 ] ) . toMatchObject ( { status : 403 } )
177+ expect ( _logs . length ) . toBe ( 1 )
178+ expect ( _logs ) . toContainMatchObject ( { user : 'alice' , ip : '::1' } )
179+ } )
180+ } )
138181} )
0 commit comments