From 66d4d7f5d2257d84ff206ff48f564671b4769854 Mon Sep 17 00:00:00 2001 From: Zack Schuster Date: Wed, 27 May 2020 03:49:11 -0700 Subject: [PATCH] test: sort auth & client units by file --- test/auth.ts | 97 ++++++++++++++++++++++++++++++++ test/authssl.ts | 70 ----------------------- test/{authplain.ts => client.ts} | 49 +++++++++------- test/server.ts | 38 ------------- 4 files changed, 127 insertions(+), 127 deletions(-) create mode 100644 test/auth.ts delete mode 100644 test/authssl.ts rename test/{authplain.ts => client.ts} (76%) delete mode 100644 test/server.ts diff --git a/test/auth.ts b/test/auth.ts new file mode 100644 index 0000000..dc51803 --- /dev/null +++ b/test/auth.ts @@ -0,0 +1,97 @@ +import test from 'ava'; +import mailparser from 'mailparser'; +import smtp from 'smtp-server'; + +import { client as c, message as m } from '../email'; + +function onAuth( + auth: smtp.SMTPServerAuthentication, + _session: smtp.SMTPServerSession, + callback: ( + err: Error | null | undefined, + response?: smtp.SMTPServerAuthenticationResponse | undefined + ) => void +) { + if (auth.username == 'pooh' && auth.password == 'honey') { + callback(null, { user: 'pooh' }); + } else { + return callback(new Error('invalid user / pass')); + } +} + +const port = 2526; +const client = new c.Client({ + port, + user: 'pooh', + password: 'honey', + ssl: true, +}); +let server: smtp.SMTPServer | null = null; + +test.afterEach.cb((t) => server?.close(t.end)); + +test.cb('PLAIN authentication 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 smtp.SMTPServer({ + secure: true, + authMethods: ['PLAIN'], + onAuth, + onData(stream, _session, callback: () => void) { + mailparser + .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, () => { + client.send(new m.Message(msg), (err) => { + if (err) { + throw err; + } + }); + }); +}); + +test.cb('LOGIN authentication 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 smtp.SMTPServer({ + secure: true, + authMethods: ['LOGIN'], + onAuth, + onData(stream, _session, callback: () => void) { + mailparser + .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, () => { + client.send(new m.Message(msg), (err) => { + if (err) { + throw err; + } + }); + }); +}); diff --git a/test/authssl.ts b/test/authssl.ts deleted file mode 100644 index 642c638..0000000 --- a/test/authssl.ts +++ /dev/null @@ -1,70 +0,0 @@ -import type { Readable } from 'stream'; -import test from 'ava'; -import mailparser from 'mailparser'; -import smtp from 'smtp-server'; - -import { client as c, message as m } from '../email'; - -const port = 2526; - -const client = new c.Client({ - port, - user: 'pooh', - password: 'honey', - ssl: true, -}); -const server = new smtp.SMTPServer({ secure: true, authMethods: ['LOGIN'] }); - -type UnPromisify = T extends Promise ? U : T; -const send = ( - message: m.Message, - verify: ( - mail: UnPromisify> - ) => void, - done: () => void -) => { - server.onData = (stream: Readable, _session, callback: () => void) => { - mailparser.simpleParser(stream).then(verify).finally(done); - stream.on('end', callback); - }; - client.send(message, (err) => { - if (err) { - throw err; - } - }); -}; - -test.before.cb((t) => { - server.listen(port, function () { - server.onAuth = function (auth, _session, callback) { - if (auth.username == 'pooh' && auth.password == 'honey') { - callback(null, { user: 'pooh' }); - } else { - return callback(new Error('invalid user / pass')); - } - }; - t.end(); - }); -}); - -test.after.cb((t) => server.close(t.end)); - -test.cb('authorize ssl', (t) => { - const msg = { - subject: 'this is a test TEXT message from emailjs', - from: 'pooh@gmail.com', - to: 'rabbit@gmail.com', - text: 'hello friend, i hope this message finds you well.', - }; - - 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 - ); -}); diff --git a/test/authplain.ts b/test/client.ts similarity index 76% rename from test/authplain.ts rename to test/client.ts index 0dc785c..0488fdb 100644 --- a/test/authplain.ts +++ b/test/client.ts @@ -1,9 +1,8 @@ -import type { Readable } from 'stream'; import test from 'ava'; import mailparser from 'mailparser'; import smtp from 'smtp-server'; -import { client as c, message as m } from '../email'; +import { client as c, message as m, smtp as s } from '../email'; type UnPromisify = T extends Promise ? U : T; @@ -23,7 +22,7 @@ const send = ( ) => void, done: () => void ) => { - server.onData = (stream: Readable, _session, callback: () => void) => { + server.onData = (stream, _session, callback: () => void) => { mailparser.simpleParser(stream).then(verify).finally(done); stream.on('end', callback); }; @@ -49,24 +48,36 @@ test.before.cb((t) => { test.after.cb((t) => server.close(t.end)); -test.cb('authorize plain', (t) => { +test.cb('client invokes callback exactly once for invalid connection', (t) => { + t.plan(1); + const client = new c.Client({ host: 'bar.baz' }); 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.", + from: 'foo@bar.baz', + to: 'foo@bar.baz', + subject: 'hello world', + text: 'hello world', }; + client.send(new m.Message(msg), (err) => { + t.not(err, null); + t.end(); + }); +}); - 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 - ); +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); + + connectionOptions.timeout = undefined; + t.is(new c.Client(connectionOptions).smtp.timeout, s.DEFAULT_TIMEOUT); }); test('client deduplicates recipients', (t) => { @@ -76,7 +87,7 @@ test('client deduplicates recipients', (t) => { cc: 'gannon@gmail.com', bcc: 'gannon@gmail.com', }; - const stack = client.createMessageStack(new m.Message(msg)); + const stack = new c.Client({}).createMessageStack(new m.Message(msg)); t.true(stack.to.length === 1); t.is(stack.to[0].address, 'gannon@gmail.com'); }); diff --git a/test/server.ts b/test/server.ts deleted file mode 100644 index 65725b9..0000000 --- a/test/server.ts +++ /dev/null @@ -1,38 +0,0 @@ -import test from 'ava'; - -import { client as c, message as m, smtp as s } from '../email'; - -test.cb( - 'connecting to wrong email server should not invoke callback multiple times', - (t) => { - t.plan(1); - const client = new c.Client({ host: 'bar.baz' }); - const msg = { - from: 'foo@bar.baz', - to: 'foo@bar.baz', - subject: 'hello world', - text: 'hello world', - }; - client.send(new m.Message(msg), (err) => { - t.not(err, null); - t.end(); - }); - } -); - -test('should have a default timeout', async (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); - - connectionOptions.timeout = undefined; - t.is(new c.Client(connectionOptions).smtp.timeout, s.DEFAULT_TIMEOUT); -});