diff --git a/src/decorators/authorization-decorator.js b/src/decorators/authorization-decorator.js index a3f78aee4..9702d2235 100644 --- a/src/decorators/authorization-decorator.js +++ b/src/decorators/authorization-decorator.js @@ -86,4 +86,4 @@ function checkFogToken(f) { module.exports = { checkAuthToken: checkAuthToken, checkFogToken: checkFogToken -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/schemas/agent.js b/src/schemas/agent.js index 3d3c19cc5..5dd990aa4 100644 --- a/src/schemas/agent.js +++ b/src/schemas/agent.js @@ -104,10 +104,10 @@ const straceData = { "id": "/straceData", "type": "object", "properties": { - "microserviceId": {"type": "string"}, + "microserviceUuid": {"type": "string"}, "buffer": {"type": "string"} }, - "required": ["microserviceId", "buffer"], + "required": ["microserviceUuid", "buffer"], "additionalProperties": false }; diff --git a/src/sequelize/managers/strace-manager.js b/src/sequelize/managers/strace-manager.js index f910e196b..8d0f50422 100644 --- a/src/sequelize/managers/strace-manager.js +++ b/src/sequelize/managers/strace-manager.js @@ -26,7 +26,7 @@ class StraceManager extends BaseManager { return Strace } - async pushBufferByMicroserviceId(uuid, pushingData, transaction) { + async pushBufferByMicroserviceUuid(uuid, pushingData, transaction) { const strace = await this.findOne({ microserviceUuid: uuid }, transaction); diff --git a/src/services/agent-service.js b/src/services/agent-service.js index 8160eb82e..ab71dd9b9 100644 --- a/src/services/agent-service.js +++ b/src/services/agent-service.js @@ -315,9 +315,9 @@ const updateAgentStrace = async function (straceData, fog, transaction) { await Validator.validate(straceData, Validator.schemas.updateAgentStrace); for (const strace of straceData.straceData) { - const microserviceId = strace.microserviceId; + const microserviceUuid = strace.microserviceUuid; const buffer = strace.buffer; - await StraceManager.pushBufferByMicroserviceId(microserviceId, buffer, transaction) + await StraceManager.pushBufferByMicroserviceUuid(microserviceUuid, buffer, transaction) } }; diff --git a/src/services/diagnostic-service.js b/src/services/diagnostic-service.js index 741f122f4..474db7797 100644 --- a/src/services/diagnostic-service.js +++ b/src/services/diagnostic-service.js @@ -80,6 +80,11 @@ const getMicroserviceStraceData = async function (id, data, user, isCLI, transac const postMicroserviceStraceDatatoFtp = async function (id, data, user, isCLI, transaction) { await Validator.validate(data, Validator.schemas.stracePostToFtp); const straceData = await StraceDiagnosticManager.findOne({microserviceUuid: id}, transaction); + + if (!straceData) { + throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_STRACE, id)) + } + const dir = config.get('Diagnostics:DiagnosticDir'); const filePath = dir + '/' + id; diff --git a/test/src/controllers/agent-controller.test.js b/test/src/controllers/agent-controller.test.js new file mode 100644 index 000000000..4524ba76b --- /dev/null +++ b/test/src/controllers/agent-controller.test.js @@ -0,0 +1,767 @@ +const {expect} = require('chai'); +const sinon = require('sinon'); + +const AgentController = require('../../../src/controllers/agent-controller'); +const AgentService = require('../../../src/services/agent-service'); + +describe('Agent Controller', () => { + def('subject', () => AgentController); + def('sandbox', () => sinon.createSandbox()); + + afterEach(() => $sandbox.restore()); + + describe('.agentProvisionEndPoint()', () => { + def('type', () => 1); + def('key', () => 'testKey'); + + def('req', () => ({ + body: { + type: $type, + key: $key + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.agentProvisionEndPoint($req)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'agentProvision').returns($response); + }); + + it('calls AgentService.agentProvision with correct args', async () => { + await $subject; + expect(AgentService.agentProvision).to.have.been.calledWith({ + type: $type, + key: $key + }); + }); + + context('when AgentService#agentProvision fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#agentProvision succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.getAgentConfigEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentConfigEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentConfig').returns($response); + }); + + it('calls AgentService.getAgentConfig with correct args', async () => { + await $subject; + expect(AgentService.getAgentConfig).to.have.been.calledWith($fog) + }); + + context('when AgentService#getAgentConfig fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentConfig succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + + }); + + describe('.updateAgentConfigEndPoint()', () => { + def('fog', () => 'fog!'); + + def('networkInterface', () => 'testNetworkInterface'); + def('dockerUrl', () => 'testDockerUrl'); + def('diskLimit', 15); + def('diskDirectory', () => 'testDiskDirectory'); + def('memoryLimit', () => 25); + def('cpuLimit', () => 35); + def('logLimit', () => 45); + def('logDirectory', () => 'testLogDirectory'); + def('logFileCount', () => 5); + def('statusFrequency', () => 60); + def('changeFrequency', () => 30); + def('deviceScanFrequency', () => 40); + def('watchdogEnabled', () => true); + def('latitude', () => 30); + def('longitude', () => 40); + def('gpsMode', () => 'testGpsMode'); + + def('req', () => ({ + body: { + networkInterface: $networkInterface, + dockerUrl: $dockerUrl, + diskLimit: $diskLimit, + diskDirectory: $diskDirectory, + memoryLimit: $memoryLimit, + cpuLimit: $cpuLimit, + logLimit: $logLimit, + logDirectory: $logDirectory, + logFileCount: $logFileCount, + statusFrequency: $statusFrequency, + changeFrequency: $changeFrequency, + devicesScanFrequency: $deviceScanFrequency, + watchdogEnabled: $watchdogEnabled, + latitude: $latitude, + longitude: $longitude, + gpsMode: $gpsMode + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateAgentConfigEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'updateAgentConfig').returns($response); + }); + + it('calls AgentService.updateAgentConfig with correct args', async () => { + await $subject; + expect(AgentService.updateAgentConfig).to.have.been.calledWith({ + networkInterface: $networkInterface, + dockerUrl: $dockerUrl, + diskLimit: $diskLimit, + diskDirectory: $diskDirectory, + memoryLimit: $memoryLimit, + cpuLimit: $cpuLimit, + logLimit: $logLimit, + logDirectory: $logDirectory, + logFileCount: $logFileCount, + statusFrequency: $statusFrequency, + changeFrequency: $changeFrequency, + devicesScanFrequency: $deviceScanFrequency, + watchdogEnabled: $watchdogEnabled, + latitude: $latitude, + longitude: $longitude, + gpsMode: $gpsMode + }, $fog); + }); + + context('when AgentService#updateAgentConfig fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#updateAgentConfig succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.getAgentConfigChangesEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentConfigChangesEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentConfigChanges').returns($response); + }); + + it('calls AgentService.getAgentConfigChanges with correct args', async () => { + await $subject; + expect(AgentService.getAgentConfigChanges).to.have.been.calledWith($fog); + }); + + context('when AgentService#getAgentConfigChanges fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentConfigChanges succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.updateAgentStatusEndPoint()', () => { + def('fog', () => 'fog!'); + + def('daemonStatus', () => 'testDaemonStatus'); + def('daemonOperatingDuration', () => 'testDaemonOperatingDuration'); + def('daemonLastStart', () => 5); + def('memoryUsage', () => 25); + def('diskUsage', () => 35); + def('cpuUsage', () => 45); + def('memoryViolation', () => true); + def('diskViolation', () => true); + def('cpuViolation', () => true); + def('microserviceStatus', () => 'RUNNING'); + def('repositoryCount', () => 5); + def('repositoryStatus', () => 'testRepositoryStatus'); + def('systemTime', () => 1555555); + def('lastStatusTime', () => 15555555); + def('ipAddress', () => 'testIpAddress'); + def('processedMessages', () => 155); + def('microserviceMessageCounts', () => 1555); + def('messageSpeed', () => 5); + def('lastCommandTime', () => 155555555); + def('tunnelStatus', () => 'testTunnelStatus'); + def('version', () => '1.5.6'); + def('isReadyToUpgrade', () => true); + def('isReadyToRollback', () => true); + + def('req', () => ({ + body: { + daemonStatus: $daemonStatus, + daemonOperatingDuration: $daemonOperatingDuration, + daemonLastStart: $daemonLastStart, + memoryUsage: $memoryUsage, + diskUsage: $diskUsage, + cpuUsage: $cpuUsage, + memoryViolation: $memoryViolation, + diskViolation: $diskViolation, + cpuViolation: $cpuViolation, + microserviceStatus: $microserviceStatus, + repositoryCount: $repositoryCount, + repositoryStatus: $repositoryStatus, + systemTime: $systemTime, + lastStatusTime: $lastStatusTime, + ipAddress: $ipAddress, + processedMessages: $processedMessages, + microserviceMessageCounts: $microserviceMessageCounts, + messageSpeed: $messageSpeed, + lastCommandTime: $lastCommandTime, + tunnelStatus: $tunnelStatus, + version: $version, + IsReadyToUpgrade: $isReadyToUpgrade, + isReadyToRollback: $isReadyToRollback + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateAgentStatusEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'updateAgentStatus').returns($response); + }); + + it('calls AgentService.updateAgentStatus with correct args', async () => { + await $subject; + expect(AgentService.updateAgentStatus).to.have.been.calledWith({ + daemonStatus: $daemonStatus, + daemonOperatingDuration: $daemonOperatingDuration, + daemonLastStart: $daemonLastStart, + memoryUsage: $memoryUsage, + diskUsage: $diskUsage, + cpuUsage: $cpuUsage, + memoryViolation: $memoryViolation, + diskViolation: $diskViolation, + cpuViolation: $cpuViolation, + microserviceStatus: $microserviceStatus, + repositoryCount: $repositoryCount, + repositoryStatus: $repositoryStatus, + systemTime: $systemTime, + lastStatusTime: $lastStatusTime, + ipAddress: $ipAddress, + processedMessages: $processedMessages, + microserviceMessageCounts: $microserviceMessageCounts, + messageSpeed: $messageSpeed, + lastCommandTime: $lastCommandTime, + tunnelStatus: $tunnelStatus, + version: $version, + IsReadyToUpgrade: $isReadyToUpgrade, + isReadyToRollback: $isReadyToRollback + }, $fog); + }); + + context('when AgentService#updateAgentStatus fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#updateAgentStatus succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.getAgentMicroservicesEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentMicroservicesEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentMicroservices').returns($response); + }); + + it('calls AgentService.getAgentMicroservices with correct args', async () => { + await $subject; + expect(AgentService.getAgentMicroservices).to.have.been.calledWith($fog); + }); + + context('when AgentService#getAgentMicroservices fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentMicroservices succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getAgentMicroserviceEndPoint()', () => { + def('fog', () => 'fog!'); + def('microserviceUuid', () => 'testUuid'); + + def('req', () => ({ + params: { + microserviceUuid: $microserviceUuid, + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentMicroserviceEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentMicroservice').returns($response); + }); + + it('calls AgentService.getAgentMicroservice with correct args', async () => { + await $subject; + expect(AgentService.getAgentMicroservice).to.have.been.calledWith($microserviceUuid, $fog); + }); + + context('when AgentService#getAgentMicroservice fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentMicroservice succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getAgentRegistriesEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentRegistriesEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentRegistries').returns($response); + }); + + it('calls AgentService.getAgentRegistries with correct args', async () => { + await $subject; + expect(AgentService.getAgentRegistries).to.have.been.calledWith($fog); + }); + + context('when AgentService#getAgentRegistries fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentRegistries succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getAgentTunnelEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentTunnelEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentTunnel').returns($response); + }); + + it('calls AgentService.getAgentTunnel with correct args', async () => { + await $subject; + expect(AgentService.getAgentTunnel).to.have.been.calledWith($fog); + }); + + context('when AgentService#getAgentTunnel fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentTunnel succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getAgentStraceEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentStraceEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentStrace').returns($response); + }); + + it('calls AgentService.getAgentStrace with correct args', async () => { + await $subject; + expect(AgentService.getAgentStrace).to.have.been.calledWith($fog); + }); + + context('when AgentService#getAgentStrace fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentStrace succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('updateAgentStraceEndPoint()', () => { + def('fog', () => 'fog!'); + def('microserviceUuid', () => 'microserviceUuid'); + def('buffer', () => 'testBuffer'); + + def('straceData', [{ + microserviceUuid: $microserviceUuid, + buffer: $buffer + }]); + + def('req', () => ({ + body: { + straceData: $straceData + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateAgentStraceEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'updateAgentStrace').returns($response); + }); + + it('calls AgentService.updateAgentStrace with correct args', async () => { + await $subject; + expect(AgentService.updateAgentStrace).to.have.been.calledWith({ + straceData: $straceData + }, $fog); + }); + + context('when AgentService#updateAgentStrace fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#updateAgentStrace succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getAgentChangeVersionCommandEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getAgentChangeVersionCommandEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getAgentChangeVersionCommand').returns($response); + }); + + it('calls AgentService.getAgentChangeVersionCommand with correct args', async () => { + await $subject; + expect(AgentService.getAgentChangeVersionCommand).to.have.been.calledWith($fog); + }); + + context('when AgentService#getAgentChangeVersionCommand fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getAgentChangeVersionCommand succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('updateHalHardwareInfoEndPoint()', () => { + def('fog', () => 'fog!'); + + def('info', () => 'testInfo'); + + def('req', () => ({ + body: { + info: $info + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateHalHardwareInfoEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'updateHalHardwareInfo').returns($response); + }); + + it('calls AgentService.updateHalHardwareInfo with correct args', async () => { + await $subject; + expect(AgentService.updateHalHardwareInfo).to.have.been.calledWith({ + info: $info + }, $fog); + }); + + context('when AgentService#updateHalHardwareInfo fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#updateHalHardwareInfo succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('updateHalUsbInfoEndPoint()', () => { + def('fog', () => 'fog!'); + + def('info', () => 'testInfo'); + + def('req', () => ({ + body: { + info: $info + } + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateHalUsbInfoEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'updateHalUsbInfo').returns($response); + }); + + it('calls AgentService.updateHalUsbInfo with correct args', async () => { + await $subject; + expect(AgentService.updateHalUsbInfo).to.have.been.calledWith({ + info: $info + }, $fog); + }); + + context('when AgentService#updateHalUsbInfo fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#updateHalUsbInfo succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('deleteNodeEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteNodeEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'deleteNode').returns($response); + }); + + it('calls AgentService.deleteNode with correct args', async () => { + await $subject; + expect(AgentService.deleteNode).to.have.been.calledWith($fog); + }); + + context('when AgentService#deleteNode fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#deleteNode succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('getImageSnapshotEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.getImageSnapshotEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'getImageSnapshot').returns($response); + }); + + it('calls AgentService.getImageSnapshot with correct args', async () => { + await $subject; + expect(AgentService.getImageSnapshot).to.have.been.calledWith($fog); + }); + + context('when AgentService#getImageSnapshot fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#getImageSnapshot succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('putImageSnapshotEndPoint()', () => { + def('fog', () => 'fog!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.putImageSnapshotEndPoint($req, $fog)); + + beforeEach(() => { + $sandbox.stub(AgentService, 'putImageSnapshot').returns($response); + }); + + it('calls AgentService.putImageSnapshot with correct args', async () => { + await $subject; + expect(AgentService.putImageSnapshot).to.have.been.calledWith($req, $fog); + }); + + context('when AgentService#putImageSnapshot fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when AgentService#putImageSnapshot succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + +}); \ No newline at end of file diff --git a/test/src/controllers/catalog-controller.test.js b/test/src/controllers/catalog-controller.test.js new file mode 100644 index 000000000..18c611d04 --- /dev/null +++ b/test/src/controllers/catalog-controller.test.js @@ -0,0 +1,320 @@ +const {expect} = require('chai'); +const sinon = require('sinon'); + +const CatalogController = require('../../../src/controllers/catalog-controller'); +const CatalogService = require('../../../src/services/catalog-service'); + +describe('Catalog Controller', () => { + def('subject', () => CatalogController); + def('sandbox', () => sinon.createSandbox()); + + afterEach(() => $sandbox.restore()); + + describe('.createCatalogItemEndPoint()', () => { + def('user', () => 'user!'); + + def('name', () => 'testName'); + def('description', () => 'testDescription'); + def('category', () => 'testCategory'); + def('containerImage', () => 'testContainerImage'); + def('fogTypeId', () => 'testFogTypeId'); + def('images', () => [{ + containerImage: $containerImage, + fogTypeId: $fogTypeId + }]); + def('publisher', () => 'testPublisher'); + def('diskRequired', () => 15); + def('ramRequired', () => 25); + def('picture', () => 'testPicture'); + def('isPublic', () => false); + def('registryId', () => 5); + def('inputInfoType', () => 'testInfoType'); + def('inputInfoFormat', () => 'testInfoFormat'); + def('inputType', () => ({ + infoType: $inputInfoType, + infoFormat: $inputInfoFormat + })); + def('outputInfoType', () => 'testInfoType'); + def('outputInfoFormat', () => 'testInfoFormat'); + def('outputType', () => ({ + infoType: $outputInfoType, + infoFormat: $outputInfoFormat + })); + def('configExample', () => '{}'); + + def('req', () => ({ + body: { + name: $name, + description: $description, + category: $category, + images: $images, + publisher: $publisher, + diskRequired: $diskRequired, + ramRequired: $ramRequired, + picture: $picture, + isPublic: $isPublic, + registryId: $registryId, + inputType: $inputType, + outputType: $outputType, + configExample: $configExample + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.createCatalogItemEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(CatalogService, 'createCatalogItem').returns($response); + }); + + it('calls CatalogService.createCatalogItem with correct args', async () => { + await $subject; + expect(CatalogService.createCatalogItem).to.have.been.calledWith({ + name: $name, + description: $description, + category: $category, + images: $images, + publisher: $publisher, + diskRequired: $diskRequired, + ramRequired: $ramRequired, + picture: $picture, + isPublic: $isPublic, + registryId: $registryId, + inputType: $inputType, + outputType: $outputType, + configExample: $configExample + }, $user); + }); + + context('when CatalogService#createCatalogItem fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when CatalogService#createCatalogItem succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.listCatalogItemsEndPoint()', () => { + def('user', () => 'user!'); + + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.listCatalogItemsEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(CatalogService, 'listCatalogItems').returns($response); + }); + + it('calls CatalogService.listCatalogItems with correct args', async () => { + await $subject; + expect(CatalogService.listCatalogItems).to.have.been.calledWith($user, false) + }); + + context('when CatalogService#listCatalogItems fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when CatalogService#listCatalogItems succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + + }); + + + + describe('.listCatalogItemEndPoint()', () => { + def('user', () => 'user!'); + def('id', () => 15); + + def('req', () => ({ + params: { + id: $id + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.listCatalogItemEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(CatalogService, 'getCatalogItem').returns($response); + }); + + it('calls CatalogService.getCatalogItem with correct args', async () => { + await $subject; + expect(CatalogService.getCatalogItem).to.have.been.calledWith($id, $user, false); + }); + + context('when CatalogService#getCatalogItem fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when CatalogService#getCatalogItem succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.deleteCatalogItemEndPoint()', () => { + def('user', () => 'user!'); + def('id', () => 15); + + def('req', () => ({ + params: { + id: $id + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.deleteCatalogItemEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(CatalogService, 'deleteCatalogItem').returns($response); + }); + + it('calls CatalogService.deleteCatalogItem with correct args', async () => { + await $subject; + expect(CatalogService.deleteCatalogItem).to.have.been.calledWith($id, $user, false); + }); + + context('when CatalogService#deleteCatalogItem fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when CatalogService#deleteCatalogItem succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.updateCatalogItemEndPoint()', () => { + def('user', () => 'user!'); + def('id', () => 15); + + def('name', () => 'testName'); + def('description', () => 'testDescription'); + def('category', () => 'testCategory'); + def('containerImage', () => 'testContainerImage'); + def('fogTypeId', () => 'testFogTypeId'); + def('images', () => [{ + containerImage: $containerImage, + fogTypeId: $fogTypeId + }]); + def('publisher', () => 'testPublisher'); + def('diskRequired', () => 15); + def('ramRequired', () => 25); + def('picture', () => 'testPicture'); + def('isPublic', () => false); + def('registryId', () => 5); + def('inputInfoType', () => 'testInfoType'); + def('inputInfoFormat', () => 'testInfoFormat'); + def('inputType', () => ({ + infoType: $inputInfoType, + infoFormat: $inputInfoFormat + })); + def('outputInfoType', () => 'testInfoType'); + def('outputInfoFormat', () => 'testInfoFormat'); + def('outputType', () => ({ + infoType: $outputInfoType, + infoFormat: $outputInfoFormat + })); + def('configExample', () => '{}'); + + def('req', () => ({ + params: { + id: $id + }, + body: { + name: $name, + description: $description, + category: $category, + images: $images, + publisher: $publisher, + diskRequired: $diskRequired, + ramRequired: $ramRequired, + picture: $picture, + isPublic: $isPublic, + registryId: $registryId, + inputType: $inputType, + outputType: $outputType, + configExample: $configExample + } + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.updateCatalogItemEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(CatalogService, 'updateCatalogItem').returns($response); + }); + + it('calls CatalogService.updateCatalogItem with correct args', async () => { + await $subject; + expect(CatalogService.updateCatalogItem).to.have.been.calledWith($id, { + name: $name, + description: $description, + category: $category, + images: $images, + publisher: $publisher, + diskRequired: $diskRequired, + ramRequired: $ramRequired, + picture: $picture, + isPublic: $isPublic, + registryId: $registryId, + inputType: $inputType, + outputType: $outputType, + configExample: $configExample + }, $user, false); + }); + + context('when CatalogService.updateCatalogItem fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when CatalogService.updateCatalogItem succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + + }); + +}); \ No newline at end of file diff --git a/test/src/controllers/controller-controller.test.js b/test/src/controllers/controller-controller.test.js new file mode 100644 index 000000000..fe5b70471 --- /dev/null +++ b/test/src/controllers/controller-controller.test.js @@ -0,0 +1,115 @@ +const {expect} = require('chai'); +const sinon = require('sinon'); + +const Controller = require('../../../src/controllers/controller'); +const ControllerService = require('../../../src/services/controller-service'); + +describe('Controller', () => { + def('subject', () => Controller); + def('sandbox', () => sinon.createSandbox()); + + afterEach(() => $sandbox.restore()); + + describe('.statusControllerEndPoint()', () => { + def('req', () => ({ + body: {} + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.statusControllerEndPoint($req)); + + beforeEach(() => { + $sandbox.stub(ControllerService, 'statusController').returns($response); + }); + + it('calls ControllerService.statusController with correct args', async () => { + await $subject; + expect(ControllerService.statusController).to.have.been.calledWith(false); + }); + + context('when ControllerService#statusController fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when ControllerService#statusController succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + + describe('.emailActivationEndPoint()', () => { + def('req', () => ({ + body: {} + })); + def('response', () => Promise.resolve()); + def('subject', () => $subject.emailActivationEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(ControllerService, 'emailActivation').returns($response); + }); + + it('calls ControllerService.emailActivation with correct args', async () => { + await $subject; + expect(ControllerService.emailActivation).to.have.been.calledWith(false) + }); + + context('when ControllerService#emailActivation fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when ControllerService#emailActivation succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + + }); + + describe('.fogTypesEndPoint()', () => { + def('req', () => ({ + body: {} + })); + + def('response', () => Promise.resolve()); + def('subject', () => $subject.fogTypesEndPoint($req, $user)); + + beforeEach(() => { + $sandbox.stub(ControllerService, 'getFogTypes').returns($response); + }); + + it('calls ControllerService.getFogTypes with correct args', async () => { + await $subject; + expect(ControllerService.getFogTypes).to.have.been.calledWith(false); + }); + + context('when ControllerService#getFogTypes fails', () => { + const error = 'Error!'; + + def('response', () => Promise.reject(error)); + + it(`fails with "${error}"`, () => { + return expect($subject).to.be.rejectedWith(error) + }) + }); + + context('when ControllerService#getFogTypes succeeds', () => { + it(`succeeds`, () => { + return expect($subject).to.eventually.equal(undefined) + }) + }) + }); + +}); \ No newline at end of file