import { webServer, emailServer, BASE_URL, EMAIL_DOMAIN, EMAIL_PORT } from "."; import nodemailer from "nodemailer"; import axios from "axios"; import qs from "qs"; import { JSDOM } from "jsdom"; test("create feed", async () => { const identifier = await createFeed(); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; const alternate = await getAlternate( entry.querySelector("link")!.getAttribute("href")! ); expect(feed.querySelector("feed > title")!.textContent).toBe("My Feed"); expect(entry.querySelector("title")!.textContent).toBe( "“My Feed” Inbox Created" ); expect(alternate.querySelector("p")!.textContent).toMatch( "Sign up for the newsletter with" ); }); describe("receive email", () => { test("‘updated’ field is updated", async () => { const identifier = await createFeed(); const before = await getFeed(identifier); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "
HTML content
", }); const after = await getFeed(identifier); expect(after.querySelector("feed > updated")!.textContent).not.toBe( before.querySelector("feed > updated")!.textContent ); }); test("HTML content", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "HTML content
", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; const alternate = await getAlternate( entry.querySelector("link")!.getAttribute("href")! ); expect(entry.querySelector("author > name")!.textContent).toBe( "publisher@example.com" ); expect(entry.querySelector("title")!.textContent).toBe("New Message"); expect(entry.querySelector("content")!.textContent).toMatch("HTML content"); expect(alternate.querySelector("p")!.textContent).toMatch("HTML content"); }); test("text content", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", text: "TEXT content", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; const alternate = await getAlternate( entry.querySelector("link")!.getAttribute("href")! ); expect(entry.querySelector("content")!.textContent).toMatch("TEXT content"); expect(alternate.querySelector("p")!.textContent).toMatch("TEXT content"); }); test("rich text content", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", text: "TEXT content\n\nhttps://www.leafac.com\n\nMore text", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; const alternate = await getAlternate( entry.querySelector("link")!.getAttribute("href")! ); expect(alternate.querySelector("a")!.getAttribute("href")).toBe( "https://www.leafac.com" ); }); test("invalid XML character in HTML", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "Invalid XML character (backspace): |\b|💩
", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; expect(entry.querySelector("content")!.textContent).toMatchInlineSnapshot(` "Invalid XML character (backspace): ||💩
" `); }); test("invalid XML character in text", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", text: "Invalid XML character (backspace): |\b|💩", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; expect(entry.querySelector("content")!.textContent).toMatchInlineSnapshot( `"Invalid XML character (backspace): ||💩
"` ); }); test("missing ‘from’", async () => { const identifier = await createFeed(); await emailClient.sendMail({ to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "HTML content
", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; expect(entry.querySelector("author > name")!.textContent).toBe(""); expect(entry.querySelector("title")!.textContent).toBe("New Message"); }); test("nonexistent ‘to’", async () => { await emailClient.sendMail({ from: "publisher@example.com", to: `nonexistent@${EMAIL_DOMAIN}`, subject: "New Message", html: "HTML content
", }); }); test("missing ‘subject’", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, html: "HTML content
", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; expect(entry.querySelector("title")!.textContent).toBe(""); expect(entry.querySelector("author > name")!.textContent).toBe( "publisher@example.com" ); }); test("missing ‘content’", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", }); const feed = await getFeed(identifier); const entry = feed.querySelector("feed > entry:first-of-type")!; expect(entry.querySelector("content")!.textContent!.trim()).toBe(""); expect(entry.querySelector("title")!.textContent).toBe("New Message"); }); test("truncation", async () => { const identifier = await createFeed(); const alternatesURLs = new ArrayNORMAL SIZE
"`); }); }); const webClient = axios.create({ baseURL: BASE_URL, }); const emailClient = nodemailer.createTransport( `smtp://${EMAIL_DOMAIN}:${EMAIL_PORT}` ); afterAll(() => { webServer.close(); emailServer.close(); }); async function createFeed(): Promise