diff --git a/tests/PermissionManager.test.js b/tests/PermissionManager.test.js index 38102a6..fe9e7dc 100644 --- a/tests/PermissionManager.test.js +++ b/tests/PermissionManager.test.js @@ -5,42 +5,47 @@ beforeEach(() => { perms = { developer: { default: 5 }, administrator: 10 }; }); -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', () => { +describe('PermissionManager', () => { + test('Constructor', () => { + expect(() => new PermissionManager()).toThrow(); + }); - 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, 'developer', 5)).toBe(true); - expect(PermissionManager.checkPermissions(perms, 'developer', 10)).toBe(false); + 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, 'developer', 5)).toBe(true); + expect(PermissionManager.checkPermissions(perms, 'developer', 10)).toBe(false); + + expect(PermissionManager.checkPermissions(perms, 'bargus', 0)).toBe(false); + + }); }); \ No newline at end of file diff --git a/tests/UserStructures.test.js b/tests/UserStructures.test.js new file mode 100644 index 0000000..f97ae2f --- /dev/null +++ b/tests/UserStructures.test.js @@ -0,0 +1,100 @@ +const AbstractUser = require('../src/server/interfaces/AbstractUser'); +const { User, UserApplication } = require('../src/server/structures'); +const { Util } = require('../src/util'); + +test('Abstract user', () => { + expect(() => new AbstractUser()).toThrow(); + const Extended = class extends AbstractUser { }; + + expect(() => new Extended()).toThrow(); + expect(() => new Extended(() => null)).toThrow(); + expect(() => new Extended(() => null, { id: 123 })).toThrow(); + expect(() => new Extended(() => null, { id: '123' })).toThrow(); + expect(() => new Extended(() => null, { id: '123', name: 'dingus' })).not.toThrow(); + + const save = jest.fn(); + + const now = Date.now(); + const permissions = { + administrator: 5 + }; + const instance = new Extended(save, { id: '1ph45', name: 'navy', permissions, createdTimestamp: now }); + instance.save(); + + expect(save).toHaveBeenCalled(); + expect(instance.hasPermission('administrator', 10)).toBe(false); + expect(instance.hasPermission('administrator')).toBe(true); + expect(instance.json).toHaveProperty('id'); + expect(instance.jsonPrivate).toHaveProperty('_id'); + expect(instance.createdAt.getTime()).toEqual(now); + expect(instance.cachedTimestamp).toBeGreaterThanOrEqual(now); + +}); + +test('User structure', async () => { + + expect(() => new User()).toThrow(); + const save = jest.fn(); + const permissions = { developer: 5 }; + const id = Util.randomString(); + const name = 'navy'; + const password = Util.randomString(); + const instance = new User(save, { id, name, permissions, password }); + + expect(instance.passwordLoginEnabled).toBe(true); + expect(instance.avatar).toBe(null); + + const saveApp = jest.fn(); + const appName = 'ship'; + const description = 'A ship'; + await expect(() => instance.createApplication(appName, { description })).rejects.toThrow(); + const app = await instance.createApplication(appName, { description }, saveApp); + expect(app.name).toBe(appName); + expect(app.description).toBe(description); + expect(saveApp).toHaveBeenCalledWith(app); + + const pw = 'Pw123'; + await instance.setPassword(pw, true); + expect(instance.passwordLoginEnabled).toBe(true); + expect(await instance.authenticate(pw)).toBe(true); + + await expect(() => instance.setPassword({ prop: 'val' })).rejects.toThrow(); + + const profile = { username: 'dingus', id: 'asf231t' }; + instance.addExternalProfile('platform', profile); + expect(instance.json.externalProfiles[0]).toEqual(profile); + expect(instance.hasExternalProfile('platform')).toEqual(true); + + expect(instance.jsonPrivate).toHaveProperty('_id', id); + expect(instance.json).toHaveProperty('id', id); + expect(instance.json).toHaveProperty('displayName', name); + expect(instance.json).not.toHaveProperty('password'); + + instance.save(); + expect(save).toHaveBeenCalledWith(instance); + +}); + +test('Application structure', () => { + expect(() => new UserApplication()).toThrow(); + + const save = jest.fn(); + const permissions = { developer: 5 }; + const id = Util.randomString(); + const ownerId = Util.randomString(); + const name = 'ship'; + const token = Util.randomUUID(); + const instance = new UserApplication(save, { id, name, permissions, token, ownerId }); + + expect(instance.ownerId).toEqual(ownerId); + expect(instance.jsonPrivate).toHaveProperty('_id', id); + expect(instance.jsonPrivate).toHaveProperty('token', token); + expect(instance.json).toHaveProperty('id', id); + expect(instance.json).toHaveProperty('name', name); + expect(instance.json).toHaveProperty('ownerId', ownerId); + expect(instance.json).not.toHaveProperty('password'); + + instance.save(); + expect(save).toHaveBeenCalledWith(instance); + +}); \ No newline at end of file