1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-07-07 12:40:37 +00:00
emailjs/test/auth.ts

327 lines
7.9 KiB
TypeScript
Raw Normal View History

2020-05-27 10:49:11 +00:00
import test from 'ava';
2020-05-27 12:49:21 +00:00
import { simpleParser } from 'mailparser';
import {
SMTPServer,
SMTPServerAuthentication,
SMTPServerAuthenticationResponse,
SMTPServerSession,
} from 'smtp-server';
2020-05-27 10:49:11 +00:00
2020-05-27 18:15:55 +00:00
import { AUTH_METHODS, SMTPClient, Message } from '../email';
2020-05-27 10:49:11 +00:00
function onAuth(
2020-05-27 12:49:21 +00:00
auth: SMTPServerAuthentication,
_session: SMTPServerSession,
2020-05-27 10:49:11 +00:00
callback: (
err: Error | null | undefined,
2020-05-27 12:49:21 +00:00
response?: SMTPServerAuthenticationResponse | undefined
2020-05-27 10:49:11 +00:00
) => void
) {
2020-05-27 16:32:53 +00:00
const { accessToken, method, username, password } = auth;
if (
(method === AUTH_METHODS.XOAUTH2 && password != null
? accessToken === 'pooh'
: username === 'pooh') &&
(method === AUTH_METHODS.XOAUTH2 && password == null
? accessToken === 'honey'
: password === 'honey')
) {
2020-05-27 10:49:11 +00:00
callback(null, { user: 'pooh' });
} else {
return callback(new Error('invalid user / pass'));
}
}
const port = 2526;
2020-05-27 12:49:21 +00:00
let server: SMTPServer | null = null;
2020-05-27 10:49:11 +00:00
test.afterEach.cb((t) => server?.close(t.end));
test.cb('no authentication (unencrypted) should succeed', (t) => {
2020-05-27 11:14:47 +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-27 12:49:21 +00:00
server = new SMTPServer({
2020-05-27 11:14:47 +00:00
authMethods: [],
authOptional: true,
onData(stream, _session, callback: () => void) {
2020-05-27 12:49:21 +00:00
simpleParser(stream)
2020-05-27 11:14:47 +00:00
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({ port }).send(new Message(msg), (err) => {
2020-05-27 11:14:47 +00:00
if (err) {
throw err;
}
});
});
});
test.cb('no authentication (encrypted) should succeed', (t) => {
2020-05-27 10:49:11 +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-27 12:49:21 +00:00
server = new SMTPServer({
authMethods: [],
authOptional: true,
2020-05-27 10:49:11 +00:00
secure: true,
onData(stream, _session, callback: () => void) {
2020-05-27 12:49:21 +00:00
simpleParser(stream)
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({ port, ssl: true }).send(new Message(msg), (err) => {
if (err) {
throw err;
}
});
});
});
test.cb('PLAIN authentication (unencrypted) should succeed', (t) => {
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-27 12:49:21 +00:00
server = new SMTPServer({
2020-05-27 15:59:43 +00:00
authMethods: [AUTH_METHODS.PLAIN],
hideSTARTTLS: true,
2020-05-27 10:49:11 +00:00
onAuth,
onData(stream, _session, callback: () => void) {
2020-05-27 12:49:21 +00:00
simpleParser(stream)
2020-05-27 10:49:11 +00:00
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({
port,
user: 'pooh',
password: 'honey',
2020-05-27 15:59:43 +00:00
authentication: [AUTH_METHODS.PLAIN],
2020-05-27 12:34:05 +00:00
}).send(new Message(msg), (err) => {
2020-05-27 10:49:11 +00:00
if (err) {
throw err;
}
});
});
});
test.cb('PLAIN authentication (encrypted) should succeed', (t) => {
2020-05-27 10:49:11 +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-27 12:49:21 +00:00
server = new SMTPServer({
2020-05-27 15:59:43 +00:00
authMethods: [AUTH_METHODS.PLAIN],
2020-05-27 10:49:11 +00:00
secure: true,
onAuth,
onData(stream, _session, callback: () => void) {
2020-05-27 12:49:21 +00:00
simpleParser(stream)
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({
port,
user: 'pooh',
password: 'honey',
2020-05-27 15:59:43 +00:00
authentication: [AUTH_METHODS.PLAIN],
ssl: true,
2020-05-27 12:34:05 +00:00
}).send(new Message(msg), (err) => {
if (err) {
throw err;
}
});
});
});
test.cb('LOGIN authentication (unencrypted) should succeed', (t) => {
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-27 12:49:21 +00:00
server = new SMTPServer({
2020-05-27 15:59:43 +00:00
authMethods: [AUTH_METHODS.LOGIN],
hideSTARTTLS: true,
onAuth,
onData(stream, _session, callback: () => void) {
2020-05-27 12:49:21 +00:00
simpleParser(stream)
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({
port,
user: 'pooh',
password: 'honey',
2020-05-27 15:59:43 +00:00
authentication: [AUTH_METHODS.LOGIN],
2020-05-27 12:34:05 +00:00
}).send(new Message(msg), (err) => {
if (err) {
throw err;
}
});
});
});
test.cb('LOGIN authentication (encrypted) should succeed', (t) => {
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-27 12:49:21 +00:00
server = new SMTPServer({
2020-05-27 15:59:43 +00:00
authMethods: [AUTH_METHODS.LOGIN],
secure: true,
2020-05-27 10:49:11 +00:00
onAuth,
onData(stream, _session, callback: () => void) {
2020-05-27 12:49:21 +00:00
simpleParser(stream)
2020-05-27 10:49:11 +00:00
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({
port,
user: 'pooh',
password: 'honey',
ssl: true,
2020-05-27 15:59:43 +00:00
authentication: [AUTH_METHODS.LOGIN],
2020-05-27 12:34:05 +00:00
}).send(new Message(msg), (err) => {
2020-05-27 10:49:11 +00:00
if (err) {
throw err;
}
});
});
});
2020-05-27 16:32:53 +00:00
test.cb('XOAUTH2 authentication (unencrypted) should succeed', (t) => {
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.",
};
server = new SMTPServer({
authMethods: [AUTH_METHODS.XOAUTH2],
hideSTARTTLS: true,
onAuth,
onData(stream, _session, callback: () => void) {
simpleParser(stream)
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({
2020-05-27 16:32:53 +00:00
port,
user: 'pooh',
password: 'honey',
authentication: [AUTH_METHODS.XOAUTH2],
}).send(new Message(msg), (err) => {
if (err) {
throw err;
}
});
});
});
test.cb('XOAUTH2 authentication (encrypted) should succeed', (t) => {
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.",
};
server = new SMTPServer({
authMethods: [AUTH_METHODS.XOAUTH2],
secure: true,
onAuth,
onData(stream, _session, callback: () => void) {
simpleParser(stream)
.then((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);
})
.finally(t.end);
stream.on('end', callback);
},
});
server.listen(port, () => {
2020-05-27 18:15:55 +00:00
new SMTPClient({
2020-05-27 16:32:53 +00:00
port,
user: 'pooh',
password: 'honey',
ssl: true,
authentication: [AUTH_METHODS.XOAUTH2],
}).send(new Message(msg), (err) => {
if (err) {
throw err;
}
});
});
});