webserver-framework/tests/PermissionManager.test.js
2023-06-22 02:11:31 +03:00

68 lines
2.7 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('Permission validation', () => {
expect(() => PermissionManager.validatePermissions(perms)).toThrow();
perms.developer.bruh = 'str';
perms.administrator = { default: 10 };
expect(() => PermissionManager.validatePermissions(perms)).toThrow();
perms.developer.bruh = 10;
expect(() => PermissionManager.validatePermissions(perms)).toThrow();
});
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, 'developer')).toBe(true);
perms.developer = 0;
expect(PermissionManager.checkPermissions(perms, 'administrator', 10)).toBe(true);
expect(PermissionManager.checkPermissions(perms, 'administrator:removeuser')).toBe(true);
// Ignore admin perms if user has them
expect(PermissionManager.checkPermissions(perms, 'blargus', 0, true)).toBe(false);
// Don't ignore admin
expect(PermissionManager.checkPermissions(perms, 'blargus', 0)).toBe(true);
perms.administrator = { default: 10 };
expect(PermissionManager.checkPermissions(perms, 'blargus', 0)).toBe(true);
});
});