Test public key dao

This commit is contained in:
Tankred Hase 2014-12-10 17:53:16 +01:00
parent 3944b1741e
commit a58448483a
1 changed files with 28 additions and 43 deletions

View File

@ -1,8 +1,7 @@
'use strict'; 'use strict';
var RestDAO = require('../../../src/js/service/rest'), var RestDAO = require('../../../src/js/service/rest'),
PublicKeyDAO = require('../../../src/js/service/publickey'), PublicKeyDAO = require('../../../src/js/service/publickey');
appConfig = require('../../../src/js/app-config');
describe('Public Key DAO unit tests', function() { describe('Public Key DAO unit tests', function() {
@ -10,32 +9,29 @@ describe('Public Key DAO unit tests', function() {
beforeEach(function() { beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO); restDaoStub = sinon.createStubInstance(RestDAO);
pubkeyDao = new PublicKeyDAO(restDaoStub, appConfig); pubkeyDao = new PublicKeyDAO(restDaoStub);
}); });
afterEach(function() {}); afterEach(function() {});
describe('get', function() { describe('get', function() {
it('should fail', function(done) { it('should fail', function(done) {
restDaoStub.get.yields(42); restDaoStub.get.returns(rejects(42));
pubkeyDao.get('id', function(err, key) { pubkeyDao.get('id').catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(key).to.not.exist;
expect(restDaoStub.get.calledOnce).to.be.true; expect(restDaoStub.get.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should work', function(done) { it('should work', function(done) {
restDaoStub.get.yields(null, { restDaoStub.get.returns(resolves({
_id: '12345', _id: '12345',
publicKey: 'asdf' publicKey: 'asdf'
}); }));
pubkeyDao.get('id', function(err, key) { pubkeyDao.get('id').then(function(key) {
expect(err).to.not.exist;
expect(key).to.exist;
expect(key._id).to.exist; expect(key._id).to.exist;
expect(key.publicKey).to.exist; expect(key.publicKey).to.exist;
expect(restDaoStub.get.calledOnce).to.be.true; expect(restDaoStub.get.calledOnce).to.be.true;
@ -46,31 +42,27 @@ describe('Public Key DAO unit tests', function() {
describe('verify', function() { describe('verify', function() {
it('should fail', function(done) { it('should fail', function(done) {
restDaoStub.get.yields(42); restDaoStub.get.returns(rejects(42));
pubkeyDao.verify('id', function(err) { pubkeyDao.verify('id').catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
done(); done();
}); });
}); });
it('should not error for 400', function(done) { it('should not error for 400', function(done) {
restDaoStub.get.yields({ restDaoStub.get.returns(rejects({
code: 400 code: 400
}); }));
pubkeyDao.verify('id', function(err) { pubkeyDao.verify('id').then(done);
expect(err).to.not.exist;
done();
});
}); });
it('should work', function(done) { it('should work', function(done) {
var uuid = 'c621e328-8548-40a1-8309-adf1955e98a9'; var uuid = 'c621e328-8548-40a1-8309-adf1955e98a9';
restDaoStub.get.yields(null); restDaoStub.get.returns(resolves());
pubkeyDao.verify(uuid, function(err) { pubkeyDao.verify(uuid).then(function() {
expect(err).to.not.exist;
expect(restDaoStub.get.calledWith(sinon.match(function(arg) { expect(restDaoStub.get.calledWith(sinon.match(function(arg) {
return arg.uri === '/verify/' + uuid && arg.type === 'text'; return arg.uri === '/verify/' + uuid && arg.type === 'text';
}))).to.be.true; }))).to.be.true;
@ -81,23 +73,21 @@ describe('Public Key DAO unit tests', function() {
describe('get by userId', function() { describe('get by userId', function() {
it('should fail', function(done) { it('should fail', function(done) {
restDaoStub.get.yields(42); restDaoStub.get.returns(rejects(42));
pubkeyDao.getByUserId('userId', function(err, key) { pubkeyDao.getByUserId('userId').catch(function(err) {
expect(err).to.exist; expect(err).to.exist;
expect(key).to.not.exist;
expect(restDaoStub.get.calledOnce).to.be.true; expect(restDaoStub.get.calledOnce).to.be.true;
done(); done();
}); });
}); });
it('should react to 404', function(done) { it('should react to 404', function(done) {
restDaoStub.get.yields({ restDaoStub.get.returns(resolves({
code: 404 code: 404
}); }));
pubkeyDao.getByUserId('userId', function(err, key) { pubkeyDao.getByUserId('userId').then(function(key) {
expect(err).to.not.exist;
expect(key).to.not.exist; expect(key).to.not.exist;
expect(restDaoStub.get.calledOnce).to.be.true; expect(restDaoStub.get.calledOnce).to.be.true;
done(); done();
@ -105,10 +95,9 @@ describe('Public Key DAO unit tests', function() {
}); });
it('should return empty array', function(done) { it('should return empty array', function(done) {
restDaoStub.get.yields(null, []); restDaoStub.get.returns(resolves([]));
pubkeyDao.getByUserId('userId', function(err, key) { pubkeyDao.getByUserId('userId').then(function(key) {
expect(err).to.not.exist;
expect(key).to.not.exist; expect(key).to.not.exist;
expect(restDaoStub.get.calledOnce).to.be.true; expect(restDaoStub.get.calledOnce).to.be.true;
done(); done();
@ -116,14 +105,12 @@ describe('Public Key DAO unit tests', function() {
}); });
it('should work', function(done) { it('should work', function(done) {
restDaoStub.get.yields(null, [{ restDaoStub.get.returns(resolves([{
_id: '12345', _id: '12345',
publicKey: 'asdf' publicKey: 'asdf'
}]); }]));
pubkeyDao.getByUserId('userId', function(err, key) { pubkeyDao.getByUserId('userId').then(function(key) {
expect(err).to.not.exist;
expect(key).to.exist;
expect(key._id).to.exist; expect(key._id).to.exist;
expect(key.publicKey).to.exist; expect(key.publicKey).to.exist;
expect(restDaoStub.get.calledOnce).to.be.true; expect(restDaoStub.get.calledOnce).to.be.true;
@ -134,13 +121,12 @@ describe('Public Key DAO unit tests', function() {
describe('put', function() { describe('put', function() {
it('should fail', function(done) { it('should fail', function(done) {
restDaoStub.put.yields(); restDaoStub.put.returns(resolves());
pubkeyDao.put({ pubkeyDao.put({
_id: '12345', _id: '12345',
publicKey: 'asdf' publicKey: 'asdf'
}, function(err) { }).then(function() {
expect(err).to.not.exist;
expect(restDaoStub.put.calledOnce).to.be.true; expect(restDaoStub.put.calledOnce).to.be.true;
done(); done();
}); });
@ -149,10 +135,9 @@ describe('Public Key DAO unit tests', function() {
describe('remove', function() { describe('remove', function() {
it('should fail', function(done) { it('should fail', function(done) {
restDaoStub.remove.yields(); restDaoStub.remove.returns(resolves());
pubkeyDao.remove('12345', function(err) { pubkeyDao.remove('12345').then(function(err) {
expect(err).to.not.exist;
expect(restDaoStub.remove.calledOnce).to.be.true; expect(restDaoStub.remove.calledOnce).to.be.true;
done(); done();
}); });