This commit is contained in:
Leandro Facchinetti 2020-03-20 15:03:31 -04:00
parent 792a592eda
commit c72a764c93
2 changed files with 15 additions and 26 deletions

View File

@ -22,13 +22,14 @@ const webApp = express()
)
)
.post("/", (req, res) => {
const inbox: Inbox = { name: req.body.name, token: newToken() };
fs.writeFileSync(feedPath(inbox.token), renderXML(Feed(inbox)));
const name = req.body.name;
const token = newToken();
fs.writeFileSync(feedPath(token), renderXML(Feed({ name, token })));
res.send(
renderHTML(
<Layout>
<h1>{name} Inbox Created</h1>
<Created inbox={inbox}></Created>
<Created token={token}></Created>
</Layout>
)
);
@ -103,11 +104,6 @@ if (process.env.NODE_ENV === "production") {
developmentEmailServer.listen(2525);
}
type Inbox = {
name: string;
token: string;
};
function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
@ -183,7 +179,7 @@ function Form() {
);
}
function Created({ inbox: { name, token } }: { inbox: Inbox }) {
function Created({ token }: { token: string }) {
return (
<>
<p>
@ -213,8 +209,7 @@ function Created({ inbox: { name, token } }: { inbox: Inbox }) {
);
}
function Feed(inbox: Inbox) {
const { name, token } = inbox;
function Feed({ name, token }: { name: string; token: string }) {
return {
feed: {
$: { xmlns: "http://www.w3.org/2005/Atom" },
@ -242,7 +237,7 @@ function Feed(inbox: Inbox) {
title: `${name}” Inbox Created`,
author: "Kill the Newsletter!",
content: ReactDOMServer.renderToStaticMarkup(
<Created inbox={inbox}></Created>
<Created token={token}></Created>
)
})
}
@ -280,7 +275,7 @@ function now(): string {
return new Date().toISOString();
}
export function feedPath(token: string): string {
function feedPath(token: string): string {
return `static/feeds/${token}.xml`;
}

View File

@ -1,18 +1,12 @@
import {
developmentWebServer,
developmentEmailServer,
feedPath,
feedEmail
} from ".";
import { developmentWebServer, developmentEmailServer, feedEmail } from ".";
import nodemailer from "nodemailer";
import axios from "axios";
import qs from "qs";
import fs from "fs";
test("create feed", async () => {
const token = await createFeed();
expect(readFeed(token)).toMatch("My Feed");
expect(await readFeed(token)).toMatch("My Feed");
});
describe("receive email", () => {
@ -30,7 +24,7 @@ describe("receive email", () => {
subject: "New Message",
html: "<p>HTML content</p>"
});
const feed = readFeed(token);
const feed = await readFeed(token);
expect(feed).toMatch("publisher@example.com");
expect(feed).toMatch("New Message");
expect(feed).toMatch("HTML content");
@ -44,7 +38,7 @@ describe("receive email", () => {
subject: "New Message",
text: "TEXT content"
});
const feed = readFeed(token);
const feed = await readFeed(token);
expect(feed).toMatch("TEXT content");
});
@ -57,7 +51,7 @@ describe("receive email", () => {
subject: "New Message",
text: `REPETITION ${repetition} `.repeat(10_000)
});
const feed = readFeed(token);
const feed = await readFeed(token);
expect(feed).toMatch("REPETITION 3");
expect(feed).not.toMatch("REPETITION 0");
}, 10_000);
@ -80,6 +74,6 @@ async function createFeed(): Promise<string> {
).data.match(/(\w{20}).xml/)![1];
}
function readFeed(token: string): string {
return fs.readFileSync(feedPath(token), "utf8");
async function readFeed(token: string): Promise<string> {
return (await axios.get(`http://localhost:8000/feeds/${token}.xml`)).data;
}