1
0
mirror of https://github.com/eleith/emailjs.git synced 2024-06-14 12:09:03 +00:00
emailjs/test/message.ts

489 lines
12 KiB
TypeScript
Raw Normal View History

2020-04-21 03:20:42 +00:00
import { readFileSync, createReadStream } from 'fs';
import { join } from 'path';
2020-05-27 12:49:21 +00:00
2020-04-21 03:20:42 +00:00
import test from 'ava';
2020-05-27 12:49:21 +00:00
import { simpleParser } from 'mailparser';
import { SMTPServer } from 'smtp-server';
2020-04-21 03:20:42 +00:00
2020-05-27 18:15:55 +00:00
import { SMTPClient, Message, MessageAttachment } from '../email';
2020-04-21 03:20:42 +00:00
const port = 2526;
2020-05-27 18:15:55 +00:00
const client = new SMTPClient({
2020-04-23 04:26:49 +00:00
port,
user: 'pooh',
password: 'honey',
ssl: true,
});
2020-05-27 15:59:53 +00:00
const server = new SMTPServer({ secure: true });
2020-04-21 03:20:42 +00:00
type UnPromisify<T> = T extends Promise<infer U> ? U : T;
2020-04-23 04:26:49 +00:00
const send = (
2020-05-27 12:34:05 +00:00
message: Message,
2020-05-27 12:49:21 +00:00
verify: (mail: UnPromisify<ReturnType<typeof simpleParser>>) => void,
2020-05-01 16:25:32 +00:00
done: () => void
2020-04-23 04:26:49 +00:00
) => {
2020-05-27 11:14:24 +00:00
server.onData = (stream, _session, callback: () => void) => {
2020-05-27 12:49:21 +00:00
simpleParser(stream, { skipTextLinks: true } as Record<string, unknown>)
2020-05-24 20:08:21 +00:00
.then(verify)
.finally(done);
2020-04-21 03:20:42 +00:00
stream.on('end', callback);
};
2020-04-23 04:26:49 +00:00
client.send(message, (err) => {
2020-04-21 03:20:42 +00:00
if (err) {
throw err;
}
});
};
2020-04-25 20:17:35 +00:00
test.before.cb((t) => {
2020-04-25 03:03:31 +00:00
server.listen(port, function () {
server.onAuth = function (auth, _session, callback) {
2020-04-21 03:20:42 +00:00
if (auth.username == 'pooh' && auth.password == 'honey') {
callback(null, { user: 'pooh' });
} else {
return callback(new Error('invalid user / pass'));
}
};
2020-04-25 20:17:35 +00:00
t.end();
2020-04-21 03:20:42 +00:00
});
});
2020-04-25 20:17:35 +00:00
test.after.cb((t) => server.close(t.end));
2020-04-21 03:20:42 +00:00
2020-04-23 04:26:49 +00:00
test.cb('simple text message', (t) => {
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this is a test TEXT message from emailjs',
from: 'zelda@gmail.com',
to: 'gannon@gmail.com',
cc: 'gannon@gmail.com',
bcc: 'gannon@gmail.com',
2020-04-21 03:20:42 +00:00
text: 'hello friend, i hope this message finds you well.',
'message-id': 'this is a special id',
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(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.is(mail.messageId, '<' + msg['message-id'] + '>');
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('null text message', (t) => {
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this is a test TEXT message from emailjs',
from: 'zelda@gmail.com',
to: 'gannon@gmail.com',
text: null,
'message-id': 'this is a special id',
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.text, '\n\n\n');
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('empty text message', (t) => {
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this is a test TEXT message from emailjs',
from: 'zelda@gmail.com',
to: 'gannon@gmail.com',
text: '',
'message-id': 'this is a special id',
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.text, '\n\n\n');
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('simple unicode text message', (t) => {
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this ✓ is a test ✓ TEXT message from emailjs',
from: 'zelda✓ <zelda@gmail.com>',
to: 'gannon✓ <gannon@gmail.com>',
text: 'hello ✓ friend, i hope this message finds you well.',
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(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
);
2020-04-21 03:20:42 +00:00
});
2020-05-01 20:04:33 +00:00
test.cb('very large text message', (t) => {
2020-04-21 03:20:42 +00:00
// thanks to jart+loberstech for this one!
const msg = {
subject: 'this is a test TEXT message from emailjs',
from: 'ninjas@gmail.com',
to: 'pirates@gmail.com',
2020-04-23 04:26:49 +00:00
text: readFileSync(join(__dirname, 'attachments/smtp.txt'), 'utf-8'),
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.text, msg.text.replace(/\r/g, '') + '\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
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('very large text data message', (t) => {
2020-04-21 03:20:42 +00:00
const text =
'<html><body><pre>' +
readFileSync(join(__dirname, 'attachments/smtp.txt'), 'utf-8') +
'</pre></body></html>';
const msg = {
subject: 'this is a test TEXT+DATA message from emailjs',
from: 'lobsters@gmail.com',
to: 'lizards@gmail.com',
text:
'hello friend if you are seeing this, you can not view html emails. it is attached inline.',
2020-04-23 04:26:49 +00:00
attachment: ({
data: text,
alternative: true,
2020-05-27 12:34:05 +00:00
} as unknown) as MessageAttachment,
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.html, text.replace(/\r/g, ''));
t.is(mail.text, msg.text + '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('html data message', (t) => {
const html = readFileSync(join(__dirname, 'attachments/smtp.html'), 'utf-8');
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this is a test TEXT+HTML+DATA message from emailjs',
from: 'obama@gmail.com',
to: 'mitt@gmail.com',
2020-04-23 04:26:49 +00:00
attachment: ({
data: html,
alternative: true,
2020-05-27 12:34:05 +00:00
} as unknown) as MessageAttachment,
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.html, html.replace(/\r/g, ''));
t.is(mail.text, '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('html file message', (t) => {
const html = readFileSync(join(__dirname, 'attachments/smtp.html'), 'utf-8');
2020-04-21 03:20:42 +00:00
const msg = {
subject: 'this is a test TEXT+HTML+FILE message from emailjs',
from: 'thomas@gmail.com',
to: 'nikolas@gmail.com',
2020-04-23 04:26:49 +00:00
attachment: ({
path: join(__dirname, 'attachments/smtp.html'),
alternative: true,
2020-05-27 12:34:05 +00:00
} as unknown) as MessageAttachment,
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.html, html.replace(/\r/g, ''));
t.is(mail.text, '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('html with image embed message', (t) => {
const html = readFileSync(join(__dirname, 'attachments/smtp2.html'), 'utf-8');
2020-04-21 03:20:42 +00:00
const image = readFileSync(join(__dirname, 'attachments/smtp.gif'));
const msg = {
subject: 'this is a test TEXT+HTML+IMAGE message from emailjs',
from: 'ninja@gmail.com',
to: 'pirate@gmail.com',
2020-04-23 04:26:49 +00:00
attachment: ({
2020-04-21 03:20:42 +00:00
path: join(__dirname, 'attachments/smtp2.html'),
alternative: true,
related: [
{
path: join(__dirname, 'attachments/smtp.gif'),
type: 'image/gif',
name: 'smtp-diagram.gif',
headers: { 'Content-ID': '<smtp-diagram@local>' },
},
],
2020-05-27 12:34:05 +00:00
} as unknown) as MessageAttachment,
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(
mail.attachments[0].content.toString('base64'),
image.toString('base64')
);
t.is(mail.html, html.replace(/\r/g, ''));
t.is(mail.text, '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('html data and attachment message', (t) => {
2020-04-21 03:20:42 +00:00
const html = readFileSync(join(__dirname, 'attachments/smtp.html'), 'utf-8');
const msg = {
subject: 'this is a test TEXT+HTML+FILE message from emailjs',
from: 'thomas@gmail.com',
to: 'nikolas@gmail.com',
attachment: [
{ path: join(__dirname, 'attachments/smtp.html'), alternative: true },
{ path: join(__dirname, 'attachments/smtp.gif') },
2020-05-27 12:34:05 +00:00
] as MessageAttachment[],
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(mail.html, html.replace(/\r/g, ''));
t.is(mail.text, '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('attachment message', (t) => {
2020-04-21 03:20:42 +00:00
const pdf = readFileSync(join(__dirname, 'attachments/smtp.pdf'));
const msg = {
subject: 'this is a test TEXT+ATTACHMENT message from emailjs',
from: 'washing@gmail.com',
to: 'lincoln@gmail.com',
text: 'hello friend, i hope this message and pdf finds you well.',
attachment: {
path: join(__dirname, 'attachments/smtp.pdf'),
type: 'application/pdf',
name: 'smtp-info.pdf',
2020-05-27 12:34:05 +00:00
} as MessageAttachment,
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(
mail.attachments[0].content.toString('base64'),
pdf.toString('base64')
);
t.is(mail.text, msg.text + '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('attachment sent with unicode filename message', (t) => {
2020-04-21 03:20:42 +00:00
const pdf = readFileSync(join(__dirname, 'attachments/smtp.pdf'));
const msg = {
subject: 'this is a test TEXT+ATTACHMENT message from emailjs',
from: 'washing@gmail.com',
to: 'lincoln@gmail.com',
text: 'hello friend, i hope this message and pdf finds you well.',
attachment: {
path: join(__dirname, 'attachments/smtp.pdf'),
type: 'application/pdf',
name: 'smtp-✓-info.pdf',
2020-05-27 12:34:05 +00:00
} as MessageAttachment,
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(
mail.attachments[0].content.toString('base64'),
pdf.toString('base64')
);
t.is(mail.attachments[0].filename, 'smtp-✓-info.pdf');
t.is(mail.text, msg.text + '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('attachments message', (t) => {
2020-04-21 03:20:42 +00:00
const pdf = readFileSync(join(__dirname, 'attachments/smtp.pdf'));
const tar = readFileSync(join(__dirname, 'attachments/postfix-2.8.7.tar.gz'));
const msg = {
subject: 'this is a test TEXT+2+ATTACHMENTS message from emailjs',
from: 'sergey@gmail.com',
to: 'jobs@gmail.com',
text: 'hello friend, i hope this message and attachments finds you well.',
attachment: [
{
path: join(__dirname, 'attachments/smtp.pdf'),
type: 'application/pdf',
name: 'smtp-info.pdf',
},
{
path: join(__dirname, 'attachments/postfix-2.8.7.tar.gz'),
type: 'application/tar-gz',
name: 'postfix.source.2.8.7.tar.gz',
},
2020-05-27 12:34:05 +00:00
] as MessageAttachment[],
2020-04-21 03:20:42 +00:00
};
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(
mail.attachments[0].content.toString('base64'),
pdf.toString('base64')
);
t.is(
mail.attachments[1].content.toString('base64'),
tar.toString('base64')
);
t.is(mail.text, msg.text + '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
2020-04-23 04:26:49 +00:00
test.cb('streams message', (t) => {
2020-04-21 03:20:42 +00:00
const pdf = readFileSync(join(__dirname, 'attachments/smtp.pdf'));
const tar = readFileSync(join(__dirname, 'attachments/postfix-2.8.7.tar.gz'));
const stream = createReadStream(join(__dirname, 'attachments/smtp.pdf'));
2020-04-23 04:26:49 +00:00
const stream2 = createReadStream(
join(__dirname, 'attachments/postfix-2.8.7.tar.gz')
);
2020-04-21 03:20:42 +00:00
const msg = {
2020-04-23 04:26:49 +00:00
subject: 'this is a test TEXT+2+STREAMED+ATTACHMENTS message from emailjs',
2020-04-21 03:20:42 +00:00
from: 'stanford@gmail.com',
to: 'mit@gmail.com',
text:
'hello friend, i hope this message and streamed attachments finds you well.',
2020-04-23 04:26:49 +00:00
attachment: ([
2020-04-21 03:20:42 +00:00
{ stream, type: 'application/pdf', name: 'smtp-info.pdf' },
2020-04-23 04:26:49 +00:00
{
stream: stream2,
type: 'application/x-gzip',
name: 'postfix.source.2.8.7.tar.gz',
},
2020-05-27 12:34:05 +00:00
] as unknown) as MessageAttachment[],
2020-04-21 03:20:42 +00:00
};
stream.pause();
stream2.pause();
2020-05-01 16:25:32 +00:00
send(
2020-05-27 12:34:05 +00:00
new Message(msg),
2020-05-01 16:25:32 +00:00
(mail) => {
t.is(
mail.attachments[0].content.toString('base64'),
pdf.toString('base64')
);
t.is(
mail.attachments[1].content.toString('base64'),
tar.toString('base64')
);
t.is(mail.text, msg.text + '\n');
t.is(mail.subject, msg.subject);
t.is(mail.from?.text, msg.from);
t.is(mail.to?.text, msg.to);
},
t.end
);
2020-04-21 03:20:42 +00:00
});
test.cb('message validation fails without `from` header', (t) => {
2020-05-27 12:34:05 +00:00
const msg = new Message({});
msg.valid((isValid, reason) => {
t.false(isValid);
t.is(reason, 'Message must have a `from` header');
t.end();
});
});
test.cb('message validation fails without `to`, `cc`, or `bcc` header', (t) => {
2020-05-27 12:34:05 +00:00
const msg = new Message({
from: 'piglet@gmail.com',
});
msg.valid((isValid, reason) => {
t.false(isValid);
t.is(reason, 'Message must have at least one `to`, `cc`, or `bcc` header');
t.end();
});
});
test.cb('message validation succeeds with only `cc` recipient header', (t) => {
2020-05-27 12:34:05 +00:00
const msg = new Message({
from: 'piglet@gmail.com',
cc: 'pooh@gmail.com',
});
msg.valid((isValid) => {
t.true(isValid);
t.end();
});
});
test.cb('message validation succeeds with only `bcc` recipient header', (t) => {
2020-05-27 12:34:05 +00:00
const msg = new Message({
from: 'piglet@gmail.com',
bcc: 'pooh@gmail.com',
});
msg.valid((isValid) => {
t.true(isValid);
t.end();
});
});