wildduck/test/api-test.js

457 lines
17 KiB
JavaScript
Raw Normal View History

2017-07-26 20:50:54 +08:00
/*eslint no-unused-expressions: 0, prefer-arrow-callback: 0, no-console:0 */
/* globals before: false, after: false */
2017-07-26 20:50:54 +08:00
'use strict';
const supertest = require('supertest');
2017-07-26 20:50:54 +08:00
const chai = require('chai');
const expect = chai.expect;
chai.config.includeStack = true;
const server = supertest.agent('http://localhost:8080');
2017-07-26 20:50:54 +08:00
2020-07-31 18:36:07 +08:00
describe('API tests', function () {
let userId, asp, address, inbox;
2017-12-13 16:33:13 +08:00
this.timeout(10000); // eslint-disable-line no-invalid-this
before(async () => {
// ensure that we have an existing user account
const response = await server
.post('/users')
.send({
2017-12-13 16:33:13 +08:00
username: 'testuser',
password: 'secretpass',
address: 'testuser@example.com',
name: 'test user'
})
.expect(200);
expect(response.body.success).to.be.true;
expect(response.body.id).to.exist;
userId = response.body.id;
});
after(async () => {
if (!userId) {
return;
}
const response = await server.delete(`/users/${userId}`).expect(200);
expect(response.body.success).to.be.true;
userId = false;
});
2020-09-25 17:58:00 +08:00
describe('user', () => {
it('should POST /domainaliases', async () => {
const response = await server
.post('/domainaliases')
.send({
alias: 'jõgeva.öö',
domain: 'example.com'
})
.expect(200);
expect(response.body.success).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should GET /users/:user', async () => {
const response = await server.get(`/users/${userId}`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.id).to.equal(userId);
expect(response.body.name).to.equal('test user');
});
2020-09-25 17:58:00 +08:00
it('should PUT /users/:user', async () => {
const response = await server
.put(`/users/${userId}`)
.send({
name: 'user test'
})
.expect(200);
expect(response.body.success).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should GET /users/:user (updated name)', async () => {
const response = await server.get(`/users/${userId}`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.id).to.equal(userId);
expect(response.body.name).to.equal('user test');
});
});
2020-09-25 17:58:00 +08:00
describe('authenticate', () => {
it('should POST /authenticate with success', async () => {
const response = await server
.post(`/authenticate`)
.send({
username: 'testuser@example.com',
password: 'secretpass',
scope: 'master'
})
.expect(200);
expect(response.body.success).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should POST /authenticate with failure', async () => {
const response = await server
.post(`/authenticate`)
.send({
username: 'testuser@example.com',
password: 'invalid',
scope: 'master'
})
.expect(403);
expect(response.body.error).to.exist;
expect(response.body.success).to.not.be.true;
});
2020-09-25 17:58:00 +08:00
it('should POST /authenticate using alias domain', async () => {
const response = await server
.post(`/authenticate`)
.send({
username: 'testuser@jõgeva.öö',
password: 'secretpass',
scope: 'master'
})
.expect(200);
expect(response.body.success).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should POST /authenticate with failure using alias domain', async () => {
const response = await server
.post(`/authenticate`)
.send({
username: 'testuser@jõgeva.öö',
password: 'invalid',
scope: 'master'
})
.expect(403);
expect(response.body.error).to.exist;
expect(response.body.success).to.not.be.true;
});
});
2020-09-25 17:58:00 +08:00
describe('asp', () => {
it('should POST /users/:user/asps to generate ASP', async () => {
const response = await server
.post(`/users/${userId}/asps`)
.send({
description: 'test',
scopes: ['imap', 'smtp'],
generateMobileconfig: true
})
.expect(200);
expect(response.body.error).to.not.exist;
expect(response.body.success).to.be.true;
expect(response.body.password).to.exist;
expect(response.body.mobileconfig).to.exist;
asp = response.body.password;
});
2020-09-25 17:58:00 +08:00
it('should POST /authenticate using ASP and allowed scope', async () => {
const response = await server
.post(`/authenticate`)
.send({
username: 'testuser@jõgeva.öö',
password: asp,
scope: 'imap'
})
.expect(200);
expect(response.body.success).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should POST /authenticate with failure using ASP and master scope', async () => {
const response = await server
.post(`/authenticate`)
.send({
username: 'testuser@jõgeva.öö',
password: asp,
scope: 'master'
})
.expect(403);
expect(response.body.error).to.exist;
expect(response.body.success).to.not.be.true;
});
});
2020-09-25 17:58:00 +08:00
describe('addresses', () => {
it('should GET /users/:user/addresses', async () => {
const response = await server.get(`/users/${userId}/addresses`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.results.length).to.equal(1);
expect(response.body.results[0].address).to.equal('testuser@example.com');
expect(response.body.results[0].main).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should POST users/:user/addresses', async () => {
const response1 = await server
.post(`/users/${userId}/addresses`)
.send({
address: 'alias1@example.com',
2020-09-25 21:49:38 +08:00
main: true,
metaData: {
tere: 123
}
2020-09-25 17:58:00 +08:00
})
.expect(200);
expect(response1.body.success).to.be.true;
const response2 = await server
.post(`/users/${userId}/addresses`)
.send({
address: 'alias2@example.com'
})
.expect(200);
expect(response2.body.success).to.be.true;
});
2020-09-25 17:58:00 +08:00
it('should GET /users/:user (after email update)', async () => {
const response = await server.get(`/users/${userId}`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.id).to.equal(userId);
expect(response.body.address).to.equal('alias1@example.com');
});
2020-09-25 17:58:00 +08:00
it('should GET /users/:user/addresses (updated listing)', async () => {
const response = await server.get(`/users/${userId}/addresses`).expect(200);
2020-09-25 21:49:38 +08:00
2020-09-25 17:58:00 +08:00
expect(response.body.success).to.be.true;
expect(response.body.results.length).to.equal(3);
2020-09-25 21:49:38 +08:00
2020-09-25 17:58:00 +08:00
response.body.results.sort((a, b) => a.id.localeCompare(b.id));
2020-09-25 17:58:00 +08:00
expect(response.body.results[0].address).to.equal('testuser@example.com');
expect(response.body.results[0].main).to.be.false;
2020-09-25 17:58:00 +08:00
expect(response.body.results[1].address).to.equal('alias1@example.com');
expect(response.body.results[1].main).to.be.true;
2020-09-25 21:49:38 +08:00
expect(response.body.results[1].metaData).to.not.exist;
2020-09-25 21:49:38 +08:00
// no metaData present
2020-09-25 17:58:00 +08:00
expect(response.body.results[2].address).to.equal('alias2@example.com');
expect(response.body.results[2].main).to.be.false;
2020-09-25 17:58:00 +08:00
address = response.body.results[2];
});
2020-09-25 17:58:00 +08:00
it('should DELETE users/:user/addresses/:address', async () => {
const response = await server.delete(`/users/${userId}/addresses/${address.id}`).expect(200);
expect(response.body.success).to.be.true;
});
2020-09-25 21:49:38 +08:00
it('should GET /users/:user/addresses (with metaData)', async () => {
const response = await server.get(`/users/${userId}/addresses?metaData=true`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.results.length).to.equal(2);
response.body.results.sort((a, b) => a.id.localeCompare(b.id));
expect(response.body.results[1].address).to.equal('alias1@example.com');
expect(response.body.results[1].main).to.be.true;
expect(response.body.results[1].metaData.tere).to.equal(123);
address = response.body.results[1];
});
it('should GET /users/:user/address/:address', async () => {
const response = await server.get(`/users/${userId}/addresses/${address.id}`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.metaData.tere).to.equal(123);
});
2020-09-25 17:58:00 +08:00
it('should GET /users/:user/addresses (after DELETE)', async () => {
const response = await server.get(`/users/${userId}/addresses`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.results.length).to.equal(2);
response.body.results.sort((a, b) => a.id.localeCompare(b.id));
2020-09-25 17:58:00 +08:00
expect(response.body.results[0].address).to.equal('testuser@example.com');
expect(response.body.results[0].main).to.be.false;
2020-09-25 17:58:00 +08:00
expect(response.body.results[1].address).to.equal('alias1@example.com');
expect(response.body.results[1].main).to.be.true;
});
2020-09-28 18:45:41 +08:00
describe('forwarded', () => {
let address = false;
it('should POST /addresses/forwarded', async () => {
const response = await server
.post(`/addresses/forwarded`)
.send({
address: 'my.new.address@example.com',
targets: ['my.old.address@example.com', 'smtp://mx2.zone.eu:25'],
forwards: 500,
metaData: {
tere: 123
},
tags: ['tere', 'vana']
})
.expect(200);
expect(response.body.success).to.be.true;
address = response.body.id;
});
it('should GET /addresses/forwarded/:address', async () => {
const response = await server.get(`/addresses/forwarded/${address}`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.metaData.tere).to.equal(123);
expect(response.body.tags).to.deep.equal(['tere', 'vana']);
});
it('should PUT /addresses/forwarded/:address', async () => {
const response = await server
.put(`/addresses/forwarded/${address}`)
.send({
metaData: {
tere: 124
}
})
.expect(200);
expect(response.body.success).to.be.true;
// check updated data
const updatedResponse = await server.get(`/addresses/forwarded/${address}`).expect(200);
expect(updatedResponse.body.success).to.be.true;
expect(updatedResponse.body.metaData.tere).to.equal(124);
});
it('should DELETE /addresses/forwarded/:address', async () => {
const response = await server.delete(`/addresses/forwarded/${address}`).expect(200);
expect(response.body.success).to.be.true;
});
});
});
2020-09-25 17:58:00 +08:00
describe('mailboxes', () => {
it('should GET /users/:user/mailboxes', async () => {
const response = await server.get(`/users/${userId}/mailboxes`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.results.length).to.gte(4);
2020-09-25 17:58:00 +08:00
inbox = response.body.results.find(result => result.path === 'INBOX');
expect(inbox).to.exist;
});
});
2020-07-31 18:36:07 +08:00
2020-09-25 17:58:00 +08:00
describe('autoreply', () => {
it('should PUT /users/:user/autoreply', async () => {
let r;
2020-07-31 18:36:07 +08:00
2020-09-25 17:58:00 +08:00
r = await server.get(`/users/${userId}/autoreply`).expect(200);
expect(r.body).to.deep.equal({
success: true,
status: false,
name: '',
subject: '',
text: '',
html: '',
start: false,
end: false
});
r = await server
.put(`/users/${userId}/autoreply`)
.send({
status: true,
name: 'AR name',
subject: 'AR subject',
text: 'Away from office until Dec.19',
start: '2017-11-15T00:00:00.000Z',
end: '2017-12-19T00:00:00.000Z'
})
.expect(200);
expect(r.body.success).to.be.true;
r = await server.get(`/users/${userId}/autoreply`).expect(200);
expect(r.body).to.deep.equal({
success: true,
2020-07-31 18:36:07 +08:00
status: true,
name: 'AR name',
subject: 'AR subject',
text: 'Away from office until Dec.19',
2020-09-25 17:58:00 +08:00
html: '',
2020-07-31 18:36:07 +08:00
start: '2017-11-15T00:00:00.000Z',
end: '2017-12-19T00:00:00.000Z'
2020-09-25 17:58:00 +08:00
});
r = await server
.put(`/users/${userId}/autoreply`)
.send({
name: 'AR name v2',
subject: '',
start: false
})
.expect(200);
expect(r.body.success).to.be.true;
r = await server.get(`/users/${userId}/autoreply`).expect(200);
expect(r.body).to.deep.equal({
success: true,
status: true,
2020-07-31 18:36:07 +08:00
name: 'AR name v2',
subject: '',
2020-09-25 17:58:00 +08:00
text: 'Away from office until Dec.19',
html: '',
start: false,
end: '2017-12-19T00:00:00.000Z'
});
await server.delete(`/users/${userId}/autoreply`).expect(200);
r = await server.get(`/users/${userId}/autoreply`).expect(200);
expect(r.body).to.deep.equal({
success: true,
status: false,
name: '',
subject: '',
text: '',
html: '',
start: false,
end: false
});
});
});
describe('domainaccess', () => {
let tag = 'account:123';
let domain;
it('should POST domainaccess/:tag/block', async () => {
const response1 = await server
.post(`/domainaccess/${tag}/block`)
.send({
domain: 'example.com'
})
.expect(200);
expect(response1.body.success).to.be.true;
const response2 = await server
.post(`/domainaccess/${tag}/block`)
.send({
domain: 'jõgeva.ee'
})
.expect(200);
expect(response2.body.success).to.be.true;
});
it('should GET /domainaccess/:tag/block', async () => {
const response = await server.get(`/domainaccess/${tag}/block`).expect(200);
expect(response.body.success).to.be.true;
expect(response.body.results.length).to.equal(2);
expect(response.body.results[0].domain).to.equal('example.com');
expect(response.body.results[1].domain).to.equal('jõgeva.ee');
domain = response.body.results[1];
2020-07-31 18:36:07 +08:00
});
2020-09-25 17:58:00 +08:00
it('should DELETE domainaccess/:domain', async () => {
const response = await server.delete(`/domainaccess/${domain.id}`).expect(200);
expect(response.body.success).to.be.true;
2020-07-31 18:36:07 +08:00
});
});
2017-12-13 16:33:13 +08:00
});