|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Tests for GET /api/v1/audit-logs/[id] — verifies the lookup is constrained |
| 5 | + * by the organization scope and 404s for rows outside it. |
| 6 | + */ |
| 7 | +import { createMockRequest, dbChainMock, dbChainMockFns } from '@sim/testing' |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const { |
| 11 | + mockCheckRateLimit, |
| 12 | + mockValidateEnterpriseAuditAccess, |
| 13 | + mockBuildOrgScopeCondition, |
| 14 | + mockGetOrgWorkspaceIds, |
| 15 | +} = vi.hoisted(() => ({ |
| 16 | + mockCheckRateLimit: vi.fn(), |
| 17 | + mockValidateEnterpriseAuditAccess: vi.fn(), |
| 18 | + mockBuildOrgScopeCondition: vi.fn(), |
| 19 | + mockGetOrgWorkspaceIds: vi.fn(), |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@sim/db', () => dbChainMock) |
| 23 | + |
| 24 | +vi.mock('@/app/api/v1/middleware', () => ({ |
| 25 | + checkRateLimit: mockCheckRateLimit, |
| 26 | + createRateLimitResponse: vi.fn(), |
| 27 | +})) |
| 28 | + |
| 29 | +vi.mock('@/app/api/v1/audit-logs/auth', () => ({ |
| 30 | + validateEnterpriseAuditAccess: mockValidateEnterpriseAuditAccess, |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('@/app/api/v1/audit-logs/query', () => ({ |
| 34 | + buildOrgScopeCondition: mockBuildOrgScopeCondition, |
| 35 | + getOrgWorkspaceIds: mockGetOrgWorkspaceIds, |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('@/app/api/v1/logs/meta', () => ({ |
| 39 | + getUserLimits: vi.fn().mockResolvedValue({}), |
| 40 | + createApiResponse: vi.fn((body: unknown) => ({ body, headers: {} })), |
| 41 | +})) |
| 42 | + |
| 43 | +import { GET } from '@/app/api/v1/audit-logs/[id]/route' |
| 44 | + |
| 45 | +const ORG_ID = 'org-1' |
| 46 | +const MEMBER_IDS = ['admin-1', 'member-1'] |
| 47 | +const ORG_WORKSPACE_IDS = ['ws-org-1'] |
| 48 | +const SCOPE_SENTINEL = { type: 'org-scope-sentinel' } |
| 49 | + |
| 50 | +const AUDIT_ROW = { |
| 51 | + id: 'log-1', |
| 52 | + workspaceId: 'ws-org-1', |
| 53 | + actorId: 'member-1', |
| 54 | + actorName: 'Member', |
| 55 | + actorEmail: 'member@example.com', |
| 56 | + action: 'workflow.created', |
| 57 | + resourceType: 'workflow', |
| 58 | + resourceId: 'wf-1', |
| 59 | + resourceName: 'My Workflow', |
| 60 | + description: 'Created workflow', |
| 61 | + metadata: {}, |
| 62 | + ipAddress: '127.0.0.1', |
| 63 | + userAgent: 'test', |
| 64 | + createdAt: new Date('2026-01-01T00:00:00Z'), |
| 65 | +} |
| 66 | + |
| 67 | +function callRoute(id: string) { |
| 68 | + const request = createMockRequest( |
| 69 | + 'GET', |
| 70 | + undefined, |
| 71 | + {}, |
| 72 | + `http://localhost:3000/api/v1/audit-logs/${id}` |
| 73 | + ) |
| 74 | + return GET(request, { params: Promise.resolve({ id }) }) |
| 75 | +} |
| 76 | + |
| 77 | +describe('GET /api/v1/audit-logs/[id]', () => { |
| 78 | + beforeEach(() => { |
| 79 | + vi.clearAllMocks() |
| 80 | + mockCheckRateLimit.mockResolvedValue({ allowed: true, userId: 'admin-1' }) |
| 81 | + mockValidateEnterpriseAuditAccess.mockResolvedValue({ |
| 82 | + success: true, |
| 83 | + context: { organizationId: ORG_ID, orgMemberIds: MEMBER_IDS }, |
| 84 | + }) |
| 85 | + mockGetOrgWorkspaceIds.mockResolvedValue(ORG_WORKSPACE_IDS) |
| 86 | + mockBuildOrgScopeCondition.mockReturnValue(SCOPE_SENTINEL) |
| 87 | + }) |
| 88 | + |
| 89 | + it('constrains the lookup with the org scope condition (includeDeparted)', async () => { |
| 90 | + dbChainMockFns.limit.mockResolvedValueOnce([AUDIT_ROW]) |
| 91 | + |
| 92 | + const response = await callRoute('log-1') |
| 93 | + |
| 94 | + expect(response.status).toBe(200) |
| 95 | + expect(mockBuildOrgScopeCondition).toHaveBeenCalledWith({ |
| 96 | + organizationId: ORG_ID, |
| 97 | + orgWorkspaceIds: ORG_WORKSPACE_IDS, |
| 98 | + orgMemberIds: MEMBER_IDS, |
| 99 | + includeDeparted: true, |
| 100 | + }) |
| 101 | + expect(dbChainMockFns.where).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ |
| 103 | + type: 'and', |
| 104 | + conditions: expect.arrayContaining([SCOPE_SENTINEL]), |
| 105 | + }) |
| 106 | + ) |
| 107 | + }) |
| 108 | + |
| 109 | + it('returns 404 when the row is outside the organization scope', async () => { |
| 110 | + dbChainMockFns.limit.mockResolvedValueOnce([]) |
| 111 | + |
| 112 | + const response = await callRoute('log-outside-org') |
| 113 | + |
| 114 | + expect(response.status).toBe(404) |
| 115 | + const body = await response.json() |
| 116 | + expect(body.error).toBe('Audit log not found') |
| 117 | + }) |
| 118 | + |
| 119 | + it('excludes ipAddress and userAgent from the response', async () => { |
| 120 | + dbChainMockFns.limit.mockResolvedValueOnce([AUDIT_ROW]) |
| 121 | + |
| 122 | + const response = await callRoute('log-1') |
| 123 | + const body = await response.json() |
| 124 | + |
| 125 | + expect(body.data.id).toBe('log-1') |
| 126 | + expect(body.data.ipAddress).toBeUndefined() |
| 127 | + expect(body.data.userAgent).toBeUndefined() |
| 128 | + }) |
| 129 | +}) |
0 commit comments