forked from leafac/kill-the-newsletter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
272 lines (254 loc) · 9.25 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
html: "<p>HTML content</p>",
});
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: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
html: "<p>HTML content</p>",
});
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(
);
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: "[email protected]",
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: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
text: "TEXT content\n\nhttps://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://leafac.com"
);
});
test("invalid XML character in HTML", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
html: "<p>Invalid XML character (backspace): |\b|💩</p>",
});
const feed = await getFeed(identifier);
const entry = feed.querySelector("feed > entry:first-of-type")!;
expect(entry.querySelector("content")!.textContent).toMatchInlineSnapshot(`
"<p>Invalid XML character (backspace): ||💩</p>
"
`);
});
test("invalid XML character in text", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "[email protected]",
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(
`"<p>Invalid XML character (backspace): ||💩</p>"`
);
});
test("missing ‘from’", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
html: "<p>HTML content</p>",
});
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: "[email protected]",
to: `nonexistent@${EMAIL_DOMAIN}`,
subject: "New Message",
html: "<p>HTML content</p>",
});
});
test("missing ‘subject’", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
html: "<p>HTML content</p>",
});
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(
);
});
test("missing ‘content’", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "[email protected]",
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 Array<string>();
for (const repetition of [...new Array(4).keys()]) {
await emailClient.sendMail({
from: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
text: `REPETITION ${repetition} `.repeat(10_000),
});
const feed = await getFeed(identifier);
const entry = feed.querySelector("feed > entry:first-of-type")!;
alternatesURLs.push(entry.querySelector("link")!.getAttribute("href")!);
}
const feed = await getFeed(identifier);
expect(
feed.querySelector("entry:first-of-type > content")!.textContent
).toMatch("REPETITION 3");
expect(
feed.querySelector("entry:last-of-type > content")!.textContent
).toMatch("REPETITION 1");
expect((await getAlternate(alternatesURLs[3]!)).textContent).toMatch(
"REPETITION 3"
);
await expect(getAlternate(alternatesURLs[0]!)).rejects.toThrowError();
});
test("too big entry", async () => {
const identifier = await createFeed();
await emailClient.sendMail({
from: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
text: "TOO BIG".repeat(100_000),
});
expect((await getFeed(identifier)).querySelector("entry")).toBeNull();
await emailClient.sendMail({
from: "[email protected]",
to: `${identifier}@${EMAIL_DOMAIN}`,
subject: "New Message",
text: `NORMAL SIZE`,
});
expect(
(await getFeed(identifier)).querySelector("entry > content")!.textContent
).toMatchInlineSnapshot(`"<p>NORMAL SIZE</p>"`);
});
});
test("‘noindex’ header", async () => {
const identifier = await createFeed();
const feed = await getFeed(identifier);
const entry = feed.querySelector("feed > entry:first-of-type")!;
const alternatePath = entry.querySelector("link")!.getAttribute("href")!;
expect((await webClient.get(`/`)).headers["x-robots-tag"]).toBeUndefined();
expect(
(await webClient.get(`/feeds/${identifier}.xml`)).headers["x-robots-tag"]
).toBe("noindex");
expect((await webClient.get(alternatePath)).headers["x-robots-tag"]).toBe(
"noindex"
);
});
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<string> {
return JSDOM.fragment(
(
await webClient.post(
"/",
qs.stringify({
name: "My Feed",
})
)
).data
)
.querySelector("code")!
.textContent!.split("@")[0];
}
async function getFeed(identifier: string): Promise<Document> {
return new JSDOM((await webClient.get(`/feeds/${identifier}.xml`)).data, {
contentType: "text/xml",
}).window.document;
}
async function getAlternate(url: string): Promise<DocumentFragment> {
return JSDOM.fragment((await webClient.get(url)).data);
}