2020-07-27 01:28:12 +00:00
|
|
|
import { promisify } from 'util';
|
2020-07-27 04:37:13 +00:00
|
|
|
|
2020-07-27 01:04:23 +00:00
|
|
|
import test, { CbExecutionContext } from 'ava';
|
2020-05-27 12:49:21 +00:00
|
|
|
import { simpleParser } from 'mailparser';
|
|
|
|
import { SMTPServer } from 'smtp-server';
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-05-27 18:15:55 +00:00
|
|
|
import { DEFAULT_TIMEOUT, SMTPClient, Message } from '../email';
|
2020-04-21 03:20:42 +00:00
|
|
|
|
|
|
|
type UnPromisify<T> = T extends Promise<infer U> ? U : T;
|
|
|
|
|
2020-07-27 04:37:13 +00:00
|
|
|
let port = 3000;
|
|
|
|
let greylistPort = 4000;
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-07-27 04:37:13 +00:00
|
|
|
function send(
|
2020-07-27 01:04:23 +00:00
|
|
|
t: CbExecutionContext,
|
2020-05-27 12:34:05 +00:00
|
|
|
message: Message,
|
2020-07-27 01:04:23 +00:00
|
|
|
verify: (mail: UnPromisify<ReturnType<typeof simpleParser>>) => void
|
2020-07-27 04:37:13 +00:00
|
|
|
) {
|
2020-07-27 01:04:23 +00:00
|
|
|
const server = new SMTPServer({
|
|
|
|
secure: true,
|
|
|
|
onAuth(auth, _session, callback) {
|
2020-05-27 15:59:53 +00:00
|
|
|
if (auth.username === 'pooh' && auth.password === 'honey') {
|
2020-04-21 03:20:42 +00:00
|
|
|
callback(null, { user: 'pooh' });
|
|
|
|
} else {
|
|
|
|
return callback(new Error('invalid user / pass'));
|
|
|
|
}
|
2020-07-27 01:04:23 +00:00
|
|
|
},
|
2020-07-27 04:37:13 +00:00
|
|
|
async onData(stream, _session, callback: () => void) {
|
|
|
|
const mail = await simpleParser(stream, {
|
2020-07-27 01:04:23 +00:00
|
|
|
skipHtmlToText: true,
|
|
|
|
skipTextToHtml: true,
|
|
|
|
skipImageLinks: true,
|
2020-07-27 04:37:13 +00:00
|
|
|
} as Record<string, unknown>);
|
|
|
|
verify(mail);
|
|
|
|
callback();
|
2020-07-27 01:04:23 +00:00
|
|
|
},
|
2020-04-21 03:20:42 +00:00
|
|
|
});
|
|
|
|
|
2020-07-27 01:04:23 +00:00
|
|
|
const p = port++;
|
|
|
|
server.listen(p, () => {
|
|
|
|
new SMTPClient({
|
|
|
|
port: p,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
}).send(message, (err) => {
|
|
|
|
server.close();
|
|
|
|
t.end(err);
|
|
|
|
});
|
|
|
|
});
|
2020-07-27 04:37:13 +00:00
|
|
|
}
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-07-27 01:28:12 +00:00
|
|
|
test('client invokes callback exactly once for invalid connection', async (t) => {
|
2020-05-27 10:49:11 +00:00
|
|
|
t.plan(1);
|
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-07-27 01:28:12 +00:00
|
|
|
const client = new SMTPClient({ host: 'bar.baz' });
|
|
|
|
const sendAsync = promisify(client.send.bind(client));
|
|
|
|
try {
|
|
|
|
await sendAsync(new Message(msg));
|
|
|
|
} catch (err) {
|
2020-07-27 01:04:23 +00:00
|
|
|
t.true(err instanceof Error);
|
2020-07-27 01:28:12 +00:00
|
|
|
}
|
2020-05-27 10:49:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('client has a default connection timeout', (t) => {
|
|
|
|
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-05-27 06:25:31 +00:00
|
|
|
test('client deduplicates recipients', (t) => {
|
|
|
|
const msg = {
|
|
|
|
from: 'zelda@gmail.com',
|
|
|
|
to: 'gannon@gmail.com',
|
|
|
|
cc: 'gannon@gmail.com',
|
|
|
|
bcc: 'gannon@gmail.com',
|
|
|
|
};
|
2020-07-27 01:04:23 +00:00
|
|
|
const stack = new SMTPClient({}).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-06-09 01:37:19 +00:00
|
|
|
test.cb('client accepts array recipients', (t) => {
|
|
|
|
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];
|
|
|
|
|
|
|
|
msg.valid((isValid) => {
|
|
|
|
t.true(isValid);
|
2020-07-27 01:04:23 +00:00
|
|
|
const stack = new SMTPClient({}).createMessageStack(msg);
|
2020-06-09 01:37:19 +00:00
|
|
|
t.is(stack.to.length, 3);
|
|
|
|
t.deepEqual(
|
|
|
|
stack.to.map((x) => x.address),
|
|
|
|
['gannon1@gmail.com', 'gannon2@gmail.com', 'gannon3@gmail.com']
|
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test.cb('client accepts array sender', (t) => {
|
|
|
|
const msg = new Message({
|
|
|
|
from: ['zelda@gmail.com'],
|
|
|
|
to: ['gannon1@gmail.com'],
|
|
|
|
});
|
|
|
|
|
|
|
|
msg.header.from = [msg.header.from as string];
|
|
|
|
|
|
|
|
msg.valid((isValid) => {
|
|
|
|
t.true(isValid);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-27 01:28:12 +00:00
|
|
|
test('client rejects message without `from` header', async (t) => {
|
|
|
|
t.plan(2);
|
2020-05-27 06:03:59 +00:00
|
|
|
const msg = {
|
|
|
|
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 01:28:12 +00:00
|
|
|
const client = new SMTPClient({});
|
|
|
|
const sendAsync = promisify(client.send.bind(client));
|
|
|
|
try {
|
|
|
|
await sendAsync(new Message(msg));
|
|
|
|
} catch (err) {
|
2020-05-27 06:03:59 +00:00
|
|
|
t.true(err instanceof Error);
|
|
|
|
t.is(err?.message, 'Message must have a `from` header');
|
2020-07-27 01:28:12 +00:00
|
|
|
}
|
2020-05-27 06:03:59 +00:00
|
|
|
});
|
|
|
|
|
2020-05-27 05:53:45 +00:00
|
|
|
test.cb('client rejects message without `to`, `cc`, or `bcc` header', (t) => {
|
|
|
|
const msg = {
|
|
|
|
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 01:04:23 +00:00
|
|
|
new SMTPClient({}).send(new Message(msg), (err) => {
|
2020-05-27 05:53:45 +00:00
|
|
|
t.true(err instanceof Error);
|
|
|
|
t.is(
|
|
|
|
err?.message,
|
|
|
|
'Message must have at least one `to`, `cc`, or `bcc` header'
|
|
|
|
);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
2020-05-27 05:47:24 +00:00
|
|
|
|
2020-05-27 05:53:45 +00:00
|
|
|
test.cb('client allows message with only `cc` recipient header', (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 01:04:23 +00:00
|
|
|
send(t, new Message(msg), (mail) => {
|
|
|
|
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-05-27 05:53:45 +00:00
|
|
|
test.cb('client allows message with only `bcc` recipient header', (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 01:04:23 +00:00
|
|
|
send(t, new Message(msg), (mail) => {
|
|
|
|
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-05-27 05:53:45 +00:00
|
|
|
test('client constructor throws if `password` supplied without `user`', (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
|
|
|
|
|
|
|
test.cb('client supports greylisting', (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++;
|
|
|
|
greylistServer.listen(p, () => {
|
|
|
|
new SMTPClient({
|
|
|
|
port: p,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
}).send(new Message(msg), (err) => {
|
|
|
|
greylistServer.close();
|
|
|
|
if (err) {
|
|
|
|
t.fail();
|
|
|
|
}
|
|
|
|
t.pass();
|
|
|
|
t.end();
|
|
|
|
});
|
2020-07-26 01:44:33 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test.cb('client only responds once to greylisting', (t) => {
|
|
|
|
t.plan(3);
|
|
|
|
|
|
|
|
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++;
|
|
|
|
greylistServer.listen(p, () => {
|
|
|
|
new SMTPClient({
|
|
|
|
port: p,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
}).send(new Message(msg), (err) => {
|
|
|
|
greylistServer.close();
|
2020-07-26 01:44:33 +00:00
|
|
|
if (err) {
|
|
|
|
t.pass();
|
|
|
|
t.end();
|
|
|
|
} else {
|
|
|
|
t.fail();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|