Skip to content

Commit

Permalink
added additional API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Dec 23, 2024
1 parent 1a9f91a commit 58fb425
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.
14 changes: 13 additions & 1 deletion config/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
# JSON formatted settings
settings = '''
{
"webhooksEnabled": false,
"webhooksEnabled": true,
"webhooks": "http://127.0.0.1:7078/webhooks",
"webhookEvents": ["*"],
"webhooksCustomHeaders": [
{
"key": "Custom-Header",
"value": "Custom-Value"
}
],
"serviceUrl": "http://127.0.0.1:7077",
"ignoreMailCertErrors": true,
"serviceSecret": "a cat"
Expand Down Expand Up @@ -31,3 +39,7 @@ redis = "redis://127.0.0.1:6379/13"
[api]
host = "0.0.0.0"
port = 7077


[webhooksServer]
port = 7078
87 changes: 86 additions & 1 deletion test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const assert = require('node:assert').strict;
const nodemailer = require('nodemailer');
const Redis = require('ioredis');
const redis = new Redis(config.dbs.redis);
const webhooksServer = require('./webhooks-server');

const accessToken = '2aa97ad0456d6624a55d30780aa2ff61bfb7edc6fa00935b40814b271e718660';

Expand All @@ -18,10 +19,12 @@ const defaultAccountId = 'main-account';
test('API tests', async t => {
t.before(async () => {
testAccount = await nodemailer.createTestAccount();
await webhooksServer.init();
});

t.after(() => {
t.after(async () => {
redis.quit();
await webhooksServer.quit();
});

await t.test('list existing users (empty list)', async () => {
Expand Down Expand Up @@ -91,7 +94,9 @@ test('API tests', async t => {
.expect(200);

assert.strictEqual(response.body.state, 'new');
});

await t.test('wait until added account is available', async () => {
// wait until connected
let available = false;
while (!available) {
Expand All @@ -106,6 +111,12 @@ test('API tests', async t => {
break;
}
}

// check if we have all expected webhooks
let webhooks = webhooksServer.webhooks.get(defaultAccountId);
for (let event of ['accountAdded', 'authenticationSuccess', 'accountInitialized']) {
assert.ok(webhooks.some(wh => wh.event === event));
}
});

await t.test('list existing users (1 account)', async () => {
Expand Down Expand Up @@ -134,4 +145,78 @@ test('API tests', async t => {

assert.strictEqual(response.body.total, 0);
});

await t.test('upload email to Inbox and wait for a messageNew webhook', async () => {
const response1 = await server
.post(`/v1/account/${defaultAccountId}/message`)
.send({
path: 'Inbox',
flags: ['\\Seen'],
from: {
name: 'Test Sender',
address: '[email protected]'
},
to: [
{
name: 'Test Received',
address: '[email protected]'
}
],
subject: 'Test message 🤣',
text: 'Hello world! 🙃',
messageId: '<[email protected]>'
})
.expect(200);
assert.ok(response1.body.id);

const response2 = await server
.post(`/v1/account/${defaultAccountId}/message`)
.send({
path: 'Inbox',
flags: [],
from: {
name: 'Test Sender',
address: '[email protected]'
},
to: [
{
name: 'Test Received',
address: '[email protected]'
}
],
subject: 'Test message 🤣',
text: 'Hello world! 🙃',
messageId: '<[email protected]>'
})
.expect(200);

assert.ok(response2.body.id);

let received = false;
let messageNewWebhook1 = false;
let messageNewWebhook2 = false;
while (!received) {
await new Promise(r => setTimeout(r, 1000));
let webhooks = webhooksServer.webhooks.get(defaultAccountId);
messageNewWebhook1 = webhooks.find(wh => wh.path === 'INBOX' && wh.event === 'messageNew' && wh.data.messageId === '<[email protected]>');
messageNewWebhook2 = webhooks.find(wh => wh.path === 'INBOX' && wh.event === 'messageNew' && wh.data.messageId === '<[email protected]>');
if (messageNewWebhook1 && messageNewWebhook2) {
received = true;
}
}

assert.equal(messageNewWebhook1.data.subject, 'Test message 🤣');
});

await t.test('list inbox messages (2 messages)', async () => {
const response = await server.get(`/v1/account/${defaultAccountId}/messages?path=INBOX`).expect(200);

assert.strictEqual(response.body.total, 2);
assert.equal(response.body.messages[0].messageId, '<[email protected]>');
});

await t.test('list mailboxes with counters', async () => {
const response = await server.get(`/v1/account/${defaultAccountId}/mailboxes?counters=true`).expect(200);
assert.ok(response.body.mailboxes.some(mb => mb.specialUse === '\\Inbox' && mb.status.messages === 2 && mb.status.unseen === 1));
});
});
39 changes: 39 additions & 0 deletions test/webhooks-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const config = require('wild-config');
const Hapi = require('@hapi/hapi');

const webhooks = new Map();

const server = Hapi.server({
port: config.webhooksServer.port,
host: '0.0.0.0'
});

server.route({
method: 'POST',
path: '/webhooks',
handler: async (request, h) => {
let account = request.payload.account || '';
if (!webhooks.has(account)) {
webhooks.set(account, []);
}
console.log('WEBHOOK', JSON.stringify(request.payload));
webhooks.get(account).push(request.payload);
return h.response('OK').code(200);
}
});

const init = async () => {
await server.start();
console.log('Webhooks Server running on %s', server.info.uri);
};

module.exports = {
init,
webhooks,
async quit() {
await server.stop({ timeout: 5 * 1000 });
console.log('Webhooks Server closed');
}
};

0 comments on commit 58fb425

Please sign in to comment.