aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/config.test.ts38
-rw-r--r--src/util/config.ts53
2 files changed, 53 insertions, 38 deletions
diff --git a/src/util/config.test.ts b/src/util/config.test.ts
index b1e7df3..a065c47 100644
--- a/src/util/config.test.ts
+++ b/src/util/config.test.ts
@@ -179,6 +179,9 @@ describe("handleStartup", () => {
exit.mockClear();
});
+ const confStartup = (): Promise<MongoClient> =>
+ handleStartup("", "/apple", "/public", "localhost");
+
it("should pass back MongoClient", async () => {
const superClient = {} as MongoClient;
const fsAccess = jest.spyOn(fs.promises, "access").mockResolvedValue();
@@ -193,7 +196,7 @@ describe("handleStartup", () => {
(template: templates.Template) => new Promise(res => res(template))
);
- const result = await handleStartup();
+ const result = await confStartup();
expect(fsAccess).toHaveBeenCalledTimes(1);
expect(pathAbsolute).toHaveBeenCalledTimes(1);
@@ -209,16 +212,29 @@ describe("handleStartup", () => {
});
it("should exit if HOST_DIR is not read/write accessible", async () => {
+ const superClient = {} as MongoClient;
const fsAccess = jest.spyOn(fs.promises, "access").mockRejectedValue("boo");
const pathAbsolute = jest.spyOn(path, "isAbsolute").mockReturnValue(true);
const pathJoin = jest.spyOn(path, "join").mockReturnValue("path");
+ const mongoClient = jest.spyOn(MongoClient, "connect").mockImplementation(
+ () => new Promise<MongoClient>(res => res(superClient))
+ );
+ const processTemplate = jest
+ .spyOn(templates, "default")
+ .mockImplementation(
+ (template: templates.Template) => new Promise(res => res(template))
+ );
- const result = await handleStartup();
+ const result = await confStartup();
expect(fsAccess).toHaveBeenCalledTimes(1);
expect(exit).toHaveBeenCalledWith(1);
expect(pathAbsolute).not.toHaveBeenCalled();
+ expect(mongoClient).not.toHaveBeenCalled();
+ expect(processTemplate).not.toHaveBeenCalled();
expect(result).toBeUndefined();
+ processTemplate.mockRestore();
+ mongoClient.mockRestore();
pathAbsolute.mockRestore();
pathJoin.mockRestore();
fsAccess.mockRestore();
@@ -232,11 +248,21 @@ describe("handleStartup", () => {
const mongoClient = jest.spyOn(MongoClient, "connect").mockImplementation(
() => new Promise<MongoClient>(res => res(superClient))
);
+ const processTemplate = jest
+ .spyOn(templates, "default")
+ .mockImplementation(
+ (template: templates.Template) => new Promise(res => res(template))
+ );
- await handleStartup();
+ const result = await confStartup();
+ expect(fsAccess).toHaveBeenCalledTimes(1);
expect(pathAbsolute).toHaveBeenCalledTimes(1);
expect(exit).toHaveBeenCalledWith(1);
+ expect(mongoClient).not.toHaveBeenCalled();
+ expect(processTemplate).not.toHaveBeenCalled();
+ expect(result).toBeUndefined();
+ processTemplate.mockRestore();
mongoClient.mockRestore();
pathAbsolute.mockRestore();
pathJoin.mockRestore();
@@ -258,10 +284,14 @@ describe("handleStartup", () => {
(template: templates.Template) => new Promise(res => res(template))
);
- await handleStartup();
+ const result = await confStartup();
+ expect(fsAccess).toHaveBeenCalledTimes(1);
+ expect(pathAbsolute).toHaveBeenCalledTimes(1);
expect(mongoClient).toHaveBeenCalledTimes(1);
expect(exit).toHaveBeenCalledWith(1);
+ expect(processTemplate).not.toHaveBeenCalled();
+ expect(result).toBeUndefined();
processTemplate.mockRestore();
mongoClient.mockRestore();
pathAbsolute.mockRestore();
diff --git a/src/util/config.ts b/src/util/config.ts
index 195d85b..3eb8d1c 100644
--- a/src/util/config.ts
+++ b/src/util/config.ts
@@ -45,48 +45,33 @@ export const persistTemplate = async (input: Template): Promise<void> => {
}
};
-const MONGO_URI = configOrError("MONGO_URI");
-const TARGET_URL = process.env.TARGET_URL ?? "http://localhost:3000";
-const HOST_DIR = configOrError("HOST_DIR");
-
-export const handleStartup = async (): Promise<MongoClient> => {
+export const handleStartup = async (
+ mongoUri: string,
+ hostDir: string,
+ publicPath: string,
+ targetUrl: string
+): Promise<MongoClient> => {
try {
- await fs.promises.access(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK);
- if (!path.isAbsolute(HOST_DIR)) {
- logger.error("HOST_DIR must be an absolute path");
- process.exit(1);
+ await fs.promises.access(hostDir, fs.constants.R_OK | fs.constants.W_OK);
+ if (!path.isAbsolute(hostDir)) {
+ await Promise.reject("hostDir must be an absolute path");
}
- const mongo = await MongoClient.connect(MONGO_URI, {
+ const mongo = await MongoClient.connect(mongoUri, {
useUnifiedTopology: true
- }).catch((err: MongoError) => {
- logger.error(err.message ?? "Unable to connect to database");
- process.exit(1);
- });
+ }).catch((err: MongoError) =>
+ Promise.reject(err.message ?? "Unable to connect to database")
+ );
await persistTemplate({
- inputFile: path.join(
- __dirname,
- "..",
- "..",
- "public",
- "templates",
- "bash.template"
- ),
- outputFile: path.join(HOST_DIR, "bash"),
- context: { TARGET_URL }
+ inputFile: path.join(publicPath, "templates", "bash.template"),
+ outputFile: path.join(hostDir, "bash"),
+ context: { targetUrl }
} as Template);
await persistTemplate({
- inputFile: path.join(
- __dirname,
- "..",
- "..",
- "public",
- "templates",
- "index.html.template"
- ),
- outputFile: path.join(HOST_DIR, "index.html"),
- context: { TARGET_URL }
+ inputFile: path.join(publicPath, "templates", "index.html.template"),
+ outputFile: path.join(hostDir, "index.html"),
+ context: { targetUrl }
} as Template);
return mongo;