diff --git a/lib/agentLogic.js b/lib/agentLogic.js index 51d28a2..74a1318 100644 --- a/lib/agentLogic.js +++ b/lib/agentLogic.js @@ -280,7 +280,7 @@ function deleteTrans(req, res) { var id = req.param('id_trans', null); if (id) { - dataSrv.deleteTrans(id, function (e) { + dataSrv.deleteTrans(req.prefix, id, function (e) { if (e) { logger.info('deleteTrans', [ {errors: [e]}, @@ -334,7 +334,7 @@ function getQueue(req, res) { res.send(500, {errors: [String(err)]}); } else { var mapTrans = function (v) { - var id = v.split('|')[1]; + var id = v.split('|')[2]; return { id: id, href: 'http://' + req.headers.host + '/trans/' + id + '?queues=All' @@ -797,7 +797,7 @@ function transMeta(req, res) { ]); res.send(400, {errors: ['missing id']}); } else { - dataSrv.getTransactionMeta(id, function (errM, dataM) { + dataSrv.getTransactionMeta(req.prefix, id, function (errM, dataM) { if (errM) { logger.info('transMeta', [ {errors: [errM]}, @@ -810,7 +810,7 @@ function transMeta(req, res) { dataM = dataM || {}; if (queues !== null) { - dataSrv.getTransaction(id, queues, summary, function (errQ, dataQ) { + dataSrv.getTransaction(req.prefix, id, queues, summary, function (errQ, dataQ) { if (errQ) { logger.info('transMeta', [ {errors: [errQ]}, diff --git a/lib/dataSrv.js b/lib/dataSrv.js index 3e61d0c..ff355c8 100644 --- a/lib/dataSrv.js +++ b/lib/dataSrv.js @@ -43,7 +43,7 @@ var pushTransaction = function(appPrefix, provision, callback) { var priority = provision.priority + ':', //contains "H" || "L" queues = provision.queue, //[{},{}] //list of ids extTransactionId = uuid.v4(), - transactionId = config.dbKeyTransPrefix + extTransactionId, + transactionId = config.dbKeyTransPrefix + appPrefix + '|' + extTransactionId, //setting up the bach proceses for async module. processBatch = [], dbTr = dbCluster.getTransactionDb(extTransactionId), @@ -126,7 +126,7 @@ var pushTransaction = function(appPrefix, provision, callback) { var updateTransMeta = function(appPrefix, extTransactionId, provision, callback) { 'use strict'; - var transactionId = config.dbKeyTransPrefix + extTransactionId, + var transactionId = config.dbKeyTransPrefix + appPrefix + '|' + extTransactionId, processBatch = [], priority, queue, @@ -466,7 +466,7 @@ function retrieveData(queue, transactionList, callback) { 'use strict'; var ghostBusterBatch = transactionList.map(function prepareDataBatch(transaction) { - var extTransactionId = transaction.split('|')[1], + var extTransactionId = transaction.split('|')[2], dbTr = dbCluster.getTransactionDb(extTransactionId); return checkData(queue, dbTr, transaction, extTransactionId); }); @@ -506,7 +506,7 @@ function checkData(queue, dbTr, transactionId, extTransactionId) { //uses state emum ('pending', 'closed', 'error') //callback return transaction info -var getTransaction = function(extTransactionId, state, summary, callback) { +var getTransaction = function(appPrefix, extTransactionId, state, summary, callback) { 'use strict'; var err = null, // dbTr = null, // @@ -522,7 +522,7 @@ var getTransaction = function(extTransactionId, state, summary, callback) { } else { //obtain transaction info dbTr = dbCluster.getTransactionDb(extTransactionId); - transactionId = config.dbKeyTransPrefix + extTransactionId; + transactionId = config.dbKeyTransPrefix + appPrefix + '|' + extTransactionId; dbTr.hgetall(transactionId + ':state', function on_data(err, data) { if (err) { manageError(err, callback); @@ -585,14 +585,14 @@ var getTransaction = function(extTransactionId, state, summary, callback) { //callback return transaction info -var getTransactionMeta = function(extTransactionId, callback) { +var getTransactionMeta = function(appPrefix, extTransactionId, callback) { 'use strict'; var err, dbTr, transactionId; //obtain transaction info dbTr = dbCluster.getTransactionDb(extTransactionId); - transactionId = config.dbKeyTransPrefix + extTransactionId; + transactionId = config.dbKeyTransPrefix + appPrefix + '|' + extTransactionId; dbTr.hgetall(transactionId + ':meta', function onDataMeta(err, data) { if (err) { manageError(err, callback); @@ -651,11 +651,11 @@ var getQueue = function(appPrefix, queueId, callback) { }; -var deleteTrans = function(extTransactionId, cb) { +var deleteTrans = function(appPrefix, extTransactionId, cb) { 'use strict'; var dbTr = dbCluster.getTransactionDb(extTransactionId), - meta = config.dbKeyTransPrefix + - extTransactionId + ':meta', state = config.dbKeyTransPrefix + + meta = config.dbKeyTransPrefix + appPrefix + '|' + + extTransactionId + ':meta', state = config.dbKeyTransPrefix + appPrefix + '|' extTransactionId + ':state'; dbTr.del(meta, state, function onDeleted(err) { @@ -665,94 +665,13 @@ var deleteTrans = function(extTransactionId, cb) { }); }; -var setPayload = function(extTransactionId, payload, cb) { - 'use strict'; - var dbTr = dbCluster.getTransactionDb(extTransactionId), - meta = config.dbKeyTransPrefix + - extTransactionId + ':meta'; - - helper.exists(dbTr, meta, function(errE, value) { - if (errE) { - cb(errE); - } - else if (! value) { - cb(extTransactionId + ' does not exist'); - } - else { - - dbTr.hset(meta, 'payload', payload, function cbSetPayload(err) { - if (cb) { - cb(err); - } - }); - } - }); -}; - -var setUrlCallback = function(extTransactionId, urlCallback, cb) { - 'use strict'; - var dbTr = dbCluster.getTransactionDb(extTransactionId), - meta = config.dbKeyTransPrefix + - extTransactionId + ':meta'; - - helper.exists(dbTr, meta, function(errE, value) { - if (errE) { - cb(errE); - } - else if (! value) { - cb(extTransactionId + ' does not exist'); - } - else { - - dbTr.hset(meta, 'callback', urlCallback, function cbSetUrlCallback(err) { - if (cb) { - cb(err); - } - }); - } - }); -}; - - -//deprecated -var setExpirationDate = function(extTransactionId, date, cb) { - 'use strict'; - var dbTr = dbCluster.getTransactionDb(extTransactionId), - meta = config.dbKeyTransPrefix + - extTransactionId + ':meta', state = config.dbKeyTransPrefix + - extTransactionId + ':state'; - - helper.exists(dbTr, meta, function(errE, value) { - if (errE) { - cb(errE); - } - else if (! value) { - cb(extTransactionId + ' does not exist'); - } - else { - dbTr.hset(meta, 'expirationDate', date, - function cbHsetExpirationDate(errE) { - helper.setExpirationDate(dbTr, meta, {expirationDate: date}, - function cbExpirationDateMeta(errM) { - helper.setExpirationDate(dbTr, state, {expirationDate: date}, - function cbExpirationDateState(errS) { - if (cb) { - cb(errE || errM || errS); - } - }); - }); - }); - } - }); -}; - var repushUndeliveredTransaction = function(appPrefix, queue, priority, extTransactionID, cb) { var priority = priority + ':', db = dbCluster.getDb(queue.id), dbTr = dbCluster.getTransactionDb(extTransactionID), - transactionID = config.dbKeyTransPrefix + extTransactionID; + transactionID = config.dbKeyTransPrefix + appPrefix + '|' + extTransactionID; async.parallel([ helper.pushHeadParallel(db, {id: appPrefix + queue.id}, priority, transactionID), @@ -792,6 +711,7 @@ exports.pushTransaction = pushTransaction; /** * + * @param {string} appPrefix For secure/non secure behaviour. * @param {string} extTransactionId valid uuid.v1. * @param {string} state enum takes one of 'All', 'Pending', 'Delivered'. * @param {boolean} summary true for summary, optional. @@ -801,6 +721,7 @@ exports.getTransaction = getTransaction; /** * + * @param {string} appPrefix For secure/non secure behaviour. * @param {string} extTransactionId valid uuid.v1. * @param {string} state enum takes one of 'All', 'Pending', 'Delivered'. * @param {boolean} summary true for summary, optional. @@ -869,32 +790,7 @@ exports.getSecHash = getSecHash; exports.deleteTrans = deleteTrans; /** - * - * @param {string} extTransactionId valid uuid.v1. - * @param {string} payload - * @param cb - */ -exports.setPayload = setPayload; - -/** - * - * @param {string} extTransactionId valid uuid.v1. - * @param {string} URL for callback. - * @param cb - */ - -exports.setUrlCallback = setUrlCallback; - -/** - * - * @param {string} extTransactionId valid uuid.v1. - * @param date - * @param cb - */ -exports.setExpirationDate = setExpirationDate; - -/** - * + * @param {string} appPrefix For secure/non secure behaviour. * @param transactionId * @param provision * @param callback diff --git a/test/unit/orgTest.js b/test/unit/orgTest.js new file mode 100644 index 0000000..fc852df --- /dev/null +++ b/test/unit/orgTest.js @@ -0,0 +1,228 @@ +var async = require('async'); +var should = require('should'); +var config = require('./../config.js') +var utils = require('./../utils.js'); + +var agent = require('../../.'); + +describe('Organization Test', function() { + + var QUEUE = 'Q1'; + var ORG_A = 'TID'; + var ORG_B = 'OIL'; + var PAYLOAD = 'TEST TRANSACTION'; + var PRIORITY = 'H'; + var transID; + + before(function(done){ + agent.start(done); + }); + + after(function(done) { + utils.cleanBBDD(function() { + agent.stop(done); + } ); + }); + + beforeEach(function(done) { + //Clean BBDD and push a transaction in one organization + utils.cleanBBDD(function() { + var trans = utils.createTransaction(PAYLOAD, PRIORITY, [ { 'id': QUEUE } ]); + utils.pushTransactionOrg(ORG_A, trans, function(err, response, data) { + + response.statusCode.should.be.equal(200); + should.not.exist(err); + + data.should.have.property('data'); + transID = data.data; + + done(); + + }); + }); + }); + + it('GET /trans/id only works for ' + ORG_A, function(done) { + + var orgATest = function(done) { + utils.getTransStateOrg(ORG_A, transID, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.have.property('payload'); + data.should.have.property('priority'); + + data.payload.should.be.equal(PAYLOAD); + data.priority.should.be.equal(PRIORITY); + + done(); + + }); + }; + + var orgBTest = function(done) { + utils.getTransStateOrg(ORG_B, transID, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + Object.keys(data).length.should.be.equal(0); + + done(); + + }); + } + + async.parallel([orgATest, orgBTest], done); + + }); + + it('PUT /trans/id only works for ' + ORG_A, function(done) { + + var trans = utils.createTransaction(PAYLOAD, PRIORITY, [ { 'id': QUEUE } ]); + + var orgATest = function(done) { + utils.putTransactionOrg(ORG_A, transID, trans, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.have.property('ok', true); + + done(); + + }); + }; + + var orgBTest = function(done) { + utils.putTransactionOrg(ORG_B, transID, trans, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(400); + + data.should.have.property('errors'); + data.errors.length.should.be.equal(1); + data.errors.should.include(transID + ' does not exist'); + + done(); + + }); + } + + async.parallel([orgATest, orgBTest], done); + }); + + it('GET /queue/' + QUEUE + ' only works for ' + ORG_A, function(done) { + var orgATest = function(done) { + utils.getQueueStateOrg(ORG_A, QUEUE, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.have.property('high'); + data.high.length.should.be.equal(1); + data.high.pop().id.should.be.equal(transID); + + done(); + + }); + }; + + var orgBTest = function(done) { + utils.getTransStateOrg(ORG_B, QUEUE, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + Object.keys(data).length.should.be.equal(0); + + done(); + + }); + } + + async.parallel([orgATest, orgBTest], done); + }); + + it('POST /queue/' + QUEUE + '/pop only works for ' + ORG_A, function(done) { + + this.timeout(5000); + + var orgATest = function(done) { + utils.popOrg(ORG_A, QUEUE, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.not.have.property('error'); + data.should.have.property('ok'); + data.should.have.property('data'); + data.data.length.should.be.equal(1); + (data.data.pop()).should.be.equal(PAYLOAD); + + done(); + + }); + }; + + var orgBTest = function(done) { + utils.popTimeoutOrg(ORG_B, QUEUE, 1, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.not.have.property('error'); + data.should.have.property('ok'); + data.should.have.property('data'); + data.data.length.should.be.equal(0); + + done(); + + }); + } + + async.series([orgBTest, orgATest], done); + }); + + it('GET /queue/' + QUEUE + '/peek only works for ' + ORG_A, function(done) { + + var orgATest = function(done) { + utils.peekOrg(ORG_A, QUEUE, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.not.have.property('error'); + data.should.have.property('ok'); + data.should.have.property('data'); + data.data.length.should.be.equal(1); + (data.data.pop()).should.be.equal(PAYLOAD); + + done(); + + }); + }; + + var orgBTest = function(done) { + utils.peekOrg(ORG_B, QUEUE, 1, function(err, response, data) { + + should.not.exist(err); + response.statusCode.should.be.equal(200); + + data.should.not.have.property('error'); + data.should.have.property('ok'); + data.should.have.property('data'); + data.data.length.should.be.equal(0); + + done(); + + }); + } + + async.parallel([orgATest, orgBTest], done); + }); + + //TODO: Add subscription test + +}); \ No newline at end of file