mail/test/unit/service/admin-dao-test.js

140 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-10-07 14:32:23 -04:00
'use strict';
var RestDAO = require('../../../src/js/service/rest'),
AdminDAO = require('../../../src/js/service/admin'),
appConfig = require('../../../src/js/app-config');
2014-10-07 14:32:23 -04:00
describe('Admin DAO unit tests', function() {
2014-10-07 14:32:23 -04:00
var adminDao, restDaoStub,
emailAddress = 'test@example.com',
password = 'secret';
2014-10-07 14:32:23 -04:00
beforeEach(function() {
restDaoStub = sinon.createStubInstance(RestDAO);
adminDao = new AdminDAO(restDaoStub, appConfig);
2014-10-07 14:32:23 -04:00
});
afterEach(function() {});
describe('createUser', function() {
it('should fail due to incomplete args', function(done) {
var opt = {
emailAddress: emailAddress
};
2014-12-11 13:07:04 -05:00
adminDao.createUser(opt).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
});
});
2014-10-07 14:32:23 -04:00
it('should fail if user already exists', function(done) {
var opt = {
emailAddress: emailAddress,
password: password,
phone: '12345'
};
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(opt, '/user').returns(rejects({
2014-10-07 14:32:23 -04:00
code: 409
2014-12-11 13:07:04 -05:00
}));
2014-12-11 13:07:04 -05:00
adminDao.createUser(opt).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err.message).to.contain('already taken');
expect(restDaoStub.post.calledOnce).to.be.true;
done();
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
it('should fail due to unknown error', function(done) {
var opt = {
emailAddress: emailAddress,
password: password,
phone: '12345'
};
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(opt, '/user').returns(rejects(new Error()));
2014-12-11 13:07:04 -05:00
adminDao.createUser(opt).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(restDaoStub.post.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) {
var opt = {
emailAddress: emailAddress,
password: password,
phone: '12345'
};
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(opt, '/user').returns(resolves());
2014-12-11 13:07:04 -05:00
adminDao.createUser(opt).then(function() {
2014-10-07 14:32:23 -04:00
expect(restDaoStub.post.calledOnce).to.be.true;
done();
});
});
2014-10-07 14:32:23 -04:00
});
2014-10-07 14:32:23 -04:00
describe('validateUser', function() {
it('should fail due to incomplete args', function(done) {
var opt = {
emailAddress: emailAddress
};
2014-09-19 12:59:13 -04:00
2014-12-11 13:07:04 -05:00
adminDao.validateUser(opt).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
done();
2014-09-19 12:59:13 -04:00
});
2014-10-07 14:32:23 -04:00
});
2014-09-19 12:59:13 -04:00
2014-10-07 14:32:23 -04:00
it('should fail due to error in rest api', function(done) {
var opt = {
emailAddress: emailAddress,
token: 'H45Z6D'
};
2014-09-19 12:59:13 -04:00
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(opt, '/user/validate').returns(rejects(new Error()));
2014-09-19 12:59:13 -04:00
2014-12-11 13:07:04 -05:00
adminDao.validateUser(opt).catch(function(err) {
2014-10-07 14:32:23 -04:00
expect(err).to.exist;
expect(restDaoStub.post.calledOnce).to.be.true;
done();
2014-09-19 12:59:13 -04:00
});
2014-10-07 14:32:23 -04:00
});
it('should work with no error object', function(done) {
var opt = {
emailAddress: emailAddress,
token: 'H45Z6D'
};
2014-09-19 12:59:13 -04:00
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(opt, '/user/validate').returns(resolves());
2014-09-19 12:59:13 -04:00
2014-12-11 13:07:04 -05:00
adminDao.validateUser(opt).then(function() {
2014-10-07 14:32:23 -04:00
expect(restDaoStub.post.calledOnce).to.be.true;
done();
});
});
2014-09-19 12:59:13 -04:00
2014-10-07 14:32:23 -04:00
it('should work with 202', function(done) {
var opt = {
emailAddress: emailAddress,
token: 'H45Z6D'
};
2014-12-11 13:07:04 -05:00
restDaoStub.post.withArgs(opt, '/user/validate').returns(rejects({
2014-10-07 14:32:23 -04:00
code: 202
2014-12-11 13:07:04 -05:00
}));
2014-09-19 12:59:13 -04:00
2014-12-11 13:07:04 -05:00
adminDao.validateUser(opt).then(function() {
2014-10-07 14:32:23 -04:00
expect(restDaoStub.post.calledOnce).to.be.true;
done();
2014-09-19 12:59:13 -04:00
});
});
});
});