diff --git a/Master/endpoints.js b/Master/endpoints.js index 2a39cf0..2a0208e 100644 --- a/Master/endpoints.js +++ b/Master/endpoints.js @@ -21,7 +21,7 @@ exports.checkUpdate = function (context, AlertlogicMasterTimer, callback) { return callback(null); } else { // Endpoint settings do not exist. Update them. - let alApiEndpoint = process.env.CUSTOMCONNSTR_APP_AL_API_ENDPOINT; + let alApiEndpoint = process.env.CUSTOMCONNSTR_APP_AL_API_ENDPOINT; let alResidency = process.env.CUSTOMCONNSTR_APP_AL_RESIDENCY; let aimsCreds = { access_key_id : process.env.CUSTOMCONNSTR_APP_CI_ACCESS_KEY_ID, diff --git a/Master/o365collector.js b/Master/o365collector.js index 986fa33..1101e78 100644 --- a/Master/o365collector.js +++ b/Master/o365collector.js @@ -43,7 +43,7 @@ exports.checkRegister = function (context, AlertlogicMasterTimer, azcollectSvc, exports.checkin = function (context, AlertlogicMasterTimer, azcollectSvc, callback) { return m_o365mgmnt.subscriptionsList( - function(listErr, subscrptions, httpRequest, response) { + function(listErr, subscriptions, httpRequest, response) { if (listErr) { azcollectSvc.checkin('o365', process.env.O365_COLLECTOR_ID, 'error', `${listErr}`) @@ -53,15 +53,14 @@ exports.checkin = function (context, AlertlogicMasterTimer, azcollectSvc, callba .catch(function(exception) { return callback(`Unable to checkin ${exception}`); }); - return callback(listErr); } else { - return _checkEnableAuditStreams(context, subscrptions, + return _checkEnableAuditStreams(context, subscriptions, function(enableErr, checkResults) { if (enableErr) { azcollectSvc.checkin('o365', process.env.O365_COLLECTOR_ID, 'error', `${enableErr}`) .then(resp => { - callback(null, resp); + return callback(null, resp); }) .catch(function(exception) { return callback(`Unable to checkin ${exception}`); @@ -70,7 +69,7 @@ exports.checkin = function (context, AlertlogicMasterTimer, azcollectSvc, callba azcollectSvc.checkin('o365', process.env.O365_COLLECTOR_ID, 'ok', `${checkResults}`) .then(resp => { - callback(null, resp); + return callback(null, resp); }) .catch(function(exception) { return callback(`Unable to checkin ${exception}`); diff --git a/README.md b/README.md index d4e8953..75435cd 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,7 @@ A notification contains a link to the actual data which is retrieved by the `Col 1. Run Master function locally: `npm run local-master` 1. Run Updater function locally: `npm run local-updater` 1. Run O365WebHook function locally: `npm run local-o365webhook` -1. Run `npm test` in order to perform code analysis. +1. Run `npm test` in order to perform code analysis and unit tests. Please use the following [code style](https://github.com/airbnb/javascript) as much as possible. diff --git a/package.json b/package.json index 0b0ea80..6d5dc43 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "azure_collector", - "version": "1.0.2", + "version": "1.0.3", "dependencies": { "async": "*", "azure": "^2.0.0-preview", diff --git a/test/master_appsettings_test.js b/test/master_appsettings_test.js new file mode 100644 index 0000000..cc80bf4 --- /dev/null +++ b/test/master_appsettings_test.js @@ -0,0 +1,116 @@ +/* ----------------------------------------------------------------------------- + * @copyright (C) 2017, Alert Logic, Inc + * @doc + * + * Unit tests for Master function + * + * @end + * ----------------------------------------------------------------------------- + */ + +var testMock = require('./mock'); + +var assert = require('assert'); +var rewire = require('rewire'); +var sinon = require('sinon'); + +var m_appsettings = rewire('../Master/appsettings'); + +describe('Master Function appsettings.js Units', function() { + var private_appAdCreds; + var private_websiteClient; + var msListApplicationSettingsStub; + var msUpdateApplicationSettingsStub; + + before(function() { + private_appAdCreds = m_appsettings.__get__('g_appAdCreds'); + private_websiteClient = m_appsettings.__get__('g_websiteClient'); + + retrieveTokenFromCacheStub = sinon.stub(private_appAdCreds, '_retrieveTokenFromCache').callsFake( + function fakeFn(callback) { + var mockToken = { + 'tokenType' : 'Bearer', + 'expiresIn' : 3599, + 'expiresOn': '2017-09-26T11:34:40.703Z', + 'resource' : 'https://management.azure.com', + 'accessToken' : 'some-token', + 'isMRRT' : true, + '_clientId' : process.env.CUSTOMCONNSTR_APP_CLIENT_ID, + '_authority' :' https://login.microsoftonline.com/' + process.env.APP_TENANT_ID + }; + return callback(null, mockToken); + }); + msListApplicationSettingsStub = sinon.stub(private_websiteClient.webApps, 'listApplicationSettings').callsFake( + function fakeFn(rgName, name, options, callback) { + var mockSettings = { + properties : { + MOCK_SETTING : 'mock-value' + } + }; + return callback(null, mockSettings, null, null); + }); + msUpdateApplicationSettingsStub = sinon.stub(private_websiteClient.webApps, 'updateApplicationSettings').callsFake( + function fakeFn(rgName, name, settings, options, callback) { + return callback(null, null, null, null); + }); + }); + after(function() { + msListApplicationSettingsStub.restore(); + msUpdateApplicationSettingsStub.restore(); + retrieveTokenFromCacheStub.restore(); + }); + beforeEach(function() { + msListApplicationSettingsStub.resetHistory(); + msUpdateApplicationSettingsStub.resetHistory(); + }); + + describe('Azure web application setting tests', function() { + it('checks getAppsettings()', function(done) { + m_appsettings.getAppsettings(function(err, settings){ + if (err) + return done(err); + + sinon.assert.callCount(msListApplicationSettingsStub, 1); + done(); + }); + }); + + it('checks setAppsettings()', function(done) { + var testSettings = { + test : 'test' + }; + m_appsettings.setAppsettings(testSettings, function(err, settings) { + if (err) + return done(err); + + sinon.assert.callCount(msUpdateApplicationSettingsStub, 1); + done(); + }); + }); + + it('checks updateAppsettings()', function(done) { + var testSettings = { + test : 'test' + }; + m_appsettings.updateAppsettings(testSettings, function(err, settings) { + if (err) + return done(err); + + var expectedSettings = { + properties : { + MOCK_SETTING : 'mock-value', + test : 'test' + } + }; + sinon.assert.callCount(msListApplicationSettingsStub, 1); + sinon.assert.callCount(msUpdateApplicationSettingsStub, 1); + sinon.assert.calledWith(msUpdateApplicationSettingsStub, + process.env.APP_RESOURCE_GROUP, + process.env.WEBSITE_SITE_NAME, + expectedSettings, + null); + done(); + }); + }); + }); +}); diff --git a/test/master_endpoints_test.js b/test/master_endpoints_test.js new file mode 100644 index 0000000..de0894c --- /dev/null +++ b/test/master_endpoints_test.js @@ -0,0 +1,87 @@ +/* ----------------------------------------------------------------------------- + * @copyright (C) 2017, Alert Logic, Inc + * @doc + * + * Unit tests for Master function + * + * @end + * ----------------------------------------------------------------------------- + */ + +var assert = require('assert'); +var sinon = require('sinon'); + +var testMock = require('./mock'); + +var m_endpoints = require('../Master/endpoints'); +var m_appsettings = require('../Master/appsettings'); + +describe('Master Function endpoints.js Units', function() { + var updateSettingsStub = null; + var endpointsStub = null; + + before(function() { + var residency = process.env.CUSTOMCONNSTR_APP_AL_RESIDENCY; + + updateSettingsStub = sinon.stub(m_appsettings, 'updateAppsettings').callsFake( + function fakeFn(settings, callback) { + return callback(null, settings); + }); + endpointsStub = sinon.stub(m_endpoints.Endpoints.prototype, 'getEndpoint'); + endpointsStub.withArgs('ingest', residency).resolves({ + ingest : 'new-ingest-endpoint' + }); + endpointsStub.withArgs('azcollect', residency).resolves({ + azcollect : 'new-azcollect-endpoint' + }); + }); + after(function() { + if (updateSettingsStub) updateSettingsStub.restore(); + if (endpointsStub) endpointsStub.restore(); + }); + beforeEach(function() { + if (updateSettingsStub) updateSettingsStub.resetHistory(); + if (endpointsStub) endpointsStub.resetHistory(); + }); + + describe('AL endpoints retrieval tests', function() { + it('checks endpoints values are reused if already fetched', function(done) { + process.env.APP_INGEST_ENDPOINT = 'existing-ingest-endpoint'; + process.env.APP_AZCOLLECT_ENDPOINT = 'existing-azcollect-endpoint'; + m_endpoints.checkUpdate(testMock.context, testMock.timer, + function(err){ + if (err) { + return done(err); + } else { + sinon.assert.callCount(endpointsStub, 0); + sinon.assert.callCount(updateSettingsStub, 0); + return done(); + } + }); + }); + + it('checks endpoints values are saved as app settings', function(done) { + process.env.APP_INGEST_ENDPOINT = null; + process.env.APP_AZCOLLECT_ENDPOINT = null; + var residency = process.env.CUSTOMCONNSTR_APP_AL_RESIDENCY; + + m_endpoints.checkUpdate(testMock.context, testMock.timer, + function(err){ + if (err) { + return done(err); + } else { + var expectedSettings = { + APP_INGEST_ENDPOINT : 'new-ingest-endpoint', + APP_AZCOLLECT_ENDPOINT : 'new-azcollect-endpoint' + }; + sinon.assert.callCount(endpointsStub, 2); + sinon.assert.calledWith(endpointsStub, 'ingest', residency); + sinon.assert.calledWith(endpointsStub, 'azcollect', residency); + sinon.assert.callCount(updateSettingsStub, 1); + sinon.assert.calledWith(updateSettingsStub, expectedSettings); + return done(); + } + }); + }); + }); +}); diff --git a/test/master_o365collector_test.js b/test/master_o365collector_test.js new file mode 100644 index 0000000..a9f0e3e --- /dev/null +++ b/test/master_o365collector_test.js @@ -0,0 +1,414 @@ +/* ----------------------------------------------------------------------------- + * @copyright (C) 2017, Alert Logic, Inc + * @doc + * + * Unit tests for Master function + * + * @end + * ----------------------------------------------------------------------------- + */ + +var assert = require('assert'); +var rewire = require('rewire'); +var sinon = require('sinon'); + +var testMock = require('./mock'); +var m_o365mgmnt = require('../lib/o365_mgmnt'); + +var m_azcollect = require('../Master/azcollect'); +var m_o365collector = rewire('../Master/o365collector'); +var m_appsettings = require('../Master/appsettings'); + +describe('Master Function o365collector.js Units', function() { + var private_checkEnableAuditStreams; + var msSubscriptionsStartStub; + var updateSettingsStub = null; + + before(function() { + private_checkEnableAuditStreams = m_o365collector.__get__('_checkEnableAuditStreams'); + msSubscriptionsStartStub = sinon.stub(m_o365mgmnt, 'subscriptionsStart').callsFake( + function fakeFn(contentType, webhook, callback) { + return callback(null); + }); + }); + after(function() { + msSubscriptionsStartStub.restore(); + }); + beforeEach(function() { + msSubscriptionsStartStub.resetHistory(); + }); + afterEach(function() { + if (updateSettingsStub) updateSettingsStub.restore(); + }); + + describe('_checkEnableAuditStreams()', function() { + it('enables configured streams', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.Exchange", "Audit.General"]'; + private_checkEnableAuditStreams(testMock.context, [], function(err, streams){ + if (err) + return done(err); + + sinon.assert.callCount(msSubscriptionsStartStub, 3); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.AzureActiveDirectory"); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.Exchange"); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.General"); + return done(); + }); + }); + + it('checks already enabled streams with proper webhook configs', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.Exchange", "Audit.SharePoint", "Audit.General"]'; + private_checkEnableAuditStreams(testMock.context, testMock.allEnabledStreams, function(err, streams){ + if (err) + return done(err); + + assert.equal(4, streams.length); + sinon.assert.callCount(msSubscriptionsStartStub, 0); + return done(); + }); + }); + + it('checks subscriptionStart is called for already enabled webhooks if web app is being reinstalled', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.Exchange", "Audit.SharePoint", "Audit.General"]'; + var twoOldEnabledStreams = [ + { + "contentType": "Audit.AzureActiveDirectory", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://old-app.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.Exchange", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://old-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.General", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.SharePoint", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + } + ]; + private_checkEnableAuditStreams(testMock.context, twoOldEnabledStreams, function(err, streams){ + if (err) + return done(err); + + sinon.assert.callCount(msSubscriptionsStartStub, 2); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.AzureActiveDirectory"); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.Exchange"); + return done(); + }); + }); + + it('checks subscriptionStart is called for disabled streams', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.General"]'; + var disabledStreams = [ + { + "contentType": "Audit.AzureActiveDirectory", + "status": "disabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.General", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + } + ]; + private_checkEnableAuditStreams(testMock.context, disabledStreams, function(err, streams){ + if (err) + return done(err); + + sinon.assert.callCount(msSubscriptionsStartStub, 1); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.AzureActiveDirectory"); + return done(); + }); + }); + + it('checks subscriptionStart is called for disabled webhooks', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.General"]'; + var disabledStreams = [ + { + "contentType": "Audit.AzureActiveDirectory", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.General", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "disabled" + } + } + ]; + private_checkEnableAuditStreams(testMock.context, disabledStreams, function(err, streams){ + if (err) + return done(err); + + sinon.assert.callCount(msSubscriptionsStartStub, 1); + sinon.assert.calledWith(msSubscriptionsStartStub, "Audit.General"); + return done(); + }); + }); + }); + + describe('O365 collector checkin tests', function() { + it('checks successfull OK checkin', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.General"]'; + var enabledStreams = [ + { + "contentType": "Audit.AzureActiveDirectory", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.General", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "disabled" + } + } + ]; + var msSubscriptionsListStub = sinon.stub(m_o365mgmnt, 'subscriptionsList').callsFake( + function fakeFn(callback) { + return callback(null, enabledStreams, null, null); + }); + var azcollectSvc = new m_azcollect.Azcollect('api-endpoint', 'creds'); + sinon.stub(azcollectSvc, 'checkin').resolves([{}]); + + m_o365collector.checkin(testMock.context, testMock.timer, azcollectSvc, + function(err, resp){ + if (err) { + msSubscriptionsListStub.restore(); + return done(err); + } else { + sinon.assert.callCount(azcollectSvc.checkin, 1); + sinon.assert.calledWith(azcollectSvc.checkin, + 'o365', process.env.O365_COLLECTOR_ID, 'ok'); + msSubscriptionsListStub.restore(); + return done(); + } + }); + }); + + it('checks successfull Error checkin during Office subscriptionList error', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.General"]'; + var enabledStreams = [ + { + "contentType": "Audit.AzureActiveDirectory", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.General", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "disabled" + } + } + ]; + var listError = 'Office subscriptionList error'; + var msSubscriptionsListStub = sinon.stub(m_o365mgmnt, 'subscriptionsList').callsFake( + function fakeFn(callback) { + return callback(listError); + }); + var azcollectSvc = new m_azcollect.Azcollect('api-endpoint', 'creds'); + sinon.stub(azcollectSvc, 'checkin').resolves([{}]); + + m_o365collector.checkin(testMock.context, testMock.timer, azcollectSvc, + function(err, resp){ + if (err) { + msSubscriptionsListStub.restore(); + return done(err); + } else { + sinon.assert.callCount(azcollectSvc.checkin, 1); + sinon.assert.calledWith(azcollectSvc.checkin, + 'o365', process.env.O365_COLLECTOR_ID, 'error', listError); + msSubscriptionsListStub.restore(); + return done(null); + } + }); + }); + + it('checks successfull Error checkin during Office subscriptionStart error', function(done) { + process.env.O365_CONTENT_STREAMS = + '["Audit.AzureActiveDirectory", "Audit.General"]'; + var enabledStreams = [ + { + "contentType": "Audit.AzureActiveDirectory", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "enabled" + } + }, + { + "contentType": "Audit.General", + "status": "enabled", + "webhook": { + "authId": null, + "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "expiration": "", + "status": "disabled" + } + } + ]; + var startError = 'Office subscriptionStart error'; + var msSubscriptionsListStub = sinon.stub(m_o365mgmnt, 'subscriptionsList').callsFake( + function fakeFn(callback) { + return callback(null, enabledStreams, null, null); + }); + msSubscriptionsStartStub.restore(); + var msSubscriptionsStartErrorStub = sinon.stub(m_o365mgmnt, 'subscriptionsStart').callsFake( + function fakeFn(contentType, webhook, callback) { + return callback(startError); + }); + var azcollectSvc = new m_azcollect.Azcollect('api-endpoint', 'creds'); + sinon.stub(azcollectSvc, 'checkin').resolves([{}]); + + m_o365collector.checkin(testMock.context, testMock.timer, azcollectSvc, + function(err, resp){ + if (err) { + msSubscriptionsListStub.restore(); + msSubscriptionsStartErrorStub.restore(); + return done(err); + } else { + sinon.assert.callCount(azcollectSvc.checkin, 1); + sinon.assert.calledWith(azcollectSvc.checkin, + 'o365', process.env.O365_COLLECTOR_ID, 'error', startError); + msSubscriptionsListStub.restore(); + msSubscriptionsStartErrorStub.restore(); + return done(null); + } + }); + }); + }); + + describe('O365 collector register tests', function() { + it('checks collector and host id are reused if already registered', function(done) { + process.env.O365_COLLECTOR_ID = 'existing-collector-id'; + process.env.O365_HOST_ID = 'existing-collector-id'; + updateSettingsStub = sinon.stub(m_appsettings, 'updateAppsettings').callsFake( + function fakeFn(settings, callback) { + return callback(null, settings); + }); + var azcollectSvc = new m_azcollect.Azcollect('api-endpoint', 'creds'); + sinon.stub(azcollectSvc, 'register_o365').resolves({ + source : { + id : 'new-source-id', + host : { + id : 'new-host-id' + } + } + }); + + m_o365collector.checkRegister(testMock.context, testMock.timer, azcollectSvc, + function(err, resp){ + if (err) { + return done(err); + } else { + sinon.assert.callCount(updateSettingsStub, 0); + sinon.assert.callCount(azcollectSvc.register_o365, 0); + return done(); + } + }); + }); + + it('checks updateSettings is called during registration', function(done) { + process.env.O365_COLLECTOR_ID = null; + updateSettingsStub = sinon.stub(m_appsettings, 'updateAppsettings').callsFake( + function fakeFn(settings, callback) { + return callback(null, settings); + }); + var azcollectSvc = new m_azcollect.Azcollect('api-endpoint', 'creds'); + sinon.stub(azcollectSvc, 'register_o365').resolves({ + source : { + id : 'new-source-id', + host : { + id : 'new-host-id' + } + } + }); + + m_o365collector.checkRegister(testMock.context, testMock.timer, azcollectSvc, + function(err, resp){ + if (err) { + return done(err); + } else { + var expectedSettings = { + O365_COLLECTOR_ID: 'new-source-id', + O365_HOST_ID: 'new-host-id' + }; + sinon.assert.callCount(updateSettingsStub, 1); + sinon.assert.calledWith(updateSettingsStub, expectedSettings); + return done(); + } + }); + }); + }); +}); diff --git a/test/master_test.js b/test/master_test.js deleted file mode 100644 index 9839161..0000000 --- a/test/master_test.js +++ /dev/null @@ -1,61 +0,0 @@ -/* ----------------------------------------------------------------------------- - * @copyright (C) 2017, Alert Logic, Inc - * @doc - * - * Unit tests for Master function - * - * @end - * ----------------------------------------------------------------------------- - */ - -var assert = require('assert'); -var rewire = require('rewire'); -var sinon = require('sinon'); - -var testMock = require('./mock'); -var m_o365mgmnt = require('../lib/o365_mgmnt'); - -var o365collector = rewire('../Master/o365collector'); - -describe('Master Function Units', function() { - var private_checkEnableAuditStreams; - var subscriptionsStartStub; - - before(function() { - private_checkEnableAuditStreams = o365collector.__get__('_checkEnableAuditStreams'); - subscriptionsStartStub = sinon.stub(m_o365mgmnt, 'subscriptionsStart').callsFake( - function fakeFn(contentType, webhook, callback) { - return callback(null); - }); - }); - after(function() { - subscriptionsStartStub.restore(); - }); - - describe('_checkEnableAuditStreams()', function() { - it('checks already enabled streams with proper webhook configs', function(done) { - process.env.O365_CONTENT_STREAMS = - '["Audit.AzureActiveDirectory", "Audit.Exchange", "Audit.SharePoint", "Audit.General"]'; - private_checkEnableAuditStreams(testMock.context, testMock.allEnabledStreams, function(err, streams){ - if (err) - return done(err); - - assert.equal(4, streams.length); - assert.equal(0, subscriptionsStartStub.callCount); - done(); - }); - }); - - it('checks subscriptionStart is called for already enabled webhooks if web app is being reinstalled', function(done) { - process.env.O365_CONTENT_STREAMS = - '["Audit.AzureActiveDirectory", "Audit.Exchange", "Audit.SharePoint", "Audit.General"]'; - private_checkEnableAuditStreams(testMock.context, testMock.oneOldEnabledStream, function(err, streams){ - if (err) - return done(err); - - assert.equal(1, subscriptionsStartStub.callCount); - done(); - }); - }); - }); -}); diff --git a/test/mock.js b/test/mock.js index 8db594e..3430aad 100644 --- a/test/mock.js +++ b/test/mock.js @@ -9,15 +9,25 @@ */ const util = require('util'); +const fs = require('fs'); +const m_alUtil = require('../lib/al_util'); process.env.WEBSITE_HOSTNAME = 'kkuzmin-app-o365.azurewebsites.net'; +process.env.WEBSITE_SITE_NAME = 'kkuzmin-app-o365.azurewebsites.net'; process.env.O365_CONTENT_STREAMS = '["Audit.AzureActiveDirectory", "Audit.Exchange", "Audit.SharePoint", "Audit.General"]'; process.env.TMP = '/tmp/'; process.env.APP_SUBSCRIPTION_ID = 'subscription-id'; process.env.CUSTOMCONNSTR_APP_CLIENT_ID = 'client-id'; process.env.CUSTOMCONNSTR_APP_CLIENT_SECRET = 'client-secret'; +process.env.CUSTOMCONNSTR_APP_CI_ACCESS_KEY_ID = 'ci-access-key-id'; +process.env.CUSTOMCONNSTR_APP_CI_SECRET_KEY = 'ci-secret-key'; process.env.APP_TENANT_ID = 'test.onmicrosoft.com'; process.env.O365_TENANT_ID = 'test.onmicrosoft.com'; +process.env.APP_RESOURCE_GROUP = 'resource-group'; +process.env.O365_COLLECTOR_ID = 'o365-collector-id'; +process.env.O365_HOST_ID = 'o365-host-id'; +process.env.CUSTOMCONNSTR_APP_AL_RESIDENCY = 'default'; +process.env.CUSTOMCONNSTR_APP_AL_API_ENDPOINT = 'al-api-endpoint'; var context = { invocationId: 'ID', @@ -86,7 +96,7 @@ var allEnabledStreams = [ } ]; -var oneOldEnabledStream = [ +var twoOldEnabledStreams = [ { "contentType": "Audit.AzureActiveDirectory", "status": "enabled", @@ -102,7 +112,7 @@ var oneOldEnabledStream = [ "status": "enabled", "webhook": { "authId": null, - "address": "https://kkuzmin-app-o365.azurewebsites.net/api/o365/webhook", + "address": "https://old-app-o365.azurewebsites.net/api/o365/webhook", "expiration": "", "status": "enabled" } @@ -129,6 +139,14 @@ var oneOldEnabledStream = [ } ]; -exports.allEnabledStreams = allEnabledStreams; -exports.oneOldEnabledStream = oneOldEnabledStream; -exports.context = context; +var timer = { + isPastDue: false, + last: '2017-08-03T13:30:00', + next: '2017-08-03T13:45:00' +}; + +module.exports = { + allEnabledStreams : allEnabledStreams, + context : context, + timer : timer +};