async'ify the groups code
This commit is contained in:
@@ -6,384 +6,194 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
database = require('../database.js'),
|
||||
const BoxError = require('../boxerror.js'),
|
||||
common = require('./common.js'),
|
||||
expect = require('expect.js'),
|
||||
groups = require('../groups.js'),
|
||||
hat = require('../hat.js'),
|
||||
mailboxdb = require('../mailboxdb.js'),
|
||||
userdb = require('../userdb.js');
|
||||
|
||||
var GROUP0_NAME = 'administrators',
|
||||
group0Object;
|
||||
|
||||
var GROUP1_NAME = 'externs',
|
||||
group1Object;
|
||||
|
||||
const DOMAIN_0 = {
|
||||
domain: 'example.com',
|
||||
zoneName: 'example.com',
|
||||
config: { provider: 'manual' },
|
||||
wellKnown: null
|
||||
};
|
||||
|
||||
var USER_0 = {
|
||||
id: 'uuid213',
|
||||
username: 'uuid213',
|
||||
password: 'secret',
|
||||
email: 'safe@me.com',
|
||||
fallbackEmail: 'safefallback@me.com',
|
||||
role: 'user',
|
||||
salt: 'morton',
|
||||
createdAt: 'sometime back',
|
||||
resetToken: hat(256),
|
||||
displayName: '',
|
||||
source: '',
|
||||
permissions: null
|
||||
};
|
||||
|
||||
var USER_1 = { // this user has not signed up yet
|
||||
id: 'uuid222',
|
||||
username: null,
|
||||
password: '',
|
||||
email: 'safe2@me.com',
|
||||
fallbackEmail: 'safe2fallback@me.com',
|
||||
role: 'user',
|
||||
salt: 'morton',
|
||||
createdAt: 'sometime back',
|
||||
resetToken: hat(256),
|
||||
displayName: '',
|
||||
source: '',
|
||||
permissions: null
|
||||
};
|
||||
|
||||
function setup(done) {
|
||||
// ensure data/config/mount paths
|
||||
async.series([
|
||||
database.initialize,
|
||||
database._clear
|
||||
], done);
|
||||
}
|
||||
|
||||
function cleanup(done) {
|
||||
async.series([
|
||||
database._clear,
|
||||
database.uninitialize
|
||||
], done);
|
||||
}
|
||||
safe = require('safetydance');
|
||||
|
||||
describe('Groups', function () {
|
||||
const { setup, cleanup, ADMIN, USER } = common;
|
||||
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
it('cannot create group - too small', function (done) {
|
||||
groups.create('', '', function (error) {
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
done();
|
||||
});
|
||||
});
|
||||
describe('add/get/del', function () {
|
||||
let group0Name = 'administrators', group0Object;
|
||||
let group1Name = 'externs', group1Object;
|
||||
|
||||
it('cannot create group - too big', function (done) {
|
||||
groups.create(new Array(256).join('a'), '', function (error) {
|
||||
it('cannot add group - too small', async function () {
|
||||
const [error] = await safe(groups.add({ name: '' }));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot create group - bad name', function (done) {
|
||||
groups.create('bad:name', '', function (error) {
|
||||
it('cannot add group - too big', async function () {
|
||||
const [error] = await safe(groups.add({ name: new Array(256).join('a') }));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot create group - reserved', function (done) {
|
||||
groups.create('users', '', function (error) {
|
||||
it('cannot add group - bad name', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'bad:name' }));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot create group - invalid', function (done) {
|
||||
groups.create('cloudron+admin', '', function (error) {
|
||||
it('cannot add group - reserved', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'users' }));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot create group - invalid source', function (done) {
|
||||
groups.create('cloudron+admin', 'unknownsource', function (error) {
|
||||
it('cannot add group - invalid', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'cloudron+admin' }));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can create valid group', function (done) {
|
||||
groups.create(GROUP0_NAME, '', function (error, result) {
|
||||
it('cannot add group - invalid source', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'somegroup', source: 'unknownsource' }));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('can add valid groups', async function () {
|
||||
let [error, result] = await safe(groups.add({ name: group0Name }));
|
||||
expect(error).to.be(null);
|
||||
group0Object = result;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot create existing group with mixed case', function (done) {
|
||||
var name = GROUP0_NAME[0].toUpperCase() + GROUP0_NAME.substr(1);
|
||||
groups.create(name, '', function (error) {
|
||||
[error, result] = await safe(groups.add({ name: group1Name }));
|
||||
expect(error).to.be(null);
|
||||
group1Object = result;
|
||||
});
|
||||
|
||||
it('cannot add existing group with mixed case', async function () {
|
||||
const name = group0Name[0].toUpperCase() + group0Name.substr(1);
|
||||
const [error] = await safe(groups.add({ name }));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot add existing group', function (done) {
|
||||
groups.create(GROUP0_NAME, 'ldap', function (error) {
|
||||
it('cannot add existing group', async function () {
|
||||
const [error] = await safe(groups.add({name: group0Name, source: 'ldap' }));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot get invalid group', function (done) {
|
||||
groups.get('sometrandom', function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
it('cannot get invalid group', async function () {
|
||||
const result = await groups.get('sometrandom');
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
});
|
||||
|
||||
it('can get valid group', function (done) {
|
||||
groups.get(group0Object.id, function (error, group) {
|
||||
expect(error).to.be(null);
|
||||
expect(group.name).to.equal(GROUP0_NAME);
|
||||
done();
|
||||
it('can get valid group', async function () {
|
||||
const result = await groups.get(group0Object.id);
|
||||
expect(result.name).to.equal(group0Name);
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot delete invalid group', function (done) {
|
||||
groups.remove('random', function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
it('isMember returns false', async function () {
|
||||
const isMember = await groups.isMember(group0Object.id, ADMIN.id);
|
||||
expect(isMember).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
it('can delete valid group', function (done) {
|
||||
groups.remove(group0Object.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
it('can add member to the group', async function () {
|
||||
await groups.addMember(group0Object.id, ADMIN.id);
|
||||
});
|
||||
});
|
||||
|
||||
it('did delete mailbox', function (done) {
|
||||
mailboxdb.getList(GROUP0_NAME.toLowerCase(), DOMAIN_0.domain, function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Group membership', function () {
|
||||
before(function (done) {
|
||||
async.series([
|
||||
setup,
|
||||
function (next) {
|
||||
groups.create(GROUP0_NAME, '', function (error, result) {
|
||||
if (error) return next(error);
|
||||
group0Object = result;
|
||||
next();
|
||||
});
|
||||
},
|
||||
userdb.add.bind(null, USER_0.id, USER_0),
|
||||
userdb.add.bind(null, USER_1.id, USER_1)
|
||||
], done);
|
||||
});
|
||||
after(cleanup);
|
||||
|
||||
it('cannot add non-existent user', function (done) {
|
||||
groups.addMember(group0Object.id, 'randomuser', function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot add non-existent group', function (done) {
|
||||
groups.addMember('randomgroup', USER_0.id, function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('isMember returns false', function (done) {
|
||||
groups.isMember(group0Object.id, USER_0.id, function (error, member) {
|
||||
expect(error).to.be(null);
|
||||
expect(member).to.be(false);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can add member', function (done) {
|
||||
groups.addMember(group0Object.id, USER_0.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot add same member again', function (done) {
|
||||
groups.addMember(group0Object.id, USER_0.id, function (error) {
|
||||
it('cannot add same member to the group', async function () {
|
||||
const [error] = await safe(groups.addMember(group0Object.id, ADMIN.id));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can add member without username', function (done) {
|
||||
groups.addMember(group0Object.id, USER_1.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
it('isMember returns true', async function () {
|
||||
const isMember = await groups.isMember(group0Object.id, ADMIN.id);
|
||||
expect(isMember).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('isMember returns true', function (done) {
|
||||
groups.isMember(group0Object.id, USER_0.id, function (error, member) {
|
||||
expect(error).to.be(null);
|
||||
expect(member).to.be(true);
|
||||
done();
|
||||
it('cannot add invalid user to group', async function () {
|
||||
const [error] = await safe(groups.addMember(group0Object.id, 'random'));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
});
|
||||
});
|
||||
|
||||
it('can get members', function (done) {
|
||||
groups.getMembers(group0Object.id, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.length).to.be(2);
|
||||
expect(result[0]).to.be(USER_0.id);
|
||||
expect(result[1]).to.be(USER_1.id);
|
||||
done();
|
||||
it('cannot add non-existent group', async function () {
|
||||
const [error] = await safe(groups.addMember('randomgroup', ADMIN.id));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot get members of non-existent group', function (done) {
|
||||
groups.getMembers('randomgroup', function (error, result) {
|
||||
it('can set members', async function () {
|
||||
await groups.setMembers(group0Object.id, [ ADMIN.id, USER.id ]);
|
||||
});
|
||||
|
||||
it('cannot set duplicate members', async function () {
|
||||
const [error] = await safe(groups.setMembers(group0Object.id, [ ADMIN.id, USER.id, ADMIN.id ]));
|
||||
expect(error.reason).to.be(BoxError.CONFLICT);
|
||||
});
|
||||
|
||||
it('can list users of group', async function () {
|
||||
const result = await groups.getMembers(group0Object.id);
|
||||
expect(result).to.eql([ ADMIN.id, USER.id ]);
|
||||
});
|
||||
|
||||
it('cannot list members of non-existent group', async function () {
|
||||
const result = await groups.getMembers('randomgroup');
|
||||
expect(result.length).to.be(0); // currently, we cannot differentiate invalid groups and empty groups
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot remove non-existent user', function (done) {
|
||||
groups.removeMember(group0Object.id, 'randomuser', function (error) {
|
||||
it('cannot delete non-existent member', async function () {
|
||||
const [error] = await safe(groups.removeMember(group0Object.id, 'random'));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot remove non-existent group', function (done) {
|
||||
groups.removeMember('randomgroup', USER_0.id, function (error) {
|
||||
it('cannot remove member from non-existent group', async function () {
|
||||
const [error] = await safe(groups.removeMember('randomgroup', ADMIN.id));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can set members', function (done) {
|
||||
groups.setMembers(group0Object.id, [ USER_0.id ], function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
it('can remove existing member', async function () {
|
||||
await groups.removeMember(group0Object.id, USER.id);
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot set duplicate members', function (done) {
|
||||
groups.setMembers(group0Object.id, [ USER_0.id, USER_0.id ], function (error) {
|
||||
it('can getWithMembers', async function () {
|
||||
const result = await groups.getWithMembers(group0Object.id);
|
||||
expect(result.name).to.be(group0Name);
|
||||
expect(result.userIds).to.eql([ ADMIN.id ]);
|
||||
});
|
||||
|
||||
it('can set groups', async function () {
|
||||
await groups.setMembership(ADMIN.id, [ group0Object.id ]);
|
||||
});
|
||||
|
||||
it('cannot set user to same group twice', async function () {
|
||||
const [error] = await safe(groups.setMembership(ADMIN.id, [ group0Object.id, group0Object.id ]));
|
||||
expect(error.reason).to.be(BoxError.CONFLICT);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove member', function (done) {
|
||||
groups.removeMember(group0Object.id, USER_0.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
it('can set user to multiple groups', async function () {
|
||||
await groups.setMembership(ADMIN.id, [ group0Object.id, group1Object.id ]);
|
||||
});
|
||||
});
|
||||
|
||||
it('has no members', function (done) {
|
||||
groups.getMembers(group0Object.id, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.length).to.be(0);
|
||||
done();
|
||||
it('can get groups membership', async function () {
|
||||
const groupIds = await groups.getMembership(ADMIN.id);
|
||||
expect(groupIds.length).to.be(2);
|
||||
expect(groupIds.sort()).to.eql([ group0Object.id, group1Object.id ].sort());
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove group with no members', function (done) {
|
||||
groups.remove(group0Object.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
it('can getAll', async function () {
|
||||
const result = await groups.getAll();
|
||||
expect(result.length).to.be(2);
|
||||
expect(result[0].name).to.be(group0Name);
|
||||
expect(result[1].name).to.be(group1Name);
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove group with member', function (done) {
|
||||
groups.create(GROUP0_NAME, '', function (error, result) {
|
||||
expect(error).to.eql(null);
|
||||
group0Object = result;
|
||||
|
||||
groups.addMember(group0Object.id, USER_0.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
groups.remove(group0Object.id, function (error) {
|
||||
expect(error).to.eql(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Set user groups', function () {
|
||||
before(function (done) {
|
||||
async.series([
|
||||
setup,
|
||||
function (next) {
|
||||
groups.create(GROUP0_NAME, '', function (error, result) {
|
||||
if (error) return next(error);
|
||||
group0Object = result;
|
||||
next();
|
||||
});
|
||||
},
|
||||
function (next) {
|
||||
groups.create(GROUP1_NAME, '', function (error, result) {
|
||||
if (error) return next(error);
|
||||
group1Object = result;
|
||||
next();
|
||||
});
|
||||
},
|
||||
userdb.add.bind(null, USER_0.id, USER_0)
|
||||
], done);
|
||||
});
|
||||
after(cleanup);
|
||||
|
||||
it('can set user to single group', function (done) {
|
||||
groups.setMembership(USER_0.id, [ group0Object.id ], function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
groups.getMembership(USER_0.id, function (error, groupIds) {
|
||||
expect(error).to.be(null);
|
||||
expect(groupIds.length).to.be(1);
|
||||
expect(groupIds[0]).to.be(group0Object.id);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot set user to same group twice', function (done) {
|
||||
groups.setMembership(USER_0.id, [ group0Object.id, group0Object.id ], function (error) {
|
||||
expect(error.reason).to.be(BoxError.CONFLICT);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can set user to multiple groups', function (done) {
|
||||
groups.setMembership(USER_0.id, [ group0Object.id, group1Object.id ], function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
groups.getMembership(USER_0.id, function (error, groupIds) {
|
||||
expect(error).to.be(null);
|
||||
expect(groupIds.length).to.be(2);
|
||||
expect(groupIds.sort()).to.eql([ group0Object.id, group1Object.id ].sort());
|
||||
done();
|
||||
});
|
||||
it('can getAllWithMembers', async function () {
|
||||
const result = await groups.getAllWithMembers();
|
||||
expect(result.length).to.be(2);
|
||||
expect(result[0].name).to.be(group0Name);
|
||||
expect(result[1].userIds).to.eql([ ADMIN.id ]);
|
||||
expect(result[1].name).to.be(group1Name);
|
||||
});
|
||||
|
||||
it('cannot delete invalid group', async function () {
|
||||
const [error] = await safe(groups.remove('random'));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
});
|
||||
|
||||
it('can delete valid group', async function () {
|
||||
await groups.setMembers(group0Object.id, [ ADMIN.id, USER.id ]); // ensure group has some members
|
||||
await groups.remove(group0Object.id);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user