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

131 lines
4.6 KiB
JavaScript

/* eslint-disable max-classes-per-file */
const { ObjectId } = require('mongodb');
const { UserDatabaseInterface } = require('../src/server/interfaces');
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 save = jest.fn();
const Extended = class extends AbstractUser {
save () {
save();
}
};
const UserDB = class extends UserDatabaseInterface {};
const userdb = new UserDB();
const objId = new ObjectId();
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(userdb, { id: objId, name: 'dingus' })).not.toThrow();
const now = Date.now();
const permissions = {
administrator: 5
};
const instance = new Extended(userdb, { id: objId, 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 saveApp = jest.fn();
const UserDB = class extends UserDatabaseInterface {
updateApplication (...args) {
saveApp(...args);
}
updateUser (...args) {
save(...args);
}
};
const userdb = new UserDB();
const permissions = { developer: 5 };
const id = new ObjectId();
const name = 'navy';
const password = Util.randomString();
const instance = new User(userdb, { id, name, permissions, password });
expect(instance.passwordLoginEnabled).toBe(true);
expect(instance.avatar).toBe(null);
const appName = 'ship';
const description = 'A ship';
const appId = new ObjectId();
const token = Util.randomUUID();
await expect(() => new UserApplication(userdb, { description })).toThrow();
const app = new UserApplication(userdb, { id: appId, token, name: appName, description, ownerId: instance.id });
app.save();
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.toString());
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 UserDB = class extends UserDatabaseInterface {
updateApplication (...args) {
save(...args);
}
};
const userdb = new UserDB();
const permissions = { developer: 5 };
const id = new ObjectId();
const ownerId = new ObjectId();
const name = 'ship';
const token = Util.randomUUID();
const instance = new UserApplication(userdb, { id, name, permissions, token, ownerId });
expect(instance.ownerId).toEqual(ownerId.toString());
expect(instance.jsonPrivate).toHaveProperty('_id', id);
expect(instance.jsonPrivate).toHaveProperty('token', token);
expect(instance.json).toHaveProperty('id', id.toString());
expect(instance.json).toHaveProperty('name', name);
expect(instance.json).toHaveProperty('ownerId', ownerId);
expect(instance.json).not.toHaveProperty('password');
instance.save();
expect(save).toHaveBeenCalledWith(instance);
});