This commit is contained in:
Leandro Facchinetti 2021-03-14 23:09:27 +00:00
parent 9c4ea7a434
commit 1ed63f27ad
1 changed files with 20 additions and 39 deletions

View File

@ -23,7 +23,7 @@ test("Kill the Newsletter!", async () => {
prefixUrl: webApplication.get("url"), prefixUrl: webApplication.get("url"),
}); });
const emailClient = nodemailer.createTransport(webApplication.get("email")); const emailClient = nodemailer.createTransport(webApplication.get("email"));
const emailHost = new URL(webApplication.get("url")).hostname; const emailHostname = new URL(webApplication.get("url")).hostname;
// Create feed // Create feed
const create = (await webClient.post("", { form: { name: "A newsletter" } })) const create = (await webClient.post("", { form: { name: "A newsletter" } }))
@ -52,19 +52,20 @@ test("Kill the Newsletter!", async () => {
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for a second to test that the <updated> field will be updated await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait for a second to test that the <updated> field will be updated
await emailClient.sendMail({ await emailClient.sendMail({
from: "publisher@example.com", from: "publisher@example.com",
to: `${feedReference}@${emailHost}`, to: `${feedReference}@${emailHostname}`,
subject: "Test email with HTML", subject: "Test email with HTML",
html: html`<p>Some HTML</p>`, html: html`<p>Some HTML</p>`,
}); });
const feed = (await webClient.get(`feeds/${feedReference}.xml`)).body; const feedWithHTMLEntry = (await webClient.get(`feeds/${feedReference}.xml`))
expect(feed.match(/<updated>(.+?)<\/updated>/)![1]).not.toBe( .body;
expect(feedWithHTMLEntry.match(/<updated>(.+?)<\/updated>/)![1]).not.toBe(
feedOriginal.body.match(/<updated>(.+?)<\/updated>/)![1] feedOriginal.body.match(/<updated>(.+?)<\/updated>/)![1]
); );
expect(feed).toMatch( expect(feedWithHTMLEntry).toMatch(
html`<author><name>publisher@example.com</name></author>` html`<author><name>publisher@example.com</name></author>`
); );
expect(feed).toMatch(html`<title>Test email with HTML</title>`); expect(feedWithHTMLEntry).toMatch(html`<title>Test email with HTML</title>`);
expect(feed).toMatch( expect(feedWithHTMLEntry).toMatch(
// prettier-ignore // prettier-ignore
html`<content type="html">${`<p>Some HTML</p>`}\n</content>` html`<content type="html">${`<p>Some HTML</p>`}\n</content>`
); );
@ -72,7 +73,7 @@ test("Kill the Newsletter!", async () => {
// Test email with text // Test email with text
await emailClient.sendMail({ await emailClient.sendMail({
from: "publisher@example.com", from: "publisher@example.com",
to: `${feedReference}@${emailHost}`, to: `${feedReference}@${emailHostname}`,
subject: "Test email with text", subject: "Test email with text",
text: "A link: https://kill-the-newsletter.com", text: "A link: https://kill-the-newsletter.com",
}); });
@ -81,20 +82,20 @@ test("Kill the Newsletter!", async () => {
html`<content type="html">${`<p>A link: <a href="https://kill-the-newsletter.com">https://kill-the-newsletter.com</a></p>`}</content>` html`<content type="html">${`<p>A link: <a href="https://kill-the-newsletter.com">https://kill-the-newsletter.com</a></p>`}</content>`
); );
// Test email missing from // Test email missing fields
await emailClient.sendMail({ await emailClient.sendMail({
to: `${feedReference}@${emailHost}`, to: `${feedReference}@${emailHostname}`,
subject: "Test email missing from",
text: "A link: https://kill-the-newsletter.com",
}); });
expect((await webClient.get(`feeds/${feedReference}.xml`)).body).toMatch( const feedMissingFields = (await webClient.get(`feeds/${feedReference}.xml`))
html`<author><name></name></author>` .body;
); expect(feedMissingFields).toMatch(html`<author><name></name></author>`);
expect(feedMissingFields).toMatch(html`<title></title>`);
expect(feedMissingFields).toMatch(html`<content type="html"></content>`);
// Test email to nonexistent to (gets ignored) // Test email to nonexistent to (gets ignored)
await emailClient.sendMail({ await emailClient.sendMail({
from: "publisher@example.com", from: "publisher@example.com",
to: `nonexistent@${emailHost}`, to: `nonexistent@${emailHostname}`,
subject: "Test email to nonexistent to (gets ignored)", subject: "Test email to nonexistent to (gets ignored)",
text: "A link: https://kill-the-newsletter.com", text: "A link: https://kill-the-newsletter.com",
}); });
@ -102,31 +103,11 @@ test("Kill the Newsletter!", async () => {
"Test email to nonexistent to (gets ignored)" "Test email to nonexistent to (gets ignored)"
); );
// Test email missing subject
await emailClient.sendMail({
from: "publisher@example.com",
to: `${feedReference}@${emailHost}`,
text: "A link: https://kill-the-newsletter.com",
});
expect((await webClient.get(`feeds/${feedReference}.xml`)).body).toMatch(
html`<title></title>`
);
// Test email missing content
await emailClient.sendMail({
from: "publisher@example.com",
to: `${feedReference}@${emailHost}`,
subject: "Test email missing content",
});
expect((await webClient.get(`feeds/${feedReference}.xml`)).body).toMatch(
html`<content type="html"></content>`
);
// Test truncation // Test truncation
for (let index = 1; index <= 5; index++) for (let index = 1; index <= 5; index++)
await emailClient.sendMail({ await emailClient.sendMail({
from: "publisher@example.com", from: "publisher@example.com",
to: `${feedReference}@${emailHost}`, to: `${feedReference}@${emailHostname}`,
subject: `Test truncation: ${index}`, subject: `Test truncation: ${index}`,
text: `TRUNCATION ${index} `.repeat(10_000), text: `TRUNCATION ${index} `.repeat(10_000),
}); });
@ -138,7 +119,7 @@ test("Kill the Newsletter!", async () => {
// Test email thats too long // Test email thats too long
await emailClient.sendMail({ await emailClient.sendMail({
from: "publisher@example.com", from: "publisher@example.com",
to: `${feedReference}@${emailHost}`, to: `${feedReference}@${emailHostname}`,
subject: "Test email thats too long", subject: "Test email thats too long",
text: `TOO LONG `.repeat(100_000), text: `TOO LONG `.repeat(100_000),
}); });
@ -151,7 +132,7 @@ test("Kill the Newsletter!", async () => {
// Test email after truncation // Test email after truncation
await emailClient.sendMail({ await emailClient.sendMail({
from: "publisher@example.com", from: "publisher@example.com",
to: `${feedReference}@${emailHost}`, to: `${feedReference}@${emailHostname}`,
subject: "Test email after truncation", subject: "Test email after truncation",
text: "A link: https://kill-the-newsletter.com", text: "A link: https://kill-the-newsletter.com",
}); });