From 68e1eb54354000f4d4a2e976de0e04edfd097734 Mon Sep 17 00:00:00 2001 From: Railag Date: Wed, 12 Dec 2018 13:36:28 +0300 Subject: [PATCH] test(core) unit tests for microservices & registry controllers (EWC-382) --- src/controllers/iofog-controller.js | 42 +- src/controllers/microservices-controller.js | 72 +- src/routes/iofog.js | 20 +- src/routes/microservices.js | 2 +- .../microservices-controller.test.js | 622 ++++++++++++++++++ .../controllers/registry-controller.test.js | 211 ++++++ 6 files changed, 901 insertions(+), 68 deletions(-) create mode 100644 test/src/controllers/microservices-controller.test.js create mode 100644 test/src/controllers/registry-controller.test.js diff --git a/src/controllers/iofog-controller.js b/src/controllers/iofog-controller.js index b68d47730..72d27e9c0 100644 --- a/src/controllers/iofog-controller.js +++ b/src/controllers/iofog-controller.js @@ -16,47 +16,47 @@ const AuthDecorator = require('../decorators/authorization-decorator'); const FogService = require('../services/iofog-service'); const qs = require('qs'); -async function _createFog(req, user) { +async function createFogEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const newFog = req.body; return await FogService.createFog(newFog, user, false) } -async function _updateFog(req, user) { +async function updateFogEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const updateFog = req.body; updateFog.uuid = req.params.uuid; return await FogService.updateFog(updateFog, user, false) } -async function _deleteFog(req, user) { +async function deleteFogEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const deleteFog = {}; deleteFog.uuid = req.params.uuid; return await FogService.deleteFog(deleteFog, user, false) } -async function _getFog(req, user) { +async function getFogEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const getFog = {}; getFog.uuid = req.params.uuid; return await FogService.getFogWithTransaction(getFog, user, false) } -async function _getFogList(req, user) { +async function getFogListEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.query)); - const query = qs.parse(req.query) + const query = qs.parse(req.query); return await FogService.getFogList(query.filters, user, false) } -async function _generateProvisioningKey(req, user) { +async function generateProvisionKeyEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const fog = {}; fog.uuid = req.params.uuid; return await FogService.generateProvisioningKey(fog, user, false) } -async function _setFogVersionCommand(req, user) { +async function setFogVersionCommandEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const fogVersionCommand = {}; fogVersionCommand.uuid = req.params.uuid; @@ -64,14 +64,14 @@ async function _setFogVersionCommand(req, user) { return await FogService.setFogVersionCommand(fogVersionCommand, user, false) } -async function _setFogRebootCommand(req, user) { +async function setFogRebootCommandEndPoint(req, user) { logger.info("Parameters:" + JSON.stringify(req.body)); const fog = {}; fog.uuid = req.params.uuid; return await FogService.setFogRebootCommand(fog, user, false) } -async function _getHalHardwareInfo(req, user) { +async function getHalHardwareInfoEndPoint(req, user) { const uuidObj = { uuid: req.params.uuid }; @@ -82,7 +82,7 @@ async function _getHalHardwareInfo(req, user) { } -async function _getHalUsbInfo(req, user) { +async function getHalUsbInfoEndPoint(req, user) { const uuidObj = { uuid: req.params.uuid }; @@ -93,14 +93,14 @@ async function _getHalUsbInfo(req, user) { } module.exports = { - createFog: AuthDecorator.checkAuthToken(_createFog), - updateFog: AuthDecorator.checkAuthToken(_updateFog), - deleteFog: AuthDecorator.checkAuthToken(_deleteFog), - getFog: AuthDecorator.checkAuthToken(_getFog), - getFogList: AuthDecorator.checkAuthToken(_getFogList), - generateProvisioningKey: AuthDecorator.checkAuthToken(_generateProvisioningKey), - setFogVersionCommand: AuthDecorator.checkAuthToken(_setFogVersionCommand), - setFogRebootCommand: AuthDecorator.checkAuthToken(_setFogRebootCommand), - getHalHardwareInfo: AuthDecorator.checkAuthToken(_getHalHardwareInfo), - getHalUsbInfo: AuthDecorator.checkAuthToken(_getHalUsbInfo) + createFogEndPoint: AuthDecorator.checkAuthToken(createFogEndPoint), + updateFogEndPoint: AuthDecorator.checkAuthToken(updateFogEndPoint), + deleteFogEndPoint: AuthDecorator.checkAuthToken(deleteFogEndPoint), + getFogEndPoint: AuthDecorator.checkAuthToken(getFogEndPoint), + getFogListEndPoint: AuthDecorator.checkAuthToken(getFogListEndPoint), + generateProvisioningKeyEndPoint: AuthDecorator.checkAuthToken(generateProvisionKeyEndPoint), + setFogVersionCommandEndPoint: AuthDecorator.checkAuthToken(setFogVersionCommandEndPoint), + setFogRebootCommandEndPoint: AuthDecorator.checkAuthToken(setFogRebootCommandEndPoint), + getHalHardwareInfoEndPoint: AuthDecorator.checkAuthToken(getHalHardwareInfoEndPoint), + getHalUsbInfoEndPoint: AuthDecorator.checkAuthToken(getHalUsbInfoEndPoint) }; \ No newline at end of file diff --git a/src/controllers/microservices-controller.js b/src/controllers/microservices-controller.js index 7cbfb0ae5..e5501be60 100644 --- a/src/controllers/microservices-controller.js +++ b/src/controllers/microservices-controller.js @@ -15,7 +15,7 @@ const logger = require('../logger'); const AuthDecorator = require('./../decorators/authorization-decorator'); const MicroservicesService = require('../services/microservices-service'); -const _createMicroservicesOnFogEndPoint = async function (req, user) { +const createMicroserviceOnFogEndPoint = async function (req, user) { const microservice = req.body; logger.info("Parameters:" + JSON.stringify(microservice)); @@ -23,7 +23,7 @@ const _createMicroservicesOnFogEndPoint = async function (req, user) { return await MicroservicesService.createMicroserviceOnFog(microservice, user, false) }; -const _getMicroserviceEndPoint = async function (req, user) { +const getMicroserviceEndPoint = async function (req, user) { const microserviceUuid = req.params.uuid; logger.info("Microservice uuid:" + JSON.stringify(microserviceUuid)); @@ -31,7 +31,7 @@ const _getMicroserviceEndPoint = async function (req, user) { return await MicroservicesService.getMicroservice(microserviceUuid, user, false) }; -const _updateMicroserviceEndPoint = async function (req, user) { +const updateMicroserviceEndPoint = async function (req, user) { const microservice = req.body; const microserviceUuid = req.params.uuid; @@ -41,7 +41,7 @@ const _updateMicroserviceEndPoint = async function (req, user) { return await MicroservicesService.updateMicroservice(microserviceUuid, microservice, user, false) }; -const _deleteMicroserviceEndPoint = async function (req, user) { +const deleteMicroserviceEndPoint = async function (req, user) { const microserviceUuid = req.params.uuid; const microserviceData = req.body || {}; logger.info("Microservice uuid:" + JSON.stringify(microserviceUuid)); @@ -50,7 +50,7 @@ const _deleteMicroserviceEndPoint = async function (req, user) { return await MicroservicesService.deleteMicroservice(microserviceUuid, microserviceData, user, false) }; -const _getMicroservicesByFlowEndPoint = async function (req, user) { +const getMicroservicesByFlowEndPoint = async function (req, user) { const flowId = req.query.flowId; logger.info("Flow id:" + flowId); @@ -58,35 +58,35 @@ const _getMicroservicesByFlowEndPoint = async function (req, user) { return await MicroservicesService.listMicroservices(flowId, user, false) }; -const _createMicroserviceRouteEndPoint = async function (req, user) { +const createMicroserviceRouteEndPoint = async function (req, user) { const sourceUuid = req.params.uuid; - const distUuid = req.params.receiverUuid; - logger.info(`Creating route from ${sourceUuid} to ${distUuid}`); - return await MicroservicesService.createRoute(sourceUuid, distUuid, user, false) -} + const destUuid = req.params.receiverUuid; + logger.info(`Creating route from ${sourceUuid} to ${destUuid}`); + return await MicroservicesService.createRoute(sourceUuid, destUuid, user, false) +}; -const _deleteMicroserviceRouteEndPoint = async function (req, user) { +const deleteMicroserviceRouteEndPoint = async function (req, user) { const sourceUuid = req.params.uuid; - const distUuid = req.params.receiverUuid; - logger.info(`Creating route from ${sourceUuid} to ${distUuid}`); - return await MicroservicesService.deleteRoute(sourceUuid, distUuid, user, false) -} + const destUuid = req.params.receiverUuid; + logger.info(`Creating route from ${sourceUuid} to ${destUuid}`); + return await MicroservicesService.deleteRoute(sourceUuid, destUuid, user, false) +}; -const _createMicroservicePortMappingEndPoint = async function (req, user) { +const createMicroservicePortMappingEndPoint = async function (req, user) { const uuid = req.params.uuid; const portMappingData = req.body; logger.info(`Creating port mapping for ${uuid}`); return await MicroservicesService.createPortMapping(uuid, portMappingData, user, false) -} +}; -const _deleteMicroservicePortMappingEndPoint = async function (req, user) { +const deleteMicroservicePortMappingEndPoint = async function (req, user) { const uuid = req.params.uuid; const internalPort = req.params.internalPort; logger.info(`Deleting port mapping for ${uuid}`); return await MicroservicesService.deletePortMapping(uuid, internalPort, user, false) -} +}; -const _listMicroservicePortMappingsEndPoint = async function (req, user) { +const listMicroservicePortMappingsEndPoint = async function (req, user) { const uuid = req.params.uuid; logger.info(`Getting all port mappings for ${uuid}`); const ports = await MicroservicesService.listMicroservicePortMappings(uuid, user, false); @@ -95,7 +95,7 @@ const _listMicroservicePortMappingsEndPoint = async function (req, user) { } }; -const _createMicroserviceVolumeMappingEndPoint = async function (req, user) { +const createMicroserviceVolumeMappingEndPoint = async function (req, user) { const microserviceUuid = req.params.uuid; const volumeMappingData = req.body; logger.info(`Creating volume mapping for ${microserviceUuid}`); @@ -105,7 +105,7 @@ const _createMicroserviceVolumeMappingEndPoint = async function (req, user) { } }; -const _listMicroserviceVolumeMappingsEndPoint = async function (req, user) { +const listMicroserviceVolumeMappingsEndPoint = async function (req, user) { const uuid = req.params.uuid; logger.info(`Getting all volume mappings for ${uuid}`); const volumeMappings = await MicroservicesService.listVolumeMappings(uuid, user, false); @@ -114,7 +114,7 @@ const _listMicroserviceVolumeMappingsEndPoint = async function (req, user) { } }; -const _deleteMicroserviceVolumeMappingEndPoint = async function (req, user) { +const deleteMicroserviceVolumeMappingEndPoint = async function (req, user) { const uuid = req.params.uuid; const id = req.params.id; logger.info(`Deleting volume mapping ${id} for ${uuid}`); @@ -122,17 +122,17 @@ const _deleteMicroserviceVolumeMappingEndPoint = async function (req, user) { }; module.exports = { - createMicroservicesOnFogEndPoint: AuthDecorator.checkAuthToken(_createMicroservicesOnFogEndPoint), - getMicroserviceEndPoint: AuthDecorator.checkAuthToken(_getMicroserviceEndPoint), - updateMicroserviceEndPoint: AuthDecorator.checkAuthToken(_updateMicroserviceEndPoint), - deleteMicroserviceEndPoint: AuthDecorator.checkAuthToken(_deleteMicroserviceEndPoint), - getMicroservicesByFlowEndPoint: AuthDecorator.checkAuthToken(_getMicroservicesByFlowEndPoint), - createMicroserviceRouteEndPoint: AuthDecorator.checkAuthToken(_createMicroserviceRouteEndPoint), - deleteMicroserviceRouteEndPoint: AuthDecorator.checkAuthToken(_deleteMicroserviceRouteEndPoint), - createMicroservicePortMappingEndPoint: AuthDecorator.checkAuthToken(_createMicroservicePortMappingEndPoint), - deleteMicroservicePortMappingEndPoint: AuthDecorator.checkAuthToken(_deleteMicroservicePortMappingEndPoint), - getMicroservicePortMappingListEndPoint: AuthDecorator.checkAuthToken(_listMicroservicePortMappingsEndPoint), - createMicroserviceVolumeMappingEndPoint: AuthDecorator.checkAuthToken(_createMicroserviceVolumeMappingEndPoint), - listMicroserviceVolumeMappingsEndPoint: AuthDecorator.checkAuthToken(_listMicroserviceVolumeMappingsEndPoint), - deleteMicroserviceVolumeMappingEndPoint: AuthDecorator.checkAuthToken(_deleteMicroserviceVolumeMappingEndPoint) + createMicroserviceOnFogEndPoint: AuthDecorator.checkAuthToken(createMicroserviceOnFogEndPoint), + getMicroserviceEndPoint: AuthDecorator.checkAuthToken(getMicroserviceEndPoint), + updateMicroserviceEndPoint: AuthDecorator.checkAuthToken(updateMicroserviceEndPoint), + deleteMicroserviceEndPoint: AuthDecorator.checkAuthToken(deleteMicroserviceEndPoint), + getMicroservicesByFlowEndPoint: AuthDecorator.checkAuthToken(getMicroservicesByFlowEndPoint), + createMicroserviceRouteEndPoint: AuthDecorator.checkAuthToken(createMicroserviceRouteEndPoint), + deleteMicroserviceRouteEndPoint: AuthDecorator.checkAuthToken(deleteMicroserviceRouteEndPoint), + createMicroservicePortMappingEndPoint: AuthDecorator.checkAuthToken(createMicroservicePortMappingEndPoint), + deleteMicroservicePortMappingEndPoint: AuthDecorator.checkAuthToken(deleteMicroservicePortMappingEndPoint), + getMicroservicePortMappingListEndPoint: AuthDecorator.checkAuthToken(listMicroservicePortMappingsEndPoint), + createMicroserviceVolumeMappingEndPoint: AuthDecorator.checkAuthToken(createMicroserviceVolumeMappingEndPoint), + listMicroserviceVolumeMappingsEndPoint: AuthDecorator.checkAuthToken(listMicroserviceVolumeMappingsEndPoint), + deleteMicroserviceVolumeMappingEndPoint: AuthDecorator.checkAuthToken(deleteMicroserviceVolumeMappingEndPoint) }; \ No newline at end of file diff --git a/src/routes/iofog.js b/src/routes/iofog.js index 2853e97ed..9fa549444 100644 --- a/src/routes/iofog.js +++ b/src/routes/iofog.js @@ -32,7 +32,7 @@ module.exports = [ } ]; - const getFogList = ResponseDecorator.handleErrors(FogController.getFogList, successCode, errCodes); + const getFogList = ResponseDecorator.handleErrors(FogController.getFogListEndPoint, successCode, errCodes); const responseObject = await getFogList(req); res @@ -56,7 +56,7 @@ module.exports = [ } ]; - const createFog = ResponseDecorator.handleErrors(FogController.createFog, successCode, errCodes); + const createFog = ResponseDecorator.handleErrors(FogController.createFogEndPoint, successCode, errCodes); const responseObject = await createFog(req); res @@ -84,7 +84,7 @@ module.exports = [ } ]; - const updateFog = ResponseDecorator.handleErrors(FogController.updateFog, successCode, errCodes); + const updateFog = ResponseDecorator.handleErrors(FogController.updateFogEndPoint, successCode, errCodes); const responseObject = await updateFog(req); res @@ -108,7 +108,7 @@ module.exports = [ } ]; - const deleteFog = ResponseDecorator.handleErrors(FogController.deleteFog, successCode, errCodes); + const deleteFog = ResponseDecorator.handleErrors(FogController.deleteFogEndPoint, successCode, errCodes); const responseObject = await deleteFog(req); res @@ -132,7 +132,7 @@ module.exports = [ } ]; - const getFog = ResponseDecorator.handleErrors(FogController.getFog, successCode, errCodes); + const getFog = ResponseDecorator.handleErrors(FogController.getFogEndPoint, successCode, errCodes); const responseObject = await getFog(req); res @@ -156,7 +156,7 @@ module.exports = [ } ]; - const generateFogProvisioningKey = ResponseDecorator.handleErrors(FogController.generateProvisioningKey, successCode, errCodes); + const generateFogProvisioningKey = ResponseDecorator.handleErrors(FogController.generateProvisioningKeyEndPoint, successCode, errCodes); const responseObject = await generateFogProvisioningKey(req); res @@ -184,7 +184,7 @@ module.exports = [ } ]; - const setFogVersionCommand = ResponseDecorator.handleErrors(FogController.setFogVersionCommand, successCode, errCodes); + const setFogVersionCommand = ResponseDecorator.handleErrors(FogController.setFogVersionCommandEndPoint, successCode, errCodes); const responseObject = await setFogVersionCommand(req); res @@ -213,7 +213,7 @@ module.exports = [ ]; - const setFogRebootCommand = ResponseDecorator.handleErrors(FogController.setFogRebootCommand, successCode, errCodes); + const setFogRebootCommand = ResponseDecorator.handleErrors(FogController.setFogRebootCommandEndPoint, successCode, errCodes); const responseObject = await setFogRebootCommand(req); res @@ -237,7 +237,7 @@ module.exports = [ } ]; - const getHalHardwareInfo = ResponseDecorator.handleErrors(FogController.getHalHardwareInfo, successCode, errCodes); + const getHalHardwareInfo = ResponseDecorator.handleErrors(FogController.getHalHardwareInfoEndPoint, successCode, errCodes); const responseObject = await getHalHardwareInfo(req); res @@ -261,7 +261,7 @@ module.exports = [ } ]; - const getHalUsbInfo = ResponseDecorator.handleErrors(FogController.getHalUsbInfo, successCode, errCodes); + const getHalUsbInfo = ResponseDecorator.handleErrors(FogController.getHalUsbInfoEndPoint, successCode, errCodes); const responseObject = await getHalUsbInfo(req); res diff --git a/src/routes/microservices.js b/src/routes/microservices.js index 6dc6fd813..5a8c55106 100644 --- a/src/routes/microservices.js +++ b/src/routes/microservices.js @@ -54,7 +54,7 @@ module.exports = [ } ]; - const createMicroservicesOnFogEndPoint = ResponseDecorator.handleErrors(MicroservicesController.createMicroservicesOnFogEndPoint, successCode, errorCodes); + const createMicroservicesOnFogEndPoint = ResponseDecorator.handleErrors(MicroservicesController.createMicroserviceOnFogEndPoint, successCode, errorCodes); const responseObject = await createMicroservicesOnFogEndPoint(req); res diff --git a/test/src/controllers/microservices-controller.test.js b/test/src/controllers/microservices-controller.test.js new file mode 100644 index 000000000..405a3625c --- /dev/null +++ b/test/src/controllers/microservices-controller.test.js @@ -0,0 +1,622 @@ +const {expect} = require('chai'); +const sinon = require('sinon'); + +const MicroservicesController = require('../../../src/controllers/microservices-controller'); +const MicroservicesService = require('../../../src/services/microservices-service'); + +describe('Microservices Controller', () => { + def('subject', () => MicroservicesController); + def('sandbox', () => sinon.createSandbox()); + + afterEach(() => $sandbox.restore()); + + describe('.createMicroserviceOnFogEndPoint()', () => { + def('user', () => 'user!'); + + def('name', () => 'testName'); + def('config', () => "{}"); + def('catalogItemId', () => 5); + def('flowId', () => 1); + def('iofogUuid', () => "testUuid"); + def('rootHostAccess', () => true); + def('logSize', () => 15); + def('volumeMappings', () => [{ + hostDestination: "/var/dest", + containerDestination: "/var/dest", + accessMode: true + }]); + def('ports', () => [ + { + internal: 15, + external: 55, + publicMode: true + } + ]); + def('routes', () => [ + "testAnotherUuid" + ]); + + def('req', () => ({ + body: { + name: $name, + config: $config, + catalogItemId: $catalogItemId, + flowId: $flowId, + iofogUuid: $iofogUuid, + rootHostAccess: $rootHostAccess, + logSize: $logSize, + volumeMappings: $volumeMappings, + ports: $ports, + routes: $routes + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.createMicroserviceOnFogEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'createMicroserviceOnFog').returns($response); + }); + + it('calls MicroservicesService.createMicroserviceOnFog with correct args', async () => { + await $subject; + expect(MicroservicesService.createMicroserviceOnFog).to.have.been.calledWith({ + name: $name, + config: $config, + catalogItemId: $catalogItemId, + flowId: $flowId, + iofogUuid: $iofogUuid, + rootHostAccess: $rootHostAccess, + logSize: $logSize, + volumeMappings: $volumeMappings, + ports: $ports, + routes: $routes + }, $user, false); + }); + + context('when MicroservicesService#createMicroserviceOnFog fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#createMicroserviceOnFog succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.getMicroserviceEndPoint()', () => { + def('user', () => 'user!'); + + def('uuid', () => 'testUuid'); + + def('req', () => ({ + params: { + uuid: $uuid + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getMicroserviceEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'getMicroservice').returns($response); + }); + + it('calls MicroservicesService.getMicroservice with correct args', async () => { + await $subject; + expect(MicroservicesService.getMicroservice).to.have.been.calledWith($uuid, $user, false) + }); + + context('when MicroservicesService#getMicroservice fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#getMicroservice succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + + }); + + describe('.updateMicroserviceEndPoint()', () => { + def('user', () => 'user!'); + def('uuid', () => 'newTestUuid'); + + def('name', () => 'testName'); + def('config', () => "{}"); + def('rebuild', () => true); + def('iofogUuid', () => "testUuid"); + def('rootHostAccess', () => true); + def('logSize', () => 15); + def('volumeMappings', () => [{ + hostDestination: "/var/dest", + containerDestination: "/var/dest", + accessMode: true + }]); + + def('req', () => ({ + params: { + uuid: $uuid + }, + body: { + name: $name, + config: $config, + rebuild: $rebuild, + iofogUuid: $iofogUuid, + rootHostAccess: $rootHostAccess, + logSize: $logSize, + volumeMappings: $volumeMappings + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateMicroserviceEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'updateMicroservice').returns($response); + }); + + it('calls MicroservicesService.updateMicroservice with correct args', async () => { + await $subject; + expect(MicroservicesService.updateMicroservice).to.have.been.calledWith($uuid, { + name: $name, + config: $config, + rebuild: $rebuild, + iofogUuid: $iofogUuid, + rootHostAccess: $rootHostAccess, + logSize: $logSize, + volumeMappings: $volumeMappings + }, $user, false); + }); + + context('when MicroservicesService#updateMicroservice fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#updateMicroservice succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.deleteMicroserviceEndPoint()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + + def('withCleanup', () => 'withCleanup'); + + def('req', () => ({ + params: { + uuid: $uuid + }, + body: { + withCleanup: $withCleanup + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteMicroserviceEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'deleteMicroservice').returns($response); + }); + + it('calls MicroservicesService.deleteMicroservice with correct args', async () => { + await $subject; + expect(MicroservicesService.deleteMicroservice).to.have.been.calledWith($uuid, { + withCleanup: $withCleanup + }, $user, false); + }); + + context('when MicroservicesService#deleteMicroservice fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#deleteMicroservice succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.getMicroservicesByFlowEndPoint()', () => { + def('user', () => 'user!'); + def('flowId', () => 1); + + def('req', () => ({ + query: { + flowId: $flowId + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getMicroservicesByFlowEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'listMicroservices').returns($response); + }); + + it('calls MicroservicesService.listMicroservices with correct args', async () => { + await $subject; + expect(MicroservicesService.listMicroservices).to.have.been.calledWith($flowId, $user, false); + }); + + context('when MicroservicesService#listMicroservices fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#listMicroservices succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.createMicroserviceRouteEndPoint()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + def('receiverUuid', () => 'testReceiverUuid'); + + def('req', () => ({ + params: { + uuid: $uuid, + receiverUuid: $receiverUuid + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.createMicroserviceRouteEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'createRoute').returns($response); + }); + + it('calls MicroservicesService.createRoute with correct args', async () => { + await $subject; + expect(MicroservicesService.createRoute).to.have.been.calledWith($uuid, $receiverUuid, $user, false); + }); + + context('when MicroservicesService#createRoute fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#createRoute succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('deleteMicroserviceRouteEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + def('receiverUuid', () => 'testReceiverUuid'); + + def('req', () => ({ + params: { + uuid: $uuid, + receiverUuid: $receiverUuid + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteMicroserviceRouteEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'deleteRoute').returns($response); + }); + + it('calls MicroservicesService.deleteRoute with correct args', async () => { + await $subject; + expect(MicroservicesService.deleteRoute).to.have.been.calledWith($uuid, $receiverUuid, $user, false); + }); + + context('when MicroservicesService#deleteRoute fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#deleteRoute succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('createMicroservicePortMappingEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + def('internal', () => 55); + def('external', () => 66); + def('publicMode', () => true); + + def('req', () => ({ + params: { + uuid: $uuid + }, + body: { + internal: $internal, + external: $external, + publicMode: $publicMode + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.createMicroservicePortMappingEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'createPortMapping').returns($response); + }); + + it('calls MicroservicesService.createPortMapping with correct args', async () => { + await $subject; + expect(MicroservicesService.createPortMapping).to.have.been.calledWith($uuid, { + internal: $internal, + external: $external, + publicMode: $publicMode + }, $user, false); + }); + + context('when MicroservicesService#createPortMapping fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#createPortMapping succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('deleteMicroservicePortMappingEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + def('internalPort', () => 55); + + def('req', () => ({ + params: { + uuid: $uuid, + internalPort: $internalPort + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteMicroservicePortMappingEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'deletePortMapping').returns($response); + }); + + it('calls MicroservicesService.deletePortMapping with correct args', async () => { + await $subject; + expect(MicroservicesService.deletePortMapping).to.have.been.calledWith($uuid, $internalPort, $user, false); + }); + + context('when MicroservicesService#deletePortMapping fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#deletePortMapping succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getMicroservicePortMappingListEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + + def('req', () => ({ + params: { + uuid: $uuid + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getMicroservicePortMappingListEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'listMicroservicePortMappings').returns($response); + }); + + it('calls MicroservicesService.listMicroservicePortMappings with correct args', async () => { + await $subject; + expect(MicroservicesService.listMicroservicePortMappings).to.have.been.calledWith($uuid, $user, false); + }); + + context('when MicroservicesService#listMicroservicePortMappings fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#listMicroservicePortMappings succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.have.property('ports'); + }) + }) + }); + + describe('createMicroserviceVolumeMappingEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + + def('hostDestination', () => '/var/dest'); + def('containerDestination', () => '/var/dest'); + def('accessMode', () => 'rw'); + + def('req', () => ({ + params: { + uuid: $uuid + }, + body: { + hostDestination: $hostDestination, + containerDestination: $containerDestination, + accessMode: $accessMode + } + })); + def('response', () => Promise.resolve({id: 15})); + def('subject', () => $subject.createMicroserviceVolumeMappingEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'createVolumeMapping').returns($response); + }); + + it('calls MicroservicesService.createVolumeMapping with correct args', async () => { + await $subject; + expect(MicroservicesService.createVolumeMapping).to.have.been.calledWith($uuid, { + hostDestination: $hostDestination, + containerDestination: $containerDestination, + accessMode: $accessMode + }, $user, false); + }); + + context('when MicroservicesService#createVolumeMapping fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#createVolumeMapping succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.have.property('id'); + }) + }) + }); + + describe('listMicroserviceVolumeMappingsEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + + def('req', () => ({ + params: { + uuid: $uuid + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.listMicroserviceVolumeMappingsEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'listVolumeMappings').returns($response); + }); + + it('calls MicroservicesService.listVolumeMappings with correct args', async () => { + await $subject; + expect(MicroservicesService.listVolumeMappings).to.have.been.calledWith($uuid, $user, false); + }); + + context('when MicroservicesService#listVolumeMappings fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#listVolumeMappings succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.have.property('volumeMappings'); + }) + }) + }); + + describe('deleteMicroserviceVolumeMappingEndPoint.()', () => { + def('user', () => 'user!'); + def('uuid', () => 'testUuid'); + def('id', () => 35); + + def('req', () => ({ + params: { + uuid: $uuid, + id: $id + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteMicroserviceVolumeMappingEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(MicroservicesService, 'deleteVolumeMapping').returns($response); + }); + + it('calls MicroservicesService.deleteVolumeMapping with correct args', async () => { + await $subject; + expect(MicroservicesService.deleteVolumeMapping).to.have.been.calledWith($uuid, $id, $user, false); + }); + + context('when MicroservicesService#deleteVolumeMapping fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when MicroservicesService#deleteVolumeMapping succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + +}); \ No newline at end of file diff --git a/test/src/controllers/registry-controller.test.js b/test/src/controllers/registry-controller.test.js new file mode 100644 index 000000000..843d539d4 --- /dev/null +++ b/test/src/controllers/registry-controller.test.js @@ -0,0 +1,211 @@ +const {expect} = require('chai'); +const sinon = require('sinon'); + +const RegistryController = require('../../../src/controllers/registry-controller'); +const RegistryService = require('../../../src/services/registry-service'); + +describe('Registry Controller', () => { + def('subject', () => RegistryController); + def('sandbox', () => sinon.createSandbox()); + + afterEach(() => $sandbox.restore()); + + describe('.createRegistryEndPoint()', () => { + def('user', () => 'user!'); + + def('url', () => 'url'); + def('isPublic', () => true); + def('username', () => 'testUsername'); + def('password', () => 'testPassword'); + def('email', () => 'test@gmail.com'); + def('requiresCert', () => true); + def('certificate', () => "certificateString"); + + def('req', () => ({ + body: { + url: $url, + isPublic: $isPublic, + username: $username, + password: $password, + email: $email, + requiresCert: $requiresCert, + certificate: $certificate + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.createRegistryEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(RegistryService, 'createRegistry').returns($response); + }); + + it('calls RegistryService.createRegistry with correct args', async () => { + await $subject; + expect(RegistryService.createRegistry).to.have.been.calledWith({ + url: $url, + isPublic: $isPublic, + username: $username, + password: $password, + email: $email, + requiresCert: $requiresCert, + certificate: $certificate + }, $user); + }); + + context('when RegistryService#createRegistry fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when RegistryService#createRegistry succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.getRegistriesEndPoint()', () => { + def('user', () => 'user!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getRegistriesEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(RegistryService, 'findRegistries').returns($response); + }); + + it('calls RegistryService.findRegistries with correct args', async () => { + await $subject; + expect(RegistryService.findRegistries).to.have.been.calledWith($user, false) + }); + + context('when RegistryService#findRegistries fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when RegistryService#findRegistries succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + + }); + + describe('.deleteRegistryEndPoint()', () => { + def('user', () => 'user!'); + def('id', () => 15); + + def('req', () => ({ + params: { + id: $id + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteRegistryEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(RegistryService, 'deleteRegistry').returns($response); + }); + + it('calls RegistryService.deleteRegistry with correct args', async () => { + await $subject; + expect(RegistryService.deleteRegistry).to.have.been.calledWith({ + id: parseInt($req.params.id) + }, $user, false) + }); + + context('when RegistryService#deleteRegistry fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when RegistryService#deleteRegistry succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.updateRegistryEndPoint()', () => { + def('user', () => 'user!'); + def('id', () => 15); + + def('url', () => 'updatedUrl'); + def('isPublic', () => false); + def('username', () => 'updatedUsername'); + def('password', () => 'updatedPassword'); + def('email', () => 'updatedTest@gmail.com'); + def('requiresCert', () => false); + def('certificate', () => "updatedCertificateString"); + + def('req', () => ({ + params: { + id: $id + }, + body: { + url: $url, + isPublic: $isPublic, + username: $username, + password: $password, + email: $email, + requiresCert: $requiresCert, + certificate: $certificate + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateRegistryEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(RegistryService, 'updateRegistry').returns($response); + }); + + it('calls RegistryService.updateRegistry with correct args', async () => { + await $subject; + expect(RegistryService.updateRegistry).to.have.been.calledWith({ + url: $url, + isPublic: $isPublic, + username: $username, + password: $password, + email: $email, + requiresCert: $requiresCert, + certificate: $certificate + }, $id, $user, false); + }); + + context('when RegistryService#updateRegistry fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when RegistryService#updateRegistry succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); +}); \ No newline at end of file