1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-06-28 09:39:02 +00:00
emailjs/test/authplain.ts

71 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-05-24 14:47:49 +00:00
import type { Readable } from 'stream';
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
import { client as c, message as m } from '../email';
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
) => {
server.onData = (stream: Readable, _session, callback: () => void) => {
2020-05-01 16:25:32 +00:00
mailparser.simpleParser(stream).then(verify).then(done).catch(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-04-23 04:26:49 +00:00
test.cb('authorize plain', (t) => {
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this is a test TEXT message from emailjs',
from: 'piglet@gmail.com',
to: 'pooh@gmail.com',
text: "It is hard to be brave when you're only a Very Small Animal.",
};
2020-05-01 16:25:32 +00:00
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.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});