Files
cloudron-box/src/routes/test/mail-test.js
T

678 lines
31 KiB
JavaScript
Raw Normal View History

2018-01-20 18:30:14 -08:00
'use strict';
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
2021-06-29 14:26:34 -07:00
const common = require('./common.js'),
2018-01-20 18:30:14 -08:00
expect = require('expect.js'),
2018-01-25 15:38:29 -08:00
mail = require('../../mail.js'),
2019-07-26 10:49:29 -07:00
settings = require('../../settings.js'),
2018-01-29 18:51:53 +01:00
superagent = require('superagent'),
2019-02-15 10:55:15 -08:00
_ = require('underscore');
2018-01-20 18:30:14 -08:00
describe('Mail API', function () {
2021-06-29 14:26:34 -07:00
const { setup, cleanup, serverUrl, owner, dashboardDomain } = common;
2021-10-11 19:51:29 -07:00
let publicKey;
before(async () => {
await setup();
const d = await mail.getDomain(dashboardDomain);
publicKey = d.dkimKey.publicKey.split('\n').slice(1, -2).join(''); // remove header, footer and new lines
});
2018-01-20 18:30:14 -08:00
after(cleanup);
2018-01-29 17:18:01 +01:00
describe('crud', function () {
2021-06-29 14:26:34 -07:00
it('cannot get non-existing domain', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/doesnotexist.com`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-29 17:18:01 +01:00
});
2021-06-29 14:26:34 -07:00
it('can get domain', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.domain).to.equal(dashboardDomain);
expect(response.body.enabled).to.equal(false);
expect(response.body.mailFromValidation).to.equal(true);
expect(response.body.catchAll).to.be.an(Array);
expect(response.body.catchAll.length).to.equal(0);
expect(response.body.relay).to.be.an('object');
expect(response.body.relay.provider).to.equal('cloudron-smtp');
2018-01-29 17:18:01 +01:00
});
});
2018-01-29 17:45:10 +01:00
describe('status', function () {
2021-06-29 14:26:34 -07:00
let resolve = null;
let dnsAnswerQueue = [];
let dkimDomain, spfDomain, mxDomain, dmarcDomain;
2018-01-20 18:30:14 -08:00
before(function (done) {
2022-02-03 16:15:14 -08:00
const dig = require('../../dig.js');
2018-01-20 18:30:14 -08:00
// replace dns resolveTxt()
2022-02-03 16:15:14 -08:00
resolve = dig.resolve;
dig.resolve = async function (hostname, type/*, options*/) {
2018-01-20 18:30:14 -08:00
expect(hostname).to.be.a('string');
2021-08-27 09:52:24 -07:00
if (!dnsAnswerQueue[hostname] || !(type in dnsAnswerQueue[hostname])) throw new Error('no mock answer');
2018-01-20 18:30:14 -08:00
2021-08-27 09:52:24 -07:00
if (dnsAnswerQueue[hostname][type] === null) throw new Error({ code: 'ENODATA'} );
2018-02-08 10:21:31 -08:00
2021-08-27 09:52:24 -07:00
return dnsAnswerQueue[hostname][type];
2018-01-20 18:30:14 -08:00
};
2021-06-29 14:26:34 -07:00
dkimDomain = `cloudron._domainkey.${dashboardDomain}`; // no suffix for provisioned domains
spfDomain = dashboardDomain;
mxDomain = dashboardDomain;
dmarcDomain = '_dmarc.' + dashboardDomain;
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/enable`)
.query({ access_token: owner.token })
2020-03-31 12:04:46 -07:00
.send({ enabled: true })
2021-06-29 14:26:34 -07:00
.end(function (err, response) {
expect(response.statusCode).to.equal(202);
2019-02-08 11:08:14 -08:00
2020-03-31 12:04:46 -07:00
done();
2018-02-08 10:21:31 -08:00
});
2018-01-20 18:30:14 -08:00
});
after(function (done) {
2022-02-03 16:15:14 -08:00
const dig = require('../../dig.js');
2018-01-20 18:30:14 -08:00
2022-02-03 16:15:14 -08:00
dig.resolve = resolve;
2018-01-20 18:30:14 -08:00
2020-03-31 12:04:46 -07:00
done();
2018-01-20 18:30:14 -08:00
});
2021-06-29 14:26:34 -07:00
it('does not fail when dns errors', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/status`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
2018-01-20 18:30:14 -08:00
});
function clearDnsAnswerQueue() {
dnsAnswerQueue = { };
dnsAnswerQueue[dkimDomain] = { };
dnsAnswerQueue[spfDomain] = { };
dnsAnswerQueue[mxDomain] = { };
dnsAnswerQueue[dmarcDomain] = { };
}
2021-06-29 14:26:34 -07:00
it('succeeds with dns errors', async function () {
2018-01-20 18:30:14 -08:00
clearDnsAnswerQueue();
2021-06-29 14:26:34 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.dns.dkim).to.be.an('object');
expect(response.body.dns.dkim.domain).to.eql(dkimDomain);
expect(response.body.dns.dkim.type).to.eql('TXT');
expect(response.body.dns.dkim.value).to.eql(null);
2021-10-11 19:51:29 -07:00
expect(response.body.dns.dkim.expected).to.eql('v=DKIM1; t=s; p=' + publicKey);
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dkim.status).to.eql(false);
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.domain).to.eql(spfDomain);
expect(response.body.dns.spf.type).to.eql('TXT');
expect(response.body.dns.spf.value).to.eql(null);
expect(response.body.dns.spf.expected).to.eql('v=spf1 a:' + settings.dashboardFqdn() + ' ~all');
expect(response.body.dns.spf.status).to.eql(false);
expect(response.body.dns.dmarc).to.be.an('object');
expect(response.body.dns.dmarc.type).to.eql('TXT');
expect(response.body.dns.dmarc.value).to.eql(null);
expect(response.body.dns.dmarc.expected).to.eql('v=DMARC1; p=reject; pct=100');
expect(response.body.dns.dmarc.status).to.eql(false);
expect(response.body.dns.mx).to.be.an('object');
expect(response.body.dns.mx.type).to.eql('MX');
expect(response.body.dns.mx.value).to.eql(null);
expect(response.body.dns.mx.expected).to.eql('10 ' + settings.mailFqdn() + '.');
expect(response.body.dns.mx.status).to.eql(false);
expect(response.body.dns.ptr).to.be.an('object');
expect(response.body.dns.ptr.type).to.eql('PTR');
// expect(response.body.ptr.value).to.eql(null); this will be anything random
expect(response.body.dns.ptr.expected).to.eql(settings.mailFqdn());
expect(response.body.dns.ptr.status).to.eql(false);
});
it('succeeds with "undefined" spf, dkim, dmarc, mx, ptr records', async function () {
2018-01-20 18:30:14 -08:00
clearDnsAnswerQueue();
dnsAnswerQueue[dkimDomain].TXT = null;
dnsAnswerQueue[spfDomain].TXT = null;
dnsAnswerQueue[mxDomain].MX = null;
dnsAnswerQueue[dmarcDomain].TXT = null;
2021-06-29 14:26:34 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.statusCode).to.equal(200);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.expected).to.eql('v=spf1 a:' + settings.dashboardFqdn() + ' ~all');
expect(response.body.dns.spf.status).to.eql(false);
expect(response.body.dns.spf.value).to.eql(null);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dkim).to.be.an('object');
2021-10-11 19:51:29 -07:00
expect(response.body.dns.dkim.expected).to.eql('v=DKIM1; t=s; p=' + publicKey);
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dkim.status).to.eql(false);
expect(response.body.dns.dkim.value).to.eql(null);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dmarc).to.be.an('object');
expect(response.body.dns.dmarc.expected).to.eql('v=DMARC1; p=reject; pct=100');
expect(response.body.dns.dmarc.status).to.eql(false);
expect(response.body.dns.dmarc.value).to.eql(null);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.mx).to.be.an('object');
expect(response.body.dns.mx.status).to.eql(false);
expect(response.body.dns.mx.expected).to.eql('10 ' + settings.mailFqdn() + '.');
expect(response.body.dns.mx.value).to.eql(null);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.ptr).to.be.an('object');
expect(response.body.dns.ptr.expected).to.eql(settings.mailFqdn());
expect(response.body.dns.ptr.status).to.eql(false);
// expect(response.body.ptr.value).to.eql(null); this will be anything random
2018-01-20 18:30:14 -08:00
});
2021-06-29 14:26:34 -07:00
it('succeeds with all different spf, dkim, dmarc, mx, ptr records', async function () {
2018-01-20 18:30:14 -08:00
clearDnsAnswerQueue();
2020-04-22 17:16:52 -07:00
dnsAnswerQueue[mxDomain].MX = [ { priority: '20', exchange: settings.mailFqdn() }, { priority: '10', exchange: 'some.other.server' } ];
2018-02-08 10:21:31 -08:00
dnsAnswerQueue[dmarcDomain].TXT = [['v=DMARC2; p=reject; pct=100']];
2021-10-11 19:51:29 -07:00
dnsAnswerQueue[dkimDomain].TXT = [['v=DKIM2; t=s; p=' + publicKey]];
2018-02-08 10:21:31 -08:00
dnsAnswerQueue[spfDomain].TXT = [['v=spf1 a:random.com ~all']];
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.statusCode).to.equal(200);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.expected).to.eql('v=spf1 a:' + settings.dashboardFqdn() + ' a:random.com ~all');
expect(response.body.dns.spf.status).to.eql(false);
expect(response.body.dns.spf.value).to.eql('v=spf1 a:random.com ~all');
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dkim).to.be.an('object');
2021-10-11 19:51:29 -07:00
expect(response.body.dns.dkim.expected).to.eql('v=DKIM1; t=s; p=' + publicKey);
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dkim.status).to.eql(true); // as long as p= matches we are good
2021-10-11 19:51:29 -07:00
expect(response.body.dns.dkim.value).to.eql('v=DKIM2; t=s; p=' + publicKey);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dmarc).to.be.an('object');
expect(response.body.dns.dmarc.expected).to.eql('v=DMARC1; p=reject; pct=100');
expect(response.body.dns.dmarc.status).to.eql(false);
expect(response.body.dns.dmarc.value).to.eql('v=DMARC2; p=reject; pct=100');
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.mx).to.be.an('object');
expect(response.body.dns.mx.status).to.eql(true);
expect(response.body.dns.mx.expected).to.eql('10 ' + settings.mailFqdn() + '.');
expect(response.body.dns.mx.value).to.eql('20 ' + settings.mailFqdn() + '. 10 some.other.server.');
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.ptr).to.be.an('object');
expect(response.body.dns.ptr.expected).to.eql(settings.mailFqdn());
expect(response.body.dns.ptr.status).to.eql(false);
// expect(response.body.ptr.value).to.eql(null); this will be anything random
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.relay).to.be.an('object');
2018-01-20 18:30:14 -08:00
});
2021-06-29 14:26:34 -07:00
it('succeeds with existing embedded spf', async function () {
2018-01-20 18:30:14 -08:00
clearDnsAnswerQueue();
2019-07-26 10:49:29 -07:00
dnsAnswerQueue[spfDomain].TXT = [['v=spf1 a:example.com a:' + settings.mailFqdn() + ' ~all']];
2018-01-20 18:30:14 -08:00
2021-08-27 09:52:24 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/status`)
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token });
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.statusCode).to.equal(200);
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.domain).to.eql(spfDomain);
expect(response.body.dns.spf.type).to.eql('TXT');
expect(response.body.dns.spf.value).to.eql('v=spf1 a:example.com a:' + settings.mailFqdn() + ' ~all');
expect(response.body.dns.spf.expected).to.eql('v=spf1 a:example.com a:' + settings.mailFqdn() + ' ~all');
expect(response.body.dns.spf.status).to.eql(true);
2018-01-20 18:30:14 -08:00
});
2021-06-29 14:26:34 -07:00
it('succeeds with modified DMARC1 values', async function () {
2018-08-12 13:43:45 -07:00
clearDnsAnswerQueue();
dnsAnswerQueue[dmarcDomain].TXT = [['v=DMARC1; p=reject; rua=mailto:rua@example.com; pct=100']];
2021-08-27 09:52:24 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/status`)
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token });
2018-08-12 13:43:45 -07:00
2021-06-29 14:26:34 -07:00
expect(response.statusCode).to.equal(200);
2018-08-12 13:43:45 -07:00
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dmarc).to.be.an('object');
expect(response.body.dns.dmarc.expected).to.eql('v=DMARC1; p=reject; pct=100');
expect(response.body.dns.dmarc.status).to.eql(true);
expect(response.body.dns.dmarc.value).to.eql('v=DMARC1; p=reject; rua=mailto:rua@example.com; pct=100');
2018-08-12 13:43:45 -07:00
});
2021-06-29 14:26:34 -07:00
it('succeeds with all correct records', async function () {
2018-01-20 18:30:14 -08:00
clearDnsAnswerQueue();
2019-07-26 10:49:29 -07:00
dnsAnswerQueue[mxDomain].MX = [ { priority: '10', exchange: settings.mailFqdn() } ];
2018-02-08 10:21:31 -08:00
dnsAnswerQueue[dmarcDomain].TXT = [['v=DMARC1; p=reject; pct=100']];
2021-10-11 19:51:29 -07:00
dnsAnswerQueue[dkimDomain].TXT = [['v=DKIM1; t=s; p=', publicKey ]];
2021-05-05 12:29:04 -07:00
dnsAnswerQueue[spfDomain].TXT = [['v=spf1 a:' + settings.dashboardFqdn() + ' ~all']];
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
2018-01-20 18:30:14 -08:00
2021-06-29 14:26:34 -07:00
expect(response.statusCode).to.equal(200);
expect(response.body.dns.dkim).to.be.an('object');
expect(response.body.dns.dkim.domain).to.eql(dkimDomain);
expect(response.body.dns.dkim.type).to.eql('TXT');
2021-10-11 19:51:29 -07:00
expect(response.body.dns.dkim.value).to.eql('v=DKIM1; t=s; p=' + publicKey);
expect(response.body.dns.dkim.expected).to.eql('v=DKIM1; t=s; p=' + publicKey);
2021-06-29 14:26:34 -07:00
expect(response.body.dns.dkim.status).to.eql(true);
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.domain).to.eql(spfDomain);
expect(response.body.dns.spf.type).to.eql('TXT');
expect(response.body.dns.spf.value).to.eql('v=spf1 a:' + settings.dashboardFqdn() + ' ~all');
expect(response.body.dns.spf.expected).to.eql('v=spf1 a:' + settings.dashboardFqdn() + ' ~all');
expect(response.body.dns.spf.status).to.eql(true);
expect(response.body.dns.dmarc).to.be.an('object');
expect(response.body.dns.dmarc.expected).to.eql('v=DMARC1; p=reject; pct=100');
expect(response.body.dns.dmarc.status).to.eql(true);
expect(response.body.dns.dmarc.value).to.eql('v=DMARC1; p=reject; pct=100');
expect(response.body.dns.mx).to.be.an('object');
expect(response.body.dns.mx.status).to.eql(true);
expect(response.body.dns.mx.expected).to.eql('10 ' + settings.mailFqdn() + '.');
expect(response.body.dns.mx.value).to.eql('10 ' + settings.mailFqdn() + '.');
2018-01-20 18:30:14 -08:00
});
});
2018-01-29 17:45:10 +01:00
describe('mail from validation', function () {
2021-06-29 14:26:34 -07:00
it('get mail from validation succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.mailFromValidation).to.eql(true);
});
2021-06-29 14:26:34 -07:00
it('cannot set without enabled field', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/mail_from_validation`)
.query({ access_token: owner.token })
.send({ })
2021-06-29 14:26:34 -07:00
.ok(() => true);
expect(response.statusCode).to.equal(400);
});
2021-06-29 14:26:34 -07:00
it('can set with enabled field', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/mail_from_validation`)
.query({ access_token: owner.token })
.send({ enabled: false });
expect(response.statusCode).to.equal(202);
});
});
2018-01-29 17:45:10 +01:00
describe('catch_all', function () {
2021-06-29 14:26:34 -07:00
it('get catch_all succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.catchAll).to.eql([]);
});
2021-06-29 14:26:34 -07:00
it('cannot set without addresses field', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/catch_all`)
.query({ access_token: owner.token })
.send({ })
.ok(() => true);
expect(response.statusCode).to.equal(400);
});
2021-06-29 14:26:34 -07:00
it('cannot set with bad addresses field', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/catch_all`)
.query({ access_token: owner.token })
2018-04-13 12:54:40 +02:00
.send({ addresses: [ 'user1', 123 ] })
2021-06-29 14:26:34 -07:00
.ok(() => true);
expect(response.statusCode).to.equal(400);
});
2021-06-29 14:26:34 -07:00
it('set succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/catch_all`)
.query({ access_token: owner.token })
.send({ addresses: [ 'user1' ] });
expect(response.statusCode).to.equal(202);
});
2021-06-29 14:26:34 -07:00
it('get succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.catchAll).to.eql([ 'user1' ]);
});
});
2018-01-29 17:45:10 +01:00
describe('mail relay', function () {
2021-06-29 14:26:34 -07:00
it('get mail relay succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.relay).to.eql({ provider: 'cloudron-smtp' });
});
2021-06-29 14:26:34 -07:00
it('cannot set without provider field', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/relay`)
.query({ access_token: owner.token })
.send({ })
2021-06-29 14:26:34 -07:00
.ok(() => true);
expect(response.statusCode).to.equal(400);
});
2021-06-29 14:26:34 -07:00
it('cannot set with bad host', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/relay`)
.query({ access_token: owner.token })
.send({ provider: 'external-smtp', host: true })
2021-06-29 14:26:34 -07:00
.ok(() => true);
expect(response.statusCode).to.equal(400);
});
2021-06-29 14:26:34 -07:00
it('set fails because mail server is unreachable', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/relay`)
.query({ access_token: owner.token })
.send({ provider: 'external-smtp', host: 'host', port: 25, username: 'u', password: 'p', tls: true })
2021-06-29 14:26:34 -07:00
.ok(() => true);
expect(response.statusCode).to.equal(400);
});
2021-06-29 14:26:34 -07:00
it('get succeeds', async function () {
const relay = { provider: 'external-smtp', host: 'host', port: 25, username: 'u', password: 'p', tls: true };
2021-06-29 14:26:34 -07:00
await mail.setMailRelay(dashboardDomain, relay, { skipVerify: true });
2021-06-29 14:26:34 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(_.omit(response.body.relay, 'password')).to.eql(_.omit(relay, 'password'));
});
});
2018-01-29 18:51:53 +01:00
describe('mailboxes', function () {
2021-06-29 14:26:34 -07:00
const MAILBOX_NAME = 'support';
it('add succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes`)
2022-08-17 23:18:38 +02:00
.send({ name: MAILBOX_NAME, ownerId: owner.id, ownerType: 'user', active: true, storageQuota: 10, messagesQuota: 20 })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
2018-01-29 18:51:53 +01:00
});
2021-06-29 14:26:34 -07:00
it('cannot add again', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes`)
2022-08-17 23:18:38 +02:00
.send({ name: MAILBOX_NAME, ownerId: owner.id, ownerType: 'user', active: true, storageQuota: 10, messagesQuota: 20 })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(409);
2018-01-29 18:51:53 +01:00
});
2021-06-29 14:26:34 -07:00
it('get fails if not exist', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/someuserdoesnotexist`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-29 18:51:53 +01:00
});
2021-06-29 14:26:34 -07:00
it('get succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/${MAILBOX_NAME}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.mailbox).to.be.an('object');
expect(response.body.mailbox.name).to.equal(MAILBOX_NAME);
expect(response.body.mailbox.ownerId).to.equal(owner.id);
expect(response.body.mailbox.ownerType).to.equal('user');
expect(response.body.mailbox.aliasName).to.equal(null);
expect(response.body.mailbox.aliasDomain).to.equal(null);
expect(response.body.mailbox.domain).to.equal(dashboardDomain);
2022-08-17 23:18:38 +02:00
expect(response.body.mailbox.storageQuota).to.equal(10);
expect(response.body.mailbox.messagesQuota).to.equal(20);
2018-01-29 18:51:53 +01:00
});
2021-06-29 14:26:34 -07:00
it('listing succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.mailboxes.length).to.eql(1);
expect(response.body.mailboxes[0]).to.be.an('object');
expect(response.body.mailboxes[0].name).to.equal(MAILBOX_NAME);
expect(response.body.mailboxes[0].ownerId).to.equal(owner.id);
expect(response.body.mailboxes[0].ownerType).to.equal('user');
expect(response.body.mailboxes[0].aliases).to.eql([]);
expect(response.body.mailboxes[0].domain).to.equal(dashboardDomain);
2022-08-17 23:18:38 +02:00
expect(response.body.mailboxes[0].storageQuota).to.equal(10);
expect(response.body.mailboxes[0].messagesQuota).to.equal(20);
2018-01-29 18:51:53 +01:00
});
2021-06-29 14:26:34 -07:00
it('disable fails even if not exist', async function () {
const response = await superagent.del(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/someuserdoesnotexist`)
2020-07-27 22:26:10 -07:00
.send({ deleteMails: false })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-29 18:51:53 +01:00
});
2021-06-29 14:26:34 -07:00
it('disable succeeds', async function () {
const response = await superagent.del(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/${MAILBOX_NAME}`)
2020-07-27 22:26:10 -07:00
.send({ deleteMails: false })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
const response2 = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/mailboxes/' + MAILBOX_NAME)
.query({ access_token: owner.token })
.ok(() => true);
expect(response2.statusCode).to.equal(404);
2018-01-29 18:51:53 +01:00
});
});
2018-01-29 19:39:07 +01:00
describe('aliases', function () {
2021-06-29 14:26:34 -07:00
const MAILBOX_NAME = 'support';
2021-08-17 15:45:57 -07:00
after(async function () {
await mail._delByDomain(dashboardDomain);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('add the mailbox', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes`)
2022-08-17 23:18:38 +02:00
.send({ name: MAILBOX_NAME, ownerId: owner.id, ownerType: 'user', active: true, storageQuota: 10, messagesQuota: 20 })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('set fails if aliases is missing', async function () {
const response = await superagent.put(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/${MAILBOX_NAME}/aliases`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('set fails if user does not exist', async function () {
const response = await superagent.put(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/randomuser/aliases`)
.send({ aliases: [{ name: 'hello', domain: dashboardDomain}, {name: 'there', domain: dashboardDomain}] })
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('set fails if aliases is the wrong type', async function () {
const response = await superagent.put(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/${MAILBOX_NAME}/aliases`)
2020-04-22 18:16:33 -07:00
.send({ aliases: 'hello, there' })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('set succeeds', async function () {
const response = await superagent.put(`${serverUrl}/api/v1/mail/${dashboardDomain}/mailboxes/${MAILBOX_NAME}/aliases`)
.send({ aliases: [{ name: 'hello', domain: dashboardDomain}, {name: 'there', domain: dashboardDomain}] })
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(202);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('get succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/mailboxes/' + MAILBOX_NAME + '/aliases')
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.aliases).to.eql([{ name: 'hello', domain: dashboardDomain}, {name: 'there', domain: dashboardDomain}]);
2018-01-29 19:39:07 +01:00
});
2021-06-29 14:26:34 -07:00
it('get fails if mailbox does not exist', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/mailboxes/somerandomuser/aliases')
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-29 19:39:07 +01:00
});
});
2018-01-30 12:23:08 +01:00
describe('mailinglists', function () {
2021-06-29 14:26:34 -07:00
const LIST_NAME = 'people';
2021-08-17 15:45:57 -07:00
after(async function () {
await mail._delByDomain(dashboardDomain);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('add fails without groupId', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('add fails with invalid groupId', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists`)
2018-01-30 12:23:08 +01:00
.send({ groupId: {} })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('add fails without members array', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists`)
2018-04-05 16:07:51 -07:00
.send({ name: LIST_NAME })
2021-06-29 14:26:34 -07:00
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('add succeeds', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists`)
.send({ name: LIST_NAME, members: [ `admin2@${dashboardDomain}`, `${owner.username}@${dashboardDomain}`], membersOnly: false, active: true })
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('add twice fails', async function () {
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists`)
.send({ name: LIST_NAME, members: [ `admin2@${dashboardDomain}`, `${owner.username}@${dashboardDomain}`], membersOnly: false, active: true })
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(409);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('get fails if not exist', async function (){
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists/doesnotexist`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('get succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists/${LIST_NAME}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.list).to.be.an('object');
expect(response.body.list.name).to.equal(LIST_NAME);
expect(response.body.list.ownerId).to.equal('admin');
expect(response.body.list.aliasName).to.equal(null);
expect(response.body.list.domain).to.equal(dashboardDomain);
expect(response.body.list.members).to.eql([ `admin2@${dashboardDomain}`, `superadmin@${dashboardDomain}` ]);
expect(response.body.list.membersOnly).to.be(false);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('get all succeeds', async function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.body.lists).to.be.an(Array);
expect(response.body.lists.length).to.equal(1);
expect(response.body.lists[0].name).to.equal(LIST_NAME);
expect(response.body.lists[0].ownerId).to.equal('admin');
expect(response.body.lists[0].aliasName).to.equal(null);
expect(response.body.lists[0].domain).to.equal(dashboardDomain);
expect(response.body.lists[0].members).to.eql([ `admin2@${dashboardDomain}`, `superadmin@${dashboardDomain}` ]);
expect(response.body.lists[0].membersOnly).to.be(false);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('del fails if list does not exist', async function () {
const response = await superagent.del(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/lists/' + 'doesnotexist')
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
2018-01-30 12:23:08 +01:00
});
2021-06-29 14:26:34 -07:00
it('del succeeds', async function () {
const response = await superagent.del(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists/${LIST_NAME}`)
.query({ access_token: owner.token });
2018-01-30 12:23:08 +01:00
2021-06-29 14:26:34 -07:00
expect(response.statusCode).to.equal(204);
const response2 = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists/${LIST_NAME}`)
.query({ access_token: owner.token })
.ok(() => true);
expect(response2.statusCode).to.equal(404);
2018-01-30 12:23:08 +01:00
});
});
2018-01-20 18:30:14 -08:00
});