From 700d301a59ef80363c5c7077a20cdf4b6141cc6a Mon Sep 17 00:00:00 2001 From: Tim Saunders Date: Thu, 22 Aug 2024 17:20:42 +0100 Subject: [PATCH] Fix queries unit tests --- package.json | 2 +- tests/unit/server/lib/queries.tests.js | 384 ++++++++++++------------- 2 files changed, 191 insertions(+), 195 deletions(-) diff --git a/package.json b/package.json index f5a0cf1b..404e32ff 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "serve:dev": "gulp run", "serve:prod": "cross-env NODE_ENV=production node index.js", "test-working": "npm run test:pre && cross-env NODE_ENV=test mocha --timeout 10000 tests/unit/mocha.js ./tests/unit/server/authorize.tests.js", - "test-dev": "npm run test:pre && cross-env NODE_ENV=test mocha --timeout 10000 tests/unit/mocha.js ./tests/unit/server/plugins/auth.tests.js", + "test-dev": "npm run test:pre && cross-env NODE_ENV=test mocha --timeout 10000 tests/unit/mocha.js ./tests/unit/server/lib/queries.tests.js", "test": "npm run test:pre && cross-env NODE_ENV=test nyc mocha --timeout 10000 tests/unit/mocha.js ./tests/unit/**/*.tests.js", "test:watch": "npm run test:pre && cross-env NODE_ENV=test mocha --timeout 10000 tests/unit/mocha.js ./tests/unit/**/*.tests.js --watch", "test:pre": "npm run test:clean", diff --git a/tests/unit/server/lib/queries.tests.js b/tests/unit/server/lib/queries.tests.js index e04fb0ef..80f11709 100644 --- a/tests/unit/server/lib/queries.tests.js +++ b/tests/unit/server/lib/queries.tests.js @@ -1,231 +1,231 @@ import uuid from 'node-uuid'; import Promise from 'bluebird'; import { expect } from 'chai'; - import { - getUserGroups, - getDynamicUserGroups, - getGroupExpanded + +getUserGroups, +getDynamicUserGroups, +getGroupExpanded } from '../../../../server/lib/queries'; const mockDatabase = (groups, roles, permissions) => ({ hash: uuid.v4(), getGroup: () => - new Promise((resolve) => { - resolve(groups[0]); - }), + new Promise((resolve) => { + resolve(groups[0]); + }), getGroups: () => - new Promise((resolve) => { - resolve(groups); - }), + new Promise((resolve) => { + resolve(groups); + }), getRoles: () => - new Promise((resolve) => { - resolve(roles); - }), + new Promise((resolve) => { + resolve(roles); + }), getPermissions: () => - new Promise((resolve) => { - resolve(permissions); - }) + new Promise((resolve) => { + resolve(permissions); + }) }); const mockConnections = (connections) => ({ hash: uuid.v4(), connections: { getAll: () => - new Promise((resolve) => { - resolve(connections); - }) + new Promise((resolve) => { + resolve(connections); + }) } }); describe('Queries', () => { describe('#getUserGroups', () => { - it('should return an empty array if user does not belong to any groups', (done) => { + it('should return an empty array if user does not belong to any groups', async () => { const db = mockDatabase([ - { _id: '123', name: 'Group 1', members: [ '111', '222', '333' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666' ] } + { _id: '123', name: 'Group 1', members: [ '111', '222', '333' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666' ] } ]); - getUserGroups(db, '777') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(0); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(db, '777'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(0); + } catch (err) { + throw err; + } }); - it('should group memberships if user belongs to groups', (done) => { + it('should group memberships if user belongs to groups', async () => { const db = mockDatabase([ - { _id: '123', name: 'Group 1', members: [ '111', '222', '333' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3', members: [ '777' ] } + { _id: '123', name: 'Group 1', members: [ '111', '222', '333' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3', members: [ '777' ] } ]); - getUserGroups(db, '777') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(2); - expect(groups[0].name).to.equal('Group 2'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(db, '777'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(2); + expect(groups[0].name).to.equal('Group 2'); + } catch (err) { + throw err; + } }); - it('should handle empty group memberships', (done) => { + it('should handle empty group memberships', async () => { const db = mockDatabase([ - { _id: '123', name: 'Group 1' }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3' } + { _id: '123', name: 'Group 1' }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3' } ]); - getUserGroups(db, '777') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(1); - expect(groups[0].name).to.equal('Group 2'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(db, '777'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(1); + expect(groups[0].name).to.equal('Group 2'); + } catch (err) { + throw err; + } }); - it('should handle nested groups', (done) => { + it('should handle nested groups', async () => { const db = mockDatabase([ - { _id: '123', name: 'Group 1', nested: [ '456' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3' } + { _id: '123', name: 'Group 1', nested: [ '456' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3' } ]); - getUserGroups(db, '777') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(2); - expect(groups[0].name).to.equal('Group 1'); - expect(groups[1].name).to.equal('Group 2'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(db, '777'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(2); + expect(groups[0].name).to.equal('Group 1'); + expect(groups[1].name).to.equal('Group 2'); + } catch (err) { + throw err; + } }); - it('should handle nested groups that dont belong to the current user', (done) => { + it('should handle nested groups that dont belong to the current user', async () => { const db = mockDatabase([ - { _id: '123', name: 'Group 1', nested: [ '789' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3' } + { _id: '123', name: 'Group 1', nested: [ '789' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3' } ]); - getUserGroups(db, '777') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(1); - expect(groups[0].name).to.equal('Group 2'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(db, '777'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(1); + expect(groups[0].name).to.equal('Group 2'); + } catch (err) { + throw err; + } }); - it('should handle nested groups (cyclic)', (done) => { + it('should handle nested groups (cyclic)', async () => { const db = mockDatabase([ - { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } + { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } ]); - getUserGroups(db, '777') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(3); - expect(groups[0].name).to.equal('Group 1'); - expect(groups[1].name).to.equal('Group 2'); - expect(groups[2].name).to.equal('Group 3'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(db, '777'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(3); + expect(groups[0].name).to.equal('Group 1'); + expect(groups[1].name).to.equal('Group 2'); + expect(groups[2].name).to.equal('Group 3'); + } catch (err) { + throw err; + } }); - it('should ignore existing group memberships if null', (done) => { + it('should ignore existing group memberships if null', async () => { const myDb = mockDatabase([ - { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } + { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } ]); - getUserGroups(myDb, '777', 'fabrikam-ad', null) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(3); - expect(groups[0].name).to.equal('Group 1'); - expect(groups[1].name).to.equal('Group 2'); - expect(groups[2].name).to.equal('Group 3'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(myDb, '777', 'fabrikam-ad', null); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(3); + expect(groups[0].name).to.equal('Group 1'); + expect(groups[1].name).to.equal('Group 2'); + expect(groups[2].name).to.equal('Group 3'); + } catch (err) { + throw err; + } }); - it('should ignore existing group memberships if undefined', (done) => { + it('should ignore existing group memberships if undefined', async () => { const myDb = mockDatabase([ - { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } + { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } ]); - getUserGroups(myDb, '777', 'fabrikam-ad', undefined) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(3); - expect(groups[0].name).to.equal('Group 1'); - expect(groups[1].name).to.equal('Group 2'); - expect(groups[2].name).to.equal('Group 3'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(myDb, '777', 'fabrikam-ad', undefined); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(3); + expect(groups[0].name).to.equal('Group 1'); + expect(groups[1].name).to.equal('Group 2'); + expect(groups[2].name).to.equal('Group 3'); + } catch (err) { + throw err; + } }); - it('should ignore existing group memberships if not an array', (done) => { + it('should ignore existing group memberships if not an array', async () => { const myDb = mockDatabase([ - { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, - { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, - { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } + { _id: '123', name: 'Group 1', nested: [ '456', '789' ] }, + { _id: '456', name: 'Group 2', members: [ '444', '555', '666', '777' ] }, + { _id: '789', name: 'Group 3', nested: [ '123', '456', '789' ] } ]); - getUserGroups(myDb, '777', 'fabrikam-ad', 'this is not right') - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(3); - expect(groups[0].name).to.equal('Group 1'); - expect(groups[1].name).to.equal('Group 2'); - expect(groups[2].name).to.equal('Group 3'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getUserGroups(myDb, '777', 'fabrikam-ad', 'this is not right'); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(3); + expect(groups[0].name).to.equal('Group 1'); + expect(groups[1].name).to.equal('Group 2'); + expect(groups[2].name).to.equal('Group 3'); + } catch (err) { + throw err; + } }); }); describe('#getDynamicUserGroups', () => { - it('should not run if connection is empty', (done) => { - getDynamicUserGroups(null, []) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(0); - done(); - }) - .catch((err) => done(err)); + it('should not run if connection is empty', async () => { + try { + const groups = await getDynamicUserGroups(null, []); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(0); + } catch (err) { + throw err; + } }); - it('should not run if existing group memberships are empty', (done) => { - getDynamicUserGroups('123', null) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(0); - done(); - }) - .catch((err) => done(err)); + it('should not run if existing group memberships are empty', async () => { + try { + const groups = await getDynamicUserGroups('123', null); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(0); + } catch (err) { + throw err; + } }); - it('should return empty if the current transaction does not match any groups', (done) => { + it('should return empty if the current transaction does not match any groups', async () => { mockConnections([ - { id: 'abc', name: 'my-ad' }, - { id: 'def', name: 'other-ad' } + { id: 'abc', name: 'my-ad' }, + { id: 'def', name: 'other-ad' } ]); // eslint-disable-line no-unused-vars const db = mockDatabase([ @@ -233,55 +233,55 @@ describe('Queries', () => { _id: '123', name: 'Group 1', mappings: [ - { _id: '12345', groupName: 'Domain Users', connectionName: 'abc' } + { _id: '12345', groupName: 'Domain Users', connectionName: 'abc' } ] }, { _id: '456', name: 'Group 2', mappings: [ - { _id: '67890', groupName: 'Domain Users', connectionName: 'def' } + { _id: '67890', groupName: 'Domain Users', connectionName: 'def' } ] } ]); - getDynamicUserGroups(db, 'abc', [ 'Domain Admins' ]) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(0); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getDynamicUserGroups(db, 'abc', [ 'Domain Admins' ]); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(0); + } catch (err) { + throw err; + } }); - it('should return empty if the current transaction does not match any groups', (done) => { + it('should return empty if the current transaction does not match any groups', async () => { const db = mockDatabase([ { _id: '123', name: 'Group 1', mappings: [ - { _id: '12345', groupName: 'Domain Users', connectionName: 'abc' } + { _id: '12345', groupName: 'Domain Users', connectionName: 'abc' } ] }, { _id: '456', name: 'Group 2', mappings: [ - { _id: '67890', groupName: 'Domain Users', connectionName: 'def' } + { _id: '67890', groupName: 'Domain Users', connectionName: 'def' } ] } ]); - getDynamicUserGroups(db, 'my-ad', [ 'Domain Admins' ]) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(0); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getDynamicUserGroups(db, 'my-ad', [ 'Domain Admins' ]); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(0); + } catch (err) { + throw err; + } }); - it('should mappings that match the current transaction', (done) => { + it('should mappings that match the current transaction', async () => { const db = mockDatabase([ { _id: '123', @@ -298,7 +298,7 @@ describe('Queries', () => { _id: '456', name: 'Group 2', mappings: [ - { _id: '67890', groupName: 'Domain Users', connectionName: 'def' }, + { _id: '67890', groupName: 'Domain Users', connectionName: 'def' }, { _id: '44444', groupName: 'Domain Admins', @@ -324,22 +324,22 @@ describe('Queries', () => { } ]); - getDynamicUserGroups(db, 'my-ad', [ 'Domain Admins' ]) - .then((groups) => { - expect(groups).to.be.instanceof(Array); - expect(groups.length).to.equal(2); - expect(groups[0].name).to.equal('Group 2'); - expect(groups[1].name).to.equal('Group 3'); - done(); - }) - .catch((err) => done(err)); + try { + const groups = await getDynamicUserGroups(db, 'my-ad', [ 'Domain Admins' ]); + expect(groups).to.be.instanceof(Array); + expect(groups.length).to.equal(2); + expect(groups[0].name).to.equal('Group 2'); + expect(groups[1].name).to.equal('Group 3'); + } catch (err) { + throw err; + } }); }); describe('#getGroupExpanded', () => { - it('should return empty if the current transaction does not match any groups', (done) => { + it('should return empty if the current transaction does not match any groups', async () => { const db = mockDatabase( - [ { _id: '123', name: 'Group 1', roles: [ 'r1', 'r2' ] } ], + [ { _id: '123', name: 'Group 1', roles: [ 'r1', 'r2' ] } ], [ { _id: 'r1', @@ -384,15 +384,11 @@ describe('Queries', () => { ] ); - getGroupExpanded(db, '123') - .then((group) => { - expect(group).to.be.instanceof(Object); - expect(group.name).to.equal('Group 1'); - expect(group.roles.length).to.equal(2); - expect(group.roles[0].permissions.length).to.equal(2); - done(); - }) - .catch((err) => done(err)); + const group = await getGroupExpanded(db, '123'); + expect(group).to.be.instanceof(Object); + expect(group.name).to.equal('Group 1'); + expect(group.roles.length).to.equal(2); + expect(group.roles[0].permissions.length).to.equal(2); }); }); });