mirror of
https://github.com/eleith/emailjs.git
synced 2024-06-28 09:39:02 +00:00
test: sort auth & client units by file
This commit is contained in:
parent
b76a198a93
commit
66d4d7f5d2
97
test/auth.ts
Normal file
97
test/auth.ts
Normal file
|
@ -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;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
|
@ -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> = T extends Promise<infer U> ? U : T;
|
||||
const send = (
|
||||
message: m.Message,
|
||||
verify: (
|
||||
mail: UnPromisify<ReturnType<typeof mailparser.simpleParser>>
|
||||
) => 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
|
||||
);
|
||||
});
|
|
@ -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> = T extends Promise<infer U> ? 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');
|
||||
});
|
|
@ -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);
|
||||
});
|
Loading…
Reference in New Issue
Block a user