2016-02-07 20:34:05 -08:00
|
|
|
/* jslint node:true */
|
|
|
|
|
/* global it:false */
|
|
|
|
|
/* global describe:false */
|
|
|
|
|
/* global before:false */
|
|
|
|
|
/* global after:false */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
var async = require('async'),
|
2016-09-20 15:07:11 -07:00
|
|
|
constants = require('../constants.js'),
|
2016-02-08 09:41:21 -08:00
|
|
|
database = require('../database.js'),
|
2016-09-27 15:49:06 -07:00
|
|
|
DatabaseError = require('../databaseerror.js'),
|
2016-02-07 20:34:05 -08:00
|
|
|
expect = require('expect.js'),
|
|
|
|
|
groups = require('../groups.js'),
|
2016-02-08 09:41:21 -08:00
|
|
|
GroupError = groups.GroupError,
|
|
|
|
|
hat = require('hat'),
|
2016-09-27 15:49:06 -07:00
|
|
|
mailboxdb = require('../mailboxdb.js'),
|
2016-02-08 09:41:21 -08:00
|
|
|
userdb = require('../userdb.js');
|
2016-02-07 20:34:05 -08:00
|
|
|
|
2016-02-09 16:18:56 -08:00
|
|
|
var GROUP0_NAME = 'administrators',
|
2016-09-30 09:18:41 -07:00
|
|
|
group0Object;
|
2016-02-07 20:48:06 -08:00
|
|
|
|
2016-02-09 15:47:02 -08:00
|
|
|
var GROUP1_NAME = 'externs',
|
2016-09-30 09:18:41 -07:00
|
|
|
group1Object;
|
2016-02-09 15:47:02 -08:00
|
|
|
|
|
|
|
|
var USER_0 = {
|
|
|
|
|
id: 'uuid213',
|
|
|
|
|
username: 'uuid213',
|
|
|
|
|
password: 'secret',
|
|
|
|
|
email: 'safe@me.com',
|
|
|
|
|
admin: false,
|
|
|
|
|
salt: 'morton',
|
|
|
|
|
createdAt: 'sometime back',
|
|
|
|
|
modifiedAt: 'now',
|
|
|
|
|
resetToken: hat(256),
|
2016-05-06 15:16:22 +02:00
|
|
|
displayName: '',
|
|
|
|
|
showTutorial: false
|
2016-02-09 15:47:02 -08:00
|
|
|
};
|
|
|
|
|
|
2016-02-07 20:34:05 -08:00
|
|
|
function setup(done) {
|
|
|
|
|
// ensure data/config/mount paths
|
|
|
|
|
database.initialize(function (error) {
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
|
2016-02-07 20:48:06 -08:00
|
|
|
database._clear(done);
|
2016-02-07 20:34:05 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cleanup(done) {
|
|
|
|
|
database._clear(done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('Groups', function () {
|
|
|
|
|
before(setup);
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
|
|
it('cannot create group - too small', function (done) {
|
|
|
|
|
groups.create('a', function (error) {
|
2016-06-02 00:06:54 -07:00
|
|
|
expect(error.reason).to.be(GroupError.BAD_FIELD);
|
2016-02-07 20:34:05 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot create group - too big', function (done) {
|
2016-02-13 12:24:51 +01:00
|
|
|
groups.create(new Array(256).join('a'), function (error) {
|
2016-06-02 00:06:54 -07:00
|
|
|
expect(error.reason).to.be(GroupError.BAD_FIELD);
|
2016-02-07 20:34:05 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
it('cannot create group - bad name', function (done) {
|
|
|
|
|
groups.create('bad:name', function (error) {
|
2016-06-02 00:06:54 -07:00
|
|
|
expect(error.reason).to.be(GroupError.BAD_FIELD);
|
2016-02-08 09:41:21 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-09 15:47:02 -08:00
|
|
|
it('cannot create group - reserved', function (done) {
|
|
|
|
|
groups.create('users', function (error) {
|
2016-06-02 00:06:54 -07:00
|
|
|
expect(error.reason).to.be(GroupError.BAD_FIELD);
|
2016-02-09 15:47:02 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-09-30 12:25:42 -07:00
|
|
|
it('cannot create group - invalid', function (done) {
|
|
|
|
|
groups.create('cloudron-admin', function (error) {
|
|
|
|
|
expect(error.reason).to.be(GroupError.BAD_FIELD);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-07 20:34:05 -08:00
|
|
|
it('can create valid group', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.create(GROUP0_NAME, function (error, result) {
|
2016-02-07 20:48:06 -08:00
|
|
|
expect(error).to.be(null);
|
2016-09-30 09:18:41 -07:00
|
|
|
group0Object = result;
|
2016-02-07 20:48:06 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-09-27 15:49:06 -07:00
|
|
|
it('did create mailbox', function (done) {
|
|
|
|
|
mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), function (error, mailbox) {
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(mailbox.ownerType).to.be(mailboxdb.TYPE_GROUP);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
it('cannot add existing group', function (done) {
|
2016-02-09 16:18:56 -08:00
|
|
|
groups.create(GROUP0_NAME, function (error) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error.reason).to.be(GroupError.ALREADY_EXISTS);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-07 20:48:06 -08:00
|
|
|
it('cannot get invalid group', function (done) {
|
|
|
|
|
groups.get('sometrandom', function (error) {
|
|
|
|
|
expect(error.reason).to.be(GroupError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can get valid group', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.get(group0Object.id, function (error, group) {
|
2016-02-07 20:48:06 -08:00
|
|
|
expect(error).to.be(null);
|
2016-02-09 16:18:56 -08:00
|
|
|
expect(group.name).to.equal(GROUP0_NAME);
|
2016-02-07 20:48:06 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot delete invalid group', function (done) {
|
|
|
|
|
groups.remove('random', function (error) {
|
|
|
|
|
expect(error.reason).to.be(GroupError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can delete valid group', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.remove(group0Object.id, function (error) {
|
2016-02-07 20:34:05 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-09-27 15:49:06 -07:00
|
|
|
|
|
|
|
|
it('did delete mailbox', function (done) {
|
|
|
|
|
mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), function (error) {
|
|
|
|
|
expect(error.reason).to.be(DatabaseError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-02-07 20:34:05 -08:00
|
|
|
});
|
2016-02-08 09:41:21 -08:00
|
|
|
|
|
|
|
|
describe('Group membership', function () {
|
|
|
|
|
before(function (done) {
|
|
|
|
|
async.series([
|
|
|
|
|
setup,
|
2016-09-30 09:18:41 -07:00
|
|
|
function (next) {
|
|
|
|
|
groups.create(GROUP0_NAME, function (error, result) {
|
|
|
|
|
if (error) return next(error);
|
|
|
|
|
group0Object = result;
|
|
|
|
|
next();
|
|
|
|
|
});
|
|
|
|
|
},
|
2016-02-08 09:41:21 -08:00
|
|
|
userdb.add.bind(null, USER_0.id, USER_0)
|
|
|
|
|
], done);
|
|
|
|
|
});
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
|
|
it('cannot add non-existent user', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.addMember(group0Object.id, 'randomuser', function (error) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error.reason).to.be(GroupError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot add non-existent group', function (done) {
|
|
|
|
|
groups.addMember('randomgroup', USER_0.id, function (error) {
|
|
|
|
|
expect(error.reason).to.be(GroupError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 10:53:01 -08:00
|
|
|
it('isMember returns false', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.isMember(group0Object.id, USER_0.id, function (error, member) {
|
2016-02-08 10:53:01 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(member).to.be(false);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
it('can add member', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.addMember(group0Object.id, USER_0.id, function (error) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 10:53:01 -08:00
|
|
|
it('isMember returns true', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.isMember(group0Object.id, USER_0.id, function (error, member) {
|
2016-02-08 10:53:01 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(member).to.be(true);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
it('can get members', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.getMembers(group0Object.id, function (error, result) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(result.length).to.be(1);
|
|
|
|
|
expect(result[0]).to.be(USER_0.id);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-09-27 16:34:28 -07:00
|
|
|
it('can get list members', function (done) {
|
|
|
|
|
mailboxdb.getGroup(GROUP0_NAME.toLowerCase(), function (error, result) {
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(result.name).to.be(GROUP0_NAME.toLowerCase());
|
|
|
|
|
expect(result.ownerType).to.be(mailboxdb.TYPE_GROUP);
|
2016-09-30 09:18:41 -07:00
|
|
|
expect(result.ownerId).to.be(group0Object.id);
|
2016-09-27 16:34:28 -07:00
|
|
|
expect(result.members).to.eql([ USER_0.username ]);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
it('cannot get members of non-existent group', function (done) {
|
|
|
|
|
groups.getMembers('randomgroup', function (error, result) {
|
|
|
|
|
expect(result.length).to.be(0); // currently, we cannot differentiate invalid groups and empty groups
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot remove non-existent user', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.removeMember(group0Object.id, 'randomuser', function (error) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error.reason).to.be(GroupError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('cannot remove non-existent group', function (done) {
|
|
|
|
|
groups.removeMember('randomgroup', USER_0.id, function (error) {
|
|
|
|
|
expect(error.reason).to.be(GroupError.NOT_FOUND);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-09-29 15:15:25 -07:00
|
|
|
it('can set groups', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.setMembers(group0Object.id, [ USER_0.id ], function (error) {
|
2016-09-29 15:15:25 -07:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-02-08 09:41:21 -08:00
|
|
|
it('can remove member', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.removeMember(group0Object.id, USER_0.id, function (error) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('has no members', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.getMembers(group0Object.id, function (error, result) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(result.length).to.be(0);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can remove group with no members', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.remove(group0Object.id, function (error) {
|
2016-02-08 09:41:21 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-02-13 11:55:30 +01:00
|
|
|
|
|
|
|
|
it('can remove group with member', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.create(GROUP0_NAME, function (error, result) {
|
2016-02-13 11:55:30 +01:00
|
|
|
expect(error).to.eql(null);
|
2016-09-30 09:18:41 -07:00
|
|
|
group0Object = result;
|
2016-02-13 11:55:30 +01:00
|
|
|
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.addMember(group0Object.id, USER_0.id, function (error) {
|
2016-02-13 11:55:30 +01:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.remove(group0Object.id, function (error) {
|
2016-02-13 11:55:30 +01:00
|
|
|
expect(error).to.eql(null);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-02-08 09:41:21 -08:00
|
|
|
});
|
2016-02-09 15:47:02 -08:00
|
|
|
|
|
|
|
|
describe('Set user groups', function () {
|
|
|
|
|
before(function (done) {
|
|
|
|
|
async.series([
|
|
|
|
|
setup,
|
2016-09-30 09:18:41 -07:00
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
},
|
2016-02-09 15:47:02 -08:00
|
|
|
userdb.add.bind(null, USER_0.id, USER_0)
|
|
|
|
|
], done);
|
|
|
|
|
});
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
|
|
it('can set user to single group', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.setGroups(USER_0.id, [ group0Object.id ], function (error) {
|
2016-02-09 15:47:02 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
|
|
|
|
|
groups.getGroups(USER_0.id, function (error, groupIds) {
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(groupIds.length).to.be(1);
|
2016-09-30 09:18:41 -07:00
|
|
|
expect(groupIds[0]).to.be(group0Object.id);
|
2016-02-09 15:47:02 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can set user to multiple groups', function (done) {
|
2016-09-30 09:18:41 -07:00
|
|
|
groups.setGroups(USER_0.id, [ group0Object.id, group1Object.id ], function (error) {
|
2016-02-09 15:47:02 -08:00
|
|
|
expect(error).to.be(null);
|
|
|
|
|
|
|
|
|
|
groups.getGroups(USER_0.id, function (error, groupIds) {
|
|
|
|
|
expect(error).to.be(null);
|
|
|
|
|
expect(groupIds.length).to.be(2);
|
2016-09-30 09:18:41 -07:00
|
|
|
expect(groupIds.sort()).to.eql([ group0Object.id, group1Object.id ].sort());
|
2016-02-09 15:47:02 -08:00
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-02-13 12:24:51 +01:00
|
|
|
|
|
|
|
|
describe('Admin group', function () {
|
|
|
|
|
before(function (done) {
|
|
|
|
|
async.series([
|
|
|
|
|
setup,
|
|
|
|
|
userdb.add.bind(null, USER_0.id, USER_0)
|
|
|
|
|
], done);
|
|
|
|
|
});
|
|
|
|
|
after(cleanup);
|
|
|
|
|
|
|
|
|
|
it('cannot delete admin group ever', function (done) {
|
2016-09-20 15:07:11 -07:00
|
|
|
groups.remove(constants.ADMIN_GROUP_ID, function (error) {
|
2016-02-13 12:24:51 +01:00
|
|
|
expect(error.reason).to.equal(GroupError.NOT_ALLOWED);
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|