From 95d94c2c65f2dd30c388364e79c77fac192526f2 Mon Sep 17 00:00:00 2001 From: Zack Schuster Date: Wed, 27 May 2020 09:32:53 -0700 Subject: [PATCH] test/auth: add XOAUTH2 units --- test/auth.ts | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/test/auth.ts b/test/auth.ts index 75a87f0..2d1769c 100644 --- a/test/auth.ts +++ b/test/auth.ts @@ -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; + } + }); + }); +});