2020-07-27 01:28:12 +00:00
|
|
|
import { promisify } from 'util';
|
2020-07-27 04:37:13 +00:00
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
import test from 'ava';
|
|
|
|
import { simpleParser, ParsedMail } from 'mailparser';
|
2020-05-27 12:49:21 +00:00
|
|
|
import { SMTPServer } from 'smtp-server';
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-10-30 20:47:53 +00:00
|
|
|
import {
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
SMTPClient,
|
|
|
|
Message,
|
|
|
|
MessageHeaders,
|
|
|
|
isRFC2822Date,
|
|
|
|
} from '../email';
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
const parseMap = new Map<string, ParsedMail>();
|
2020-07-27 04:52:06 +00:00
|
|
|
const port = 3000;
|
2020-07-27 04:37:13 +00:00
|
|
|
let greylistPort = 4000;
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-07-27 04:52:06 +00:00
|
|
|
const client = new SMTPClient({
|
|
|
|
port,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
});
|
|
|
|
const server = new SMTPServer({
|
|
|
|
secure: true,
|
|
|
|
onAuth(auth, _session, callback) {
|
|
|
|
if (auth.username === 'pooh' && auth.password === 'honey') {
|
|
|
|
callback(null, { user: 'pooh' });
|
|
|
|
} else {
|
|
|
|
return callback(new Error('invalid user / pass'));
|
|
|
|
}
|
|
|
|
},
|
2020-07-27 08:58:28 +00:00
|
|
|
async onData(stream, _session, callback: () => void) {
|
2020-07-27 04:52:06 +00:00
|
|
|
const mail = await simpleParser(stream, {
|
|
|
|
skipHtmlToText: true,
|
|
|
|
skipTextToHtml: true,
|
|
|
|
skipImageLinks: true,
|
|
|
|
} as Record<string, unknown>);
|
2020-07-27 08:58:28 +00:00
|
|
|
|
|
|
|
parseMap.set(mail.subject as string, mail);
|
2020-07-27 04:52:06 +00:00
|
|
|
callback();
|
2020-07-27 08:58:28 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
async function send(headers: Partial<MessageHeaders>) {
|
2020-07-27 09:10:27 +00:00
|
|
|
return new Promise<ParsedMail>((resolve, reject) => {
|
|
|
|
client.send(new Message(headers), (err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
2020-11-30 04:53:13 +00:00
|
|
|
resolve(parseMap.get(headers.subject as string) as ParsedMail);
|
2020-07-27 09:10:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-07-27 04:37:13 +00:00
|
|
|
}
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-07-27 06:38:36 +00:00
|
|
|
test.before(async (t) => {
|
|
|
|
server.listen(port, t.pass);
|
|
|
|
});
|
2020-07-27 09:10:34 +00:00
|
|
|
test.after(async (t) => {
|
|
|
|
server.close(t.pass);
|
|
|
|
});
|
2020-07-27 06:38:36 +00:00
|
|
|
|
2020-07-27 01:28:12 +00:00
|
|
|
test('client invokes callback exactly once for invalid connection', async (t) => {
|
2020-04-21 03:20:42 +00:00
|
|
|
const msg = {
|
2020-05-27 10:49:11 +00:00
|
|
|
from: 'foo@bar.baz',
|
|
|
|
to: 'foo@bar.baz',
|
|
|
|
subject: 'hello world',
|
|
|
|
text: 'hello world',
|
2020-04-21 03:20:42 +00:00
|
|
|
};
|
2020-10-30 21:34:09 +00:00
|
|
|
await t.notThrowsAsync(
|
2020-11-30 04:53:13 +00:00
|
|
|
new Promise<void>((resolve, reject) => {
|
2020-10-30 21:34:09 +00:00
|
|
|
let counter = 0;
|
|
|
|
const invalidClient = new SMTPClient({ host: 'bar.baz' });
|
|
|
|
const incrementListener = () => {
|
|
|
|
if (counter > 0) {
|
|
|
|
reject();
|
|
|
|
} else {
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
invalidClient.smtp.addListener('incrementTestCounter', incrementListener);
|
|
|
|
invalidClient.send(new Message(msg), (err) => {
|
|
|
|
if (err == null || counter > 0) {
|
|
|
|
reject();
|
|
|
|
} else {
|
|
|
|
invalidClient.smtp.emit('incrementTestCounter');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// @ts-expect-error the error event is only accessible from the protected socket property
|
|
|
|
invalidClient.smtp.sock.once('error', () => {
|
|
|
|
invalidClient.smtp.removeListener(
|
|
|
|
'incrementTestCounter',
|
|
|
|
incrementListener
|
|
|
|
);
|
|
|
|
if (counter === 1) {
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2020-05-27 10:49:11 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 06:38:36 +00:00
|
|
|
test('client has a default connection timeout', async (t) => {
|
2020-05-27 10:49:11 +00:00
|
|
|
const connectionOptions = {
|
|
|
|
user: 'username',
|
|
|
|
password: 'password',
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: 1234,
|
|
|
|
timeout: undefined as number | null | undefined,
|
|
|
|
};
|
2020-05-27 18:15:55 +00:00
|
|
|
t.is(new SMTPClient(connectionOptions).smtp.timeout, DEFAULT_TIMEOUT);
|
2020-05-27 10:49:11 +00:00
|
|
|
|
|
|
|
connectionOptions.timeout = null;
|
2020-05-27 18:15:55 +00:00
|
|
|
t.is(new SMTPClient(connectionOptions).smtp.timeout, DEFAULT_TIMEOUT);
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-05-27 10:49:11 +00:00
|
|
|
connectionOptions.timeout = undefined;
|
2020-05-27 18:15:55 +00:00
|
|
|
t.is(new SMTPClient(connectionOptions).smtp.timeout, DEFAULT_TIMEOUT);
|
2020-04-21 03:20:42 +00:00
|
|
|
});
|
2020-05-26 15:18:31 +00:00
|
|
|
|
2020-07-27 06:38:36 +00:00
|
|
|
test('client deduplicates recipients', async (t) => {
|
2020-05-27 06:25:31 +00:00
|
|
|
const msg = {
|
|
|
|
from: 'zelda@gmail.com',
|
|
|
|
to: 'gannon@gmail.com',
|
|
|
|
cc: 'gannon@gmail.com',
|
|
|
|
bcc: 'gannon@gmail.com',
|
|
|
|
};
|
2020-07-27 05:07:04 +00:00
|
|
|
const stack = client.createMessageStack(new Message(msg));
|
2020-05-27 06:25:31 +00:00
|
|
|
t.true(stack.to.length === 1);
|
|
|
|
t.is(stack.to[0].address, 'gannon@gmail.com');
|
|
|
|
});
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
test('client accepts array recipients', async (t) => {
|
2020-06-09 01:37:19 +00:00
|
|
|
const msg = new Message({
|
|
|
|
from: 'zelda@gmail.com',
|
|
|
|
to: ['gannon1@gmail.com'],
|
|
|
|
cc: ['gannon2@gmail.com'],
|
|
|
|
bcc: ['gannon3@gmail.com'],
|
|
|
|
});
|
|
|
|
|
|
|
|
msg.header.to = [msg.header.to as string];
|
|
|
|
msg.header.cc = [msg.header.cc as string];
|
|
|
|
msg.header.bcc = [msg.header.bcc as string];
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
const isValid = await new Promise((r) => msg.valid(r));
|
|
|
|
const stack = client.createMessageStack(msg);
|
|
|
|
|
|
|
|
t.true(isValid);
|
|
|
|
t.is(stack.to.length, 3);
|
|
|
|
t.deepEqual(
|
|
|
|
stack.to.map((x) => x.address),
|
|
|
|
['gannon1@gmail.com', 'gannon2@gmail.com', 'gannon3@gmail.com']
|
|
|
|
);
|
2020-06-09 01:37:19 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
test('client accepts array sender', async (t) => {
|
2020-06-09 01:37:19 +00:00
|
|
|
const msg = new Message({
|
|
|
|
from: ['zelda@gmail.com'],
|
|
|
|
to: ['gannon1@gmail.com'],
|
|
|
|
});
|
|
|
|
msg.header.from = [msg.header.from as string];
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
const isValid = await new Promise((r) => msg.valid(r));
|
|
|
|
t.true(isValid);
|
2020-06-09 01:37:19 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 01:28:12 +00:00
|
|
|
test('client rejects message without `from` header', async (t) => {
|
2020-07-27 08:58:28 +00:00
|
|
|
const { message: error } = await t.throwsAsync(
|
2020-07-27 09:04:14 +00:00
|
|
|
send({
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
})
|
2020-07-27 08:58:28 +00:00
|
|
|
);
|
2020-07-27 06:38:36 +00:00
|
|
|
t.is(error, 'Message must have a `from` header');
|
2020-05-27 06:03:59 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 05:07:04 +00:00
|
|
|
test('client rejects message without `to`, `cc`, or `bcc` header', async (t) => {
|
2020-07-27 08:58:28 +00:00
|
|
|
const { message: error } = await t.throwsAsync(
|
2020-07-27 09:04:14 +00:00
|
|
|
send({
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
})
|
2020-07-27 08:58:28 +00:00
|
|
|
);
|
2020-07-27 06:38:36 +00:00
|
|
|
t.is(error, 'Message must have at least one `to`, `cc`, or `bcc` header');
|
2020-05-27 05:53:45 +00:00
|
|
|
});
|
2020-05-27 05:47:24 +00:00
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
test('client allows message with only `cc` recipient header', async (t) => {
|
2020-05-27 05:47:24 +00:00
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
cc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
const mail = await send(msg);
|
|
|
|
t.is(mail.text, msg.text + '\n\n\n');
|
|
|
|
t.is(mail.subject, msg.subject);
|
|
|
|
t.is(mail.from?.text, msg.from);
|
|
|
|
t.is(mail.cc?.text, msg.cc);
|
2020-05-27 05:47:24 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
test('client allows message with only `bcc` recipient header', async (t) => {
|
2020-05-27 05:47:24 +00:00
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
const mail = await send(msg);
|
|
|
|
t.is(mail.text, msg.text + '\n\n\n');
|
|
|
|
t.is(mail.subject, msg.subject);
|
|
|
|
t.is(mail.from?.text, msg.from);
|
|
|
|
t.is(mail.bcc, undefined);
|
2020-05-27 05:47:24 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 06:38:36 +00:00
|
|
|
test('client constructor throws if `password` supplied without `user`', async (t) => {
|
2020-05-27 18:15:55 +00:00
|
|
|
t.notThrows(() => new SMTPClient({ user: 'anything', password: 'anything' }));
|
|
|
|
t.throws(() => new SMTPClient({ password: 'anything' }));
|
2020-05-26 15:18:31 +00:00
|
|
|
t.throws(
|
|
|
|
() =>
|
2020-05-27 18:15:55 +00:00
|
|
|
new SMTPClient({ username: 'anything', password: 'anything' } as Record<
|
2020-05-26 15:18:31 +00:00
|
|
|
string,
|
|
|
|
unknown
|
|
|
|
>)
|
|
|
|
);
|
|
|
|
});
|
2020-07-26 01:44:33 +00:00
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
test('client supports greylisting', async (t) => {
|
2020-07-27 01:04:23 +00:00
|
|
|
t.plan(3);
|
2020-07-26 01:44:33 +00:00
|
|
|
|
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
2020-07-27 01:04:23 +00:00
|
|
|
const greylistServer = new SMTPServer({
|
|
|
|
secure: true,
|
|
|
|
onRcptTo(_address, _session, callback) {
|
|
|
|
t.pass();
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
onAuth(auth, _session, callback) {
|
|
|
|
if (auth.username === 'pooh' && auth.password === 'honey') {
|
|
|
|
callback(null, { user: 'pooh' });
|
|
|
|
} else {
|
|
|
|
return callback(new Error('invalid user / pass'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { onRcptTo } = greylistServer;
|
|
|
|
greylistServer.onRcptTo = (_address, _session, callback) => {
|
|
|
|
greylistServer.onRcptTo = (a, s, cb) => {
|
2020-07-26 01:44:33 +00:00
|
|
|
t.pass();
|
2020-07-27 01:04:23 +00:00
|
|
|
const err = new Error('greylist');
|
|
|
|
((err as never) as { responseCode: number }).responseCode = 450;
|
|
|
|
greylistServer.onRcptTo = onRcptTo;
|
2020-07-26 01:44:33 +00:00
|
|
|
onRcptTo(a, s, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
const err = new Error('greylist');
|
|
|
|
((err as never) as { responseCode: number }).responseCode = 450;
|
|
|
|
callback(err);
|
|
|
|
};
|
|
|
|
|
2020-07-27 01:04:23 +00:00
|
|
|
const p = greylistPort++;
|
2020-07-27 08:58:28 +00:00
|
|
|
await t.notThrowsAsync(
|
2020-11-30 04:53:13 +00:00
|
|
|
new Promise<void>((resolve, reject) => {
|
2020-07-27 08:58:28 +00:00
|
|
|
greylistServer.listen(p, () => {
|
|
|
|
new SMTPClient({
|
|
|
|
port: p,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
}).send(new Message(msg), (err) => {
|
|
|
|
greylistServer.close();
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
2020-07-26 01:44:33 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 08:58:28 +00:00
|
|
|
test('client only responds once to greylisting', async (t) => {
|
|
|
|
t.plan(4);
|
2020-07-26 01:44:33 +00:00
|
|
|
|
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
|
|
|
const greylistServer = new SMTPServer({
|
|
|
|
secure: true,
|
|
|
|
onRcptTo(_address, _session, callback) {
|
|
|
|
t.pass();
|
|
|
|
const err = new Error('greylist');
|
|
|
|
((err as never) as { responseCode: number }).responseCode = 450;
|
|
|
|
callback(err);
|
|
|
|
},
|
|
|
|
onAuth(auth, _session, callback) {
|
|
|
|
if (auth.username === 'pooh' && auth.password === 'honey') {
|
|
|
|
callback(null, { user: 'pooh' });
|
|
|
|
} else {
|
|
|
|
return callback(new Error('invalid user / pass'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-07-27 01:04:23 +00:00
|
|
|
const p = greylistPort++;
|
2020-07-27 08:58:28 +00:00
|
|
|
const { message: error } = await t.throwsAsync(
|
2020-11-30 04:53:13 +00:00
|
|
|
new Promise<void>((resolve, reject) => {
|
2020-07-27 08:58:28 +00:00
|
|
|
greylistServer.listen(p, () => {
|
|
|
|
new SMTPClient({
|
|
|
|
port: p,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
}).send(new Message(msg), (err) => {
|
|
|
|
greylistServer.close();
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
t.is(error, "bad response on command 'RCPT': greylist");
|
2020-07-26 01:44:33 +00:00
|
|
|
});
|
2020-10-30 20:47:53 +00:00
|
|
|
|
|
|
|
test('client send can have result awaited when promisified', async (t) => {
|
|
|
|
// bind necessary to retain internal access to client prototype
|
|
|
|
const sendAsync = promisify(client.send.bind(client));
|
|
|
|
|
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const message = await sendAsync(new Message(msg));
|
|
|
|
t.true(message instanceof Message);
|
|
|
|
t.like(message, {
|
|
|
|
alternative: null,
|
|
|
|
content: 'text/plain; charset=utf-8',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
header: {
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
subject: '=?UTF-8?Q?this_is_a_test_TEXT_message_from_emailjs?=',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
t.deepEqual(message.attachments, []);
|
|
|
|
t.true(isRFC2822Date(message.header.date as string));
|
|
|
|
t.regex(message.header['message-id'] as string, /^<.*[@]{1}.*>$/);
|
|
|
|
} catch (err) {
|
|
|
|
t.fail(err);
|
|
|
|
}
|
|
|
|
});
|
2020-10-30 21:37:59 +00:00
|
|
|
|
|
|
|
test('client sendAsync can have result awaited', async (t) => {
|
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const message = await client.sendAsync(new Message(msg));
|
|
|
|
t.true(message instanceof Message);
|
|
|
|
t.like(message, {
|
|
|
|
alternative: null,
|
|
|
|
content: 'text/plain; charset=utf-8',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
header: {
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
subject: '=?UTF-8?Q?this_is_a_test_TEXT_message_from_emailjs?=',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
t.deepEqual(message.attachments, []);
|
|
|
|
t.true(isRFC2822Date(message.header.date as string));
|
|
|
|
t.regex(message.header['message-id'] as string, /^<.*[@]{1}.*>$/);
|
|
|
|
} catch (err) {
|
|
|
|
t.fail(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('client sendAsync can have error caught when awaited', async (t) => {
|
|
|
|
const msg = {
|
|
|
|
subject: 'this is a test TEXT message from emailjs',
|
|
|
|
from: 'piglet@gmail.com',
|
|
|
|
bcc: 'pooh@gmail.com',
|
|
|
|
text: "It is hard to be brave when you're only a Very Small Animal.",
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const invalidClient = new SMTPClient({ host: 'bar.baz' });
|
|
|
|
const message = await invalidClient.sendAsync(new Message(msg));
|
|
|
|
t.true(message instanceof Message);
|
|
|
|
t.fail();
|
|
|
|
} catch (err) {
|
|
|
|
t.true(err instanceof Error);
|
|
|
|
t.pass();
|
|
|
|
}
|
|
|
|
});
|