This commit is contained in:
Leandro Facchinetti 2021-03-13 22:35:02 +00:00
parent 4951bf4b74
commit 400dc14e54
1 changed files with 53 additions and 62 deletions

View File

@ -13,89 +13,80 @@ EOF
curl smtp://localhost:2525 --mail-from publisher@example.com --mail-rcpt ru9rmeebswmcy7wx@localhost --upload-file /tmp/example-email.txt curl smtp://localhost:2525 --mail-from publisher@example.com --mail-rcpt ru9rmeebswmcy7wx@localhost --upload-file /tmp/example-email.txt
*/ */
import { beforeAll, afterAll, describe, test, expect } from "@jest/globals"; import { test, expect } from "@jest/globals";
import os from "os"; import os from "os";
import path from "path"; import path from "path";
import http from "http";
import net from "net";
import fs from "fs"; import fs from "fs";
import * as got from "got"; import * as got from "got";
import nodemailer from "nodemailer"; import nodemailer from "nodemailer";
import html from "@leafac/html"; import html from "@leafac/html";
import killTheNewsletter from "."; import killTheNewsletter from ".";
let webServer: http.Server; test("Kill the Newsletter!", async () => {
let emailServer: net.Server; // Start servers
let webClient: got.Got;
let emailClient: nodemailer.Transporter;
let emailHost: string;
beforeAll(() => {
const rootDirectory = fs.mkdtempSync( const rootDirectory = fs.mkdtempSync(
path.join(os.tmpdir(), "kill-the-newsletter--test--") path.join(os.tmpdir(), "kill-the-newsletter--test--")
); );
const { webApplication, emailApplication } = killTheNewsletter(rootDirectory); const { webApplication, emailApplication } = killTheNewsletter(rootDirectory);
webServer = webApplication.listen(new URL(webApplication.get("url")).port); const webServer = webApplication.listen(
emailServer = emailApplication.listen( new URL(webApplication.get("url")).port
);
const emailServer = emailApplication.listen(
new URL(webApplication.get("email")).port new URL(webApplication.get("email")).port
); );
webClient = got.default.extend({ prefixUrl: webApplication.get("url") }); const webClient = got.default.extend({
emailClient = nodemailer.createTransport(webApplication.get("email")); prefixUrl: webApplication.get("url"),
emailHost = new URL(webApplication.get("url")).hostname; });
}); const emailClient = nodemailer.createTransport(webApplication.get("email"));
afterAll(() => { const emailHost = new URL(webApplication.get("url")).hostname;
webServer.close();
emailServer.close();
});
test("Create feed", async () => { // Create feed
const createResponseBody = ( const create = (await webClient.post("", { form: { name: "A newsletter" } }))
await webClient.post("", { form: { name: "A newsletter" } }) .body;
).body; expect(create).toMatch(`“A newsletter” inbox created`);
expect(createResponseBody).toMatch(`“A newsletter” inbox created`); const feedReference = create.match(/\/feeds\/([a-z0-9]{16})\.xml/)![1];
const feedReference = createResponseBody.match(
/\/feeds\/([a-z0-9]{16})\.xml/ // Test feed properties
)![1]; let feedOriginal = await webClient.get(`feeds/${feedReference}.xml`);
const feedResponse = await webClient.get(`feeds/${feedReference}.xml`); expect(feedOriginal.headers["content-type"]).toMatch("application/atom+xml");
expect(feedResponse.headers["content-type"]).toMatch("application/atom+xml"); expect(feedOriginal.headers["x-robots-tag"]).toBe("noindex");
expect(feedResponse.headers["x-robots-tag"]).toBe("noindex"); expect(feedOriginal.body).toMatch(html`<title>A newsletter</title>`);
expect(feedResponse.body).toMatch(html`<title>A newsletter</title>`);
const alternateReference = feedResponse.body.match( // Test alternate
const alternateReference = feedOriginal.body.match(
/\/alternates\/([a-z0-9]{16})\.html/ /\/alternates\/([a-z0-9]{16})\.html/
)![1]; )![1];
const alternateResponse = await webClient.get( const alternate = await webClient.get(
`alternates/${alternateReference}.html` `alternates/${alternateReference}.html`
); );
expect(alternateResponse.headers["content-type"]).toMatch("text/html"); expect(alternate.headers["content-type"]).toMatch("text/html");
expect(alternateResponse.headers["x-robots-tag"]).toBe("noindex"); expect(alternate.headers["x-robots-tag"]).toBe("noindex");
expect(alternateResponse.body).toMatch(`Enjoy your readings!`); expect(alternate.body).toMatch(`Enjoy your readings!`);
});
describe("Receive email", () => { // Test email with HTML
test("HTML content", async () => { await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for a second to test that the <updated> field will be updated
const feedReference = ( await emailClient.sendMail({
await webClient.post("", { form: { name: "A newsletter" } }) from: "publisher@example.com",
).body.match(/\/feeds\/([a-z0-9]{16})\.xml/)![1]; to: `${feedReference}@${emailHost}`,
const feedBefore = (await webClient.get(`feeds/${feedReference}.xml`)).body; subject: "A subject",
await new Promise((resolve) => setTimeout(resolve, 1000)); html: html`<p>Some HTML content</p>`,
await emailClient.sendMail({
from: "publisher@example.com",
to: `${feedReference}@${emailHost}`,
subject: "A subject",
html: html`<p>Some HTML content</p>`,
});
const feed = (await webClient.get(`feeds/${feedReference}.xml`)).body;
expect(feed.match(/<updated>(.+?)<\/updated>/)![1]).not.toBe(
feedBefore.match(/<updated>(.+?)<\/updated>/)![1]
);
expect(feed).toMatch(
html`<author><name>publisher@example.com</name></author>`
);
expect(feed).toMatch(html`<title>A subject</title>`);
expect(feed).toMatch(
// prettier-ignore
html`<content type="html">${`<p>Some HTML content</p>`}\n</content>`
);
}); });
let feed = (await webClient.get(`feeds/${feedReference}.xml`)).body;
expect(feed.match(/<updated>(.+?)<\/updated>/)![1]).not.toBe(
feedOriginal.body.match(/<updated>(.+?)<\/updated>/)![1]
);
expect(feed).toMatch(
html`<author><name>publisher@example.com</name></author>`
);
expect(feed).toMatch(html`<title>A subject</title>`);
expect(feed).toMatch(
// prettier-ignore
html`<content type="html">${`<p>Some HTML content</p>`}\n</content>`
);
// Stop servers
webServer.close();
emailServer.close();
}); });
/* /*