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