|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { logger } from '@iblai/iblai-js/playwright'; |
| 3 | +import { waitForAppShell, gotoTenantPage } from '../utils/navigation'; |
| 4 | + |
| 5 | +test.describe('Journey 33: Analytics Audit', () => { |
| 6 | + test.setTimeout(200_000); |
| 7 | + |
| 8 | + test.beforeEach(async ({ page }) => { |
| 9 | + await gotoTenantPage(page, 'home', { timeout: 120_000 }); |
| 10 | + await waitForAppShell(page); |
| 11 | + |
| 12 | + // Admin gate: check if AI Analytics link is visible |
| 13 | + const analyticsLink = page.getByRole('link', { name: /ai analytics|analytics/i }); |
| 14 | + const isAdmin = await analyticsLink.isVisible({ timeout: 120_000 }).catch(() => false); |
| 15 | + if (!isAdmin) { |
| 16 | + test.skip(true, 'Analytics requires admin access — AI Analytics link not visible'); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + logger.info('Navigating to Analytics'); |
| 21 | + await analyticsLink.click(); |
| 22 | + await page.waitForURL((url) => url.href.includes('/analytics'), { timeout: 30_000 }); |
| 23 | + }); |
| 24 | + |
| 25 | + /** |
| 26 | + * Clicks the Audit tab and returns false (skipping the test) when the tab is |
| 27 | + * not visible. The audit log view itself renders one of: the filters + table, |
| 28 | + * the empty state, or a permission notice for non-authorized users. |
| 29 | + */ |
| 30 | + async function openAuditTab(page: import('@playwright/test').Page): Promise<boolean> { |
| 31 | + const auditTab = page.getByRole('tab', { name: 'Audit', exact: true }); |
| 32 | + const hasAuditTab = await auditTab.isVisible({ timeout: 120_000 }).catch(() => false); |
| 33 | + if (!hasAuditTab) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + await auditTab.click(); |
| 38 | + await expect(auditTab).toHaveAttribute('data-state', 'active', { timeout: 30_000 }); |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + test('CP-1: audit tab routes to the audit log view', async ({ page }) => { |
| 43 | + logger.info('CP-1: navigating to Audit tab'); |
| 44 | + const opened = await openAuditTab(page); |
| 45 | + if (!opened) { |
| 46 | + test.skip(true, 'Audit analytics tab not visible'); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + await page.waitForURL((url) => url.pathname.endsWith('/analytics/audit'), { |
| 51 | + timeout: 30_000, |
| 52 | + }); |
| 53 | + |
| 54 | + logger.info('CP-1: checking the audit log view rendered'); |
| 55 | + // After loading completes, one of these will appear: |
| 56 | + // - the USER/ACTION/TIME table (has results) |
| 57 | + // - "No audit log entries found for this tenant." (empty) |
| 58 | + // - "You do not have permission to view audit logs." (403) |
| 59 | + const loadedIndicator = page |
| 60 | + .getByRole('columnheader', { name: 'USER', exact: true }) |
| 61 | + .or(page.getByText('No audit log entries found for this tenant.')) |
| 62 | + .or(page.getByText('You do not have permission to view audit logs.')); |
| 63 | + |
| 64 | + await expect(loadedIndicator.first()).toBeVisible({ timeout: 120_000 }); |
| 65 | + logger.info('CP-1: audit log view rendered — table, empty state, or permission notice'); |
| 66 | + }); |
| 67 | + |
| 68 | + test('CP-2: audit log filters are visible', async ({ page }) => { |
| 69 | + logger.info('CP-2: navigating to Audit tab'); |
| 70 | + const opened = await openAuditTab(page); |
| 71 | + if (!opened) { |
| 72 | + test.skip(true, 'Audit analytics tab not visible'); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const permissionNotice = page.getByText('You do not have permission to view audit logs.'); |
| 77 | + const hasNoPermission = await permissionNotice.isVisible({ timeout: 5_000 }).catch(() => false); |
| 78 | + if (hasNoPermission) { |
| 79 | + test.skip(true, 'User lacks permission to view audit logs — filters not rendered'); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + logger.info('CP-2: checking user search and action filters'); |
| 84 | + const userSearch = page.getByRole('button', { name: 'Search for User' }); |
| 85 | + const actionFilter = page.getByText('All Actions'); |
| 86 | + |
| 87 | + const hasUserSearch = await userSearch.isVisible({ timeout: 120_000 }).catch(() => false); |
| 88 | + const hasActionFilter = await actionFilter |
| 89 | + .first() |
| 90 | + .isVisible({ timeout: 120_000 }) |
| 91 | + .catch(() => false); |
| 92 | + |
| 93 | + logger.info(`CP-2: userSearch=${hasUserSearch} actionFilter=${hasActionFilter}`); |
| 94 | + expect(hasUserSearch, 'User search filter should be visible').toBe(true); |
| 95 | + expect(hasActionFilter, 'Action filter should be visible').toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + test('CP-3: audit log table or empty state is visible', async ({ page }) => { |
| 99 | + logger.info('CP-3: navigating to Audit tab'); |
| 100 | + const opened = await openAuditTab(page); |
| 101 | + if (!opened) { |
| 102 | + test.skip(true, 'Audit analytics tab not visible'); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const permissionNotice = page.getByText('You do not have permission to view audit logs.'); |
| 107 | + const hasNoPermission = await permissionNotice.isVisible({ timeout: 5_000 }).catch(() => false); |
| 108 | + if (hasNoPermission) { |
| 109 | + test.skip(true, 'User lacks permission to view audit logs — table not rendered'); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + logger.info('CP-3: checking for audit entries table or empty state'); |
| 114 | + const tableHeader = page.getByRole('columnheader', { name: 'USER', exact: true }); |
| 115 | + const emptyState = page.getByText('No audit log entries found for this tenant.'); |
| 116 | + |
| 117 | + await expect(tableHeader.or(emptyState).first()).toBeVisible({ timeout: 120_000 }); |
| 118 | + |
| 119 | + const hasTable = await tableHeader.isVisible({ timeout: 1_000 }).catch(() => false); |
| 120 | + if (hasTable) { |
| 121 | + logger.info('CP-3: audit table rendered — verifying ACTION and TIME headers'); |
| 122 | + await expect(page.getByRole('columnheader', { name: 'ACTION', exact: true })).toBeVisible({ |
| 123 | + timeout: 30_000, |
| 124 | + }); |
| 125 | + await expect(page.getByRole('columnheader', { name: 'TIME', exact: true })).toBeVisible({ |
| 126 | + timeout: 30_000, |
| 127 | + }); |
| 128 | + } else { |
| 129 | + logger.info('CP-3: empty state rendered — no audit log entries for this tenant'); |
| 130 | + } |
| 131 | + }); |
| 132 | +}); |
0 commit comments