[feature] Mail service fixes pt.2

This commit is contained in:
Georgii Petrov
2024-04-12 13:39:47 +03:00
parent ef361e9f8b
commit d2895a5500
6 changed files with 47 additions and 250 deletions

View File

@ -1,24 +1,14 @@
const { describe, test, expect, afterAll} = require('@jest/globals');
const nodemailer = require('../../Common/node_modules/nodemailer');
const mailService = require('../../Common/sources/mailService');
const operationContext = require('../../Common/sources/operationContext');
const fs = require('fs');
const ctx = new operationContext.Context();
const bulkTestTimeout = 1000 * 16;
const largeMailContentTestTimeout = 1000 * 15;
const testFolder = '../tests/unit'
const defaultTestSMTPServer = {
host: 'smtp.ethereal.email',
port: 587,
// Account created at https://ethereal.email/, all messages in tests goes here: https://ethereal.email/messages
// Ethereial is a special SMTP sever for mailing tests in collaboration with Nodemailer.
auth: {
type: 'login',
user: 'madie.wiegand79@ethereal.email',
pass: 'ZUSjtcbaBKQdN4BGNx'
},
port: 587
};
const expectedEnvelope = { from: 'some.mail@server.com', to: ['madie.wiegand79@ethereal.email'] };
afterAll(function () {
mailService.transportersRelease();
@ -26,93 +16,36 @@ afterAll(function () {
describe('Mail service', function () {
describe('SMTP', function () {
const { host, port, auth } = defaultTestSMTPServer;
const { host, port } = defaultTestSMTPServer;
mailService.createSMTPTransporter(
ctx,
host,
port,
auth,
{ from: 'some.mail@server.com', to: 'madie.wiegand79@ethereal.email' }
);
test('Transporters life cycle', async function () {
// Accounts created at https://ethereal.email/, all messages in tests goes here: https://ethereal.email/messages
// Ethereial is a special SMTP sever for mailing tests in collaboration with Nodemailer.
const accounts = await Promise.all([nodemailer.createTestAccount(), nodemailer.createTestAccount(), nodemailer.createTestAccount()]);
const auth = accounts.map(account => { return { user: account.user, pass: account.pass }});
auth.forEach(credential => mailService.createTransporter(ctx, host, port, credential, { from: 'some.mail@ethereal.com' }));
test('Simple mail', async function () {
const mailData = await mailService.sendSMTP(ctx, host, auth.user, { text: 'simple test text', subject: 'simple mail test' });
expect(mailData.envelope).toEqual(expectedEnvelope);
});
for (let i = 0; i < auth.length; i++) {
const credentials = auth[i];
const mail = await mailService.send(
host,
credentials.user,
{ to: `some.recipient@server${i + 1}.com`, text: 'simple test text', subject: 'Mail service test' }
);
test('Bulk mails', async function () {
const promises = [];
for (let i = 1; i <= 100; i++) {
promises.push(mailService.sendSMTP(ctx, host, auth.user, { text: `bulk test text #${i}`, subject: 'bulk mails test' }));
expect(mail.envelope).toEqual({ from: 'some.mail@ethereal.com', to: [`some.recipient@server${i + 1}.com`] });
}
const result = await Promise.all(promises);
result.forEach(data => expect(data.envelope).toEqual(expectedEnvelope));
}, bulkTestTimeout);
const accountToBeDeleted = auth[1];
mailService.deleteTransporter(ctx, host, accountToBeDeleted.user);
test('Large mail content', async function () {
const readStream = fs.createReadStream(`${testFolder}/resources/16MiBFile.txt`);
const mailData = await mailService.sendSMTP(ctx, host, auth.user, { text: readStream, subject: 'large mail test' });
expect(mailData.envelope).toEqual(expectedEnvelope);
}, largeMailContentTestTimeout);
test('HTML mail content', async function () {
const readStream = fs.createReadStream(`${testFolder}/resources/htmlContent.html`);
const mailData = await mailService.sendSMTP(ctx, host, auth.user, { html: readStream, subject: 'html mail test' });
expect(mailData.envelope).toEqual(expectedEnvelope);
});
test('Mail with attachments content', async function () {
const readStream = fs.createReadStream(`${testFolder}/resources/htmlContent.html`);
const message = {
text: 'File added below',
subject: 'attachment mail test',
attachments: [
{
filename: 'report.docx',
// Will stream the file from this path.
path: `${testFolder}/resources/new.docx`
},
{
filename: 'page.html',
content: readStream
}
]
};
const mailData = await mailService.sendSMTP(ctx, host, auth.user, message);
expect(mailData.envelope).toEqual(expectedEnvelope);
});
test('Several transporters', async function() {
const secondAccountData = {
type: 'login',
user: 'jonathon.predovic@ethereal.email',
pass: 'yF554NtWzUhGwgCY37'
};
mailService.createSMTPTransporter(
ctx,
'smtp.ethereal.email',
587,
secondAccountData,
{ from: 'some.mail@server.com', to: 'jonathon.predovic@ethereal.email' }
);
const firstAccountResult = await mailService.sendSMTP(
ctx,
const errorPromise = mailService.send(
host,
auth.user,
{ text: 'First account message', subject: 'several accounts mails test' }
accountToBeDeleted.user,
{ to: 'no.recipient@server.com', text: 'simple test text', subject: 'Mail service test' }
);
const secondAccountResult = await mailService.sendSMTP(
ctx,
host,
'jonathon.predovic@ethereal.email',
{ text: 'Second account message', subject: 'several accounts mails test' }
);
expect(firstAccountResult.envelope).toEqual(expectedEnvelope);
expect(secondAccountResult.envelope).toEqual({ from: 'some.mail@server.com', to: ['jonathon.predovic@ethereal.email'] });
await expect(errorPromise).rejects.toEqual(`MailService: no transporter exists for host "${host}" and user "${accountToBeDeleted.user}"`);
});
});
});

File diff suppressed because one or more lines are too long

View File

@ -1,66 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Our Pets</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
color: #333;
}
header {
background-color: #4CAF50;
color: #fff;
padding: 20px;
text-align: center;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
h1, h2 {
text-align: center;
}
p {
line-height: 1.6;
}
</style>
</head>
<body>
<header>
<h1>Our Pets</h1>
<p>Welcome to our page about our beloved pets!</p>
</header>
<div class="container">
<h2>Meet Our Furry Friends</h2>
<p>We have a variety of pets that bring joy to our lives:</p>
<h3>Dog: Bella</h3>
<p>Bella is a friendly golden retriever who loves playing fetch and going on long walks.</p>
<h3>Cat: Whiskers</h3>
<p>Whiskers is a curious tabby cat who enjoys lounging in sunny spots and chasing toy mice.</p>
<h3>Rabbit: Snowball</h3>
<p>Snowball is an adorable white rabbit with floppy ears. She hops around the garden and munches on carrots.</p>
<h3>Parrot: Rio</h3>
<p>Rio is a colorful parrot who loves mimicking sounds and learning new words. His favorite treat is sunflower seeds.</p>
<h2>Why We Love Our Pets</h2>
<p>Our pets bring so much happiness and companionship into our lives. They are always there to greet us with excitement and provide unconditional love.</p>
<p>Do you have pets too? Share your pet stories with us!</p>
</div>
</body>
</html>

Binary file not shown.