@@ -54,16 +54,13 @@ describe('AuditService unit tests:', () => {
5454 test ( 'should log an audit entry with request context' , async ( ) => {
5555 const userId = '507f1f77bcf86cd799439011' ;
5656 const orgId = '507f1f77bcf86cd799439012' ;
57- const req = {
58- user : { _id : userId } ,
59- organization : { _id : orgId } ,
60- ip : '127.0.0.1' ,
61- headers : { 'user-agent' : 'TestAgent/1.0' } ,
62- } ;
6357
6458 await AuditService . log ( {
6559 action : 'auth.login' ,
66- req,
60+ userId,
61+ organizationId : orgId ,
62+ ip : '127.0.0.1' ,
63+ userAgent : 'TestAgent/1.0' ,
6764 targetType : 'User' ,
6865 targetId : userId ,
6966 metadata : { foo : 'bar' } ,
@@ -115,47 +112,37 @@ describe('AuditService unit tests:', () => {
115112 expect ( mockList ) . toHaveBeenCalledWith ( { userId } , 1 , 10 ) ;
116113 } ) ;
117114
118- // GDPR config flag tests
119- test ( 'should capture IP when captureIp is true (default)' , async ( ) => {
120- mockConfig . audit = { enabled : true , ttlDays : 90 , captureIp : true , captureUserAgent : true } ;
121- const req = { ip : '10.0.0.1' , headers : { 'user-agent' : 'Bot/1.0' } } ;
122- await AuditService . log ( { action : 'test.ip' , req } ) ;
115+ // GDPR config flag tests — ip/userAgent are now extracted by the caller
116+ test ( 'should store ip when provided' , async ( ) => {
117+ await AuditService . log ( { action : 'test.ip' , ip : '10.0.0.1' , userAgent : 'Bot/1.0' } ) ;
123118 expect ( mockCreate ) . toHaveBeenCalledTimes ( 1 ) ;
124119 const arg = mockCreate . mock . calls [ 0 ] [ 0 ] ;
125120 expect ( arg . ip ) . toBe ( '10.0.0.1' ) ;
126121 } ) ;
127122
128- test ( 'should set IP to empty string when captureIp is false' , async ( ) => {
129- mockConfig . audit = { enabled : true , ttlDays : 90 , captureIp : false , captureUserAgent : true } ;
130- const req = { ip : '10.0.0.1' , headers : { 'user-agent' : 'Bot/1.0' } } ;
131- await AuditService . log ( { action : 'test.ip' , req } ) ;
123+ test ( 'should omit ip when undefined is passed' , async ( ) => {
124+ await AuditService . log ( { action : 'test.ip' , ip : undefined , userAgent : 'Bot/1.0' } ) ;
132125 expect ( mockCreate ) . toHaveBeenCalledTimes ( 1 ) ;
133126 const arg = mockCreate . mock . calls [ 0 ] [ 0 ] ;
134- expect ( arg . ip ) . toBe ( '' ) ;
127+ expect ( arg . ip ) . toBeUndefined ( ) ;
135128 } ) ;
136129
137- test ( 'should capture User-Agent when captureUserAgent is true (default)' , async ( ) => {
138- mockConfig . audit = { enabled : true , ttlDays : 90 , captureIp : true , captureUserAgent : true } ;
139- const req = { ip : '10.0.0.1' , headers : { 'user-agent' : 'Bot/1.0' } } ;
140- await AuditService . log ( { action : 'test.ua' , req } ) ;
130+ test ( 'should store userAgent when provided' , async ( ) => {
131+ await AuditService . log ( { action : 'test.ua' , ip : '10.0.0.1' , userAgent : 'Bot/1.0' } ) ;
141132 expect ( mockCreate ) . toHaveBeenCalledTimes ( 1 ) ;
142133 const arg = mockCreate . mock . calls [ 0 ] [ 0 ] ;
143134 expect ( arg . userAgent ) . toBe ( 'Bot/1.0' ) ;
144135 } ) ;
145136
146- test ( 'should set User-Agent to empty string when captureUserAgent is false' , async ( ) => {
147- mockConfig . audit = { enabled : true , ttlDays : 90 , captureIp : true , captureUserAgent : false } ;
148- const req = { ip : '10.0.0.1' , headers : { 'user-agent' : 'Bot/1.0' } } ;
149- await AuditService . log ( { action : 'test.ua' , req } ) ;
137+ test ( 'should omit userAgent when undefined is passed' , async ( ) => {
138+ await AuditService . log ( { action : 'test.ua' , ip : '10.0.0.1' , userAgent : undefined } ) ;
150139 expect ( mockCreate ) . toHaveBeenCalledTimes ( 1 ) ;
151140 const arg = mockCreate . mock . calls [ 0 ] [ 0 ] ;
152- expect ( arg . userAgent ) . toBe ( '' ) ;
141+ expect ( arg . userAgent ) . toBeUndefined ( ) ;
153142 } ) ;
154143
155- test ( 'should default to capturing IP and User-Agent when config keys are undefined' , async ( ) => {
156- mockConfig . audit = { enabled : true , ttlDays : 90 } ;
157- const req = { ip : '192.168.1.1' , headers : { 'user-agent' : 'DefaultBot/2.0' } } ;
158- await AuditService . log ( { action : 'test.defaults' , req } ) ;
144+ test ( 'should store ip and userAgent when provided' , async ( ) => {
145+ await AuditService . log ( { action : 'test.defaults' , ip : '192.168.1.1' , userAgent : 'DefaultBot/2.0' } ) ;
159146 expect ( mockCreate ) . toHaveBeenCalledTimes ( 1 ) ;
160147 const arg = mockCreate . mock . calls [ 0 ] [ 0 ] ;
161148 expect ( arg . ip ) . toBe ( '192.168.1.1' ) ;
0 commit comments