webserver-framework/tests/PermissionManager.test.js

68 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-02-11 17:01:56 +01:00
const { PermissionManager } = require('../src/util');
let perms = null;
beforeEach(() => {
perms = { developer: { default: 5 }, administrator: 10 };
});
describe('PermissionManager', () => {
test('Constructor', () => {
expect(() => new PermissionManager()).toThrow();
});
2023-02-11 17:01:56 +01:00
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');
2023-06-22 01:11:31 +02:00
});
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();
2023-06-22 01:11:31 +02:00
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);
2023-06-22 01:11:31 +02:00
// Ignore admin perms if user has them
expect(PermissionManager.checkPermissions(perms, 'blargus', 0, true)).toBe(false);
2023-06-22 01:11:31 +02:00
// Don't ignore admin
expect(PermissionManager.checkPermissions(perms, 'blargus', 0)).toBe(true);
perms.administrator = { default: 10 };
expect(PermissionManager.checkPermissions(perms, 'blargus', 0)).toBe(true);
});
2023-02-11 17:01:56 +01:00
});