Files
cloudron-box/src/test/groups-test.js

199 lines
7.7 KiB
JavaScript
Raw Normal View History

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';
2021-06-28 15:15:28 -07:00
const BoxError = require('../boxerror.js'),
common = require('./common.js'),
2016-02-07 20:34:05 -08:00
expect = require('expect.js'),
groups = require('../groups.js'),
2021-06-28 15:15:28 -07:00
safe = require('safetydance');
2016-02-07 20:34:05 -08:00
describe('Groups', function () {
2021-08-13 10:41:10 -07:00
const { setup, cleanup, admin, user } = common;
2021-06-28 15:15:28 -07:00
2016-02-07 20:34:05 -08:00
before(setup);
after(cleanup);
2021-06-28 15:15:28 -07:00
describe('add/get/del', function () {
let group0Name = 'administrators', group0Object;
let group1Name = 'externs', group1Object;
it('cannot add group - too small', async function () {
const [error] = await safe(groups.add({ name: '' }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.BAD_FIELD);
2016-02-07 20:34:05 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add group - too big', async function () {
const [error] = await safe(groups.add({ name: new Array(256).join('a') }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.BAD_FIELD);
2016-02-07 20:34:05 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add group - bad name', async function () {
const [error] = await safe(groups.add({ name: 'bad:name' }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.BAD_FIELD);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add group - reserved', async function () {
const [error] = await safe(groups.add({ name: 'users' }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.BAD_FIELD);
2016-02-09 15:47:02 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add group - invalid', async function () {
const [error] = await safe(groups.add({ name: 'cloudron+admin' }));
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
2021-06-28 15:15:28 -07:00
it('cannot add group - invalid source', async function () {
const [error] = await safe(groups.add({ name: 'somegroup', source: 'unknownsource' }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
2021-06-28 15:15:28 -07:00
it('can add valid groups', async function () {
let [error, result] = await safe(groups.add({ name: group0Name }));
2016-02-07 20:48:06 -08:00
expect(error).to.be(null);
2016-09-30 09:18:41 -07:00
group0Object = result;
2021-06-28 15:15:28 -07:00
[error, result] = await safe(groups.add({ name: group1Name }));
expect(error).to.be(null);
group1Object = result;
2016-02-07 20:48:06 -08:00
});
2021-06-28 15:15:28 -07:00
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 }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
});
2021-06-28 15:15:28 -07:00
it('cannot add existing group', async function () {
const [error] = await safe(groups.add({name: group0Name, source: 'ldap' }));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot get invalid group', async function () {
const result = await groups.get('sometrandom');
expect(result).to.be(null);
2016-02-07 20:48:06 -08:00
});
2021-06-28 15:15:28 -07:00
it('can get valid group', async function () {
const result = await groups.get(group0Object.id);
expect(result.name).to.equal(group0Name);
2016-02-07 20:48:06 -08:00
});
2021-06-28 15:15:28 -07:00
it('isMember returns false', async function () {
2021-08-13 10:41:10 -07:00
const isMember = await groups.isMember(group0Object.id, admin.id);
2021-06-28 15:15:28 -07:00
expect(isMember).to.be(false);
2016-02-07 20:48:06 -08:00
});
2021-06-28 15:15:28 -07:00
it('can add member to the group', async function () {
2021-08-13 10:41:10 -07:00
await groups.addMember(group0Object.id, admin.id);
2016-02-07 20:34:05 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add same member to the group', async function () {
2021-08-13 10:41:10 -07:00
const [error] = await safe(groups.addMember(group0Object.id, admin.id));
2021-06-28 15:15:28 -07:00
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
});
2016-02-08 09:41:21 -08:00
2021-06-28 15:15:28 -07:00
it('isMember returns true', async function () {
2021-08-13 10:41:10 -07:00
const isMember = await groups.isMember(group0Object.id, admin.id);
2021-06-28 15:15:28 -07:00
expect(isMember).to.be(true);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add invalid user to group', async function () {
const [error] = await safe(groups.addMember(group0Object.id, 'random'));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot add non-existent group', async function () {
2021-08-13 10:41:10 -07:00
const [error] = await safe(groups.addMember('randomgroup', admin.id));
2021-06-28 15:15:28 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('can set members', async function () {
2021-08-13 10:41:10 -07:00
await groups.setMembers(group0Object.id, [ admin.id, user.id ]);
});
2021-06-28 15:15:28 -07:00
it('cannot set duplicate members', async function () {
2021-08-13 10:41:10 -07:00
const [error] = await safe(groups.setMembers(group0Object.id, [ admin.id, user.id, admin.id ]));
2021-06-28 15:15:28 -07:00
expect(error.reason).to.be(BoxError.CONFLICT);
2016-02-08 10:53:01 -08:00
});
2021-06-28 15:15:28 -07:00
it('can list users of group', async function () {
const result = await groups.getMembers(group0Object.id);
2021-08-13 15:49:59 -07:00
expect(result.sort()).to.eql([ admin.id, user.id ].sort());
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot list members of non-existent group', async function () {
const result = await groups.getMembers('randomgroup');
2016-02-08 09:41:21 -08:00
expect(result.length).to.be(0); // currently, we cannot differentiate invalid groups and empty groups
});
2021-06-28 15:15:28 -07:00
it('cannot delete non-existent member', async function () {
const [error] = await safe(groups.removeMember(group0Object.id, 'random'));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot remove member from non-existent group', async function () {
2021-08-13 10:41:10 -07:00
const [error] = await safe(groups.removeMember('randomgroup', admin.id));
2019-10-22 16:34:17 -07:00
expect(error.reason).to.be(BoxError.NOT_FOUND);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('can remove existing member', async function () {
2021-08-13 10:41:10 -07:00
await groups.removeMember(group0Object.id, user.id);
2016-09-29 15:15:25 -07:00
});
2021-06-28 15:15:28 -07:00
it('can getWithMembers', async function () {
const result = await groups.getWithMembers(group0Object.id);
expect(result.name).to.be(group0Name);
2021-08-13 10:41:10 -07:00
expect(result.userIds).to.eql([ admin.id ]);
});
2021-06-28 15:15:28 -07:00
it('can set groups', async function () {
2021-08-13 10:41:10 -07:00
await groups.setMembership(admin.id, [ group0Object.id ]);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('cannot set user to same group twice', async function () {
2021-08-13 10:41:10 -07:00
const [error] = await safe(groups.setMembership(admin.id, [ group0Object.id, group0Object.id ]));
2021-06-28 15:15:28 -07:00
expect(error.reason).to.be(BoxError.CONFLICT);
2016-02-08 09:41:21 -08:00
});
2021-06-28 15:15:28 -07:00
it('can set user to multiple groups', async function () {
2021-08-13 10:41:10 -07:00
await groups.setMembership(admin.id, [ group0Object.id, group1Object.id ]);
2016-02-08 09:41:21 -08:00
});
2016-02-13 11:55:30 +01:00
2021-06-28 15:15:28 -07:00
it('can get groups membership', async function () {
2021-08-13 10:41:10 -07:00
const groupIds = await groups.getMembership(admin.id);
2021-06-28 15:15:28 -07:00
expect(groupIds.length).to.be(2);
expect(groupIds.sort()).to.eql([ group0Object.id, group1Object.id ].sort());
2016-02-13 11:55:30 +01:00
});
2016-02-09 15:47:02 -08:00
2021-06-29 09:44:16 -07:00
it('can list', async function () {
const result = await groups.list();
2021-06-28 15:15:28 -07:00
expect(result.length).to.be(2);
expect(result[0].name).to.be(group0Name);
expect(result[1].name).to.be(group1Name);
2016-02-09 15:47:02 -08:00
});
2021-06-29 09:44:16 -07:00
it('can listWithMembers', async function () {
const result = await groups.listWithMembers();
2021-06-28 15:15:28 -07:00
expect(result.length).to.be(2);
expect(result[0].name).to.be(group0Name);
2021-08-13 10:41:10 -07:00
expect(result[1].userIds).to.eql([ admin.id ]);
2021-06-28 15:15:28 -07:00
expect(result[1].name).to.be(group1Name);
});
2021-06-28 15:15:28 -07:00
it('cannot delete invalid group', async function () {
const [error] = await safe(groups.remove('random'));
expect(error.reason).to.be(BoxError.NOT_FOUND);
});
2016-02-09 15:47:02 -08:00
2021-06-28 15:15:28 -07:00
it('can delete valid group', async function () {
2021-08-13 10:41:10 -07:00
await groups.setMembers(group0Object.id, [ admin.id, user.id ]); // ensure group has some members
2021-06-28 15:15:28 -07:00
await groups.remove(group0Object.id);
2016-02-09 15:47:02 -08:00
});
});
});