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

test/auth: add XOAUTH2 units

This commit is contained in:
Zack Schuster 2020-05-27 09:32:53 -07:00
parent 8f1f9effc5
commit 95d94c2c65

View File

@ -17,7 +17,15 @@ function onAuth(
response?: SMTPServerAuthenticationResponse | undefined
) => void
) {
if (auth.username == 'pooh' && auth.password == 'honey') {
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')
) {
callback(null, { user: 'pooh' });
} else {
return callback(new Error('invalid user / pass'));
@ -241,3 +249,78 @@ test.cb('LOGIN authentication (encrypted) should succeed', (t) => {
});
});
});
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, () => {
new Client({
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, () => {
new Client({
port,
user: 'pooh',
password: 'honey',
ssl: true,
authentication: [AUTH_METHODS.XOAUTH2],
}).send(new Message(msg), (err) => {
if (err) {
throw err;
}
});
});
});