kill-the-newsletter/src/test.ts

85 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-03-19 17:12:16 +01:00
import {
developmentWebServer,
developmentEmailServer,
feedPath,
feedEmail
} from ".";
2020-03-19 15:48:31 +01:00
import nodemailer from "nodemailer";
2020-03-20 18:48:53 +01:00
import axios from "axios";
import qs from "qs";
2020-03-18 20:33:10 +01:00
import fs from "fs";
2020-03-18 20:21:44 +01:00
2020-03-19 02:44:21 +01:00
test("create feed", async () => {
2020-03-19 04:36:02 +01:00
const token = await createFeed();
2020-03-18 20:33:10 +01:00
2020-03-19 05:20:28 +01:00
expect(readFeed(token)).toMatch("My Feed");
2020-03-18 20:21:44 +01:00
});
2020-03-19 01:16:00 +01:00
2020-03-19 05:20:28 +01:00
describe("receive email", () => {
2020-03-19 15:48:31 +01:00
const transporter = nodemailer.createTransport({
2020-03-19 05:20:28 +01:00
host: "localhost",
port: 2525,
tls: { rejectUnauthorized: false }
});
test("HTML content", async () => {
const token = await createFeed();
await transporter.sendMail({
from: "publisher@example.com",
to: feedEmail(token),
subject: "New Message",
html: "<p>HTML content</p>"
});
const feed = readFeed(token);
expect(feed).toMatch("publisher@example.com");
expect(feed).toMatch("New Message");
expect(feed).toMatch("HTML content");
});
test("text content", async () => {
const token = await createFeed();
await transporter.sendMail({
from: "publisher@example.com",
to: feedEmail(token),
subject: "New Message",
text: "TEXT content"
});
const feed = readFeed(token);
expect(feed).toMatch("TEXT content");
});
test("truncation", async () => {
const token = await createFeed();
for (const repetition of [...new Array(4).keys()])
await transporter.sendMail({
from: "publisher@example.com",
to: feedEmail(token),
subject: "New Message",
text: `REPETITION ${repetition} `.repeat(10_000)
});
const feed = readFeed(token);
expect(feed).toMatch("REPETITION 3");
expect(feed).not.toMatch("REPETITION 0");
}, 10_000);
});
2020-03-19 04:36:02 +01:00
2020-03-19 01:16:00 +01:00
afterAll(() => {
2020-03-19 17:12:16 +01:00
developmentWebServer.close();
developmentEmailServer.close(() => {});
2020-03-19 01:16:00 +01:00
});
2020-03-19 04:36:02 +01:00
async function createFeed(): Promise<string> {
2020-03-20 18:48:53 +01:00
return (
await axios.post(
"http://localhost:8000",
qs.stringify({
name: "My Feed"
})
)
).data.match(/(\w{20}).xml/)![1];
2020-03-19 04:36:02 +01:00
}
2020-03-19 05:20:28 +01:00
function readFeed(token: string): string {
2020-03-19 16:23:19 +01:00
return fs.readFileSync(feedPath(token), "utf8");
2020-03-19 05:20:28 +01:00
}