From 8e8947e74243084c7524547485b69c1348a5910a Mon Sep 17 00:00:00 2001 From: Felix Hammerl Date: Wed, 20 Nov 2013 16:04:36 +0100 Subject: [PATCH] [WO-61] change method signature for invitation DAO --- src/js/dao/invitation-dao.js | 30 +++++++--- test/new-unit/invitation-dao-test.js | 84 +++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/js/dao/invitation-dao.js b/src/js/dao/invitation-dao.js index 761a823..5fbfc97 100644 --- a/src/js/dao/invitation-dao.js +++ b/src/js/dao/invitation-dao.js @@ -23,12 +23,19 @@ define(function() { /** * Notes an invite for the recipient by the sender in the invitation web service - * @param {String} recipient User ID of the recipient - * @param {String} sender User ID of the sender + * @param {String} options.recipient User ID of the recipient + * @param {String} options.sender User ID of the sender * @param {Function} callback(error, status) Returns information if the invitation worked (INVITE_SUCCESS), if an invitation is already pendin (INVITE_PENDING), or information if an error occurred. */ - InvitationDAO.prototype.invite = function(recipient, sender, callback) { - this._restDao.put(null, uri(recipient, sender), completed); + InvitationDAO.prototype.invite = function(options, callback) { + if (typeof options !== 'object' || typeof options.recipient !== 'string' || typeof options.recipient !== 'string') { + callback({ + errMsg: 'erroneous usage of api: incorrect parameters!' + }); + return; + } + + this._restDao.put(null, uri(options.recipient, options.sender), completed); function completed(error, res, status) { if (error) { @@ -52,12 +59,19 @@ define(function() { /** * Checks if an invitation for the recipient by the sender is present in the invitation web service - * @param {String} recipient User ID of the recipient - * @param {String} sender User ID of the sender + * @param {String} options.recipient User ID of the recipient + * @param {String} options.sender User ID of the sender * @param {Function} callback(error, status) Returns information about the invitation status, either an invitation is already on place (INVITE_PENDING), or not (INVITE_MISSING), or information if an error occurred. */ - InvitationDAO.prototype.check = function(recipient, sender, callback) { - this._restDao.get(null, uri(recipient, sender), completed); + InvitationDAO.prototype.check = function(options, callback) { + if (typeof options !== 'object' || typeof options.recipient !== 'string' || typeof options.recipient !== 'string') { + callback({ + errMsg: 'erroneous usage of api: incorrect parameters!' + }); + return; + } + + this._restDao.get(null, uri(options.recipient, options.sender), completed); function completed(error, res, status) { // 404 is a meaningful return value from the web service diff --git a/test/new-unit/invitation-dao-test.js b/test/new-unit/invitation-dao-test.js index 2645d1e..48d3568 100644 --- a/test/new-unit/invitation-dao-test.js +++ b/test/new-unit/invitation-dao-test.js @@ -30,7 +30,10 @@ define(function(require) { it('should invite the recipient', function(done) { restDaoStub.put.yieldsAsync(null, undefined, 201); - invitationDao.invite(alice, bob, function(err, status) { + invitationDao.invite({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.not.exist; expect(status).to.equal(InvitationDAO.INVITE_SUCCESS); expect(restDaoStub.put.calledWith(null, expectedUri)).to.be.true; @@ -41,7 +44,10 @@ define(function(require) { it('should point out already invited recipient', function(done) { restDaoStub.put.yieldsAsync(null, undefined, 304); - invitationDao.invite(alice, bob, function(err, status) { + invitationDao.invite({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.not.exist; expect(status).to.equal(InvitationDAO.INVITE_PENDING); done(); @@ -53,7 +59,10 @@ define(function(require) { errMsg: 'jawollja.' }); - invitationDao.invite(alice, bob, function(err, status) { + invitationDao.invite({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.exist; expect(status).to.not.exist; done(); @@ -63,19 +72,47 @@ define(function(require) { it('should not work for unexpected response', function(done) { restDaoStub.put.yieldsAsync(null, undefined, 1337); - invitationDao.invite(alice, bob, function(err, status) { + invitationDao.invite({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.exist; expect(status).to.not.exist; done(); }); }); + + it('should report erroneous usage', function() { + invitationDao.invite({ + sender: bob + }, expectError); + + invitationDao.invite({ + recipient: alice, + }, expectError); + + invitationDao.invite({ + recipient: 123, + sender: 123 + }, expectError); + + invitationDao.invite('asd', expectError); + + function expectError(err, status) { + expect(err).to.exist; + expect(status).to.not.exist; + } + }); }); describe('check', function() { it('should return pending invite', function(done) { restDaoStub.get.yieldsAsync(null, undefined, 200); - invitationDao.check(alice, bob, function(err, status) { + invitationDao.check({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.not.exist; expect(status).to.equal(InvitationDAO.INVITE_PENDING); expect(restDaoStub.get.calledWith(null, expectedUri)).to.be.true; @@ -88,7 +125,10 @@ define(function(require) { code: 404 }); - invitationDao.check(alice, bob, function(err, status) { + invitationDao.check({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.not.exist; expect(status).to.equal(InvitationDAO.INVITE_MISSING); done(); @@ -101,7 +141,10 @@ define(function(require) { errMsg: 'jawollja.' }); - invitationDao.check(alice, bob, function(err, status) { + invitationDao.check({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.exist; expect(status).to.not.exist; done(); @@ -111,12 +154,37 @@ define(function(require) { it('should not work for unexpected response', function(done) { restDaoStub.get.yieldsAsync(null, undefined, 1337); - invitationDao.check(alice, bob, function(err, status) { + invitationDao.check({ + recipient: alice, + sender: bob + }, function(err, status) { expect(err).to.exist; expect(status).to.not.exist; done(); }); }); + + it('should report erroneous usage', function() { + invitationDao.check({ + sender: bob + }, expectError); + + invitationDao.check({ + recipient: alice, + }, expectError); + + invitationDao.check({ + recipient: 123, + sender: 123 + }, expectError); + + invitationDao.check('asd', expectError); + + function expectError(err, status) { + expect(err).to.exist; + expect(status).to.not.exist; + } + }); }); }); }); \ No newline at end of file