-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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 () => { | ||
|
@@ -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) { | ||
|
@@ -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 () => { | ||
|
@@ -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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |