replace with custom superagent based on fetch API

This commit is contained in:
Girish Ramakrishnan
2025-02-14 17:26:54 +01:00
parent 68a08b1f62
commit 8e58349bfa
66 changed files with 1086 additions and 1031 deletions
+54 -57
View File
@@ -8,7 +8,7 @@
const common = require('./common.js'),
expect = require('expect.js'),
mail = require('../../mail.js'),
superagent = require('superagent'),
superagent = require('../../superagent.js'),
_ = require('../../underscore.js');
describe('Mail API', function () {
@@ -29,14 +29,14 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
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.status).to.equal(200);
expect(response.body.domain).to.equal(dashboardDomain);
expect(response.body.enabled).to.equal(false);
expect(response.body.mailFromValidation).to.equal(true);
@@ -52,7 +52,7 @@ describe('Mail API', function () {
let dnsAnswerQueue = [];
let dkimDomain, spfDomain, mxDomain, dmarcDomain;
before(function (done) {
before(async function () {
const dig = require('../../dig.js');
// replace dns resolveTxt()
@@ -72,14 +72,11 @@ describe('Mail API', function () {
mxDomain = dashboardDomain;
dmarcDomain = '_dmarc.' + dashboardDomain;
superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/enable`)
const response = await superagent.post(`${serverUrl}/api/v1/mail/${dashboardDomain}/enable`)
.query({ access_token: owner.token })
.send({ enabled: true })
.end(function (err, response) {
expect(response.statusCode).to.equal(202);
.send({ enabled: true });
done();
});
expect(response.status).to.be(202);
});
after(function (done) {
@@ -94,7 +91,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/status`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
});
function clearDnsAnswerQueue() {
@@ -110,7 +107,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.dns.dkim).to.be.an('object');
expect(response.body.dns.dkim.domain).to.eql(dkimDomain);
@@ -156,7 +153,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.expected).to.eql(`v=spf1 a:${dashboardFqdn} ~all`);
@@ -195,7 +192,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.expected).to.eql(`v=spf1 a:${dashboardFqdn} a:random.com ~all`);
@@ -233,7 +230,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/status`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.dns.spf).to.be.an('object');
expect(response.body.dns.spf.domain).to.eql(spfDomain);
@@ -251,7 +248,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}/status`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.dns.dmarc).to.be.an('object');
expect(response.body.dns.dmarc.expected).to.eql('v=DMARC1; p=reject; pct=100');
@@ -270,7 +267,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}` + '/status')
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.dns.dkim).to.be.an('object');
expect(response.body.dns.dkim.domain).to.eql(dkimDomain);
@@ -303,7 +300,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(response.body.mailFromValidation).to.eql(true);
});
@@ -313,7 +310,7 @@ describe('Mail API', function () {
.send({ })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('can set with enabled field', async function () {
@@ -321,7 +318,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.send({ enabled: false });
expect(response.statusCode).to.equal(202);
expect(response.status).to.equal(202);
});
});
@@ -329,7 +326,7 @@ describe('Mail API', function () {
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.status).to.equal(200);
expect(response.body.catchAll).to.eql([]);
});
@@ -339,7 +336,7 @@ describe('Mail API', function () {
.send({ })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('cannot set with bad addresses field', async function () {
@@ -348,7 +345,7 @@ describe('Mail API', function () {
.send({ addresses: [ 'user1', 123 ] })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('cannot set with bad addresses field', async function () {
@@ -357,7 +354,7 @@ describe('Mail API', function () {
.send({ addresses: [ 'user1' ] })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('set succeeds', async function () {
@@ -365,14 +362,14 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.send({ addresses: [ `user1@${dashboardDomain}` ] });
expect(response.statusCode).to.equal(202);
expect(response.status).to.equal(202);
});
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.status).to.equal(200);
expect(response.body.catchAll).to.eql([ `user1@${dashboardDomain}` ]);
});
});
@@ -381,7 +378,7 @@ describe('Mail API', function () {
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.status).to.equal(200);
expect(response.body.relay).to.eql({ provider: 'cloudron-smtp' });
});
@@ -391,7 +388,7 @@ describe('Mail API', function () {
.send({ })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('cannot set with bad host', async function () {
@@ -400,7 +397,7 @@ describe('Mail API', function () {
.send({ provider: 'external-smtp', host: true })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('set fails because mail server is unreachable', async function () {
@@ -409,7 +406,7 @@ describe('Mail API', function () {
.send({ provider: 'external-smtp', host: 'host', port: 25, username: 'u', password: 'p', tls: true })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('get succeeds', async function () {
@@ -420,7 +417,7 @@ describe('Mail API', function () {
const response = await superagent.get(`${serverUrl}/api/v1/mail/${dashboardDomain}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(200);
expect(response.status).to.equal(200);
expect(_.omit(response.body.relay, ['password'])).to.eql(_.omit(relay, ['password']));
});
});
@@ -433,7 +430,7 @@ describe('Mail API', function () {
.send({ name: MAILBOX_NAME, ownerId: owner.id, ownerType: 'user', active: true, storageQuota: 10, messagesQuota: 20 })
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
expect(response.status).to.equal(201);
});
it('cannot add again', async function () {
@@ -442,7 +439,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(409);
expect(response.status).to.equal(409);
});
it('get fails if not exist', async function () {
@@ -450,14 +447,14 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
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.status).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);
@@ -473,7 +470,7 @@ describe('Mail API', 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.status).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);
@@ -491,7 +488,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
it('disable succeeds', async function () {
@@ -499,12 +496,12 @@ describe('Mail API', function () {
.send({ deleteMails: false })
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
expect(response.status).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);
expect(response2.status).to.equal(404);
});
});
@@ -520,7 +517,7 @@ describe('Mail API', function () {
.send({ name: MAILBOX_NAME, ownerId: owner.id, ownerType: 'user', active: true, storageQuota: 10, messagesQuota: 20 })
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(201);
expect(response.status).to.equal(201);
});
it('set fails if aliases is missing', async function () {
@@ -528,7 +525,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('set fails if user does not exist', async function () {
@@ -537,7 +534,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
it('set fails if aliases is the wrong type', async function () {
@@ -546,7 +543,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('set succeeds', async function () {
@@ -554,14 +551,14 @@ describe('Mail API', function () {
.send({ aliases: [{ name: 'hello*', domain: dashboardDomain}, {name: 'there', domain: dashboardDomain}] })
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(202);
expect(response.status).to.equal(202);
});
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.status).to.equal(200);
expect(response.body.aliases).to.eql([{ name: 'hello*', domain: dashboardDomain}, {name: 'there', domain: dashboardDomain}]);
});
@@ -570,7 +567,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
});
@@ -586,7 +583,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('add fails with invalid groupId', async function () {
@@ -595,7 +592,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('add fails without members array', async function () {
@@ -604,7 +601,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(400);
expect(response.status).to.equal(400);
});
it('add succeeds', async function () {
@@ -612,7 +609,7 @@ describe('Mail API', function () {
.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);
expect(response.status).to.equal(201);
});
it('add twice fails', async function () {
@@ -621,7 +618,7 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(409);
expect(response.status).to.equal(409);
});
it('get fails if not exist', async function (){
@@ -629,14 +626,14 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
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.status).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');
@@ -650,7 +647,7 @@ describe('Mail API', 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.status).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);
@@ -666,20 +663,20 @@ describe('Mail API', function () {
.query({ access_token: owner.token })
.ok(() => true);
expect(response.statusCode).to.equal(404);
expect(response.status).to.equal(404);
});
it('del succeeds', async function () {
const response = await superagent.del(`${serverUrl}/api/v1/mail/${dashboardDomain}/lists/${LIST_NAME}`)
.query({ access_token: owner.token });
expect(response.statusCode).to.equal(204);
expect(response.status).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);
expect(response2.status).to.equal(404);
});
});
});