| import { describe, expect, test } from 'vitest' |
| import { getAuditLogEvents, getCategorizedAuditLogEvents } from '../lib/index' |
| import type { AuditLogEventT } from '../types' |
|
|
| describe('Audit log fields functionality', () => { |
| describe('getAuditLogEvents', () => { |
| test('should return events with fields when available', () => { |
| |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
|
|
| expect(events).toBeDefined() |
| expect(Array.isArray(events)).toBe(true) |
| expect(events.length).toBeGreaterThan(0) |
|
|
| |
| const eventsWithFields = events.filter((event) => event.fields && event.fields.length > 0) |
| expect(eventsWithFields.length).toBeGreaterThan(0) |
|
|
| |
| const eventWithFields = eventsWithFields[0] |
| expect(eventWithFields).toHaveProperty('action') |
| expect(eventWithFields).toHaveProperty('description') |
| expect(eventWithFields).toHaveProperty('fields') |
| expect(Array.isArray(eventWithFields.fields)).toBe(true) |
| expect(eventWithFields.fields!.length).toBeGreaterThan(0) |
| }) |
|
|
| test('should have fields as array of strings', () => { |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
| const eventWithFields = events.find((event) => event.fields && event.fields.length > 0) |
|
|
| if (eventWithFields) { |
| expect(Array.isArray(eventWithFields.fields)).toBe(true) |
| for (const field of eventWithFields.fields!) { |
| expect(typeof field).toBe('string') |
| expect(field.length).toBeGreaterThan(0) |
| } |
| } |
| }) |
|
|
| test('should handle events without fields gracefully', () => { |
| |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
|
|
| for (const event of events) { |
| expect(event).toHaveProperty('action') |
| expect(event).toHaveProperty('description') |
| |
| if (event.fields) { |
| expect(Array.isArray(event.fields)).toBe(true) |
| } |
| } |
| }) |
|
|
| test('should include common audit log fields', () => { |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
| const eventWithFields = events.find((event) => event.fields && event.fields.length > 0) |
|
|
| if (eventWithFields) { |
| const fields = eventWithFields.fields! |
|
|
| |
| const commonFields = ['@timestamp', 'action', 'actor'] |
| const hasCommonFields = commonFields.some((field) => fields.includes(field)) |
| expect(hasCommonFields).toBe(true) |
| } |
| }) |
| }) |
|
|
| describe('getCategorizedAuditLogEvents', () => { |
| test('should return categorized events with fields', () => { |
| const categorizedEvents = getCategorizedAuditLogEvents( |
| 'organization', |
| 'enterprise-cloud@latest', |
| ) |
|
|
| expect(categorizedEvents).toBeDefined() |
| expect(typeof categorizedEvents).toBe('object') |
|
|
| |
| const categories = Object.keys(categorizedEvents) |
| expect(categories.length).toBeGreaterThan(0) |
|
|
| |
| for (const category of categories) { |
| const events = categorizedEvents[category] |
| expect(Array.isArray(events)).toBe(true) |
|
|
| for (const event of events as AuditLogEventT[]) { |
| expect(event).toHaveProperty('action') |
| expect(event).toHaveProperty('description') |
| |
| if (event.fields) { |
| expect(Array.isArray(event.fields)).toBe(true) |
| } |
| } |
| } |
| }) |
|
|
| test('should preserve fields data through categorization', () => { |
| const categorizedEvents = getCategorizedAuditLogEvents( |
| 'organization', |
| 'enterprise-cloud@latest', |
| ) |
|
|
| |
| let eventWithFields: AuditLogEventT | null = null |
| for (const category of Object.keys(categorizedEvents)) { |
| const event = categorizedEvents[category].find( |
| (e: AuditLogEventT) => e.fields && e.fields.length > 0, |
| ) |
| if (event) { |
| eventWithFields = event |
| break |
| } |
| } |
|
|
| if (eventWithFields) { |
| expect(eventWithFields.fields).toBeDefined() |
| expect(Array.isArray(eventWithFields.fields)).toBe(true) |
| expect(eventWithFields.fields!.length).toBeGreaterThan(0) |
| } |
| }) |
| }) |
|
|
| describe('Fields data integrity', () => { |
| test('should not have duplicate fields in same event', () => { |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
|
|
| for (const event of events) { |
| if (event.fields) { |
| const uniqueFields = new Set(event.fields) |
| expect(uniqueFields.size).toBe(event.fields.length) |
| } |
| } |
| }) |
|
|
| test('should have reasonable field names', () => { |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
| const eventWithFields = events.find((event) => event.fields && event.fields.length > 0) |
|
|
| if (eventWithFields) { |
| for (const field of eventWithFields.fields!) { |
| |
| expect(field).toBeTruthy() |
| expect(typeof field).toBe('string') |
| expect(field.length).toBeGreaterThan(0) |
| expect(field.length).toBeLessThan(100) |
|
|
| |
| expect(field).not.toMatch(/[<>'"&]/) |
| } |
| } |
| }) |
|
|
| test('should handle different page types consistently', () => { |
| const pageTypes = ['organization', 'enterprise', 'user'] |
|
|
| for (const pageType of pageTypes) { |
| try { |
| const events = getAuditLogEvents(pageType, 'enterprise-cloud@latest') |
|
|
| for (const event of events) { |
| expect(event).toHaveProperty('action') |
| expect(event).toHaveProperty('description') |
|
|
| if (event.fields) { |
| expect(Array.isArray(event.fields)).toBe(true) |
| for (const field of event.fields) { |
| expect(typeof field).toBe('string') |
| } |
| } |
| } |
| } catch (error) { |
| |
| console.log(`Skipping ${pageType} page type due to: ${error}`) |
| } |
| } |
| }) |
| }) |
|
|
| describe('Type safety', () => { |
| test('should have proper TypeScript types', () => { |
| const events = getAuditLogEvents('organization', 'enterprise-cloud@latest') |
| const event = events[0] |
|
|
| |
| const action: string = event.action |
| const description: string = event.description |
| const fields: string[] | undefined = event.fields |
|
|
| expect(typeof action).toBe('string') |
| expect(typeof description).toBe('string') |
|
|
| if (fields) { |
| expect(Array.isArray(fields)).toBe(true) |
| for (const field of fields) { |
| expect(typeof field).toBe('string') |
| } |
| } |
| }) |
| }) |
| }) |
|
|