Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { RBACAPI } from '../rbac';
import { apiFetchJSON } from '@/utils/apiClient';
vi.mock('@/utils/apiClient', () => ({
apiFetchJSON: vi.fn(),
}));
describe('RBACAPI', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('getRoles', () => {
it('fetches all roles', async () => {
const mockRoles = [{ id: 'role-1', name: 'Admin' }];
vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockRoles);
const result = await RBACAPI.getRoles();
expect(apiFetchJSON).toHaveBeenCalledWith('/api/admin/roles');
expect(result).toEqual(mockRoles);
});
});
describe('getRole', () => {
it('fetches a single role by id', async () => {
const mockRole = { id: 'role-1', name: 'Admin' };
vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockRole);
const result = await RBACAPI.getRole('role-1');
expect(apiFetchJSON).toHaveBeenCalledWith('/api/admin/roles/role-1');
expect(result).toEqual(mockRole);
});
it('encodes special characters in role id', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce({});
await RBACAPI.getRole('role/1');
expect(apiFetchJSON).toHaveBeenCalledWith('/api/admin/roles/role%2F1');
});
});
describe('saveRole', () => {
it('uses POST for new role', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce({ id: 'new-role', name: 'New Role' });
await RBACAPI.saveRole({ id: 'new-role', name: 'New Role' } as any);
expect(apiFetchJSON).toHaveBeenCalledWith(
'/api/admin/roles',
expect.objectContaining({ method: 'POST' }),
);
});
it('uses PUT for existing role', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce({ id: 'role-1', name: 'Updated' });
await RBACAPI.saveRole({ id: 'role-1', name: 'Updated', createdAt: '2024-01-01' } as any);
expect(apiFetchJSON).toHaveBeenCalledWith(
'/api/admin/roles/role-1',
expect.objectContaining({ method: 'PUT' }),
);
});
});
describe('deleteRole', () => {
it('deletes a role', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);
await RBACAPI.deleteRole('role-1');
expect(apiFetchJSON).toHaveBeenCalledWith(
'/api/admin/roles/role-1',
expect.objectContaining({ method: 'DELETE' }),
);
});
});
describe('getUsers', () => {
it('fetches all users', async () => {
const mockUsers = [{ userId: 'user-1', role: 'admin' }];
vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockUsers);
const result = await RBACAPI.getUsers();
expect(apiFetchJSON).toHaveBeenCalledWith('/api/admin/users');
expect(result).toEqual(mockUsers);
});
});
describe('getUserAssignment', () => {
it('fetches user role assignment', async () => {
const mockAssignment = { userId: 'user-1', role: 'admin' };
vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockAssignment);
const result = await RBACAPI.getUserAssignment('user-1');
expect(apiFetchJSON).toHaveBeenCalledWith('/api/admin/users/user-1/roles');
expect(result).toEqual(mockAssignment);
});
});
describe('updateUserRoles', () => {
it('updates user roles', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });
await RBACAPI.updateUserRoles('user-1', ['role-1', 'role-2']);
expect(apiFetchJSON).toHaveBeenCalledWith(
'/api/admin/users/user-1/roles',
expect.objectContaining({
method: 'PUT',
body: JSON.stringify({ roleIds: ['role-1', 'role-2'] }),
}),
);
});
it('encodes stable SSO principals without changing their identity', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });
await RBACAPI.updateUserRoles('sso:oidc:okta:opaque_subject', ['viewer']);
expect(apiFetchJSON).toHaveBeenCalledWith(
'/api/admin/users/sso%3Aoidc%3Aokta%3Aopaque_subject/roles',
expect.objectContaining({
method: 'PUT',
body: JSON.stringify({ roleIds: ['viewer'] }),
}),
);
});
});
describe('deleteUser', () => {
it('deprovisions an encoded stable SSO principal', async () => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce(undefined);
await RBACAPI.deleteUser('sso:oidc:okta:opaque_subject');
expect(apiFetchJSON).toHaveBeenCalledWith(
'/api/admin/users/sso%3Aoidc%3Aokta%3Aopaque_subject',
expect.objectContaining({ method: 'DELETE' }),
);
});
});
describe('getUserPermissions', () => {
it('preserves a refused read rather than converting it to an empty permission set', async () => {
const denial = Object.assign(new Error('private refusal detail'), { status: 403 });
vi.mocked(apiFetchJSON).mockRejectedValueOnce(denial);
await expect(RBACAPI.getUserPermissions('alice')).rejects.toBe(denial);
expect(apiFetchJSON).toHaveBeenCalledTimes(1);
});
it('fetches user permissions', async () => {
const mockPermissions = [{ action: 'read', resource: 'alerts' }];
vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockPermissions);
const result = await RBACAPI.getUserPermissions('user-1');
expect(apiFetchJSON).toHaveBeenCalledWith('/api/admin/users/user-1/permissions');
expect(result).toEqual(mockPermissions);
});
it('preserves empty results, wildcards and future permission strings', async () => {
vi.mocked(apiFetchJSON)
.mockResolvedValueOnce([])
.mockResolvedValueOnce([
{ action: '*', resource: '*' },
{ action: 'future:read', resource: 'future/resource' },
]);
expect(await RBACAPI.getUserPermissions('alice')).toEqual([]);
expect(await RBACAPI.getUserPermissions('sso:oidc:example:subject/1')).toEqual([
{ action: '*', resource: '*' },
{ action: 'future:read', resource: 'future/resource' },
]);
expect(apiFetchJSON).toHaveBeenLastCalledWith(
'/api/admin/users/sso%3Aoidc%3Aexample%3Asubject%2F1/permissions',
);
});
it.each([
{ payload: null },
{ payload: undefined },
{ payload: {} },
{ payload: { permissions: [] } },
{ payload: 'unread' },
{ payload: [null] },
{ payload: [[]] },
{ payload: [{ action: 'read' }] },
{ payload: [{ action: '', resource: 'alerts' }] },
{ payload: [{ action: 'read', resource: ' ' }] },
{ payload: [{ action: true, resource: 'alerts' }] },
{
payload: [
{ action: 'read', resource: 'alerts' },
{ action: 'read', resource: 7 },
],
},
])(
'rejects an unread permission result rather than inventing access: $payload',
async ({ payload }) => {
vi.mocked(apiFetchJSON).mockResolvedValueOnce(payload);
await expect(RBACAPI.getUserPermissions('alice')).rejects.toThrow(
'Invalid saved permissions response.',
);
},
);
});
it('binds role writes to the selected organisation without retries', async () => {
const controller = new AbortController();
const options = { signal: controller.signal, expectedOrgID: 'selected-org', retry: false };
const role = {
id: 'role/one',
name: 'One',
description: '',
permissions: [],
createdAt: '2026-01-01',
};
await RBACAPI.saveRole(role, options);
await RBACAPI.deleteRole(role.id, options);
expect(apiFetchJSON).toHaveBeenNthCalledWith(1, '/api/admin/roles/role%2Fone', {
...options,
method: 'PUT',
body: JSON.stringify(role),
});
expect(apiFetchJSON).toHaveBeenNthCalledWith(2, '/api/admin/roles/role%2Fone', {
...options,
method: 'DELETE',
});
});
it('forwards an exact abortable organisation and no-retry binding for User Access', async () => {
const controller = new AbortController();
const options = { signal: controller.signal, expectedOrgID: 'selected-org', retry: false };
vi.mocked(apiFetchJSON).mockResolvedValue([]);
await RBACAPI.getUsers(options);
await RBACAPI.getRoles(options);
await RBACAPI.getUserPermissions('sso:user/a', options);
await RBACAPI.updateUserRoles('sso:user/a', ['viewer'], options);
await RBACAPI.deleteUser('sso:user/a', options);
expect(apiFetchJSON).toHaveBeenNthCalledWith(1, '/api/admin/users', options);
expect(apiFetchJSON).toHaveBeenNthCalledWith(2, '/api/admin/roles', options);
expect(apiFetchJSON).toHaveBeenNthCalledWith(
3,
'/api/admin/users/sso%3Auser%2Fa/permissions',
options,
);
expect(apiFetchJSON).toHaveBeenNthCalledWith(4, '/api/admin/users/sso%3Auser%2Fa/roles', {
...options,
method: 'PUT',
body: JSON.stringify({ roleIds: ['viewer'] }),
});
expect(apiFetchJSON).toHaveBeenNthCalledWith(5, '/api/admin/users/sso%3Auser%2Fa', {
...options,
method: 'DELETE',
});
});
});