From d9a98029e79341c609a926e34849464f6afb622d Mon Sep 17 00:00:00 2001 From: Dominic Bosch Date: Thu, 20 Mar 2014 13:24:09 +0100 Subject: [PATCH] Unit Tests fixed, removed way too overloaded persistence function set --- coffee/components-manager.coffee | 28 +- coffee/config.coffee | 2 +- coffee/persistence.coffee | 318 +--------------------- coffee/request-handler.coffee | 2 +- js-coffee/components-manager.js | 28 +- js-coffee/config.js | 1 + js-coffee/engine.js | 4 +- js-coffee/persistence.js | 401 ++-------------------------- js-coffee/request-handler.js | 2 +- js/engine.js | 13 +- testing/files/jsonTestConfig.json | 10 + testing/files/jsonWrongConfig.json | 6 - testing/test_config.coffee | 4 +- testing/test_persistence.coffee | 237 +++------------- testing/test_request-handler.coffee | 145 +++++----- 15 files changed, 190 insertions(+), 1011 deletions(-) create mode 100644 testing/files/jsonTestConfig.json delete mode 100644 testing/files/jsonWrongConfig.json diff --git a/coffee/components-manager.coffee b/coffee/components-manager.coffee index e660da8..f8499cc 100644 --- a/coffee/components-manager.coffee +++ b/coffee/components-manager.coffee @@ -58,7 +58,7 @@ commandFunctions = answ = code: 200 - db.getEventPoller obj.id, ( err, mod ) => + db.eventPollers.getModule obj.id, ( err, mod ) => if mod answ.code = 409 answ.message = 'Event Poller module name already existing: ' + obj.id @@ -73,21 +73,21 @@ commandFunctions = @log.info "CM | Storing new eventpoller with events #{ events }" answ.message = "Event Poller module successfully stored! Found following event(s): #{ events }" - db.storeEventPoller obj.id, user.username, + db.eventPollers.storeModule obj.id, user.username, code: obj.data lang: obj.lang params: obj.params events: events if obj.public is 'true' - db.publishEventPoller obj.id + db.eventPollers.publish obj.id cb answ get_event_pollers: ( user, obj, cb ) -> - db.getAvailableEventPollerIds user.username, ( err, obj ) -> + db.eventPollers.getAvailableModuleIds user.username, ( err, obj ) -> oRes = {} sem = obj.length fGetEvents = ( id ) -> - db.getEventPoller id, ( err, obj ) -> + db.eventPollers.getModule id, ( err, obj ) -> oRes[id] = obj.events if --sem is 0 cb @@ -96,17 +96,17 @@ commandFunctions = fGetEvents id for id in obj get_event_poller_params: ( user, obj, cb ) -> - db.getEventPollerRequiredParams obj.id, ( err, obj ) -> + db.eventPollers.getModuleParams obj.id, ( err, obj ) -> cb code: 200 message: obj get_action_invokers: ( user, obj, cb ) -> - db.getAvailableActionInvokerIds user.username, ( err, obj ) -> + db.actionInvokers.getAvailableModuleIds user.username, ( err, obj ) -> oRes = {} sem = obj.length fGetActions = ( id ) -> - db.getActionInvoker id, ( err, obj ) -> + db.actionInvokers.getModule id, ( err, obj ) -> oRes[id] = obj.actions if --sem is 0 cb @@ -115,7 +115,7 @@ commandFunctions = fGetActions id for id in obj get_action_invoker_params: ( user, obj, cb ) -> - db.getActionInvokerRequiredParams obj.id, ( err, obj ) -> + db.actionInvokers.getModuleParams obj.id, ( err, obj ) -> cb code: 200 message: obj @@ -124,7 +124,7 @@ commandFunctions = answ = code: 200 - db.getActionInvoker obj.id, ( err, mod ) => + db.actionInvokers.getModule obj.id, ( err, mod ) => if mod answ.code = 409 answ.message = 'Action Invoker module name already existing: ' + obj.id @@ -139,13 +139,13 @@ commandFunctions = @log.info "CM | Storing new eventpoller with actions #{ actions }" answ.message = "Action Invoker module successfully stored! Found following action(s): #{ actions }" - db.storeActionInvoker obj.id, user.username, + db.actionInvokers.storeModule obj.id, user.username, code: obj.data lang: obj.lang params: obj.params actions: actions if obj.public is 'true' - db.publishActionInvoker obj.id + db.actionInvokers.publish obj.id cb answ get_rules: ( user, obj, cb ) -> @@ -172,7 +172,7 @@ commandFunctions = db.storeRule rule.id, JSON.stringify rule db.linkRule rule.id, user.username db.activateRule rule.id, user.username - db.storeEventUserParams obj.event.module, user.username, obj.event_params - db.storeActionUserParams id, user.username, JSON.stringify params for id, params of modules + db.eventPollers.storeUserParams obj.event.module, user.username, obj.event_params + db.actionInvokers.storeUserParams id, user.username, JSON.stringify params for id, params of modules @ee.emit 'newRule', JSON.stringify rule cb answ diff --git a/coffee/config.coffee b/coffee/config.coffee index 04d5d13..0452028 100644 --- a/coffee/config.coffee +++ b/coffee/config.coffee @@ -47,7 +47,6 @@ loadConfigFile = ( configPath ) => 'http-port' 'db-port' ] - #TODO Try to get rid of crypto key try @config = JSON.parse fs.readFileSync path.resolve __dirname, '..', configPath @isReady = true @@ -58,6 +57,7 @@ loadConfigFile = ( configPath ) => console.error "Missing property in config file, requires:\n" + " - #{ confProperties.join "\n - " }" catch e + @isReady = false if not @nolog console.error "Failed loading config file: #{ e.message }" diff --git a/coffee/persistence.coffee b/coffee/persistence.coffee index 86f06af..837588d 100644 --- a/coffee/persistence.coffee +++ b/coffee/persistence.coffee @@ -47,8 +47,8 @@ exports = module.exports = ( args ) => if err.message.indexOf( 'ECONNREFUSED' ) > -1 @connRefused = true @log.error err, 'DB | Wrong port?' - @ep = new IndexedModules( 'event-poller', @db, @log ) - @ai = new IndexedModules( 'action-invoker', @db, @log ) + exports.eventPollers = new IndexedModules( 'event-poller', @db, @log ) + exports.actionInvokers = new IndexedModules( 'action-invoker', @db, @log ) ### Checks whether the db is connected and passes either an error on failure after @@ -227,7 +227,7 @@ class IndexedModules constructor: ( @setname, @db, @log ) -> @log.info "DB | Instantiated indexed modules for '#{ @setname }'" - storeModule: ( mId, data ) => + storeModule: ( mId, userId, data ) => @log.info "DB | storeModule(#{ @setname }): #{ mId }" @db.sadd "#{ @setname }s", mId, replyHandler "Storing '#{ @setname }' key '#{ mId }'" @@ -235,6 +235,7 @@ class IndexedModules replyHandler "Storing '#{ @setname }:#{ mId }'" @db.hmset "#{ @setname }:#{ mId }", 'reqparams', data['reqparams'], replyHandler "Storing '#{ @setname }:#{ mId }'" + @linkModule mId, userId #TODO add testing linkModule: ( mId, userId ) => @@ -294,324 +295,33 @@ class IndexedModules replyHandler "Deleting '#{ @setname }:#{ mId }'" #TODO remove published ids #TODO remove from linked users + # TODO remove from public modules + # TODO remove parameters - storeUserParameters: ( mId, userId, data ) => - @log.info "DB | storeUserParameters(#{ @setname }): '#{ mId }:#{ userId }'" + storeUserParams: ( mId, userId, data ) => + @log.info "DB | storeUserParams(#{ @setname }): '#{ mId }:#{ userId }'" @db.sadd "#{ @setname }-params", "#{ mId }:#{ userId }", replyHandler "Storing '#{ @setname }' module parameters key '#{ mId }'" @db.set "#{ @setname }-params:#{ mId }:#{ userId }", encrypt( data ), replyHandler "Storing '#{ @setname }' module parameters '#{ mId }:#{ userId }'" - getUserParameters: ( mId, userId, cb ) => - @log.info "DB | getUserParameters(#{ @setname }): '#{ mId }:#{ userId }'" + getUserParams: ( mId, userId, cb ) => + @log.info "DB | getUserParams(#{ @setname }): '#{ mId }:#{ userId }'" @db.get "#{ @setname }-params:#{ mId }:#{ userId }", ( err, data ) -> cb err, decrypt data - getUserParametersIds: ( cb ) => - @log.info "DB | getUserParametersIds(#{ @setname })" + getUserParamsIds: ( cb ) => + @log.info "DB | getUserParamsIds(#{ @setname })" @db.smembers "#{ @setname }-params", cb - deleteUserParameters: ( mId, userId ) => - @log.info "DB | deleteUserParameters(#{ @setname }): '#{ mId }:#{ userId }'" + deleteUserParams: ( mId, userId ) => + @log.info "DB | deleteUserParams(#{ @setname }): '#{ mId }:#{ userId }'" @db.srem "#{ @setname }-params", "#{ mId }:#{ userId }", replyHandler "Deleting '#{ @setname }-params' key '#{ mId }:#{ userId }'" @db.del "#{ @setname }-params:#{ mId }:#{ userId }", replyHandler "Deleting '#{ @setname }-params:#{ mId }:#{ userId }'" -### -## Action Invokers -### - -### -Store a string representation of an action invoker in the DB. - -@public storeActionInvoker ( *aiId, userId, data* ) -@param {String} aiId -@param {String} userId -@param {String} data -### -#TODO adapt testing -exports.storeActionInvoker = ( aiId, userId, data ) => - @ai.storeModule aiId, data - @ai.linkModule aiId, userId - -### -Make an action invoker public. - -@public publishActionInvoker ( *aiId* ) -@param {String} aiId -### - -exports.publishActionInvoker = ( aiId ) => - @ai.publish aiId - -### -Make an action invoker private. - -@public unpublishActionInvoker ( *aiId* ) -@param {String} aiId -### - -exports.unpublishActionInvoker = ( aiId ) => - @ai.unpublish aiId - -### -Query the DB for an action invoker and pass it to cb(err, obj). - -@public getActionInvoker( *aiId, cb* ) -@param {String} aiId -@param {function} cb -### -exports.getActionInvoker = ( aiId, cb ) => - @ai.getModule aiId, cb - -### -Query the DB for action invoker required params and pass it to cb(err, obj). - -@public getActionInvokerEventPollerRequiredParams( *epId, cb* ) -@param {String} epId -@param {function} cb -### -exports.getActionInvokerRequiredParams = ( epId, cb ) => - @ai.getModuleParams epId, cb - -### -Fetch all action invoker IDs and hand them to cb(err, obj). - -@public getActionInvokerIds( *cb* ) -@param {function} cb -### -exports.getActionInvokerIds = ( cb ) => - @ai.getModuleIds cb - -### -Fetch all available actin invoker IDs for a user and -hand them to cb(err, obj). - -@public getAvailableActionInvokerIds( *userId, cb* ) -@param {function} cb -### -exports.getAvailableActionInvokerIds = ( userId, cb ) => - @ai.getAvailableModuleIds userId, cb - -### -Fetch all public action invoker IDs and hand them to cb(err, obj). - -@public getPublicActionInvokerIds( *cb* ) -@param {function} cb -### -exports.getPublicActionInvokerIds = ( cb ) => - @ai.getPublicModuleIds cb - -### -Fetch all action invokers and hand them to cb(err, obj). - -@public getActionInvokers( *cb* ) -@param {function} cb -### -exports.getActionInvokers = ( cb ) => - @ai.getModules cb - -### -Fetch all action invokers and hand them to cb(err, obj). - -@public getActionInvokers( *cb* ) -@param {function} cb -### -exports.deleteActionInvoker = ( aiId ) => - @ai.deleteModule aiId - -### -Store user-specific action invoker parameters . - -@public storeActionUserParams( *userId, aiId, data* ) -@param {String} userId -@param {String} aiId -@param {String} data -### -exports.storeActionUserParams = ( aiId, userId, data ) => - @ai.storeUserParameters aiId, userId, data - -### -Query the DB for user-specific action module parameters, -and pass it to cb(err, obj). - -@public getActionUserParams( *userId, aiId, cb* ) -@param {String} userId -@param {String} aiId -@param {function} cb -### -exports.getActionUserParams = ( aiId, userId, cb ) => - @ai.getUserParameters aiId, userId, cb - -### -Fetch all action params IDs and hand them to cb(err, obj). - -@public getActionUserParamsIds( *cb* ) -@param {function} cb -### -exports.getActionUserParamsIds = ( cb ) => - @ai.getUserParametersIds cb - -### -Fetch all action modules and hand them to cb(err, obj). - -@public deleteActionUserParams( *cb* ) -@param {function} cb -### -exports.deleteActionUserParams = ( aiId, userId ) => - @ai.deleteUserParameters aiId, userId - - -### -## Event Pollers -### - -### -Store a string representation of an event poller in the DB. - -@public storeEventPoller ( *epId, userId, data* ) -@param {String} epId -@param {String} userId -@param {String} data -### -#TODO adapt testing -exports.storeEventPoller = ( epId, userId, data ) => - @ep.storeModule epId, data - @ep.linkModule epId, userId - -### -Make an event poller public. - -@public publishEventPoller ( *epId* ) -@param {String} epId -### - -exports.publishEventPoller = ( epId ) => - @ep.publish epId - -### -Make an event poller private. - -@public unpublishEventPoller ( *epId* ) -@param {String} epId -### - -exports.unpublishEventPoller = ( epId ) => - @ep.unpublish epId - -### -Query the DB for an event poller and pass it to cb(err, obj). - -@public getEventPoller( *epId, cb* ) -@param {String} epId -@param {function} cb -### -exports.getEventPoller = ( epId, cb ) => - @ep.getModule epId, cb - -### -Query the DB for event poller required params and pass it to cb(err, obj). - -@public getEventPollerRequiredParams( *epId, cb* ) -@param {String} epId -@param {function} cb -### -exports.getEventPollerRequiredParams = ( epId, cb ) => - @ep.getModuleParams epId, cb - -### -Fetch all event poller IDs and hand them to cb(err, obj). - -@public getEventPollerIds( *cb* ) -@param {function} cb -### -exports.getEventPollerIds = ( cb ) => - @ep.getModuleIds cb - -### -Fetch all available event poller IDs for a user and -hand them to cb(err, obj). - -@public getAvailableEventPollerIds( *userId, cb* ) -@param {function} cb -### -exports.getAvailableEventPollerIds = ( userId, cb ) => - @ep.getAvailableModuleIds userId, cb - -### -Fetch all public event poller IDs and hand them to cb(err, obj). - -@public getPublicEventPollerIds( *cb* ) -@param {function} cb -### -exports.getPublicEventPollerIds = ( cb ) => - @ep.getPublicModuleIds cb - -### -Fetch all event pollers and hand them to cb(err, obj). - -@public getEventPollers( *cb* ) -@param {function} cb -### -exports.getEventPollers = ( cb ) => - @ep.getModules cb - -### -Fetch all event pollers and hand them to cb(err, obj). - -@public getEventPollers( *cb* ) -@param {function} cb -### -exports.deleteEventPoller = ( epId ) => - # TODO remove from public modules - # TODO remove parameters - # TODO also do this for action invokers - @ep.deleteModule epId - -### -Store user-specific event poller parameters . - -@public storeEventUserParams( *userId, epId, data* ) -@param {String} userId -@param {String} epId -@param {String} data -### -exports.storeEventUserParams = ( epId, userId, data ) => - @ep.storeUserParameters epId, userId, data - -### -Query the DB for user-specific event module parameters, -and pass it to cb(err, obj). - -@public getEventUserParams( *userId, epId, cb* ) -@param {String} userId -@param {String} epId -@param {function} cb -### -exports.getEventUserParams = ( epId, userId, cb ) => - @ep.getUserParameters epId, userId, cb - -### -Fetch all event params IDs and hand them to cb(err, obj). - -@public getEventUserParamsIds( *cb* ) -@param {function} cb -### -exports.getEventUserParamsIds = ( cb ) => - @ep.getUserParametersIds cb - -### -Fetch all event modules and hand them to cb(err, obj). - -@public deleteEventUserParams( *cb* ) -@param {function} cb -### -exports.deleteEventUserParams = ( epId, userId ) => - @ep.deleteUserParameters epId, userId - - ### ## Rules ### diff --git a/coffee/request-handler.coffee b/coffee/request-handler.coffee index 93479a1..1f2fa5c 100644 --- a/coffee/request-handler.coffee +++ b/coffee/request-handler.coffee @@ -67,7 +67,7 @@ exports.handleEvent = ( req, resp ) -> body += data req.on 'end', -> if req.session and req.session.user - obj = JSON.parse body + obj = qs.parse body # If required event properties are present we process the event # if obj and obj.event timestamp = ( new Date ).toISOString() diff --git a/js-coffee/components-manager.js b/js-coffee/components-manager.js index 7f02cbd..4b740ac 100644 --- a/js-coffee/components-manager.js +++ b/js-coffee/components-manager.js @@ -73,7 +73,7 @@ Components Manager answ = { code: 200 }; - return db.getEventPoller(obj.id, function(err, mod) { + return db.eventPollers.getModule(obj.id, function(err, mod) { var cm, id, name, src, _ref; if (mod) { answ.code = 409; @@ -91,14 +91,14 @@ Components Manager } _this.log.info("CM | Storing new eventpoller with events " + events); answ.message = "Event Poller module successfully stored! Found following event(s): " + events; - db.storeEventPoller(obj.id, user.username, { + db.eventPollers.storeModule(obj.id, user.username, { code: obj.data, lang: obj.lang, params: obj.params, events: events }); if (obj["public"] === 'true') { - db.publishEventPoller(obj.id); + db.eventPollers.publish(obj.id); } } } @@ -106,12 +106,12 @@ Components Manager }); }, get_event_pollers: function(user, obj, cb) { - return db.getAvailableEventPollerIds(user.username, function(err, obj) { + return db.eventPollers.getAvailableModuleIds(user.username, function(err, obj) { var fGetEvents, id, oRes, sem, _i, _len, _results; oRes = {}; sem = obj.length; fGetEvents = function(id) { - return db.getEventPoller(id, function(err, obj) { + return db.eventPollers.getModule(id, function(err, obj) { oRes[id] = obj.events; if (--sem === 0) { return cb({ @@ -130,7 +130,7 @@ Components Manager }); }, get_event_poller_params: function(user, obj, cb) { - return db.getEventPollerRequiredParams(obj.id, function(err, obj) { + return db.eventPollers.getModuleParams(obj.id, function(err, obj) { return cb({ code: 200, message: obj @@ -138,12 +138,12 @@ Components Manager }); }, get_action_invokers: function(user, obj, cb) { - return db.getAvailableActionInvokerIds(user.username, function(err, obj) { + return db.actionInvokers.getAvailableModuleIds(user.username, function(err, obj) { var fGetActions, id, oRes, sem, _i, _len, _results; oRes = {}; sem = obj.length; fGetActions = function(id) { - return db.getActionInvoker(id, function(err, obj) { + return db.actionInvokers.getModule(id, function(err, obj) { oRes[id] = obj.actions; if (--sem === 0) { return cb({ @@ -162,7 +162,7 @@ Components Manager }); }, get_action_invoker_params: function(user, obj, cb) { - return db.getActionInvokerRequiredParams(obj.id, function(err, obj) { + return db.actionInvokers.getModuleParams(obj.id, function(err, obj) { return cb({ code: 200, message: obj @@ -174,7 +174,7 @@ Components Manager answ = { code: 200 }; - return db.getActionInvoker(obj.id, function(err, mod) { + return db.actionInvokers.getModule(obj.id, function(err, mod) { var actions, cm, id, name, src, _ref; if (mod) { answ.code = 409; @@ -192,14 +192,14 @@ Components Manager } _this.log.info("CM | Storing new eventpoller with actions " + actions); answ.message = "Action Invoker module successfully stored! Found following action(s): " + actions; - db.storeActionInvoker(obj.id, user.username, { + db.actionInvokers.storeModule(obj.id, user.username, { code: obj.data, lang: obj.lang, params: obj.params, actions: actions }); if (obj["public"] === 'true') { - db.publishActionInvoker(obj.id); + db.actionInvokers.publish(obj.id); } } } @@ -233,10 +233,10 @@ Components Manager db.storeRule(rule.id, JSON.stringify(rule)); db.linkRule(rule.id, user.username); db.activateRule(rule.id, user.username); - db.storeEventUserParams(obj.event.module, user.username, obj.event_params); + db.eventPollers.storeUserParams(obj.event.module, user.username, obj.event_params); for (id in modules) { params = modules[id]; - db.storeActionUserParams(id, user.username, JSON.stringify(params)); + db.actionInvokers.storeUserParams(id, user.username, JSON.stringify(params)); } _this.ee.emit('newRule', JSON.stringify(rule)); } diff --git a/js-coffee/config.js b/js-coffee/config.js index c292923..8777084 100644 --- a/js-coffee/config.js +++ b/js-coffee/config.js @@ -67,6 +67,7 @@ Configuration } } catch (_error) { e = _error; + _this.isReady = false; if (!_this.nolog) { return console.error("Failed loading config file: " + e.message); } diff --git a/js-coffee/engine.js b/js-coffee/engine.js index 68d0a00..fb34409 100644 --- a/js-coffee/engine.js +++ b/js-coffee/engine.js @@ -24,8 +24,8 @@ var updateActionModules = function() { for ( var i = 0; i < actions.length; i++ ){ var arrMod = actions[i].split(' -> '); if ( !listActionModules[user][arrMod[0]] ){ - db.getActionInvoker(arrMod[0], function( err, objAM ){ - db.getActionUserParams(arrMod[0], user, function( err, objParams ) { + db.actionInvokers.getModule(arrMod[0], function( err, objAM ){ + db.actionInvokers.getUserParams(arrMod[0], user, function( err, objParams ) { console.log (objAM); //FIXME am name is called 'actions'??? diff --git a/js-coffee/persistence.js b/js-coffee/persistence.js index 82cfc07..647ef4f 100644 --- a/js-coffee/persistence.js +++ b/js-coffee/persistence.js @@ -54,8 +54,8 @@ Persistence return _this.log.error(err, 'DB | Wrong port?'); } }); - _this.ep = new IndexedModules('event-poller', _this.db, _this.log); - return _this.ai = new IndexedModules('action-invoker', _this.db, _this.log); + exports.eventPollers = new IndexedModules('event-poller', _this.db, _this.log); + return exports.actionInvokers = new IndexedModules('action-invoker', _this.db, _this.log); }; /* @@ -284,10 +284,10 @@ Persistence this.setname = setname; this.db = db; this.log = log; - this.deleteUserParameters = __bind(this.deleteUserParameters, this); - this.getUserParametersIds = __bind(this.getUserParametersIds, this); - this.getUserParameters = __bind(this.getUserParameters, this); - this.storeUserParameters = __bind(this.storeUserParameters, this); + this.deleteUserParams = __bind(this.deleteUserParams, this); + this.getUserParamsIds = __bind(this.getUserParamsIds, this); + this.getUserParams = __bind(this.getUserParams, this); + this.storeUserParams = __bind(this.storeUserParams, this); this.deleteModule = __bind(this.deleteModule, this); this.getModules = __bind(this.getModules, this); this.getModuleIds = __bind(this.getModuleIds, this); @@ -302,11 +302,12 @@ Persistence this.log.info("DB | Instantiated indexed modules for '" + this.setname + "'"); } - IndexedModules.prototype.storeModule = function(mId, data) { + IndexedModules.prototype.storeModule = function(mId, userId, data) { this.log.info("DB | storeModule(" + this.setname + "): " + mId); this.db.sadd("" + this.setname + "s", mId, replyHandler("Storing '" + this.setname + "' key '" + mId + "'")); this.db.hmset("" + this.setname + ":" + mId, 'code', data['code'], replyHandler("Storing '" + this.setname + ":" + mId + "'")); - return this.db.hmset("" + this.setname + ":" + mId, 'reqparams', data['reqparams'], replyHandler("Storing '" + this.setname + ":" + mId + "'")); + this.db.hmset("" + this.setname + ":" + mId, 'reqparams', data['reqparams'], replyHandler("Storing '" + this.setname + ":" + mId + "'")); + return this.linkModule(mId, userId); }; IndexedModules.prototype.linkModule = function(mId, userId) { @@ -362,26 +363,26 @@ Persistence return this.db.del("" + this.setname + ":" + mId, replyHandler("Deleting '" + this.setname + ":" + mId + "'")); }; - IndexedModules.prototype.storeUserParameters = function(mId, userId, data) { - this.log.info("DB | storeUserParameters(" + this.setname + "): '" + mId + ":" + userId + "'"); + IndexedModules.prototype.storeUserParams = function(mId, userId, data) { + this.log.info("DB | storeUserParams(" + this.setname + "): '" + mId + ":" + userId + "'"); this.db.sadd("" + this.setname + "-params", "" + mId + ":" + userId, replyHandler("Storing '" + this.setname + "' module parameters key '" + mId + "'")); return this.db.set("" + this.setname + "-params:" + mId + ":" + userId, encrypt(data), replyHandler("Storing '" + this.setname + "' module parameters '" + mId + ":" + userId + "'")); }; - IndexedModules.prototype.getUserParameters = function(mId, userId, cb) { - this.log.info("DB | getUserParameters(" + this.setname + "): '" + mId + ":" + userId + "'"); + IndexedModules.prototype.getUserParams = function(mId, userId, cb) { + this.log.info("DB | getUserParams(" + this.setname + "): '" + mId + ":" + userId + "'"); return this.db.get("" + this.setname + "-params:" + mId + ":" + userId, function(err, data) { return cb(err, decrypt(data)); }); }; - IndexedModules.prototype.getUserParametersIds = function(cb) { - this.log.info("DB | getUserParametersIds(" + this.setname + ")"); + IndexedModules.prototype.getUserParamsIds = function(cb) { + this.log.info("DB | getUserParamsIds(" + this.setname + ")"); return this.db.smembers("" + this.setname + "-params", cb); }; - IndexedModules.prototype.deleteUserParameters = function(mId, userId) { - this.log.info("DB | deleteUserParameters(" + this.setname + "): '" + mId + ":" + userId + "'"); + IndexedModules.prototype.deleteUserParams = function(mId, userId) { + this.log.info("DB | deleteUserParams(" + this.setname + "): '" + mId + ":" + userId + "'"); this.db.srem("" + this.setname + "-params", "" + mId + ":" + userId, replyHandler("Deleting '" + this.setname + "-params' key '" + mId + ":" + userId + "'")); return this.db.del("" + this.setname + "-params:" + mId + ":" + userId, replyHandler("Deleting '" + this.setname + "-params:" + mId + ":" + userId + "'")); }; @@ -390,374 +391,6 @@ Persistence })(); - /* - ## Action Invokers - */ - - - /* - Store a string representation of an action invoker in the DB. - - @public storeActionInvoker ( *aiId, userId, data* ) - @param {String} aiId - @param {String} userId - @param {String} data - */ - - - exports.storeActionInvoker = function(aiId, userId, data) { - _this.ai.storeModule(aiId, data); - return _this.ai.linkModule(aiId, userId); - }; - - /* - Make an action invoker public. - - @public publishActionInvoker ( *aiId* ) - @param {String} aiId - */ - - - exports.publishActionInvoker = function(aiId) { - return _this.ai.publish(aiId); - }; - - /* - Make an action invoker private. - - @public unpublishActionInvoker ( *aiId* ) - @param {String} aiId - */ - - - exports.unpublishActionInvoker = function(aiId) { - return _this.ai.unpublish(aiId); - }; - - /* - Query the DB for an action invoker and pass it to cb(err, obj). - - @public getActionInvoker( *aiId, cb* ) - @param {String} aiId - @param {function} cb - */ - - - exports.getActionInvoker = function(aiId, cb) { - return _this.ai.getModule(aiId, cb); - }; - - /* - Query the DB for action invoker required params and pass it to cb(err, obj). - - @public getActionInvokerEventPollerRequiredParams( *epId, cb* ) - @param {String} epId - @param {function} cb - */ - - - exports.getActionInvokerRequiredParams = function(epId, cb) { - return _this.ai.getModuleParams(epId, cb); - }; - - /* - Fetch all action invoker IDs and hand them to cb(err, obj). - - @public getActionInvokerIds( *cb* ) - @param {function} cb - */ - - - exports.getActionInvokerIds = function(cb) { - return _this.ai.getModuleIds(cb); - }; - - /* - Fetch all available actin invoker IDs for a user and - hand them to cb(err, obj). - - @public getAvailableActionInvokerIds( *userId, cb* ) - @param {function} cb - */ - - - exports.getAvailableActionInvokerIds = function(userId, cb) { - return _this.ai.getAvailableModuleIds(userId, cb); - }; - - /* - Fetch all public action invoker IDs and hand them to cb(err, obj). - - @public getPublicActionInvokerIds( *cb* ) - @param {function} cb - */ - - - exports.getPublicActionInvokerIds = function(cb) { - return _this.ai.getPublicModuleIds(cb); - }; - - /* - Fetch all action invokers and hand them to cb(err, obj). - - @public getActionInvokers( *cb* ) - @param {function} cb - */ - - - exports.getActionInvokers = function(cb) { - return _this.ai.getModules(cb); - }; - - /* - Fetch all action invokers and hand them to cb(err, obj). - - @public getActionInvokers( *cb* ) - @param {function} cb - */ - - - exports.deleteActionInvoker = function(aiId) { - return _this.ai.deleteModule(aiId); - }; - - /* - Store user-specific action invoker parameters . - - @public storeActionUserParams( *userId, aiId, data* ) - @param {String} userId - @param {String} aiId - @param {String} data - */ - - - exports.storeActionUserParams = function(aiId, userId, data) { - return _this.ai.storeUserParameters(aiId, userId, data); - }; - - /* - Query the DB for user-specific action module parameters, - and pass it to cb(err, obj). - - @public getActionUserParams( *userId, aiId, cb* ) - @param {String} userId - @param {String} aiId - @param {function} cb - */ - - - exports.getActionUserParams = function(aiId, userId, cb) { - return _this.ai.getUserParameters(aiId, userId, cb); - }; - - /* - Fetch all action params IDs and hand them to cb(err, obj). - - @public getActionUserParamsIds( *cb* ) - @param {function} cb - */ - - - exports.getActionUserParamsIds = function(cb) { - return _this.ai.getUserParametersIds(cb); - }; - - /* - Fetch all action modules and hand them to cb(err, obj). - - @public deleteActionUserParams( *cb* ) - @param {function} cb - */ - - - exports.deleteActionUserParams = function(aiId, userId) { - return _this.ai.deleteUserParameters(aiId, userId); - }; - - /* - ## Event Pollers - */ - - - /* - Store a string representation of an event poller in the DB. - - @public storeEventPoller ( *epId, userId, data* ) - @param {String} epId - @param {String} userId - @param {String} data - */ - - - exports.storeEventPoller = function(epId, userId, data) { - _this.ep.storeModule(epId, data); - return _this.ep.linkModule(epId, userId); - }; - - /* - Make an event poller public. - - @public publishEventPoller ( *epId* ) - @param {String} epId - */ - - - exports.publishEventPoller = function(epId) { - return _this.ep.publish(epId); - }; - - /* - Make an event poller private. - - @public unpublishEventPoller ( *epId* ) - @param {String} epId - */ - - - exports.unpublishEventPoller = function(epId) { - return _this.ep.unpublish(epId); - }; - - /* - Query the DB for an event poller and pass it to cb(err, obj). - - @public getEventPoller( *epId, cb* ) - @param {String} epId - @param {function} cb - */ - - - exports.getEventPoller = function(epId, cb) { - return _this.ep.getModule(epId, cb); - }; - - /* - Query the DB for event poller required params and pass it to cb(err, obj). - - @public getEventPollerRequiredParams( *epId, cb* ) - @param {String} epId - @param {function} cb - */ - - - exports.getEventPollerRequiredParams = function(epId, cb) { - return _this.ep.getModuleParams(epId, cb); - }; - - /* - Fetch all event poller IDs and hand them to cb(err, obj). - - @public getEventPollerIds( *cb* ) - @param {function} cb - */ - - - exports.getEventPollerIds = function(cb) { - return _this.ep.getModuleIds(cb); - }; - - /* - Fetch all available event poller IDs for a user and - hand them to cb(err, obj). - - @public getAvailableEventPollerIds( *userId, cb* ) - @param {function} cb - */ - - - exports.getAvailableEventPollerIds = function(userId, cb) { - return _this.ep.getAvailableModuleIds(userId, cb); - }; - - /* - Fetch all public event poller IDs and hand them to cb(err, obj). - - @public getPublicEventPollerIds( *cb* ) - @param {function} cb - */ - - - exports.getPublicEventPollerIds = function(cb) { - return _this.ep.getPublicModuleIds(cb); - }; - - /* - Fetch all event pollers and hand them to cb(err, obj). - - @public getEventPollers( *cb* ) - @param {function} cb - */ - - - exports.getEventPollers = function(cb) { - return _this.ep.getModules(cb); - }; - - /* - Fetch all event pollers and hand them to cb(err, obj). - - @public getEventPollers( *cb* ) - @param {function} cb - */ - - - exports.deleteEventPoller = function(epId) { - return _this.ep.deleteModule(epId); - }; - - /* - Store user-specific event poller parameters . - - @public storeEventUserParams( *userId, epId, data* ) - @param {String} userId - @param {String} epId - @param {String} data - */ - - - exports.storeEventUserParams = function(epId, userId, data) { - return _this.ep.storeUserParameters(epId, userId, data); - }; - - /* - Query the DB for user-specific event module parameters, - and pass it to cb(err, obj). - - @public getEventUserParams( *userId, epId, cb* ) - @param {String} userId - @param {String} epId - @param {function} cb - */ - - - exports.getEventUserParams = function(epId, userId, cb) { - return _this.ep.getUserParameters(epId, userId, cb); - }; - - /* - Fetch all event params IDs and hand them to cb(err, obj). - - @public getEventUserParamsIds( *cb* ) - @param {function} cb - */ - - - exports.getEventUserParamsIds = function(cb) { - return _this.ep.getUserParametersIds(cb); - }; - - /* - Fetch all event modules and hand them to cb(err, obj). - - @public deleteEventUserParams( *cb* ) - @param {function} cb - */ - - - exports.deleteEventUserParams = function(epId, userId) { - return _this.ep.deleteUserParameters(epId, userId); - }; - /* ## Rules */ diff --git a/js-coffee/request-handler.js b/js-coffee/request-handler.js index 1779c88..a3634b1 100644 --- a/js-coffee/request-handler.js +++ b/js-coffee/request-handler.js @@ -74,7 +74,7 @@ Request Handler return req.on('end', function() { var answ, obj, rand, timestamp; if (req.session && req.session.user) { - obj = JSON.parse(body); + obj = qs.parse(body); if (obj && obj.event) { timestamp = (new Date).toISOString(); rand = (Math.floor(Math.random() * 10e9)).toString(16).toUpperCase(); diff --git a/js/engine.js b/js/engine.js index 8b2f2b4..2191b33 100644 --- a/js/engine.js +++ b/js/engine.js @@ -33,7 +33,7 @@ exports.addDBLinkAndLoadActionsAndRules = function(db_link) { //TODO only load rules on beginning, if rules require certain actions, load them in order to allow fast firing // if rules are set inactive, remove the action module from the memory db = db_link; - if(ml && db) db.getActionModules(function(err, obj) { + if(ml && db) db.actionInvokers.getModules(function(err, obj) { if(err) log.error('EN', 'retrieving Action Modules from DB!'); else { if(!obj) { @@ -44,11 +44,12 @@ exports.addDBLinkAndLoadActionsAndRules = function(db_link) { log.print('EN', 'Loading Action Module from DB: ' + el); try { m = ml.requireFromString(obj[el], el); - db.getActionModuleAuth(el, function(mod) { - return function(err, obj) { - if(obj && mod.loadCredentials) mod.loadCredentials(JSON.parse(obj)); - }; - }(m)); + // db.getActionModuleAuth(el, function(mod) { + // return function(err, obj) { + // if(obj && mod.loadCredentials) mod.loadCredentials(JSON.parse(obj)); + // }; + // }(m)); + //FIXME !!! listActionModules[el] = m; } catch(e) { e.addInfo = 'error in action module "' + el + '"'; diff --git a/testing/files/jsonTestConfig.json b/testing/files/jsonTestConfig.json new file mode 100644 index 0000000..473be0e --- /dev/null +++ b/testing/files/jsonTestConfig.json @@ -0,0 +1,10 @@ +{ + "http-port": 8125, + "db-port": 6379, + "log": { + "mode": "development", + "io-level": "info", + "file-level": "info", + "file-path": "server.log" + } +} \ No newline at end of file diff --git a/testing/files/jsonWrongConfig.json b/testing/files/jsonWrongConfig.json deleted file mode 100644 index 2da4358..0000000 --- a/testing/files/jsonWrongConfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "http_port": 2, - "db_port": 9, - "crypto_key": "", - "session_secret": "" -} \ No newline at end of file diff --git a/testing/test_config.coffee b/testing/test_config.coffee index f2caf0e..800a26c 100644 --- a/testing/test_config.coffee +++ b/testing/test_config.coffee @@ -33,8 +33,8 @@ exports.testParameters = ( test ) => exports.testDifferentConfigFile = ( test ) => test.expect 1 @conf - nolog: true - configPath: path.join 'testing', 'files', 'jsonWrongConfig.json' + # nolog: true + configPath: path.join 'testing', 'files', 'jsonTestConfig.json' test.ok @conf.isReady(), 'Different path not loaded!' test.done() diff --git a/testing/test_persistence.coffee b/testing/test_persistence.coffee index 130b380..85fe446 100644 --- a/testing/test_persistence.coffee +++ b/testing/test_persistence.coffee @@ -136,177 +136,8 @@ exports.EventQueue = ### -# Test ACTION INVOKER -### -exports.ActionInvoker = - setUp: ( cb ) => - @userId = 'tester1' - @action1id = 'test-action-invoker_1' - @action2id = 'test-action-invoker_2' - @action1 = - code: 'unit-test action invoker 1 content' - reqparams:'[param11,param12]' - @action2 = - code: 'unit-test action invoker 2 content' - reqparams:'[param21,param22]' - cb() - - tearDown: ( cb ) => - @db.deleteActionInvoker @action1id - @db.deleteActionInvoker @action2id - cb() - - testCreateAndRead: ( test ) => - test.expect 3 - # store an entry to start with - @db.storeActionInvoker @action1id, @userId, @action1 - - # test that the ID shows up in the set - @db.getActionInvokerIds ( err , obj ) => - test.ok @action1id in obj, - 'Expected key not in action-invokers set' - - # the retrieved object really is the one we expected - @db.getActionInvoker @action1id, ( err , obj ) => - test.deepEqual obj, @action1, - 'Retrieved Action Invoker is not what we expected' - - # Ensure the action invoker is in the list of all existing ones - @db.getActionInvokers ( err , obj ) => - test.deepEqual @action1, obj[@action1id], - 'Action Invoker ist not in result set' - test.done() - - testUpdate: ( test ) => - test.expect 2 - - # store an entry to start with - @db.storeActionInvoker @action1id, @userId, @action1 - @db.storeActionInvoker @action1id, @userId, @action2 - - # the retrieved object really is the one we expected - @db.getActionInvoker @action1id, ( err , obj ) => - test.deepEqual obj, @action2, - 'Retrieved Action Invoker is not what we expected' - - # Ensure the action invoker is in the list of all existing ones - @db.getActionInvokers ( err , obj ) => - test.deepEqual @action2, obj[@action1id], - 'Action Invoker ist not in result set' - test.done() - - testDelete: ( test ) => - test.expect 2 - - # store an entry to start with - @db.storeActionInvoker @action1id, @userId, @action1 - - # Ensure the action invoker has been deleted - @db.deleteActionInvoker @action1id - @db.getActionInvoker @action1id, ( err , obj ) => - test.strictEqual obj, null, - 'Action Invoker still exists' - - # Ensure the ID has been removed from the set - @db.getActionInvokerIds ( err , obj ) => - test.ok @action1id not in obj, - 'Action Invoker key still exists in set' - test.done() - - - testFetchSeveral: ( test ) => - test.expect 3 - - semaphore = 2 - - fCheckInvoker = ( modname, mod ) => - myTest = test - forkEnds = () -> - myTest.done() if --semaphore is 0 - ( err, obj ) => - myTest.deepEqual mod, obj, - "Invoker #{ modname } does not equal the expected one" - forkEnds() - - @db.storeActionInvoker @action1id, @userId, @action1 - @db.storeActionInvoker @action2id, @userId, @action2 - @db.getActionInvokerIds ( err, obj ) => - test.ok @action1id in obj and @action2id in obj, - 'Not all action invoker Ids in set' - @db.getActionInvoker @action1id, fCheckInvoker @action1id, @action1 - @db.getActionInvoker @action2id, fCheckInvoker @action2id, @action2 - - -### -# Test ACTION INVOKER PARAMS -### -#TODO add tests for required parameters per module -exports.ActionInvokerParams = - testCreateAndRead: ( test ) => - test.expect 2 - - userId = 'tester1' - actionId = 'test-action-invoker_1' - params = 'shouldn\'t this be an object?' - - # store an entry to start with - @db.storeActionUserParams actionId, userId, params - - # test that the ID shows up in the set - @db.getActionUserParamsIds ( err, obj ) => - test.ok actionId+':'+userId in obj, - 'Expected key not in action-params set' - - # the retrieved object really is the one we expected - @db.getActionUserParams actionId, userId, ( err, obj ) => - test.strictEqual obj, params, - 'Retrieved action params is not what we expected' - @db.deleteActionUserParams actionId, userId - test.done() - - testUpdate: ( test ) => - test.expect 1 - - userId = 'tester1' - actionId = 'test-action-invoker_1' - params = 'shouldn\'t this be an object?' - paramsNew = 'shouldn\'t this be a new object?' - - # store an entry to start with - @db.storeActionUserParams actionId, userId, params - @db.storeActionUserParams actionId, userId, paramsNew - - # the retrieved object really is the one we expected - @db.getActionUserParams actionId, userId, ( err, obj ) => - test.strictEqual obj, paramsNew, - 'Retrieved action params is not what we expected' - @db.deleteActionUserParams actionId, userId - test.done() - - testDelete: ( test ) => - test.expect 2 - - userId = 'tester1' - actionId = 'test-action-invoker_1' - params = 'shouldn\'t this be an object?' - - # store an entry to start with and delte it right away - @db.storeActionUserParams actionId, userId, params - @db.deleteActionUserParams actionId, userId - - # Ensure the action params have been deleted - @db.getActionUserParams actionId, userId, ( err, obj ) => - test.strictEqual obj, null, - 'Action params still exists' - # Ensure the ID has been removed from the set - @db.getActionUserParamsIds ( err, obj ) => - test.ok actionId+':'+userId not in obj, - 'Action Params key still exists in set' - test.done() - - -### -# Test EVENT POLLER +# Test Indexed Module from persistence. Testing only Event Poller is sufficient +# since Action Invoker uses the same class ### exports.EventPoller = setUp: ( cb ) => @@ -322,27 +153,26 @@ exports.EventPoller = cb() tearDown: ( cb ) => - @db.deleteEventPoller @event1id - @db.deleteEventPoller @event2id + @db.eventPollers.deleteModule @event1id + @db.eventPollers.deleteModule @event2id cb() testCreateAndRead: ( test ) => test.expect 3 - # store an entry to start with - @db.storeEventPoller @event1id, @userId, @event1 + @db.eventPollers.storeModule @event1id, @userId, @event1 # test that the ID shows up in the set - @db.getEventPollerIds ( err , obj ) => + @db.eventPollers.getModuleIds ( err , obj ) => test.ok @event1id in obj, 'Expected key not in event-pollers set' # the retrieved object really is the one we expected - @db.getEventPoller @event1id, ( err , obj ) => + @db.eventPollers.getModule @event1id, ( err , obj ) => test.deepEqual obj, @event1, 'Retrieved Event Poller is not what we expected' # Ensure the event poller is in the list of all existing ones - @db.getEventPollers ( err , obj ) => + @db.eventPollers.getModules ( err , obj ) => test.deepEqual @event1, obj[@event1id], 'Event Poller ist not in result set' test.done() @@ -351,16 +181,16 @@ exports.EventPoller = test.expect 2 # store an entry to start with - @db.storeEventPoller @event1id, @userId, @event1 - @db.storeEventPoller @event1id, @userId, @event2 + @db.eventPollers.storeModule @event1id, @userId, @event1 + @db.eventPollers.storeModule @event1id, @userId, @event2 # the retrieved object really is the one we expected - @db.getEventPoller @event1id, ( err , obj ) => + @db.eventPollers.getModule @event1id, ( err , obj ) => test.deepEqual obj, @event2, 'Retrieved Event Poller is not what we expected' # Ensure the event poller is in the list of all existing ones - @db.getEventPollers ( err , obj ) => + @db.eventPollers.getModules ( err , obj ) => test.deepEqual @event2, obj[@event1id], 'Event Poller ist not in result set' test.done() @@ -369,16 +199,16 @@ exports.EventPoller = test.expect 2 # store an entry to start with - @db.storeEventPoller @event1id, @userId, @event1 + @db.eventPollers.storeModule @event1id, @userId, @event1 # Ensure the event poller has been deleted - @db.deleteEventPoller @event1id - @db.getEventPoller @event1id, ( err , obj ) => + @db.eventPollers.deleteModule @event1id + @db.eventPollers.getModule @event1id, ( err , obj ) => test.strictEqual obj, null, 'Event Poller still exists' # Ensure the ID has been removed from the set - @db.getEventPollerIds ( err , obj ) => + @db.eventPollers.getModuleIds ( err , obj ) => test.ok @event1id not in obj, 'Event Poller key still exists in set' test.done() @@ -398,13 +228,13 @@ exports.EventPoller = "Invoker #{ modname } does not equal the expected one" forkEnds() - @db.storeEventPoller @event1id, @userId, @event1 - @db.storeEventPoller @event2id, @userId, @event2 - @db.getEventPollerIds ( err, obj ) => + @db.eventPollers.storeModule @event1id, @userId, @event1 + @db.eventPollers.storeModule @event2id, @userId, @event2 + @db.eventPollers.getModuleIds ( err, obj ) => test.ok @event1id in obj and @event2id in obj, 'Not all event poller Ids in set' - @db.getEventPoller @event1id, fCheckInvoker @event1id, @event1 - @db.getEventPoller @event2id, fCheckInvoker @event2id, @event2 + @db.eventPollers.getModule @event1id, fCheckInvoker @event1id, @event1 + @db.eventPollers.getModule @event2id, fCheckInvoker @event2id, @event2 ### @@ -419,18 +249,18 @@ exports.EventPollerParams = params = 'shouldn\'t this be an object?' # store an entry to start with - @db.storeEventUserParams eventId, userId, params + @db.eventPollers.storeUserParams eventId, userId, params # test that the ID shows up in the set - @db.getEventUserParamsIds ( err, obj ) => + @db.eventPollers.getUserParamsIds ( err, obj ) => test.ok eventId+':'+userId in obj, 'Expected key not in event-params set' # the retrieved object really is the one we expected - @db.getEventUserParams eventId, userId, ( err, obj ) => + @db.eventPollers.getUserParams eventId, userId, ( err, obj ) => test.strictEqual obj, params, 'Retrieved event params is not what we expected' - @db.deleteEventUserParams eventId, userId + @db.eventPollers.deleteUserParams eventId, userId test.done() testUpdate: ( test ) => @@ -442,14 +272,14 @@ exports.EventPollerParams = paramsNew = 'shouldn\'t this be a new object?' # store an entry to start with - @db.storeEventUserParams eventId, userId, params - @db.storeEventUserParams eventId, userId, paramsNew + @db.eventPollers.storeUserParams eventId, userId, params + @db.eventPollers.storeUserParams eventId, userId, paramsNew # the retrieved object really is the one we expected - @db.getEventUserParams eventId, userId, ( err, obj ) => + @db.eventPollers.getUserParams eventId, userId, ( err, obj ) => test.strictEqual obj, paramsNew, 'Retrieved event params is not what we expected' - @db.deleteEventUserParams eventId, userId + @db.eventPollers.deleteUserParams eventId, userId test.done() testDelete: ( test ) => @@ -460,15 +290,15 @@ exports.EventPollerParams = params = 'shouldn\'t this be an object?' # store an entry to start with and delete it right away - @db.storeEventUserParams eventId, userId, params - @db.deleteEventUserParams eventId, userId + @db.eventPollers.storeUserParams eventId, userId, params + @db.eventPollers.deleteUserParams eventId, userId # Ensure the event params have been deleted - @db.getEventUserParams eventId, userId, ( err, obj ) => + @db.eventPollers.getUserParams eventId, userId, ( err, obj ) => test.strictEqual obj, null, 'Event params still exists' # Ensure the ID has been removed from the set - @db.getEventUserParamsIds ( err, obj ) => + @db.eventPollers.getUserParamsIds ( err, obj ) => test.ok eventId+':'+userId not in obj, 'Event Params key still exists in set' test.done() @@ -878,3 +708,4 @@ exports.Roles = "User #{ @oUser.username } not stored in role tester!" test.done() + # store an entry to start with diff --git a/testing/test_request-handler.coffee b/testing/test_request-handler.coffee index 7d698a2..6ec9738 100644 --- a/testing/test_request-handler.coffee +++ b/testing/test_request-handler.coffee @@ -60,78 +60,6 @@ exports.tearDown = ( cb ) => @db.shutDown() cb() -exports.events = - setUp: ( cb ) => - @db.purgeEventQueue() - cb() - - testCorrectEvent: ( test ) => - test.expect 2 - - args = - logger: log - args[ 'request-service' ] = ( usr, obj, cb ) -> - test.ok false, 'testEvent should not cause a service request call' - args[ 'shutdown-function' ] = () -> - test.ok false, 'testEvent should not cause a system shutdown' - @rh args - - oEvt = - event: 'unittest' - eventid: 'ut234' - data: 'a lot of data' - - semaphore = 2 - fPopEvent = () => - fCb = ( err, obj ) -> - test.deepEqual obj, oEvt, 'Caught event is not what we expected' - if --semaphore is 0 - test.done() - @db.popEvent fCb - - req = createRequest() - resp = createResponse ( code, msg ) -> - test.strictEqual code, 200 - if --semaphore is 0 - test.done() - - @rh.handleEvent req, resp # set the handler to listening - postRequestData req, qs.stringify oEvt # emit the data post event - setTimeout fPopEvent, 200 # try to fetch the db entry - - testIncorrectEvent: ( test ) => - test.expect 2 - - args = - logger: log - args[ 'request-service' ] = ( usr, obj, cb ) -> - test.ok false, 'testEvent should not cause a service request call' - args[ 'shutdown-function' ] = () -> - test.ok false, 'testEvent should not cause a system shutdown' - @rh args - - oEvt = - event: 'unittest' - data: 'event misses eventid property' - - semaphore = 2 - fPopEvent = () => - fCb = ( err, obj ) -> - test.deepEqual obj, null, 'We caught an event!?' - if --semaphore is 0 - test.done() - @db.popEvent fCb - - req = createRequest() - resp = createResponse ( code, msg ) -> - test.strictEqual code, 400 - if --semaphore is 0 - test.done() - - @rh.handleEvent req, resp # set the handler to listening - postRequestData req, qs.stringify oEvt # emit the data post event - setTimeout fPopEvent, 200 # try to fetch the db entry - exports.session = setUp: ( cb ) => @user = @@ -210,6 +138,76 @@ exports.session = @rh.handleLogin req, resp # set the handler to listening postRequestData req, qs.stringify usr # emit the data post event +exports.events = + setUp: ( cb ) => + @db.purgeEventQueue() + cb() + + testCorrectEvent: ( test ) => + test.expect 2 + + args = + logger: log + args[ 'request-service' ] = ( usr, obj, cb ) -> + test.ok false, 'testEvent should not cause a service request call' + args[ 'shutdown-function' ] = () -> + test.ok false, 'testEvent should not cause a system shutdown' + @rh args + + oEvt = + event: 'unittest' + data: 'a lot of data' + + semaphore = 2 + fPopEvent = () => + fCb = ( err, obj ) -> + oEvt.eventid = obj.eventid # because the event id is generated by the system + test.deepEqual obj, oEvt, 'Caught event is not what we expected' + if --semaphore is 0 + test.done() + @db.popEvent fCb + + req = createLoggedInRequest() + resp = createResponse ( code, msg ) -> + test.strictEqual code, 200 + if --semaphore is 0 + test.done() + + @rh.handleEvent req, resp # set the handler to listening + postRequestData req, qs.stringify oEvt # emit the data post event + setTimeout fPopEvent, 200 # try to fetch the db entry + + testIncorrectEvent: ( test ) => + test.expect 2 + + args = + logger: log + args[ 'request-service' ] = ( usr, obj, cb ) -> + test.ok false, 'testEvent should not cause a service request call' + args[ 'shutdown-function' ] = () -> + test.ok false, 'testEvent should not cause a system shutdown' + @rh args + + oEvt = + data: 'event misses event type property' + + semaphore = 2 + fPopEvent = () => + fCb = ( err, obj ) -> + test.deepEqual obj, null, 'We caught an event!?' + if --semaphore is 0 + test.done() + @db.popEvent fCb + + req = createLoggedInRequest() + resp = createResponse ( code, msg ) -> + test.strictEqual code, 400 + if --semaphore is 0 + test.done() + + @rh.handleEvent req, resp # set the handler to listening + postRequestData req, qs.stringify oEvt # emit the data post event + setTimeout fPopEvent, 200 # try to fetch the db entry exports.testLoginOrPage = ( test ) => test.expect 3 @@ -281,6 +279,7 @@ exports.testUserCommands = ( test ) => # get_actionmodules oRespData = + code: 200 some: 'very' important: 'data' @@ -288,7 +287,7 @@ exports.testUserCommands = ( test ) => logger: log args[ 'request-service' ] = ( usr, obj, cb ) -> test.ok true, 'Yay we got the request!' - cb null, oRespData + cb oRespData args[ 'shutdown-function' ] = () -> test.ok false, 'testEvent should not cause a system shutdown' @rh args