134 lines
3.9 KiB
TypeScript
134 lines
3.9 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
DEFAULT_ROLES,
|
|
knownPermissions,
|
|
maskEmail,
|
|
PERMISSION_KEYS,
|
|
permissionsFromGrants,
|
|
platformRoleKeys,
|
|
redactUserDto,
|
|
redactUserDtos,
|
|
redactLogDetails,
|
|
PERMISSIONS,
|
|
} from '../../src/lib/permissions.js';
|
|
|
|
describe('maskEmail', () => {
|
|
it('masks local part and first domain label', () => {
|
|
expect(maskEmail('jan@fitos.pl')).toBe('j*n@f****.pl');
|
|
});
|
|
|
|
it('handles short local parts', () => {
|
|
expect(maskEmail('ab@x.io')).toBe('a*@x*.io');
|
|
});
|
|
|
|
it('returns falsy values untouched', () => {
|
|
expect(maskEmail(null)).toBeNull();
|
|
expect(maskEmail(undefined)).toBeUndefined();
|
|
expect(maskEmail('')).toBe('');
|
|
});
|
|
|
|
it('keeps the stable first/last letter shape', () => {
|
|
expect(maskEmail('jan.kowalski@fitos.pl')).toBe('j**********i@f****.pl');
|
|
});
|
|
});
|
|
|
|
describe('knownPermissions', () => {
|
|
it('keeps only catalog keys and preserves order', () => {
|
|
expect(knownPermissions(['users:read', 'bogus:perm', 'logs:view'])).toEqual([
|
|
'users:read',
|
|
'logs:view',
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('role resolution', () => {
|
|
it('unions permissions across grants and drops unknown keys', () => {
|
|
const user = {
|
|
roleGrants: [
|
|
{ role: { key: 'MODERATOR', permissions: ['users:read', 'users:manage', 'ghost:perm'] } },
|
|
{ role: { key: 'CUSTOM', permissions: ['users:manage', 'logs:view'] } },
|
|
],
|
|
};
|
|
expect(platformRoleKeys(user)).toEqual(['MODERATOR', 'CUSTOM']);
|
|
expect(permissionsFromGrants(user)).toEqual([
|
|
'users:read',
|
|
'users:manage',
|
|
'logs:view',
|
|
]);
|
|
});
|
|
|
|
it('treats a user without grants as permissionless', () => {
|
|
expect(permissionsFromGrants({})).toEqual([]);
|
|
expect(platformRoleKeys({})).toEqual([]);
|
|
});
|
|
|
|
it('defines ADMIN with the full catalog and MODERATOR as a subset', () => {
|
|
const admin = DEFAULT_ROLES.find((r) => r.key === 'ADMIN')!;
|
|
const moderator = DEFAULT_ROLES.find((r) => r.key === 'MODERATOR')!;
|
|
expect([...admin.permissions].sort()).toEqual([...PERMISSION_KEYS].sort());
|
|
for (const perm of moderator.permissions) {
|
|
expect(admin.permissions).toContain(perm);
|
|
}
|
|
expect(moderator.permissions).not.toContain(PERMISSIONS.PII_READ);
|
|
expect(moderator.permissions).not.toContain(PERMISSIONS.ROLES_MANAGE);
|
|
expect(admin.isSystem).toBe(true);
|
|
expect(moderator.isSystem).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('redactUserDto', () => {
|
|
const user = {
|
|
id: 'u1',
|
|
email: 'jan@fitos.pl',
|
|
phone: '+48123456789',
|
|
birthDate: '1996-05-05',
|
|
firstName: 'Jan',
|
|
};
|
|
|
|
it('leaves PII intact for a pii:read viewer', () => {
|
|
expect(redactUserDto(user, [PERMISSIONS.PII_READ])).toEqual(user);
|
|
});
|
|
|
|
it('masks email and nulls phone/birthDate otherwise', () => {
|
|
expect(redactUserDto(user, [])).toEqual({
|
|
id: 'u1',
|
|
email: 'j*n@f****.pl',
|
|
phone: null,
|
|
birthDate: null,
|
|
firstName: 'Jan',
|
|
});
|
|
});
|
|
|
|
it('does not mutate the source object', () => {
|
|
const copy = { ...user };
|
|
redactUserDto(copy, []);
|
|
expect(copy).toEqual(user);
|
|
});
|
|
|
|
it('redacts every row in a list', () => {
|
|
const out = redactUserDtos([user, { ...user, id: 'u2' }], []);
|
|
expect(out.map((u) => u.email)).toEqual(['j*n@f****.pl', 'j*n@f****.pl']);
|
|
});
|
|
});
|
|
|
|
describe('redactLogDetails', () => {
|
|
it('masks email fields at any depth', () => {
|
|
const details = {
|
|
email: 'jan@fitos.pl',
|
|
nested: { email: 'anna@fitos.pl', keep: 'value' },
|
|
list: [{ email: 'bob@fitos.pl' }],
|
|
};
|
|
expect(redactLogDetails(details, [])).toEqual({
|
|
email: 'j*n@f****.pl',
|
|
nested: { email: 'a**a@f****.pl', keep: 'value' },
|
|
list: [{ email: 'b*b@f****.pl' }],
|
|
});
|
|
});
|
|
|
|
it('is a no-op for pii:read viewers and null details', () => {
|
|
const details = { email: 'jan@fitos.pl' };
|
|
expect(redactLogDetails(details, [PERMISSIONS.PII_READ])).toBe(details);
|
|
expect(redactLogDetails(null, [])).toBeNull();
|
|
});
|
|
});
|