buster/packages/access-controls/tests/integration/get-permissioned-datasets.i...

250 lines
10 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import { getPermissionedDatasets } from '../../src/access-controls';
describe('getPermissionedDatasets Integration Tests', () => {
const testUserId = 'c2dd64cd-f7f3-4884-bc91-d46ae431901e';
const noAccessUserId = '70d05d4e-b2c1-40c5-be69-315e420fd0ab';
const limitedAccessUserId = '8e98a1fc-c4d5-401c-98d8-2cce60e11079';
describe('User Dataset Access - Full Access User', () => {
test('should return datasets for user including sales_person dataset', async () => {
// Get datasets with a reasonable page size to get all results
const result = await getPermissionedDatasets(testUserId, 0, 100);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBeGreaterThan(0);
// Check if sales_person dataset is in the results
const salesPersonDataset = result.find((dataset) => dataset.name === 'sales_person');
expect(salesPersonDataset).toBeDefined();
expect(salesPersonDataset?.name).toBe('sales_person');
// Verify dataset structure
if (salesPersonDataset) {
expect(salesPersonDataset).toHaveProperty('id');
expect(salesPersonDataset).toHaveProperty('name');
expect(salesPersonDataset).toHaveProperty('ymlFile');
expect(salesPersonDataset).toHaveProperty('createdAt');
expect(salesPersonDataset).toHaveProperty('updatedAt');
expect(salesPersonDataset).toHaveProperty('deletedAt');
expect(salesPersonDataset).toHaveProperty('dataSourceId');
expect(typeof salesPersonDataset.id).toBe('string');
expect(typeof salesPersonDataset.name).toBe('string');
expect(typeof salesPersonDataset.dataSourceId).toBe('string');
}
console.log(`Found ${result.length} datasets for user ${testUserId}`);
console.log(`Sales person dataset found: ${salesPersonDataset ? 'YES' : 'NO'}`);
});
test('should return datasets in alphabetical order', async () => {
const result = await getPermissionedDatasets(testUserId, 0, 100);
expect(result.length).toBeGreaterThan(0);
// Check if datasets are sorted alphabetically by name
for (let i = 1; i < result.length; i++) {
expect(result[i].name.localeCompare(result[i - 1].name)).toBeGreaterThanOrEqual(0);
}
// Sales person should be in the sorted position if it exists
const salesPersonIndex = result.findIndex((d) => d.name === 'sales_person');
if (salesPersonIndex >= 0) {
if (salesPersonIndex > 0) {
expect(
result[salesPersonIndex - 1].name.localeCompare('sales_person')
).toBeLessThanOrEqual(0);
}
if (salesPersonIndex < result.length - 1) {
expect(
'sales_person'.localeCompare(result[salesPersonIndex + 1].name)
).toBeLessThanOrEqual(0);
}
}
});
test('should handle pagination correctly', async () => {
// Test with different page sizes
const page1 = await getPermissionedDatasets(testUserId, 0, 10);
expect(page1.length).toBeLessThanOrEqual(10);
const page2 = await getPermissionedDatasets(testUserId, 0, 50);
expect(page2.length).toBeLessThanOrEqual(50);
// If we have more than 10 datasets, page2 should have more results than page1
if (page2.length > 10) {
expect(page2.length).toBeGreaterThan(page1.length);
}
// Test pagination offset
if (page1.length === 10) {
const nextPage = await getPermissionedDatasets(testUserId, 1, 10);
// Next page should either have results or be empty
expect(Array.isArray(nextPage)).toBe(true);
// If next page has results, they should be different from first page
if (nextPage.length > 0) {
const firstPageIds = new Set(page1.map((d) => d.id));
const nextPageIds = new Set(nextPage.map((d) => d.id));
const intersection = new Set(Array.from(firstPageIds).filter((x) => nextPageIds.has(x)));
expect(intersection.size).toBe(0); // No overlap between pages
}
}
});
test('should return expected number of datasets based on permissions', async () => {
// Get all datasets for the user
const allDatasets = await getPermissionedDatasets(testUserId, 0, 1000);
expect(Array.isArray(allDatasets)).toBe(true);
expect(allDatasets.length).toBeGreaterThan(0);
// All datasets should have valid structure
for (const dataset of allDatasets) {
expect(dataset).toHaveProperty('id');
expect(dataset).toHaveProperty('name');
expect(dataset).toHaveProperty('dataSourceId');
expect(typeof dataset.id).toBe('string');
expect(typeof dataset.name).toBe('string');
expect(typeof dataset.dataSourceId).toBe('string');
}
// Check for unique dataset IDs
const uniqueIds = new Set(allDatasets.map((d) => d.id));
expect(uniqueIds.size).toBe(allDatasets.length);
console.log(`Total datasets accessible by user: ${allDatasets.length}`);
});
});
describe('User Dataset Access - No Access User', () => {
test('should return empty array for user with no dataset access', async () => {
const result = await getPermissionedDatasets(noAccessUserId, 0, 100);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(0);
console.log(`User ${noAccessUserId} has access to ${result.length} datasets (expected: 0)`);
});
test('should handle pagination correctly for user with no access', async () => {
// Test different page configurations
const page1 = await getPermissionedDatasets(noAccessUserId, 0, 10);
const page2 = await getPermissionedDatasets(noAccessUserId, 1, 10);
const largePage = await getPermissionedDatasets(noAccessUserId, 0, 100);
expect(page1.length).toBe(0);
expect(page2.length).toBe(0);
expect(largePage.length).toBe(0);
});
});
describe('User Dataset Access - Limited Access User', () => {
test('should verify limited access user has access to sales_person dataset', async () => {
const result = await getPermissionedDatasets(limitedAccessUserId, 0, 100);
expect(Array.isArray(result)).toBe(true);
console.log(`User ${limitedAccessUserId} has access to ${result.length} datasets`);
// Skip further assertions if no datasets found (likely test data issue)
if (result.length === 0) {
console.warn(
'WARNING: Limited access user has no datasets. This may indicate a test data setup issue.'
);
return;
}
// Log all accessible dataset names for debugging
const datasetNames = result.map((d) => d.name).sort();
console.log(`Accessible datasets: ${datasetNames.join(', ')}`);
// Check for sales_person dataset access
const salesPersonDataset = result.find((dataset) => dataset.name === 'sales_person');
const employeeDeptHistoryDataset = result.find(
(dataset) => dataset.name === 'employee_department_history'
);
console.log(`Sales person dataset found: ${salesPersonDataset ? 'YES' : 'NO'}`);
console.log(
`Employee department history dataset found: ${employeeDeptHistoryDataset ? 'YES' : 'NO'}`
);
// This user should have access to sales_person dataset
expect(salesPersonDataset).toBeDefined();
expect(salesPersonDataset?.name).toBe('sales_person');
// Verify dataset structure for sales_person
if (salesPersonDataset) {
expect(salesPersonDataset).toHaveProperty('id');
expect(salesPersonDataset).toHaveProperty('name');
expect(salesPersonDataset).toHaveProperty('ymlFile');
expect(salesPersonDataset).toHaveProperty('createdAt');
expect(salesPersonDataset).toHaveProperty('updatedAt');
expect(salesPersonDataset).toHaveProperty('deletedAt');
expect(salesPersonDataset).toHaveProperty('dataSourceId');
expect(typeof salesPersonDataset.id).toBe('string');
expect(typeof salesPersonDataset.name).toBe('string');
expect(typeof salesPersonDataset.dataSourceId).toBe('string');
}
// This user should not have access to employee_department_history
expect(employeeDeptHistoryDataset).toBeUndefined();
});
test('should return datasets in alphabetical order for limited access user', async () => {
const result = await getPermissionedDatasets(limitedAccessUserId, 0, 100);
if (result.length > 1) {
// Check if datasets are sorted alphabetically by name
for (let i = 1; i < result.length; i++) {
expect(result[i].name.localeCompare(result[i - 1].name)).toBeGreaterThanOrEqual(0);
}
}
// All datasets should have valid structure
for (const dataset of result) {
expect(dataset).toHaveProperty('id');
expect(dataset).toHaveProperty('name');
expect(dataset).toHaveProperty('dataSourceId');
expect(typeof dataset.id).toBe('string');
expect(typeof dataset.name).toBe('string');
expect(typeof dataset.dataSourceId).toBe('string');
}
});
test('should handle pagination correctly for limited access user', async () => {
const page1 = await getPermissionedDatasets(limitedAccessUserId, 0, 10);
const allResults = await getPermissionedDatasets(limitedAccessUserId, 0, 100);
expect(Array.isArray(page1)).toBe(true);
expect(Array.isArray(allResults)).toBe(true);
// Page 1 should have at most 10 results or all results if fewer than 10
expect(page1.length).toBeLessThanOrEqual(Math.min(10, allResults.length));
// If there are results, the first page should contain some of them
if (allResults.length > 0) {
expect(page1.length).toBeGreaterThan(0);
}
});
});
describe('Error Handling', () => {
test('should validate input parameters', async () => {
// Test invalid UUID
await expect(getPermissionedDatasets('invalid-uuid', 0, 50)).rejects.toThrow();
// Test negative page
await expect(getPermissionedDatasets(testUserId, -1, 50)).rejects.toThrow();
// Test invalid page size
await expect(getPermissionedDatasets(testUserId, 0, 0)).rejects.toThrow();
await expect(getPermissionedDatasets(testUserId, 0, 1001)).rejects.toThrow();
});
});
});