async'ify the groups code
This commit is contained in:
@@ -13,8 +13,6 @@ const appdb = require('../appdb.js'),
|
||||
database = require('../database.js'),
|
||||
domains = require('../domains.js'),
|
||||
expect = require('expect.js'),
|
||||
groupdb = require('../groupdb.js'),
|
||||
groups = require('../groups.js'),
|
||||
hat = require('../hat.js'),
|
||||
provision = require('../provision.js'),
|
||||
userdb = require('../userdb.js');
|
||||
@@ -182,9 +180,6 @@ describe('Apps', function () {
|
||||
userdb.add.bind(null, ADMIN_0.id, ADMIN_0),
|
||||
userdb.add.bind(null, USER_0.id, USER_0),
|
||||
userdb.add.bind(null, USER_1.id, USER_1),
|
||||
groupdb.add.bind(null, GROUP_0.id, GROUP_0.name, GROUP_0.source),
|
||||
groupdb.add.bind(null, GROUP_1.id, GROUP_1.name, GROUP_1.source),
|
||||
groups.addMember.bind(null, GROUP_0.id, USER_1.id),
|
||||
appdb.add.bind(null, APP_0.id, APP_0.appStoreId, APP_0.manifest, APP_0.location, APP_0.domain, apps._translatePortBindings(APP_0.portBindings, APP_0.manifest), APP_0),
|
||||
appdb.add.bind(null, APP_1.id, APP_1.appStoreId, APP_1.manifest, APP_1.location, APP_1.domain, apps._translatePortBindings(APP_1.portBindings, APP_1.manifest), APP_1),
|
||||
appdb.add.bind(null, APP_2.id, APP_2.appStoreId, APP_2.manifest, APP_2.location, APP_2.domain, apps._translatePortBindings(APP_2.portBindings, APP_2.manifest), APP_2),
|
||||
|
||||
+20
-1
@@ -7,12 +7,14 @@ const appdb = require('../appdb.js'),
|
||||
database = require('../database.js'),
|
||||
domains = require('../domains.js'),
|
||||
fs = require('fs'),
|
||||
hat = require('../hat.js'),
|
||||
mailer = require('../mailer.js'),
|
||||
nock = require('nock'),
|
||||
path = require('path'),
|
||||
rimraf = require('rimraf'),
|
||||
settings = require('../settings.js'),
|
||||
settingsdb = require('../settingsdb.js'),
|
||||
userdb = require('../userdb.js'),
|
||||
users = require('../users.js');
|
||||
|
||||
const MANIFEST = {
|
||||
@@ -56,7 +58,7 @@ const DOMAIN = {
|
||||
const AUDIT_SOURCE = { ip: '1.2.3.4' };
|
||||
|
||||
const ADMIN = {
|
||||
id: 'admin123',
|
||||
id: null,
|
||||
username: 'admin123',
|
||||
password: 'secret123',
|
||||
email: 'admin@me.com',
|
||||
@@ -69,6 +71,21 @@ const ADMIN = {
|
||||
source: ''
|
||||
};
|
||||
|
||||
const USER = {
|
||||
id: 'userid',
|
||||
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
|
||||
};
|
||||
|
||||
const APP = {
|
||||
id: 'appid',
|
||||
appStoreId: 'appStoreId',
|
||||
@@ -101,6 +118,7 @@ exports = module.exports = {
|
||||
AUDIT_SOURCE,
|
||||
DOMAIN,
|
||||
MANIFEST,
|
||||
USER,
|
||||
APPSTORE_TOKEN: 'atoken'
|
||||
};
|
||||
|
||||
@@ -146,6 +164,7 @@ function setup(done) {
|
||||
},
|
||||
appdb.add.bind(null, APP.id, APP.appStoreId, APP.manifest, APP.location, APP.domain, APP.portBindings, APP),
|
||||
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, exports.APPSTORE_TOKEN), // appstore token
|
||||
userdb.add.bind(null, USER.id, USER),
|
||||
], done);
|
||||
}
|
||||
|
||||
|
||||
+1
-156
@@ -14,7 +14,6 @@ const appdb = require('../appdb.js'),
|
||||
database = require('../database'),
|
||||
domaindb = require('../domaindb'),
|
||||
expect = require('expect.js'),
|
||||
groupdb = require('../groupdb.js'),
|
||||
hat = require('../hat.js'),
|
||||
mailboxdb = require('../mailboxdb.js'),
|
||||
maildb = require('../maildb.js'),
|
||||
@@ -487,28 +486,6 @@ describe('database', function () {
|
||||
userdb.update(USER_0.id, { email: null }, function () {});
|
||||
}).to.throwError();
|
||||
});
|
||||
|
||||
it('cannot del non-existing user', function (done) {
|
||||
userdb.del(USER_0.id + USER_0.id, function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can del existing user', function (done) {
|
||||
userdb.del(USER_0.id, function (error) {
|
||||
expect(error).to.not.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('did remove the user', function (done) {
|
||||
userdb.count(function (error, count) {
|
||||
expect(count).to.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('apps', function () {
|
||||
@@ -592,6 +569,7 @@ describe('database', function () {
|
||||
|
||||
before(function (done) {
|
||||
async.series([
|
||||
database._clear,
|
||||
userdb.add.bind(null, USER_0.id, USER_0),
|
||||
domaindb.add.bind(null, DOMAIN_0.domain, DOMAIN_0)
|
||||
], done);
|
||||
@@ -1108,139 +1086,6 @@ describe('database', function () {
|
||||
|
||||
});
|
||||
|
||||
describe('groups', function () {
|
||||
before(function (done) {
|
||||
async.series([
|
||||
database.initialize,
|
||||
database._clear,
|
||||
userdb.add.bind(null, USER_0.id, USER_0),
|
||||
userdb.add.bind(null, USER_1.id, USER_1),
|
||||
userdb.add.bind(null, USER_2.id, USER_2)
|
||||
], done);
|
||||
});
|
||||
|
||||
var GROUP_ID_1 = 'foundersid';
|
||||
|
||||
it('can create a group', function (done) {
|
||||
groupdb.add(GROUP_ID_1, 'founders', 'ldap', function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get existing group', function (done) {
|
||||
groupdb.get(GROUP_ID_1, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.name).to.be('founders');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can add member to the group', function (done) {
|
||||
groupdb.addMember(GROUP_ID_1, USER_0.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot add invalid user to group', function (done) {
|
||||
groupdb.addMember(GROUP_ID_1, 'random', function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can set members', function (done) {
|
||||
groupdb.setMembers(GROUP_ID_1, [ USER_1.id, USER_2.id ], function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can list users of group', function (done) {
|
||||
groupdb.getMembers(GROUP_ID_1, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result).to.eql([ USER_1.id, USER_2.id ]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot delete non-existent member', function (done) {
|
||||
groupdb.removeMember(GROUP_ID_1, 'random', function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can remove existing member', function (done) {
|
||||
groupdb.removeMember(GROUP_ID_1, USER_1.id, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can getWithMembers', function (done) {
|
||||
groupdb.getWithMembers(GROUP_ID_1, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.name).to.be('founders');
|
||||
expect(result.userIds).to.eql([ USER_2.id ]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can create more groups for list order test', function (done) {
|
||||
groupdb.add(GROUP_ID_1 + 1, 'aaaa', 'ldap', function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
groupdb.add(GROUP_ID_1 + 2, 'zzzz', 'ldap', function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('can getAll', function (done) {
|
||||
groupdb.getAll(function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.length).to.be(3);
|
||||
expect(result[0].name).to.be('aaaa');
|
||||
expect(result[1].name).to.be('founders');
|
||||
expect(result[2].name).to.be('zzzz');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can getAllWithMembers', function (done) {
|
||||
groupdb.getAllWithMembers(function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.length).to.be(3);
|
||||
|
||||
expect(result[0].name).to.be('aaaa');
|
||||
expect(result[1].name).to.be('founders');
|
||||
expect(result[1].userIds).to.eql([ USER_2.id ]);
|
||||
expect(result[2].name).to.be('zzzz');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can set groups', function (done) {
|
||||
groupdb.setMembership(USER_0.id, [ GROUP_ID_1 ], function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get groups', function (done) {
|
||||
groupdb.getMembership(USER_0.id, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result).to.eql([ GROUP_ID_1 ]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('importFromFile', function () {
|
||||
before(function (done) {
|
||||
async.series([
|
||||
|
||||
@@ -5,23 +5,21 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
const async = require('async'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
database = require('../database.js'),
|
||||
constants = require('../constants.js'),
|
||||
expect = require('expect.js'),
|
||||
externalldap = require('../externalldap.js'),
|
||||
groupdb = require('../groupdb.js'),
|
||||
groups = require('../groups.js'),
|
||||
domains = require('../domains.js'),
|
||||
ldap = require('ldapjs'),
|
||||
mailboxdb = require('../mailboxdb.js'),
|
||||
mailer = require('../mailer.js'),
|
||||
server = require('../server.js'),
|
||||
settings = require('../settings.js'),
|
||||
superagent = require('superagent'),
|
||||
userdb = require('../userdb.js'),
|
||||
users = require('../users.js'),
|
||||
util = require('util'),
|
||||
_ = require('underscore');
|
||||
|
||||
var USERNAME = 'noBody';
|
||||
@@ -65,9 +63,7 @@ function cleanupUsers(done) {
|
||||
mailer._mailQueue = [];
|
||||
|
||||
async.series([
|
||||
groupdb._clear,
|
||||
userdb._clear,
|
||||
mailboxdb._clear,
|
||||
database._clear,
|
||||
], done);
|
||||
}
|
||||
|
||||
@@ -513,15 +509,13 @@ describe('External LDAP', function () {
|
||||
groupname: 'extGroup1'
|
||||
});
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getAll(function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(0);
|
||||
const result = await groups.getAll();
|
||||
expect(result.length).to.equal(0);
|
||||
|
||||
done();
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -539,15 +533,13 @@ describe('External LDAP', function () {
|
||||
it('succeeds with groups enabled', function (done) {
|
||||
gLdapGroups = [];
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getAll(function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(0);
|
||||
const result = await groups.getAll();
|
||||
expect(result.length).to.equal(0);
|
||||
|
||||
done();
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -556,15 +548,13 @@ describe('External LDAP', function () {
|
||||
groupname: 'extGroup1'
|
||||
});
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getAll(function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(1);
|
||||
const result = await groups.getAll();
|
||||
expect(result.length).to.equal(1);
|
||||
|
||||
done();
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -573,37 +563,28 @@ describe('External LDAP', function () {
|
||||
groupname: 'extGroup2'
|
||||
});
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getAll(function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(2);
|
||||
const result = await groups.getAll();
|
||||
expect(result.length).to.equal(2);
|
||||
|
||||
done();
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('does not create already existing group', function (done) {
|
||||
it('does not create already existing group', async function () {
|
||||
gLdapGroups.push({
|
||||
groupname: 'INTERNALgroup' // also tests lowercasing
|
||||
});
|
||||
|
||||
groups.create('internalgroup', '', function (error) {
|
||||
expect(error).to.equal(null);
|
||||
const externalldapSync = util.promisify(externalldap.sync);
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
expect(error).to.equal(null);
|
||||
await groups.add({ name: 'internalgroup' });
|
||||
await externalldapSync(function progress() {});
|
||||
|
||||
groups.getAll(function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(3);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
const result = await groups.getAll();
|
||||
expect(result.length).to.equal(3);
|
||||
});
|
||||
|
||||
it('adds users of groups', function (done) {
|
||||
@@ -612,19 +593,15 @@ describe('External LDAP', function () {
|
||||
member: gLdapUsers.slice(-2).map(function (u) { return `cn=${u.username},${LDAP_CONFIG.baseDn}`; })
|
||||
});
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getByName('nonemptygroup', function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
const result = await groups.getByName('nonemptygroup');
|
||||
expect(result).to.be.ok();
|
||||
|
||||
groups.getMembers(result.id, function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(2);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
const result2 = await groups.getMembers(result.id);
|
||||
expect(result2.length).to.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -634,19 +611,16 @@ describe('External LDAP', function () {
|
||||
member: gLdapUsers.map(function (u) { return `cn=${u.username},${LDAP_CONFIG.baseDn}`; }) // has 2 entries
|
||||
});
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getByName('nonemptygroup', function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
const result = await groups.getByName('nonemptygroup');
|
||||
expect(result).to.be.ok();
|
||||
|
||||
groups.getMembers(result.id, function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(2);
|
||||
const result2 = await groups.getMembers(result.id);
|
||||
expect(result2.length).to.equal(2);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -656,23 +630,18 @@ describe('External LDAP', function () {
|
||||
member: `cn=${gLdapUsers[0].username},${LDAP_CONFIG.baseDn}`
|
||||
});
|
||||
|
||||
externalldap.sync(function progress() {}, function (error) {
|
||||
externalldap.sync(function progress() {}, async function (error) {
|
||||
expect(error).to.equal(null);
|
||||
|
||||
groups.getByName('onemembergroup', function (error, result) {
|
||||
const result = await groups.getByName('onemembergroup');
|
||||
const result2 = await groups.getMembers(result.id);
|
||||
expect(result2.length).to.equal(1);
|
||||
|
||||
users.get(result2[0], function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.username).to.equal(gLdapUsers[0].username);
|
||||
|
||||
groups.getMembers(result.id, function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.length).to.equal(1);
|
||||
|
||||
users.get(result[0], function (error, result) {
|
||||
expect(error).to.equal(null);
|
||||
expect(result.username).to.equal(gLdapUsers[0].username);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+119
-309
@@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
+6
-12
@@ -133,20 +133,14 @@ function setup(done) {
|
||||
callback(null);
|
||||
});
|
||||
},
|
||||
function (callback) {
|
||||
groups.create(GROUP_NAME, '', function (error, result) {
|
||||
if (error) return callback(error);
|
||||
async function () {
|
||||
const result = await groups.add({ name: GROUP_NAME, source: '' });
|
||||
|
||||
GROUP_ID = result.id;
|
||||
|
||||
callback();
|
||||
});
|
||||
GROUP_ID = result.id;
|
||||
},
|
||||
function (callback) {
|
||||
async.series([
|
||||
groups.addMember.bind(null, GROUP_ID, USER_0.id),
|
||||
groups.addMember.bind(null, GROUP_ID, USER_1.id)
|
||||
], callback);
|
||||
async function () {
|
||||
await groups.addMember(GROUP_ID, USER_0.id);
|
||||
await groups.addMember(GROUP_ID, USER_1.id);
|
||||
}
|
||||
], done);
|
||||
}
|
||||
|
||||
@@ -35,31 +35,31 @@ describe('Volumes', function () {
|
||||
after(cleanup);
|
||||
|
||||
it('cannot add bad name', async function () {
|
||||
const [error] = await safe(volumes.add({ name: 'music/is', hostPath: '/tmp/music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
|
||||
const [error] = await safe(volumes.add({ name: 'music/is', hostPath: '/tmp/music', mountType: 'filesystem', mountOptions: {} }, AUDIT_SOURCE));
|
||||
if (!error) throw new Error('Expecting bad field error');
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot add bad path', async function () {
|
||||
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/tmp/music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
|
||||
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/tmp/music', mountType: 'filesystem', mountOptions: {} }, AUDIT_SOURCE));
|
||||
if (!error) throw new Error('Expecting bad field error');
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
let volume;
|
||||
it('can add volume', async function () {
|
||||
const id = await volumes.add({ name: 'music', hostPath: '/mnt/cloudron-test-music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE);
|
||||
const id = await volumes.add({ name: 'music', hostPath: '/mnt/cloudron-test-music', mountType: 'filesystem', mountOptions: {} }, AUDIT_SOURCE);
|
||||
expect(id).to.be.a('string');
|
||||
volume = { id, name: 'music', hostPath: '/mnt/cloudron-test-music' };
|
||||
});
|
||||
|
||||
it('cannot add duplicate path', async function () {
|
||||
const [error] = await safe(volumes.add({ name: 'music-dup', hostPath: '/mnt/cloudron-test-music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
|
||||
const [error] = await safe(volumes.add({ name: 'music-dup', hostPath: '/mnt/cloudron-test-music', mountType: 'filesystem', mountOptions: {} }, AUDIT_SOURCE));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
|
||||
it('cannot add duplicate name', async function () {
|
||||
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/mnt/cloudron-test-music2', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
|
||||
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/mnt/cloudron-test-music2', mountType: 'filesystem', mountOptions: {} }, AUDIT_SOURCE));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user