2020-04-21 03:20:42 +00:00
|
|
|
import test from 'ava';
|
2020-04-25 03:03:31 +00:00
|
|
|
import mailparser from 'mailparser';
|
|
|
|
import smtp from 'smtp-server';
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-05-27 10:49:11 +00:00
|
|
|
import { client as c, message as m, smtp as s } from '../email';
|
2020-04-21 03:20:42 +00:00
|
|
|
|
|
|
|
type UnPromisify<T> = T extends Promise<infer U> ? U : T;
|
|
|
|
|
|
|
|
const port = 2526;
|
2020-04-23 04:26:49 +00:00
|
|
|
const client = new c.Client({
|
|
|
|
port,
|
|
|
|
user: 'pooh',
|
|
|
|
password: 'honey',
|
|
|
|
ssl: true,
|
|
|
|
});
|
2020-04-25 03:03:31 +00:00
|
|
|
const server = new smtp.SMTPServer({ secure: true, authMethods: ['LOGIN'] });
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-04-23 04:26:49 +00:00
|
|
|
const send = (
|
|
|
|
message: m.Message,
|
2020-04-25 03:03:31 +00:00
|
|
|
verify: (
|
|
|
|
mail: UnPromisify<ReturnType<typeof mailparser.simpleParser>>
|
2020-05-01 16:25:32 +00:00
|
|
|
) => void,
|
|
|
|
done: () => void
|
2020-04-23 04:26:49 +00:00
|
|
|
) => {
|
2020-05-27 10:49:11 +00:00
|
|
|
server.onData = (stream, _session, callback: () => void) => {
|
2020-05-27 10:28:58 +00:00
|
|
|
mailparser.simpleParser(stream).then(verify).finally(done);
|
2020-04-21 03:20:42 +00:00
|
|
|
stream.on('end', callback);
|
|
|
|
};
|
2020-04-23 04:26:49 +00:00
|
|
|
client.send(message, (err) => {
|
2020-04-21 03:20:42 +00:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-05-01 16:25:32 +00:00
|
|
|
test.before.cb((t) => {
|
2020-04-25 03:03:31 +00:00
|
|
|
server.listen(port, function () {
|
|
|
|
server.onAuth = function (auth, _session, callback) {
|
2020-04-21 03:20:42 +00:00
|
|
|
if (auth.username == 'pooh' && auth.password == 'honey') {
|
|
|
|
callback(null, { user: 'pooh' });
|
|
|
|
} else {
|
|
|
|
return callback(new Error('invalid user / pass'));
|
|
|
|
}
|
|
|
|
};
|
2020-04-25 20:17:35 +00:00
|
|
|
t.end();
|
2020-04-21 03:20:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-01 16:25:32 +00:00
|
|
|
test.after.cb((t) => server.close(t.end));
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-05-27 10:49:11 +00:00
|
|
|
test.cb('client invokes callback exactly once for invalid connection', (t) => {
|
|
|
|
t.plan(1);
|
|
|
|
const client = new c.Client({ host: 'bar.baz' });
|
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-05-27 10:49:11 +00:00
|
|
|
client.send(new m.Message(msg), (err) => {
|
|
|
|
t.not(err, null);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
t.is(new c.Client(connectionOptions).smtp.timeout, s.DEFAULT_TIMEOUT);
|
|
|
|
|
|
|
|
connectionOptions.timeout = null;
|
|
|
|
t.is(new c.Client(connectionOptions).smtp.timeout, s.DEFAULT_TIMEOUT);
|
2020-04-21 03:20:42 +00:00
|
|
|
|
2020-05-27 10:49:11 +00:00
|
|
|
connectionOptions.timeout = undefined;
|
|
|
|
t.is(new c.Client(connectionOptions).smtp.timeout, s.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-05-27 10:49:11 +00:00
|
|
|
const stack = new c.Client({}).createMessageStack(new m.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-05-27 06:03:59 +00:00
|
|
|
test.cb('client rejects message without `from` header', (t) => {
|
|
|
|
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.",
|
|
|
|
};
|
|
|
|
client.send(new m.Message(msg), (err) => {
|
|
|
|
t.true(err instanceof Error);
|
|
|
|
t.is(err?.message, 'Message must have a `from` header');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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.",
|
|
|
|
};
|
|
|
|
client.send(new m.Message(msg), (err) => {
|
|
|
|
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.",
|
|
|
|
};
|
|
|
|
|
|
|
|
send(
|
|
|
|
new m.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);
|
|
|
|
},
|
|
|
|
t.end
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
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.",
|
|
|
|
};
|
|
|
|
|
|
|
|
send(
|
|
|
|
new m.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);
|
2020-05-27 11:31:32 +00:00
|
|
|
t.is(mail.bcc, undefined);
|
2020-05-27 05:47:24 +00:00
|
|
|
},
|
|
|
|
t.end
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-05-27 05:53:45 +00:00
|
|
|
test('client constructor throws if `password` supplied without `user`', (t) => {
|
2020-05-26 15:18:31 +00:00
|
|
|
t.notThrows(() => new c.Client({ user: 'anything', password: 'anything' }));
|
|
|
|
t.throws(() => new c.Client({ password: 'anything' }));
|
|
|
|
t.throws(
|
|
|
|
() =>
|
|
|
|
new c.Client({ username: 'anything', password: 'anything' } as Record<
|
|
|
|
string,
|
|
|
|
unknown
|
|
|
|
>)
|
|
|
|
);
|
|
|
|
});
|