Fix rest dao and invitation dao

This commit is contained in:
Tankred Hase 2014-12-10 17:20:55 +01:00
parent 67adf4a1b8
commit fb89ffd35a
5 changed files with 137 additions and 188 deletions

View File

@ -1,52 +1,51 @@
{ {
"indent": 4, "indent": 4,
"strict": true, "strict": true,
"globalstrict": true, "globalstrict": true,
"node": true, "node": true,
"browser": true, "browser": true,
"nonew": true, "nonew": true,
"curly": true, "curly": true,
"eqeqeq": true, "eqeqeq": true,
"immed": true, "immed": true,
"newcap": true, "newcap": true,
"regexp": true, "regexp": true,
"evil": true, "evil": true,
"eqnull": true, "eqnull": true,
"expr": true, "expr": true,
"trailing": true, "trailing": true,
"undef": true, "undef": true,
"unused": true, "unused": true,
"predef": [ "predef": [
"$", "$",
"inject", "inject",
"Promise", "Promise",
"self", "qMock",
"importScripts", "self",
"console", "importScripts",
"process", "console",
"chrome", "process",
"Notification", "chrome",
"Event", "Notification",
"sinon", "Event",
"mocha", "sinon",
"chai", "mocha",
"expect", "chai",
"describe", "expect",
"it", "describe",
"before", "it",
"beforeEach", "before",
"after", "beforeEach",
"afterEach", "after",
"FastClick", "afterEach",
"angular", "FastClick",
"forge", "angular",
"Lawnchair", "forge",
"_", "Lawnchair",
"openpgp", "_",
"qMock" "openpgp"
], ],
"globals": { "globals": {}
}
} }

View File

@ -8,18 +8,11 @@ module.exports = Invitation;
* The Invitation is a high level Data Access Object that access the invitation service REST endpoint. * The Invitation is a high level Data Access Object that access the invitation service REST endpoint.
* @param {Object} restDao The REST Data Access Object abstraction * @param {Object} restDao The REST Data Access Object abstraction
*/ */
function Invitation(invitationRestDao) { function Invitation(invitationRestDao, $q) {
this._restDao = invitationRestDao; this._restDao = invitationRestDao;
this._q = $q;
} }
//
// Constants
//
Invitation.INVITE_MISSING = 1;
Invitation.INVITE_PENDING = 2;
Invitation.INVITE_SUCCESS = 4;
// //
// API // API
// //
@ -28,35 +21,18 @@ Invitation.INVITE_SUCCESS = 4;
* Notes an invite for the recipient by the sender in the invitation web service * Notes an invite for the recipient by the sender in the invitation web service
* @param {String} options.recipient User ID of the recipient * @param {String} options.recipient User ID of the recipient
* @param {String} options.sender User ID of the sender * @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. * @return {Promise}
*/ */
Invitation.prototype.invite = function(options, callback) { Invitation.prototype.invite = function(options) {
if (typeof options !== 'object' || typeof options.recipient !== 'string' || typeof options.recipient !== 'string') { var self = this;
callback({ return self._q(function(resolve) {
errMsg: 'erroneous usage of api: incorrect parameters!' if (typeof options !== 'object' || typeof options.recipient !== 'string' || typeof options.sender !== 'string') {
}); throw new Error('erroneous usage of api: incorrect parameters!');
return;
}
var uri = '/invitation/recipient/' + options.recipient + '/sender/' + options.sender;
this._restDao.put({}, uri, completed);
function completed(error, res, status) {
if (error) {
callback(error);
return;
} }
resolve();
if (status === 201) { }).then(function() {
callback(null, Invitation.INVITE_SUCCESS); var uri = '/invitation/recipient/' + options.recipient + '/sender/' + options.sender;
return; return self._restDao.put({}, uri);
} else if (status === 304) { });
callback(null, Invitation.INVITE_PENDING);
return;
}
callback({
errMsg: 'unexpected invitation state'
});
}
}; };

View File

@ -98,15 +98,15 @@ RestDAO.prototype.remove = function(uri) {
// //
RestDAO.prototype._processRequest = function(options) { RestDAO.prototype._processRequest = function(options) {
return this._q(function(resolve, reject) { var self = this;
return self._q(function(resolve, reject) {
var xhr, format; var xhr, format;
if (typeof options.uri === 'undefined') { if (typeof options.uri === 'undefined') {
reject({ throw {
code: 400, code: 400,
message: 'Bad Request! URI is a mandatory parameter.' message: 'Bad Request! URI is a mandatory parameter.'
}); };
return;
} }
options.type = options.type || 'json'; options.type = options.type || 'json';
@ -118,15 +118,14 @@ RestDAO.prototype._processRequest = function(options) {
} else if (options.type === 'text') { } else if (options.type === 'text') {
format = 'text/plain'; format = 'text/plain';
} else { } else {
reject({ throw {
code: 400, code: 400,
message: 'Bad Request! Unhandled data type.' message: 'Bad Request! Unhandled data type.'
}); };
return;
} }
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open(options.method, this._baseUri + options.uri); xhr.open(options.method, self._baseUri + options.uri);
xhr.setRequestHeader('Accept', format); xhr.setRequestHeader('Accept', format);
xhr.setRequestHeader('Content-Type', format); xhr.setRequestHeader('Content-Type', format);
@ -140,7 +139,7 @@ RestDAO.prototype._processRequest = function(options) {
res = xhr.responseText; res = xhr.responseText;
} }
resolve(res, xhr.status); resolve(res);
return; return;
} }

View File

@ -1,8 +1,7 @@
'use strict'; 'use strict';
var RestDAO = require('../../../src/js/service/rest'), var RestDAO = require('../../../src/js/service/rest'),
InvitationDAO = require('../../../src/js/service/invitation'), InvitationDAO = require('../../../src/js/service/invitation');
appConfig = require('../../../src/js/app-config');
describe('Invitation DAO unit tests', function() { describe('Invitation DAO unit tests', function() {
var restDaoStub, invitationDao, var restDaoStub, invitationDao,
@ -12,94 +11,80 @@ describe('Invitation DAO unit tests', function() {
beforeEach(function() { beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO); restDaoStub = sinon.createStubInstance(RestDAO);
invitationDao = new InvitationDAO(restDaoStub, appConfig); invitationDao = new InvitationDAO(restDaoStub, window.qMock);
}); });
describe('initialization', function() { describe('initialization', function() {
it('should wire up correctly', function() { it('should wire up correctly', function() {
expect(invitationDao._restDao).to.equal(restDaoStub); expect(invitationDao._restDao).to.equal(restDaoStub);
expect(invitationDao.invite).to.exist; expect(invitationDao.invite).to.exist;
expect(InvitationDAO.INVITE_MISSING).to.equal(1);
expect(InvitationDAO.INVITE_PENDING).to.equal(2);
expect(InvitationDAO.INVITE_SUCCESS).to.equal(4);
}); });
}); });
describe('invite', function() { describe('invite', function() {
it('should invite the recipient', function(done) { it('should invite the recipient', function(done) {
restDaoStub.put.yieldsAsync(null, undefined, 201); restDaoStub.put.returns(new Promise(function(res) {
res();
}));
invitationDao.invite({ invitationDao.invite({
recipient: alice, recipient: alice,
sender: bob sender: bob
}, function(err, status) { }).then(function() {
expect(err).to.not.exist;
expect(status).to.equal(InvitationDAO.INVITE_SUCCESS);
expect(restDaoStub.put.calledWith({}, expectedUri)).to.be.true; expect(restDaoStub.put.calledWith({}, expectedUri)).to.be.true;
done(); done();
}); });
}); });
it('should point out already invited recipient', function(done) {
restDaoStub.put.yieldsAsync(null, undefined, 304);
invitationDao.invite({
recipient: alice,
sender: bob
}, function(err, status) {
expect(err).to.not.exist;
expect(status).to.equal(InvitationDAO.INVITE_PENDING);
done();
});
});
it('should not work for http error', function(done) { it('should not work for http error', function(done) {
restDaoStub.put.yieldsAsync({ restDaoStub.put.throws(new Error());
errMsg: 'jawollja.'
});
invitationDao.invite({ invitationDao.invite({
recipient: alice, recipient: alice,
sender: bob sender: bob
}, function(err, status) { }).catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(status).to.not.exist;
done(); done();
}); });
}); });
it('should not work for unexpected response', function(done) { it('should report erroneous usage', function(done) {
restDaoStub.put.yieldsAsync(null, undefined, 1337);
invitationDao.invite({ invitationDao.invite({
recipient: alice,
sender: bob sender: bob
}, function(err, status) { }, expectError);
invitationDao.invite('asd').catch(expectError);
function expectError(err) {
expect(err).to.exist; expect(err).to.exist;
expect(status).to.not.exist;
done(); done();
}); }
}); });
it('should report erroneous usage', function() { it('should report erroneous usage', function(done) {
invitationDao.invite({
sender: bob
}, expectError);
invitationDao.invite({ invitationDao.invite({
recipient: alice, recipient: alice,
}, expectError); }, expectError);
invitationDao.invite('asd').catch(expectError);
function expectError(err) {
expect(err).to.exist;
done();
}
});
it('should report erroneous usage', function(done) {
invitationDao.invite({ invitationDao.invite({
recipient: 123, recipient: 123,
sender: 123 sender: 123
}, expectError); }, expectError);
invitationDao.invite('asd', expectError); invitationDao.invite('asd').catch(expectError);
function expectError(err, status) { function expectError(err) {
expect(err).to.exist; expect(err).to.exist;
expect(status).to.not.exist; done();
} }
}); });
}); });

View File

@ -7,7 +7,7 @@ describe('Rest DAO unit tests', function() {
var restDao, xhrMock, requests; var restDao, xhrMock, requests;
beforeEach(function() { beforeEach(function() {
restDao = new RestDAO(); restDao = new RestDAO(window.qMock);
xhrMock = sinon.useFakeXMLHttpRequest(); xhrMock = sinon.useFakeXMLHttpRequest();
requests = []; requests = [];
@ -23,7 +23,7 @@ describe('Rest DAO unit tests', function() {
describe('setBaseUri', function() { describe('setBaseUri', function() {
it('should accept base uri', function() { it('should accept base uri', function() {
var baseUri = 'http://custom.com'; var baseUri = 'http://custom.com';
restDao = new RestDAO(); restDao = new RestDAO(window.qMock);
expect(restDao._baseUri).to.not.exist; expect(restDao._baseUri).to.not.exist;
restDao.setBaseUri(baseUri); restDao.setBaseUri(baseUri);
expect(restDao._baseUri).to.equal(baseUri); expect(restDao._baseUri).to.equal(baseUri);
@ -31,15 +31,14 @@ describe('Rest DAO unit tests', function() {
}); });
describe('get', function() { describe('get', function() {
it('should work with json as default type', function() { it('should work with json as default type', function(done) {
restDao.get({ restDao.get({
uri: '/asdf' uri: '/asdf'
}, function(err, data, status) { }).then(function(data) {
expect(err).to.not.exist;
expect(data.foo).to.equal('bar'); expect(data.foo).to.equal('bar');
var req = requests[0]; var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/json'); expect(req.requestHeaders.Accept).to.equal('application/json');
expect(status).to.equal(200); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -48,16 +47,15 @@ describe('Rest DAO unit tests', function() {
}, '{"foo": "bar"}'); }, '{"foo": "bar"}');
}); });
it('should work with jsonz', function() { it('should work with jsonz', function(done) {
restDao.get({ restDao.get({
uri: '/asdf', uri: '/asdf',
type: 'json' type: 'json'
}, function(err, data, status) { }).then(function(data) {
expect(err).to.not.exist;
expect(data.foo).to.equal('bar'); expect(data.foo).to.equal('bar');
var req = requests[0]; var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/json'); expect(req.requestHeaders.Accept).to.equal('application/json');
expect(status).to.equal(200); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -66,16 +64,15 @@ describe('Rest DAO unit tests', function() {
}, '{"foo": "bar"}'); }, '{"foo": "bar"}');
}); });
it('should work with plain text', function() { it('should work with plain text', function(done) {
restDao.get({ restDao.get({
uri: '/asdf', uri: '/asdf',
type: 'text' type: 'text'
}, function(err, data, status) { }).then(function(data) {
expect(err).to.not.exist;
expect(data).to.equal('foobar!'); expect(data).to.equal('foobar!');
var req = requests[0]; var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('text/plain'); expect(req.requestHeaders.Accept).to.equal('text/plain');
expect(status).to.equal(200); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -84,16 +81,15 @@ describe('Rest DAO unit tests', function() {
}, 'foobar!'); }, 'foobar!');
}); });
it('should work with xml', function() { it('should work with xml', function(done) {
restDao.get({ restDao.get({
uri: '/asdf', uri: '/asdf',
type: 'xml' type: 'xml'
}, function(err, data, status) { }).then(function(data) {
expect(err).to.not.exist;
expect(data).to.equal('<foo>bar</foo>'); expect(data).to.equal('<foo>bar</foo>');
var req = requests[0]; var req = requests[0];
expect(req.requestHeaders.Accept).to.equal('application/xml'); expect(req.requestHeaders.Accept).to.equal('application/xml');
expect(status).to.equal(200); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -102,32 +98,29 @@ describe('Rest DAO unit tests', function() {
}, '<foo>bar</foo>'); }, '<foo>bar</foo>');
}); });
it('should fail for missing uri parameter', function() { it('should fail for missing uri parameter', function(done) {
restDao.get({}, function(err, data) { restDao.get({}).catch(function(err) {
expect(err).to.exist;
expect(err.code).to.equal(400); expect(err.code).to.equal(400);
expect(data).to.not.exist; done();
}); });
}); });
it('should fail for unhandled data type', function() { it('should fail for unhandled data type', function(done) {
restDao.get({ restDao.get({
uri: '/asdf', uri: '/asdf',
type: 'snafu' type: 'snafu'
}, function(err, data) { }).catch(function(err) {
expect(err).to.exist;
expect(err.code).to.equal(400); expect(err.code).to.equal(400);
expect(data).to.not.exist; done();
}); });
}); });
it('should fail for server error', function() { it('should fail for server error', function(done) {
restDao.get({ restDao.get({
uri: '/asdf' uri: '/asdf'
}, function(err, data) { }).catch(function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500); expect(err.code).to.equal(500);
expect(data).to.not.exist; done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -138,10 +131,10 @@ describe('Rest DAO unit tests', function() {
}); });
describe('post', function() { describe('post', function() {
it('should fail', function() { it('should fail', function(done) {
restDao.post('/asdf', {}, function(err) { restDao.post('/asdf', {}).catch(function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500); expect(err.code).to.equal(500);
done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -150,11 +143,10 @@ describe('Rest DAO unit tests', function() {
}, 'Internal error'); }, 'Internal error');
}); });
it('should work', function() { it('should work', function(done) {
restDao.post('/asdf', {}, function(err, res, status) { restDao.post('/asdf', {}).then(function(res) {
expect(err).to.not.exist;
expect(res).to.equal(''); expect(res).to.equal('');
expect(status).to.equal(201); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -163,10 +155,10 @@ describe('Rest DAO unit tests', function() {
}); });
describe('put', function() { describe('put', function() {
it('should fail', function() { it('should fail', function(done) {
restDao.put('/asdf', {}, function(err) { restDao.put('/asdf', {}).catch(function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500); expect(err.code).to.equal(500);
done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -175,11 +167,10 @@ describe('Rest DAO unit tests', function() {
}, 'Internal error'); }, 'Internal error');
}); });
it('should work', function() { it('should work', function(done) {
restDao.put('/asdf', {}, function(err, res, status) { restDao.put('/asdf', {}).then(function(res) {
expect(err).to.not.exist;
expect(res).to.equal(''); expect(res).to.equal('');
expect(status).to.equal(201); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -188,10 +179,10 @@ describe('Rest DAO unit tests', function() {
}); });
describe('remove', function() { describe('remove', function() {
it('should fail', function() { it('should fail', function(done) {
restDao.remove('/asdf', function(err) { restDao.remove('/asdf').catch(function(err) {
expect(err).to.exist;
expect(err.code).to.equal(500); expect(err.code).to.equal(500);
done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);
@ -200,11 +191,10 @@ describe('Rest DAO unit tests', function() {
}, 'Internal error'); }, 'Internal error');
}); });
it('should work', function() { it('should work', function(done) {
restDao.remove('/asdf', function(err, res, status) { restDao.remove('/asdf').then(function(res) {
expect(err).to.not.exist;
expect(res).to.equal(''); expect(res).to.equal('');
expect(status).to.equal(200); done();
}); });
expect(requests.length).to.equal(1); expect(requests.length).to.equal(1);