[feature] Mail service

This commit is contained in:
Georgii Petrov
2024-04-09 19:13:35 +03:00
parent 92682e70ff
commit 438a9c82a1
8 changed files with 351 additions and 0 deletions

View File

@ -0,0 +1,88 @@
const { describe, test, expect, afterAll} = require('@jest/globals');
const config = require('../../Common/node_modules/config');
const mailService = require('../../Common/sources/mailService');
const operationContext = require('../../Common/sources/operationContext');
const fs = require('fs');
const ctx = new operationContext.Context();
const bulkTestTimeout = 1000 * 10;
const largeMailContentTestTimeout = 1000 * 7;
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'
},
};
const expectedEnvelope = { from: 'some.mail@server.com', to: ['madie.wiegand79@ethereal.email'] };
afterAll(function () {
mailService.transportersRelease();
})
describe('Mail service', function () {
describe('SMTP', function () {
const { host, port, auth } = defaultTestSMTPServer;
const transporter = mailService.createSMTPTransporter(
ctx,
host,
port,
auth,
{ from: 'some.mail@server.com', to: 'madie.wiegand79@ethereal.email' }
);
test('Simple mail', async function () {
const mailData = await transporter.sendMail({ text: 'simple test text', subject: 'simple mail test' });
expect(mailData.envelope).toEqual(expectedEnvelope);
});
test('Bulk mails', async function () {
const promises = [];
for (let i = 1; i <= 100; i++) {
promises.push(transporter.sendMail({ text: `bulk test text #${i}`, subject: 'bulk mails test' }));
}
const result = await Promise.all(promises);
result.forEach(data => expect(data.envelope).toEqual(expectedEnvelope));
}, bulkTestTimeout);
test('Large mail content', async function () {
const readStream = fs.createReadStream(`${testFolder}/resources/16MiBFile.txt`);
const mailData = await transporter.sendMail({ 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 transporter.sendMail({ 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 transporter.sendMail(message);
expect(mailData.envelope).toEqual(expectedEnvelope);
});
});
});

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,66 @@
<!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.