From 7298c7f63fdf519c32190a3881beab5ff1540b4e Mon Sep 17 00:00:00 2001 From: Leandro Facchinetti Date: Sat, 13 Mar 2021 10:45:13 +0000 Subject: [PATCH] --- configuration.local.js | 6 ++---- src/index.test.ts | 39 +++++++++++++++++++++++++++++---------- src/index.ts | 10 +++++----- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/configuration.local.js b/configuration.local.js index 4151754..1efcd1f 100644 --- a/configuration.local.js +++ b/configuration.local.js @@ -6,9 +6,7 @@ module.exports = (require) => { console.log(`Web server started at ${webApplication.get("url")}`); }); - emailApplication.listen(webApplication.get("email port"), () => { - console.log( - `Email server started on port ${webApplication.get("email port")}` - ); + emailApplication.listen(new URL(webApplication.get("email")).port, () => { + console.log(`Email server started at port ${webApplication.get("email")}`); }); }; diff --git a/src/index.test.ts b/src/index.test.ts index 8c063e1..4aa7fcb 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -1,8 +1,33 @@ -import { webServer, emailServer, BASE_URL, EMAIL_DOMAIN, EMAIL_PORT } from "."; +import { beforeAll, afterAll, describe, test, expect } from "@jest/globals"; +import os from "os"; +import path from "path"; +import http from "http"; +import net from "net"; +import fs from "fs"; +import * as got from "got"; import nodemailer from "nodemailer"; -import axios from "axios"; -import qs from "qs"; -import { JSDOM } from "jsdom"; +import killTheNewsletter from "."; + +let webServer: http.Server; +let emailServer: net.Server; +let webClient: got.Got; +let emailClient: nodemailer.Transporter; +beforeAll(() => { + const rootDirectory = fs.mkdtempSync( + path.join(os.tmpdir(), "kill-the-newsletter--test--") + ); + const { webApplication, emailApplication } = killTheNewsletter(rootDirectory); + webServer = webApplication.listen(new URL(webApplication.get("url")).port); + emailServer = emailApplication.listen( + new URL(webApplication.get("email")).port + ); + webClient = got.default.extend({ prefixUrl: webApplication.get("url") }); + emailClient = nodemailer.createTransport(webApplication.get("email")); +}); +afterAll(() => { + webServer.close(); + emailServer.close(); +}); test("create feed", async () => { const identifier = await createFeed(); @@ -235,12 +260,6 @@ test("‘noindex’ header", async () => { ); }); -const webClient = axios.create({ - baseURL: BASE_URL, -}); -const emailClient = nodemailer.createTransport( - `smtp://${EMAIL_DOMAIN}:${EMAIL_PORT}` -); afterAll(() => { webServer.close(); emailServer.close(); diff --git a/src/index.ts b/src/index.ts index faea50c..5fc37ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,8 +20,7 @@ export default function killTheNewsletter( const webApplication = express(); webApplication.set("url", "http://localhost:4000"); - webApplication.set("email port", 2525); - webApplication.set("email host", "localhost"); + webApplication.set("email", "smtp://localhost:2525"); webApplication.set("administrator", "mailto:kill-the-newsletter@leafac.com"); fs.ensureDirSync(rootDirectory); @@ -287,7 +286,8 @@ export default function killTheNewsletter(

Sign up for the newsletter with
${feedReference}@${webApplication.get("email host")}${feedReference}@${new URL(webApplication.get("email")) + .hostname}

@@ -376,7 +376,7 @@ export default function killTheNewsletter( ${feed.title} Kill the Newsletter! Inbox: - ${feedReference}@${webApplication.get("email host")} → + ${feedReference}@${new URL(webApplication.get("email")).hostname} → ${webApplication.get("url")}/feeds/${feedReference}.xml ${new Date(feed.updatedAt).toISOString()} @@ -443,7 +443,7 @@ export default function killTheNewsletter( disabledCommands: ["AUTH", "STARTTLS"], async onData(stream, session, callback) { try { - const atHost = "@" + webApplication.get("email host"); + const atHost = "@" + new URL(webApplication.get("email")).hostname; const email = await mailparser.simpleParser(stream); const from = email.from?.text ?? ""; const subject = email.subject ?? "";