migrate tests to node:test
This commit is contained in:
+54
-57
@@ -1,15 +1,12 @@
|
||||
import { describe, it, before, after } from 'node:test';
|
||||
/* jslint node:true */
|
||||
|
||||
import BoxError from '../boxerror.js';
|
||||
import common from './common.js';
|
||||
import expect from 'expect.js';
|
||||
import assert from 'node:assert/strict';
|
||||
import groups from '../groups.js';
|
||||
import safe from 'safetydance';
|
||||
|
||||
/* global it:false */
|
||||
/* global describe:false */
|
||||
/* global before:false */
|
||||
/* global after:false */
|
||||
|
||||
describe('Groups', function () {
|
||||
const { setup, cleanup, admin, user, auditSource, app } = common;
|
||||
@@ -25,72 +22,72 @@ describe('Groups', function () {
|
||||
describe('add', function () {
|
||||
it('cannot add group - too small', async function () {
|
||||
const [error] = await safe(groups.add({ name: '' }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot add group - too big', async function () {
|
||||
const [error] = await safe(groups.add({ name: new Array(256).join('a') }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot add group - bad name', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'bad:name' }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot add group - reserved', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'users' }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot add group - invalid', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'cloudron+admin' }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot add group - invalid source', async function () {
|
||||
const [error] = await safe(groups.add({ name: 'somegroup', source: 'unknownsource' }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('can add valid groups', async function () {
|
||||
let [error, result] = await safe(groups.add({ name: group0Name }, auditSource));
|
||||
expect(error).to.be(null);
|
||||
assert.equal(error, null);
|
||||
group0Object = result;
|
||||
|
||||
[error, result] = await safe(groups.add({ name: group1Name}, auditSource));
|
||||
expect(error).to.be(null);
|
||||
assert.equal(error, null);
|
||||
group1Object = result;
|
||||
});
|
||||
|
||||
it('cannot add existing group with mixed case', async function () {
|
||||
const name = group0Name[0].toUpperCase() + group0Name.slice(1);
|
||||
const [error] = await safe(groups.add({ name }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
assert.equal(error.reason, BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
|
||||
it('cannot add existing group', async function () {
|
||||
const [error] = await safe(groups.add({name: group0Name }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
assert.equal(error.reason, BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', function () {
|
||||
it('cannot get invalid group', async function () {
|
||||
const result = await groups.get('sometrandom');
|
||||
expect(result).to.be(null);
|
||||
assert.equal(result, null);
|
||||
});
|
||||
|
||||
it('can get valid group', async function () {
|
||||
const result = await groups.get(group0Object.id);
|
||||
expect(result.name).to.equal(group0Name);
|
||||
assert.equal(result.name, group0Name);
|
||||
});
|
||||
});
|
||||
|
||||
describe('members', function () {
|
||||
it('isMember returns false', async function () {
|
||||
const isMember = await groups.isMember(group0Object.id, admin.id);
|
||||
expect(isMember).to.be(false);
|
||||
assert.equal(isMember, false);
|
||||
});
|
||||
|
||||
it('can set members', async function () {
|
||||
@@ -99,34 +96,34 @@ describe('Groups', function () {
|
||||
|
||||
it('cannot set duplicate members', async function () {
|
||||
const [error] = await safe(groups.setMembers(group0Object, [ admin.id, user.id, admin.id ], {}, auditSource));
|
||||
expect(error.reason).to.be(BoxError.CONFLICT);
|
||||
assert.equal(error.reason, BoxError.CONFLICT);
|
||||
});
|
||||
|
||||
it('can list users of group', async function () {
|
||||
const result = await groups.getMemberIds(group0Object.id);
|
||||
expect(result.sort()).to.eql([ admin.id, user.id ].sort());
|
||||
assert.deepEqual(result.sort(), [ admin.id, user.id ].sort());
|
||||
});
|
||||
|
||||
it('cannot list members of non-existent group', async function () {
|
||||
const result = await groups.getMemberIds('randomgroup');
|
||||
expect(result.length).to.be(0); // currently, we cannot differentiate invalid groups and empty groups
|
||||
assert.equal(result.length, 0); // currently, we cannot differentiate invalid groups and empty groups
|
||||
});
|
||||
|
||||
it('can getWithMembers', async function () {
|
||||
const result = await groups.getWithMembers(group0Object.id);
|
||||
expect(result.name).to.be(group0Name);
|
||||
expect(result.userIds.sort()).to.eql([ admin.id, user.id ].sort());
|
||||
assert.equal(result.name, group0Name);
|
||||
assert.deepEqual(result.userIds.sort(), [ admin.id, user.id ].sort());
|
||||
});
|
||||
|
||||
it('can set group membership', async function () {
|
||||
await groups.setLocalMembership(admin, [ group0Object.id ], auditSource);
|
||||
const groupIds = await groups._getMembership(admin.id);
|
||||
expect(groupIds.length).to.be(1);
|
||||
assert.equal(groupIds.length, 1);
|
||||
});
|
||||
|
||||
it('cannot set user to same group twice', async function () {
|
||||
const [error] = await safe(groups.setLocalMembership(admin, [ group0Object.id, group0Object.id ], auditSource));
|
||||
expect(error.reason).to.be(BoxError.CONFLICT);
|
||||
assert.equal(error.reason, BoxError.CONFLICT);
|
||||
});
|
||||
|
||||
it('can set user to multiple groups', async function () {
|
||||
@@ -135,32 +132,32 @@ describe('Groups', function () {
|
||||
|
||||
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());
|
||||
assert.equal(groupIds.length, 2);
|
||||
assert.deepEqual(groupIds.sort(), [ group0Object.id, group1Object.id ].sort());
|
||||
});
|
||||
});
|
||||
|
||||
describe('list', function () {
|
||||
it('can list', async function () {
|
||||
const result = await groups.list();
|
||||
expect(result.length).to.be(2);
|
||||
expect(result[0].name).to.be(group0Name);
|
||||
expect(result[1].name).to.be(group1Name);
|
||||
assert.equal(result.length, 2);
|
||||
assert.equal(result[0].name, group0Name);
|
||||
assert.equal(result[1].name, group1Name);
|
||||
});
|
||||
|
||||
it('can listWithMembers', async function () {
|
||||
const result = await groups.listWithMembers();
|
||||
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);
|
||||
assert.equal(result.length, 2);
|
||||
assert.equal(result[0].name, group0Name);
|
||||
assert.deepEqual(result[1].userIds, [ admin.id ]);
|
||||
assert.equal(result[1].name, group1Name);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete', function () {
|
||||
it('cannot delete invalid group', async function () {
|
||||
const [error] = await safe(groups.del({ id: 'random' }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
assert.equal(error.reason, BoxError.NOT_FOUND);
|
||||
});
|
||||
|
||||
it('can delete valid group', async function () {
|
||||
@@ -174,24 +171,24 @@ describe('Groups', function () {
|
||||
|
||||
before(async function () {
|
||||
const [error, result] = await safe(groups.add({ name: 'kootam' }, auditSource));
|
||||
expect(error).to.be(null);
|
||||
assert.equal(error, null);
|
||||
groupObject = result;
|
||||
});
|
||||
|
||||
it('cannot set empty group name', async function () {
|
||||
const [error] = await safe(groups.setName(groupObject, '', auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('cannot set bad group name', async function () {
|
||||
const [error] = await safe(groups.setName(groupObject, '!kootam', auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_FIELD);
|
||||
assert.equal(error.reason, BoxError.BAD_FIELD);
|
||||
});
|
||||
|
||||
it('can set group name', async function () {
|
||||
await groups.setName(groupObject, 'kootam2', auditSource);
|
||||
groupObject = await groups.get(groupObject.id);
|
||||
expect(groupObject.name).to.be('kootam2');
|
||||
assert.equal(groupObject.name, 'kootam2');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -200,55 +197,55 @@ describe('Groups', function () {
|
||||
|
||||
before(async function () {
|
||||
const [error, result] = await safe(groups.add({ name: 'kootam' }, auditSource));
|
||||
expect(error).to.be(null);
|
||||
assert.equal(error, null);
|
||||
groupObject = result;
|
||||
});
|
||||
|
||||
it('has no app access', async function () {
|
||||
expect(groupObject.appIds).to.eql([]);
|
||||
assert.deepEqual(groupObject.appIds, []);
|
||||
|
||||
const g1 = await groups.get(groupObject.id);
|
||||
expect(g1.appIds).to.eql([]);
|
||||
assert.deepEqual(g1.appIds, []);
|
||||
|
||||
const g2 = await groups.getByName(groupObject.name);
|
||||
expect(g2.appIds).to.eql([]);
|
||||
assert.deepEqual(g2.appIds, []);
|
||||
|
||||
const g3 = await groups.getWithMembers(groupObject.id);
|
||||
expect(g3.appIds).to.eql([]);
|
||||
assert.deepEqual(g3.appIds, []);
|
||||
});
|
||||
|
||||
it('set app access', async function () {
|
||||
await groups.setAllowedApps(groupObject, [ app.id ], auditSource);
|
||||
|
||||
const g1 = await groups.get(groupObject.id);
|
||||
expect(g1.appIds).to.eql([ app.id ]);
|
||||
assert.deepEqual(g1.appIds, [ app.id ]);
|
||||
|
||||
const g2 = await groups.getByName(groupObject.name);
|
||||
expect(g2.appIds).to.eql([ app.id ]);
|
||||
assert.deepEqual(g2.appIds, [ app.id ]);
|
||||
|
||||
const g3 = await groups.getWithMembers(groupObject.id);
|
||||
expect(g3.appIds).to.eql([ app.id ]);
|
||||
assert.deepEqual(g3.appIds, [ app.id ]);
|
||||
|
||||
const allGroups = await groups.listWithMembers();
|
||||
const g4 = allGroups.filter(g => g.id === groupObject.id)[0];
|
||||
expect(g4.appIds).to.eql([ app.id ]);
|
||||
assert.deepEqual(g4.appIds, [ app.id ]);
|
||||
});
|
||||
|
||||
it('cleared app access', async function () {
|
||||
await groups.setAllowedApps(groupObject, [ ], auditSource);
|
||||
|
||||
const g1 = await groups.get(groupObject.id);
|
||||
expect(g1.appIds).to.eql([ ]);
|
||||
assert.deepEqual(g1.appIds, [ ]);
|
||||
|
||||
const g2 = await groups.getByName(groupObject.name);
|
||||
expect(g2.appIds).to.eql([ ]);
|
||||
assert.deepEqual(g2.appIds, [ ]);
|
||||
|
||||
const g3 = await groups.getWithMembers(groupObject.id);
|
||||
expect(g3.appIds).to.eql([ ]);
|
||||
assert.deepEqual(g3.appIds, [ ]);
|
||||
|
||||
const allGroups = await groups.listWithMembers();
|
||||
const g4 = allGroups.filter(g => g.id === groupObject.id)[0];
|
||||
expect(g4.appIds).to.eql([ ]);
|
||||
assert.deepEqual(g4.appIds, [ ]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -261,17 +258,17 @@ describe('Groups', function () {
|
||||
|
||||
it('cannot change name', async function () {
|
||||
const [error] = await safe(groups.setName(ldapGroup, 'ldap-kootam2', auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_STATE);
|
||||
assert.equal(error.reason, BoxError.BAD_STATE);
|
||||
});
|
||||
|
||||
it('cannot set members', async function () {
|
||||
const [error] = await safe(groups.setMembers(ldapGroup, [ admin.id ], { skipSourceSkip: false }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_STATE);
|
||||
assert.equal(error.reason, BoxError.BAD_STATE);
|
||||
});
|
||||
|
||||
it('cannot set membership', async function () {
|
||||
const [error] = await safe(groups.setLocalMembership(admin, [ ldapGroup.id ], auditSource));
|
||||
expect(error.reason).to.be(BoxError.BAD_STATE);
|
||||
assert.equal(error.reason, BoxError.BAD_STATE);
|
||||
});
|
||||
|
||||
it('does not clear remote membership', async function () {
|
||||
@@ -279,8 +276,8 @@ describe('Groups', function () {
|
||||
await groups.setLocalMembership(admin, [ group1Object.id ], auditSource);
|
||||
|
||||
const groupIds = await groups._getMembership(admin.id);
|
||||
expect(groupIds.length).to.be(2);
|
||||
expect(groupIds.sort()).to.eql([ group1Object.id, ldapGroup.id ].sort());
|
||||
assert.equal(groupIds.length, 2);
|
||||
assert.deepEqual(groupIds.sort(), [ group1Object.id, ldapGroup.id ].sort());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user