kill-the-newsletter/test.ts

196 lines
5.8 KiB
TypeScript
Raw Normal View History

2020-04-06 15:48:34 +02:00
import { webServer, emailServer, WEB_PORT, EMAIL_PORT, EMAIL_DOMAIN } 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:21:44 +01:00
2020-03-19 02:44:21 +01:00
test("create feed", async () => {
2020-03-20 20:08:04 +01:00
const identifier = await createFeed();
2020-03-18 20:33:10 +01:00
2020-03-21 16:58:28 +01:00
expect(await getFeed(identifier)).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-31 20:14:44 +02:00
test("updated field is updated", async () => {
const identifier = await createFeed();
const before = await getFeed(identifier);
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-31 20:14:44 +02:00
subject: "New Message",
2020-04-03 23:06:43 +02:00
html: "<p>HTML content</p>",
2020-03-31 20:14:44 +02:00
});
const after = await getFeed(identifier);
expect(after.match(/<updated>(.*)<\/updated>/)![1]).not.toMatch(
before.match(/<updated>(.*)<\/updated>/)![1]
);
});
2020-03-19 05:20:28 +01:00
test("HTML content", async () => {
2020-03-20 20:08:04 +01:00
const identifier = await createFeed();
2020-03-21 16:58:28 +01:00
await emailClient.sendMail({
2020-03-19 05:20:28 +01:00
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-19 05:20:28 +01:00
subject: "New Message",
2020-04-03 23:06:43 +02:00
html: "<p>HTML content</p>",
2020-03-19 05:20:28 +01:00
});
2020-03-21 16:58:28 +01:00
const feed = await getFeed(identifier);
2020-03-19 05:20:28 +01:00
expect(feed).toMatch("publisher@example.com");
expect(feed).toMatch("New Message");
expect(feed).toMatch("HTML content");
});
test("text content", async () => {
2020-03-20 20:08:04 +01:00
const identifier = await createFeed();
2020-03-21 16:58:28 +01:00
await emailClient.sendMail({
2020-03-19 05:20:28 +01:00
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-19 05:20:28 +01:00
subject: "New Message",
2020-04-03 23:06:43 +02:00
text: "TEXT content",
2020-03-19 05:20:28 +01:00
});
2020-03-21 16:58:28 +01:00
const feed = await getFeed(identifier);
2020-03-19 05:20:28 +01:00
expect(feed).toMatch("TEXT content");
});
2020-03-23 22:43:48 +01:00
test("rich text content", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-23 22:43:48 +01:00
subject: "New Message",
2020-04-06 15:48:34 +02:00
text: "TEXT content\n\nhttps://www.leafac.com\n\nMore text",
2020-03-23 22:43:48 +01:00
});
const feed = await getFeed(identifier);
expect(feed).toMatch("TEXT content");
2020-04-06 15:48:34 +02:00
expect(feed).toMatch(`href="https://www.leafac.com"`);
2020-03-23 22:43:48 +01:00
});
2020-03-31 20:14:44 +02:00
test("invalid XML character in HTML", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-31 20:14:44 +02:00
subject: "New Message",
2020-05-05 08:12:57 +02:00
html: "<p>Invalid XML character (backspace): |\b|💩</p>",
2020-03-31 20:14:44 +02:00
});
const feed = await getFeed(identifier);
2020-05-05 08:12:57 +02:00
expect(feed).toMatch("Invalid XML character (backspace): ||💩");
2020-03-31 20:14:44 +02:00
});
test("invalid XML character in text", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-31 20:14:44 +02:00
subject: "New Message",
2020-05-05 08:12:57 +02:00
text: "Invalid XML character (backspace): |\b|💩",
2020-03-31 20:14:44 +02:00
});
const feed = await getFeed(identifier);
2020-05-05 08:12:57 +02:00
expect(feed).toMatch(
"Invalid XML character (backspace): |&amp;#x8;|&amp;#x1F4A9;"
);
2020-03-31 20:14:44 +02:00
});
2020-03-23 16:08:57 +01:00
test("missing content", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-04-03 23:06:43 +02:00
subject: "New Message",
2020-03-23 16:08:57 +01:00
});
const feed = await getFeed(identifier);
expect(feed).toMatch("New Message");
});
test("missing subject", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-04-03 23:06:43 +02:00
html: "<p>HTML content</p>",
2020-03-23 16:08:57 +01:00
});
const feed = await getFeed(identifier);
expect(feed).toMatch("HTML content");
});
2020-03-19 05:20:28 +01:00
test("truncation", async () => {
2020-03-20 20:08:04 +01:00
const identifier = await createFeed();
2020-03-19 05:20:28 +01:00
for (const repetition of [...new Array(4).keys()])
2020-03-21 16:58:28 +01:00
await emailClient.sendMail({
2020-03-19 05:20:28 +01:00
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-19 05:20:28 +01:00
subject: "New Message",
2020-04-03 23:06:43 +02:00
text: `REPETITION ${repetition} `.repeat(10_000),
2020-03-19 05:20:28 +01:00
});
2020-03-21 16:58:28 +01:00
const feed = await getFeed(identifier);
2020-03-19 05:20:28 +01:00
expect(feed).toMatch("REPETITION 3");
expect(feed).not.toMatch("REPETITION 0");
2020-03-21 16:58:28 +01:00
});
2020-03-22 15:23:05 +01:00
2020-05-05 08:12:57 +02:00
test("too big entry", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "publisher@example.com",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
text: `TOO BIG`.repeat(100_000),
});
expect(await getFeed(identifier)).not.toMatch("<entry>");
await emailClient.sendMail({
from: "publisher@example.com",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
text: `NORMAL SIZE`,
});
const feed = await getFeed(identifier);
expect(feed).toMatch("<entry>");
expect(feed).toMatch("NORMAL SIZE");
});
2020-03-23 02:27:03 +01:00
test("nonexistent address", async () => {
2020-03-22 15:23:05 +01:00
await emailClient.sendMail({
from: "publisher@example.com",
2020-04-06 15:48:34 +02:00
to: `nonexistent@${EMAIL_DOMAIN}`,
2020-03-22 15:23:05 +01:00
subject: "New Message",
2020-04-03 23:06:43 +02:00
html: "<p>HTML content</p>",
2020-03-22 15:23:05 +01:00
});
});
2020-03-23 16:24:14 +01:00
test("missing from", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
2020-04-06 15:48:34 +02:00
to: `${identifier}@${EMAIL_DOMAIN}`,
2020-03-23 16:24:14 +01:00
subject: "New Message",
2020-04-03 23:06:43 +02:00
html: "<p>HTML content</p>",
2020-03-23 16:24:14 +01:00
});
const feed = await getFeed(identifier);
expect(feed).toMatch("HTML content");
});
2020-03-19 05:20:28 +01:00
});
2020-03-19 04:36:02 +01:00
2020-03-19 01:16:00 +01:00
afterAll(() => {
2020-03-20 22:31:46 +01:00
webServer.close();
2020-03-31 18:57:56 +02:00
emailServer.close();
2020-03-19 01:16:00 +01:00
});
2020-03-19 04:36:02 +01:00
2020-03-24 16:13:07 +01:00
const webClient = axios.create({
2020-04-06 15:34:44 +02:00
baseURL: `http://localhost:${WEB_PORT}`,
2020-03-24 16:13:07 +01:00
});
const emailClient = nodemailer.createTransport(
2020-04-06 15:34:44 +02:00
`smtp://localhost:${EMAIL_PORT}`
2020-03-24 16:13:07 +01:00
);
2020-03-21 16:58:28 +01:00
2020-03-19 04:36:02 +01:00
async function createFeed(): Promise<string> {
2020-03-20 18:48:53 +01:00
return (
2020-03-21 16:58:28 +01:00
await webClient.post(
"/",
2020-03-20 18:48:53 +01:00
qs.stringify({
2020-04-03 23:06:43 +02:00
name: "My Feed",
2020-03-20 18:48:53 +01:00
})
)
).data.match(/(\w{20}).xml/)![1];
2020-03-19 04:36:02 +01:00
}
2020-03-19 05:20:28 +01:00
2020-03-21 16:58:28 +01:00
async function getFeed(identifier: string): Promise<string> {
return (await webClient.get(`/feeds/${identifier}.xml`)).data;
2020-03-19 05:20:28 +01:00
}