1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-26 19:02:20 -05:00
mail/test/unit/service/publickey-dao-test.js

147 lines
4.2 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
var RestDAO = require('../../../src/js/service/rest'),
2014-12-10 11:53:16 -05:00
PublicKeyDAO = require('../../../src/js/service/publickey');
2014-10-07 14:32:23 -04:00
describe('Public Key DAO unit tests', function() {
2014-10-07 14:32:23 -04:00
var pubkeyDao, restDaoStub;
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() {});
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));
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();
});
});
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-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();
});
});
2014-10-07 14:32:23 -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));
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();
});
2014-10-07 14:32:23 -04: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-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-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();
});
});
2014-10-07 14:32:23 -04: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));
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();
});
2014-10-07 14:32:23 -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
}));
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([]));
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();
});
2014-10-07 14:32:23 -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();
});
});
2014-10-07 14:32:23 -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();
});
});
2014-10-07 14:32:23 -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());
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();
});
});
});
});