mailboxdb: merge into mail.js
This commit is contained in:
@@ -10,8 +10,6 @@ const appdb = require('../appdb.js'),
|
||||
domains = require('../domains.js'),
|
||||
expect = require('expect.js'),
|
||||
fs = require('fs'),
|
||||
mail = require('../mail.js'),
|
||||
mailboxdb = require('../mailboxdb.js'),
|
||||
mailer = require('../mailer.js'),
|
||||
nock = require('nock'),
|
||||
path = require('path'),
|
||||
@@ -133,11 +131,6 @@ exports = module.exports = {
|
||||
manifest,
|
||||
user,
|
||||
appstoreToken: 'atoken',
|
||||
|
||||
mailboxName: 'support',
|
||||
mailbox: `support@${domain.domain}`,
|
||||
mailAliasName: 'alsosupport',
|
||||
mailAlias: `alsosupport@${domain.domain}`
|
||||
};
|
||||
|
||||
function createTree(root, obj) {
|
||||
@@ -197,8 +190,6 @@ function setup(done) {
|
||||
const result = await users.add(user.email, user, auditSource);
|
||||
user.id = result;
|
||||
},
|
||||
(done) => mailboxdb.addMailbox(exports.mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true }, done),
|
||||
(done) => mailboxdb.setAliasesForName(exports.mailboxName, domain.domain, [ { name: exports.mailAliasName, domain: domain.domain} ], done),
|
||||
|
||||
tasks.stopAllTasks,
|
||||
], done);
|
||||
|
||||
@@ -12,7 +12,6 @@ const appdb = require('../appdb.js'),
|
||||
database = require('../database'),
|
||||
domains = require('../domains.js'),
|
||||
expect = require('expect.js'),
|
||||
mailboxdb = require('../mailboxdb.js'),
|
||||
reverseProxy = require('../reverseproxy.js'),
|
||||
settingsdb = require('../settingsdb.js'),
|
||||
_ = require('underscore');
|
||||
@@ -472,169 +471,4 @@ describe('database', function () {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('mailboxes', function () {
|
||||
before(function (done) {
|
||||
async.series([
|
||||
domains.add.bind(null, DOMAIN_0.domain, DOMAIN_0),
|
||||
], done);
|
||||
});
|
||||
|
||||
after(function (done) {
|
||||
database._clear(done);
|
||||
});
|
||||
|
||||
it('add user mailbox succeeds', function (done) {
|
||||
mailboxdb.addMailbox('girish', DOMAIN_0.domain, { ownerId: 'uid-0', ownerType: 'user', active: true }, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot add dup entry', function (done) {
|
||||
mailboxdb.addMailbox('girish', DOMAIN_0.domain, { ownerId: 'uid-1', ownerType: 'group', active: true }, function (error) {
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('add app mailbox succeeds', function (done) {
|
||||
mailboxdb.addMailbox('support', DOMAIN_0.domain, { ownerId: 'osticket', ownerType: 'user', active: true}, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('get succeeds', function (done) {
|
||||
mailboxdb.getMailbox('support', DOMAIN_0.domain, function (error, mailbox) {
|
||||
expect(error).to.be(null);
|
||||
expect(mailbox.name).to.equal('support');
|
||||
expect(mailbox.ownerId).to.equal('osticket');
|
||||
expect(mailbox.domain).to.equal(DOMAIN_0.domain);
|
||||
expect(mailbox.creationTime).to.be.a(Date);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('list mailboxes succeeds', function (done) {
|
||||
mailboxdb.listMailboxes(DOMAIN_0.domain, null /* search */, 1, 10, function (error, mailboxes) {
|
||||
expect(error).to.be(null);
|
||||
expect(mailboxes.length).to.be(2);
|
||||
expect(mailboxes[0].name).to.be('girish');
|
||||
expect(mailboxes[1].name).to.be('support');
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can set alias', function (done) {
|
||||
mailboxdb.setAliasesForName('support', DOMAIN_0.domain, [ { name: 'support2', domain: DOMAIN_0.domain }, { name: 'help', domain: DOMAIN_0.domain } ], function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('list all mailboxes succeeds', function (done) {
|
||||
mailboxdb.listAllMailboxes(1, 10, function (error, mailboxes) {
|
||||
expect(error).to.be(null);
|
||||
expect(mailboxes.length).to.be(2);
|
||||
expect(mailboxes[0].name).to.be('girish');
|
||||
expect(mailboxes[1].name).to.be('support');
|
||||
expect(mailboxes[1].domain).to.be(DOMAIN_0.domain);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('can get aliases of name', function (done) {
|
||||
mailboxdb.getAliasesForName('support', DOMAIN_0.domain, function (error, results) {
|
||||
expect(error).to.be(null);
|
||||
expect(results.length).to.be(2);
|
||||
expect(results[0].name).to.be('help');
|
||||
expect(results[0].domain).to.be(DOMAIN_0.domain);
|
||||
expect(results[1].name).to.be('support2');
|
||||
expect(results[1].domain).to.be(DOMAIN_0.domain);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get alias', function (done) {
|
||||
mailboxdb.getAlias('support2', DOMAIN_0.domain, function (error, result) {
|
||||
expect(error).to.be(null);
|
||||
expect(result.name).to.be('support2');
|
||||
expect(result.aliasName).to.be('support');
|
||||
expect(result.aliasDomain).to.be(DOMAIN_0.domain);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get by owner id', function (done) {
|
||||
mailboxdb.getByOwnerId('osticket', function (error, results) {
|
||||
expect(error).to.be(null);
|
||||
expect(results.length).to.be(3);
|
||||
expect(results[0].name).to.be('help');
|
||||
expect(results[1].name).to.be('support');
|
||||
expect(results[2].name).to.be('support2');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot get non-existing group', function (done) {
|
||||
mailboxdb.getList('random', DOMAIN_0.domain, function (error) {
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can change name', function (done) {
|
||||
mailboxdb.updateName('support', DOMAIN_0.domain, 'support3', DOMAIN_0.domain, function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
mailboxdb.updateName('support3', DOMAIN_0.domain, 'support', DOMAIN_0.domain, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot change name to existing one', function (done) {
|
||||
mailboxdb.updateName('support', DOMAIN_0.domain, 'support2', DOMAIN_0.domain, function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.eql(BoxError.ALREADY_EXISTS);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('unset aliases', function (done) {
|
||||
mailboxdb.setAliasesForName('support', DOMAIN_0.domain, [], function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
mailboxdb.getAliasesForName('support', DOMAIN_0.domain, function (error, results) {
|
||||
expect(error).to.be(null);
|
||||
expect(results.length).to.be(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('del succeeds', function (done) {
|
||||
mailboxdb.del('girish', DOMAIN_0.domain, function (error) {
|
||||
expect(error).to.be(null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('del by ownerId succeeds', function (done) {
|
||||
mailboxdb.delByOwnerId('osticket', function (error) {
|
||||
expect(error).to.be(null);
|
||||
|
||||
mailboxdb.getByOwnerId('osticket', function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -15,7 +15,6 @@ const appdb = require('../appdb.js'),
|
||||
ldap = require('ldapjs'),
|
||||
ldapServer = require('../ldap.js'),
|
||||
mail = require('../mail.js'),
|
||||
mailboxdb = require('../mailboxdb.js'),
|
||||
safe = require('safetydance'),
|
||||
util = require('util');
|
||||
|
||||
@@ -61,12 +60,19 @@ async function ldapSearch(dn, opts) {
|
||||
}
|
||||
|
||||
describe('Ldap', function () {
|
||||
const { setup, cleanup, admin, user, app, domain, mailbox, mailAlias, mailboxName } = common;
|
||||
const { setup, cleanup, admin, user, app, domain, auditSource } = common;
|
||||
let group;
|
||||
|
||||
const mailboxName = 'support';
|
||||
const mailbox = `support@${domain.domain}`;
|
||||
const mailAliasName = 'alsosupport';
|
||||
const mailAlias = `alsosupport@${domain.domain}`;
|
||||
|
||||
before(function (done) {
|
||||
async.series([
|
||||
setup,
|
||||
async () => await mail.addMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true }, auditSource),
|
||||
async () => await mail.setAliases(mailboxName, domain.domain, [ { name: mailAliasName, domain: domain.domain} ], auditSource),
|
||||
ldapServer.start.bind(null),
|
||||
async () => {
|
||||
group = await groups.add({ name: 'ldap-test' });
|
||||
@@ -285,13 +291,11 @@ describe('Ldap', function () {
|
||||
});
|
||||
|
||||
it('cannot get inactive mailbox', async function () {
|
||||
const updateMailbox = util.promisify(mailboxdb.updateMailbox);
|
||||
|
||||
await updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: false });
|
||||
await mail.updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: false }, auditSource);
|
||||
const [error] = await safe(ldapSearch(`cn=${mailbox},ou=mailboxes,dc=cloudron`, 'objectclass=mailbox'));
|
||||
expect(error).to.be.a(ldap.NoSuchObjectError);
|
||||
|
||||
await updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true });
|
||||
await mail.updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true }, auditSource);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -317,8 +321,8 @@ describe('Ldap', function () {
|
||||
describe('search mailing list', function () {
|
||||
const LIST_NAME = 'devs', LIST = `devs@${domain.domain}`;
|
||||
|
||||
before(function (done) {
|
||||
mailboxdb.addList(LIST_NAME, domain.domain, { members: [ mailbox , 'outsider@external.com' ], membersOnly: false, active: true }, done);
|
||||
before(async function () {
|
||||
await mail.addList(LIST_NAME, domain.domain, { members: [ mailbox , 'outsider@external.com' ], membersOnly: false, active: true }, auditSource);
|
||||
});
|
||||
|
||||
it('get specific list', async function () {
|
||||
@@ -334,9 +338,7 @@ describe('Ldap', function () {
|
||||
});
|
||||
|
||||
it('inactive list', async function () {
|
||||
const updateList = util.promisify(mailboxdb.updateList);
|
||||
|
||||
await updateList(LIST_NAME, domain.domain, { members: [ mailbox , 'outsider@external.com' ], membersOnly: false, active: false });
|
||||
await mail.updateList(LIST_NAME, domain.domain, { members: [ mailbox , 'outsider@external.com' ], membersOnly: false, active: false }, auditSource);
|
||||
const [error] = await safe(ldapSearch('cn=devs@example.com,ou=mailinglists,dc=cloudron', 'objectclass=mailGroup'));
|
||||
expect(error).to.be.a(ldap.NoSuchObjectError);
|
||||
});
|
||||
@@ -390,13 +392,11 @@ describe('Ldap', function () {
|
||||
});
|
||||
|
||||
it('does not allow for inactive mailbox', async function () {
|
||||
const updateMailbox = util.promisify(mailboxdb.updateMailbox);
|
||||
|
||||
await mail._updateDomain(domain.domain, { enabled: true });
|
||||
await updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: false });
|
||||
await mail.updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: false }, auditSource);
|
||||
const [error] = await safe(ldapBind(`cn=${mailbox},ou=sendmail,dc=cloudron`, 'badpassword'));
|
||||
expect(error).to.be.a(ldap.NoSuchObjectError);
|
||||
await updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true });
|
||||
await mail.updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true }, auditSource);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -444,14 +444,12 @@ describe('Ldap', function () {
|
||||
});
|
||||
|
||||
it('does not allow for inactive mailbox', async function () {
|
||||
const updateMailbox = util.promisify(mailboxdb.updateMailbox);
|
||||
|
||||
await mail._updateDomain(domain.domain, { enabled: true });
|
||||
await updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: false });
|
||||
await mail.updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: false }, auditSource);
|
||||
const [error] = await safe(ldapBind(`cn=${mailbox},ou=recvmail,dc=cloudron`, 'badpassword'));
|
||||
expect(error).to.be.a(ldap.NoSuchObjectError);
|
||||
await mail._updateDomain(domain.domain, { enabled: false });
|
||||
await updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true });
|
||||
await mail.updateMailbox(mailboxName, domain.domain, { ownerId: user.id, ownerType: mail.OWNERTYPE_USER, active: true }, auditSource);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -6,8 +6,10 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('./common.js'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
expect = require('expect.js'),
|
||||
mail = require('../mail.js');
|
||||
mail = require('../mail.js'),
|
||||
safe = require('safetydance');
|
||||
|
||||
describe('Mail', function () {
|
||||
const { setup, cleanup, domain, auditSource } = common;
|
||||
@@ -15,7 +17,7 @@ describe('Mail', function () {
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
describe('values', function () {
|
||||
describe('settings', function () {
|
||||
it('can get default', async function () {
|
||||
const mailConfig = await mail.getDomain(domain.domain);
|
||||
expect(mailConfig.enabled).to.be(false);
|
||||
@@ -70,4 +72,109 @@ describe('Mail', function () {
|
||||
expect(mailConfig.enabled).to.be(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mailboxes', function () {
|
||||
it('add user mailbox succeeds', async function () {
|
||||
await mail.addMailbox('girish', domain.domain, { ownerId: 'uid-0', ownerType: mail.OWNERTYPE_USER, active: true }, auditSource);
|
||||
});
|
||||
|
||||
it('cannot add dup entry', async function () {
|
||||
const [error] = await safe(mail.addMailbox('girish', domain.domain, { ownerId: 'uid-1', ownerType: mail.OWNERTYPE_GROUP, active: true }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
|
||||
it('add app mailbox succeeds', async function () {
|
||||
await mail.addMailbox('support', domain.domain, { ownerId: 'osticket', ownerType: 'user', active: true}, auditSource);
|
||||
});
|
||||
|
||||
it('get succeeds', async function () {
|
||||
const mailbox = await mail.getMailbox('support', domain.domain);
|
||||
expect(mailbox.name).to.equal('support');
|
||||
expect(mailbox.ownerId).to.equal('osticket');
|
||||
expect(mailbox.domain).to.equal(domain.domain);
|
||||
expect(mailbox.creationTime).to.be.a(Date);
|
||||
});
|
||||
|
||||
it('get non-existent mailbox', async function () {
|
||||
const mailbox = await mail.getMailbox('random', domain.domain);
|
||||
expect(mailbox).to.be(null);
|
||||
});
|
||||
|
||||
it('list mailboxes succeeds', async function () {
|
||||
const mailboxes = await mail.listMailboxes(domain.domain, null /* search */, 1, 10);
|
||||
expect(mailboxes.length).to.be(2);
|
||||
expect(mailboxes[0].name).to.be('girish');
|
||||
expect(mailboxes[1].name).to.be('support');
|
||||
});
|
||||
|
||||
it('list all mailboxes succeeds', async function () {
|
||||
const mailboxes = await mail.listAllMailboxes(1, 10);
|
||||
expect(mailboxes.length).to.be(2);
|
||||
expect(mailboxes[0].name).to.be('girish');
|
||||
expect(mailboxes[0].domain).to.be(domain.domain);
|
||||
expect(mailboxes[1].name).to.be('support');
|
||||
expect(mailboxes[1].domain).to.be(domain.domain);
|
||||
});
|
||||
|
||||
it('mailbox count succeeds', async function () {
|
||||
const count = await mail.getMailboxCount(domain.domain);
|
||||
expect(count).to.be(2);
|
||||
});
|
||||
|
||||
it('can set alias', async function () {
|
||||
await mail.setAliases('support', domain.domain, [ { name: 'support2', domain: domain.domain }, { name: 'help', domain: domain.domain } ]);
|
||||
});
|
||||
|
||||
it('can get aliases of name', async function () {
|
||||
const results = await mail.getAliases('support', domain.domain);
|
||||
expect(results.length).to.be(2);
|
||||
expect(results[0].name).to.be('help');
|
||||
expect(results[0].domain).to.be(domain.domain);
|
||||
expect(results[1].name).to.be('support2');
|
||||
expect(results[1].domain).to.be(domain.domain);
|
||||
});
|
||||
|
||||
it('unset aliases', async function () {
|
||||
await mail.setAliases('support', domain.domain, []);
|
||||
|
||||
const results = await mail.getAliases('support', domain.domain);
|
||||
expect(results.length).to.be(0);
|
||||
});
|
||||
|
||||
it('add list succeeds', async function () {
|
||||
await mail.addList('people', domain.domain, { members: [ 'test@cloudron.io' ], membersOnly: false, active: true }, auditSource);
|
||||
});
|
||||
|
||||
it('cannot add dup list', async function () {
|
||||
const [error] = await safe(mail.addList('people', domain.domain, { members: [ 'admin@cloudron.io' ], membersOnly: false, active: true }, auditSource));
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
});
|
||||
|
||||
it('cannot get non-existing list', async function () {
|
||||
const result = await mail.getList('random', domain.domain);
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
|
||||
it('del list succeeds', async function () {
|
||||
await mail.delList('people', domain.domain, auditSource);
|
||||
const result = await mail.getList('people', domain.domain);
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
|
||||
it('del non-existent list fails', async function () {
|
||||
const [error] = await safe(mail.delList('people', domain.domain, auditSource));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
});
|
||||
|
||||
it('del mailbox succeeds', async function () {
|
||||
await mail.delMailbox('girish', domain.domain, {/*options*/}, auditSource);
|
||||
const result = await mail.getMailbox('girish', domain.domain);
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
|
||||
it('del non-existent mailbox fails', async function () {
|
||||
const [error] = await safe(mail.delMailbox('girish', domain.domain, {/*options*/}, auditSource));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user