2014-10-07 14:32:23 -04:00
|
|
|
'use strict';
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-11-21 09:06:29 -05:00
|
|
|
var RestDAO = require('../../../src/js/service/rest'),
|
2014-12-10 11:53:16 -05:00
|
|
|
PublicKeyDAO = require('../../../src/js/service/publickey');
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('Public Key DAO unit tests', function() {
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
var pubkeyDao, restDaoStub;
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
restDaoStub = sinon.createStubInstance(RestDAO);
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao = new PublicKeyDAO(restDaoStub);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('get', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(rejects(42));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.get('id').catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(restDaoStub.get.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-10 11:53:16 -05:00
|
|
|
}));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.get('id').then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key._id).to.exist;
|
|
|
|
expect(key.publicKey).to.exist;
|
|
|
|
expect(restDaoStub.get.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('verify', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(rejects(42));
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.verify('id').catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
done();
|
2013-11-08 03:29:04 -05:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should not error for 400', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(rejects({
|
2014-10-07 14:32:23 -04:00
|
|
|
code: 400
|
2014-12-10 11:53:16 -05:00
|
|
|
}));
|
2014-01-08 10:04:18 -05:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.verify('id').then(done);
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', function(done) {
|
|
|
|
var uuid = 'c621e328-8548-40a1-8309-adf1955e98a9';
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(resolves());
|
2014-01-08 10:04:18 -05:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.verify(uuid).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(restDaoStub.get.calledWith(sinon.match(function(arg) {
|
|
|
|
return arg.uri === '/verify/' + uuid && arg.type === 'text';
|
|
|
|
}))).to.be.true;
|
|
|
|
done();
|
2013-11-08 03:29:04 -05:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-11-08 03:29:04 -05:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('get by userId', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(rejects(42));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.getByUserId('userId').catch(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(err).to.exist;
|
|
|
|
expect(restDaoStub.get.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should react to 404', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(resolves({
|
2014-10-07 14:32:23 -04:00
|
|
|
code: 404
|
2014-12-10 11:53:16 -05:00
|
|
|
}));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.getByUserId('userId').then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.not.exist;
|
|
|
|
expect(restDaoStub.get.calledOnce).to.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return empty array', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(resolves([]));
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.getByUserId('userId').then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key).to.not.exist;
|
|
|
|
expect(restDaoStub.get.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
it('should work', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.get.returns(resolves([{
|
2014-10-07 14:32:23 -04:00
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-10 11:53:16 -05:00
|
|
|
}]));
|
2014-10-07 14:32:23 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.getByUserId('userId').then(function(key) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(key._id).to.exist;
|
|
|
|
expect(key.publicKey).to.exist;
|
|
|
|
expect(restDaoStub.get.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('put', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.put.returns(resolves());
|
2014-10-07 14:32:23 -04:00
|
|
|
|
|
|
|
pubkeyDao.put({
|
|
|
|
_id: '12345',
|
|
|
|
publicKey: 'asdf'
|
2014-12-10 11:53:16 -05:00
|
|
|
}).then(function() {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(restDaoStub.put.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-07 14:32:23 -04:00
|
|
|
});
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-10-07 14:32:23 -04:00
|
|
|
describe('remove', function() {
|
|
|
|
it('should fail', function(done) {
|
2014-12-10 11:53:16 -05:00
|
|
|
restDaoStub.remove.returns(resolves());
|
2013-10-29 07:19:27 -04:00
|
|
|
|
2014-12-10 11:53:16 -05:00
|
|
|
pubkeyDao.remove('12345').then(function(err) {
|
2014-10-07 14:32:23 -04:00
|
|
|
expect(restDaoStub.remove.calledOnce).to.be.true;
|
|
|
|
done();
|
2013-10-29 07:19:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|