[WO-61] change method signature for invitation DAO

This commit is contained in:
Felix Hammerl 2013-11-20 16:04:36 +01:00
parent 6fa06fc4e8
commit 8e8947e742
2 changed files with 98 additions and 16 deletions

View File

@ -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

View File

@ -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;
}
});
});
});
});