Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions test/api/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ cds.env.requires['audit-log'] = {

const wait = require('node:timers/promises').setTimeout

// Matcher for localhost IPs (handles both IPv6 and IPv4-mapped IPv6)
const localhostIP = expect.stringMatching(/^(::1|::ffff:127\.0\.0\.1)$/)

describe('AuditLogService API', () => {
let __log, _logs
const _log = (...args) => {
Expand Down Expand Up @@ -145,14 +148,14 @@ describe('AuditLogService API', () => {
const response = await GET('/api/Books', { auth: BOB })
expect(response).toMatchObject({ status: 403 })
expect(_logs.length).toBe(1)
expect(_logs).toContainMatchObject({ user: 'bob', ip: '::1' })
expect(_logs).toContainMatchObject({ user: 'bob', ip: localhostIP })
})

test('late reject', async () => {
const response = await GET('/api/Books', { auth: ALICE })
expect(response).toMatchObject({ status: 403 })
expect(_logs.length).toBe(1)
expect(_logs).toContainMatchObject({ user: 'alice', ip: '::1' })
expect(_logs).toContainMatchObject({ user: 'alice', ip: localhostIP })
})

test('early reject in batch', async () => {
Expand All @@ -163,7 +166,7 @@ describe('AuditLogService API', () => {
)
expect(response).toMatchObject({ status: 403 })
expect(_logs.length).toBeGreaterThan(0) //> coding in ./srv/server.js results in 2 logs on @sap/cds^7
expect(_logs).toContainMatchObject({ user: 'bob', ip: '::1' })
expect(_logs).toContainMatchObject({ user: 'bob', ip: localhostIP })
})

test('late reject in batch', async () => {
Expand All @@ -175,7 +178,7 @@ describe('AuditLogService API', () => {
expect(response).toMatchObject({ status: 200 })
expect(response.data.responses[0]).toMatchObject({ status: 403 })
expect(_logs.length).toBe(1)
expect(_logs).toContainMatchObject({ user: 'alice', ip: '::1' })
expect(_logs).toContainMatchObject({ user: 'alice', ip: localhostIP })
})
})
})