test/connection: add encryption tests

This commit is contained in:
Zack Schuster 2022-04-29 11:56:15 -07:00
parent abe9c101a1
commit 1e28b13f21
1 changed files with 34 additions and 33 deletions

View File

@ -2,8 +2,30 @@ import test from 'ava';
import { SMTPServer } from 'smtp-server';
import { SMTPConnection } from '../email.js';
import type { SMTPConnectionOptions } from '../email.js';
const port = 6666;
let counter = 0;
function testConnection(options: Partial<SMTPConnectionOptions> = {}) {
const increment = counter++;
return new Promise<void>((resolve, reject) => {
const { ssl } = options;
const server = new SMTPServer(ssl ? { secure: true } : undefined);
server.listen(port + increment, () => {
const connection = new SMTPConnection(options);
connection.connect((err) => {
server.close();
connection.close(true);
if (err) {
reject(err);
} else {
resolve();
}
}, port + increment);
});
});
}
test('accepts a custom logger', async (t) => {
const logger = () => {
@ -12,39 +34,18 @@ test('accepts a custom logger', async (t) => {
t.is(Reflect.get(new SMTPConnection({ logger }), 'log'), logger);
});
test('can connect without ssl', async (t) => {
return await t.notThrowsAsync(
new Promise<void>((resolve, reject) => {
const server = new SMTPServer().listen(port, () => {
const connection = new SMTPConnection({ port });
connection.connect((err) => {
server.close();
connection.close(true);
if (err) {
reject(err);
} else {
resolve();
}
}, port);
});
})
);
test('can connect without encryption', async (t) => {
return await t.notThrowsAsync(testConnection());
});
test('can connect with ssl', async (t) => {
return await t.notThrowsAsync(
new Promise<void>((resolve, reject) => {
const server = new SMTPServer({ secure: true }).listen(port + 1, () => {
const connection = new SMTPConnection({ port: port + 1, tls: true });
connection.connect((err) => {
server.close();
connection.close(true);
if (err) {
reject(err);
} else {
resolve();
}
}, port);
});
})
);
return await t.notThrowsAsync(testConnection({ ssl: true }));
});
test('can connect with tls', async (t) => {
return await t.notThrowsAsync(testConnection({ tls: true }));
});
test('can connect with tls and ssl', async (t) => {
return await t.notThrowsAsync(testConnection({ ssl: true, tls: true }));
});