webserver-framework/tests/PermissionManager.test.js

52 lines
2.1 KiB
JavaScript

const { PermissionManager } = require('../src/util');
let perms = null;
beforeEach(() => {
perms = { developer: { default: 5 }, administrator: 10 };
});
describe('PermissionManager', () => {
test('Constructor', () => {
expect(() => new PermissionManager()).toThrow();
});
test('Ensure permission', () => {
expect(() => PermissionManager.ensurePermission()).toThrow();
expect(PermissionManager.DefaultPermissions).toEqual({});
PermissionManager.ensurePermission('administrator:removeuser:force:bruh:moment:broo');
PermissionManager.ensurePermission('developer:toggledebug');
PermissionManager.ensurePermission('developer');
expect(PermissionManager.DefaultPermissions).toHaveProperty('administrator.removeuser.force');
expect(PermissionManager.DefaultPermissions).toHaveProperty('developer.toggledebug');
expect(PermissionManager.DefaultPermissions).not.toHaveProperty('developer.removeuser.force');
});
test('Test permission merge', () => {
expect(perms).toHaveProperty('administrator', 10);
PermissionManager.ensurePermission('administrator:removeuser:force');
PermissionManager.merge(perms, PermissionManager.DefaultPermissions);
expect(perms).toHaveProperty('administrator.removeuser.force');
expect(perms).toHaveProperty('administrator.default', 10);
});
test('Check for permission', () => {
expect(() => PermissionManager.checkPermissions()).toThrow();
expect(() => PermissionManager.checkPermissions(perms)).toThrow();
expect(PermissionManager.checkPermissions(perms, 'administrator', 10)).toBe(true);
expect(PermissionManager.checkPermissions(perms, 'administrator:removeuser')).toBe(true);
expect(PermissionManager.checkPermissions(perms, 'developer', 5)).toBe(true);
expect(PermissionManager.checkPermissions(perms, 'developer', 10)).toBe(false);
expect(PermissionManager.checkPermissions(perms, 'blargus', 0, true)).toBe(false);
});
});