buster/packages/access-controls/tests/unit/access-controls.test.ts

87 lines
2.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AccessControlsError } from '../../src/types';
import { buildAccessQuery, formatPermissionName, isValidUuid } from '../../src/utils';
// Mock the database module
vi.mock('@buster/database', () => ({
getDb: vi.fn(),
datasets: {},
datasetPermissions: {},
datasetsToPermissionGroups: {},
permissionGroups: {},
permissionGroupsToIdentities: {},
teams: {},
teamsToUsers: {},
usersToOrganizations: {},
}));
describe('AccessControlsError', () => {
it('should create an error with message and code', () => {
const error = new AccessControlsError('Test error', 'TEST_CODE');
expect(error.message).toBe('Test error');
expect(error.code).toBe('TEST_CODE');
expect(error.name).toBe('AccessControlsError');
});
it('should create an error with just a message', () => {
const error = new AccessControlsError('Test error');
expect(error.message).toBe('Test error');
expect(error.code).toBeUndefined();
expect(error.name).toBe('AccessControlsError');
});
});
describe('Utils', () => {
describe('formatPermissionName', () => {
it('should format permission name correctly', () => {
const result = formatPermissionName('Dataset', 'Read');
expect(result).toBe('dataset:read');
});
it('should handle lowercase inputs', () => {
const result = formatPermissionName('dashboard', 'write');
expect(result).toBe('dashboard:write');
});
});
describe('buildAccessQuery', () => {
it('should build query object with userId and resourceType', () => {
const result = buildAccessQuery('user-123', 'dataset');
expect(result).toEqual({
userId: 'user-123',
resourceType: 'dataset',
});
});
});
describe('isValidUuid', () => {
it('should validate correct UUID', () => {
const validUuid = '123e4567-e89b-12d3-a456-426614174000';
expect(isValidUuid(validUuid)).toBe(true);
});
it('should reject invalid UUID', () => {
expect(isValidUuid('invalid-uuid')).toBe(false);
expect(isValidUuid('')).toBe(false);
expect(isValidUuid('123')).toBe(false);
});
});
});
describe('Access Control Functions', () => {
beforeEach(() => {
vi.clearAllMocks();
});
// Note: The main access control functions would require complex database mocking
// These tests serve as a placeholder for when the database imports are resolved
it('should have correct function exports', () => {
// This test ensures the module structure is correct
// Individual function tests would be added once database mocking is set up
expect(true).toBe(true);
});
});