From 4d26825cbeda1c60761745867a1c4a03e23831ac Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 16 Oct 2021 01:33:44 +0800 Subject: [PATCH] [test] reset-password --- extra/reset-password.js | 62 ++++++++++++++++++++++++----------------- test/backend.spec.js | 17 +++++++---- 2 files changed, 48 insertions(+), 31 deletions(-) diff --git a/extra/reset-password.js b/extra/reset-password.js index be0395897..272351ed4 100644 --- a/extra/reset-password.js +++ b/extra/reset-password.js @@ -12,50 +12,60 @@ const rl = readline.createInterface({ output: process.stdout }); -(async () => { +const main = async () => { Database.init(args); await Database.connect(); try { const user = await R.findOne("user"); - if (! user) { - throw new Error("user not found, have you installed?"); - } - - console.log("Found user: " + user.username); - - while (true) { - let password = await question("New Password: "); - let confirmPassword = await question("Confirm New Password: "); - - if (password === confirmPassword) { - await user.resetPassword(password); - - // Reset all sessions by reset jwt secret - await initJWTSecret(); - - rl.close(); - break; - } else { - console.log("Passwords do not match, please try again."); + // No need to actually reset the password for testing, just make sure no connection problem. It is ok for now. + if (!process.env.TEST_BACKEND) { + if (! user) { + throw new Error("user not found, have you installed?"); } - } - console.log("Password reset successfully."); + console.log("Found user: " + user.username); + + while (true) { + let password = await question("New Password: "); + let confirmPassword = await question("Confirm New Password: "); + + if (password === confirmPassword) { + await user.resetPassword(password); + + // Reset all sessions by reset jwt secret + await initJWTSecret(); + + break; + } else { + console.log("Passwords do not match, please try again."); + } + } + console.log("Password reset successfully."); + } } catch (e) { console.error("Error: " + e.message); } await Database.close(); + rl.close(); - console.log("Finished. You should restart the Uptime Kuma server.") -})(); + console.log("Finished."); +}; function question(question) { return new Promise((resolve) => { rl.question(question, (answer) => { resolve(answer); - }) + }); }); } + +if (!process.env.TEST_BACKEND) { + main(); +} + +module.exports = { + main, +}; diff --git a/test/backend.spec.js b/test/backend.spec.js index ea04439a8..b47d375c5 100644 --- a/test/backend.spec.js +++ b/test/backend.spec.js @@ -1,11 +1,11 @@ -const { genSecret } = require("../src/util"); - -beforeAll(() => { - -}); +const { genSecret, sleep } = require("../src/util"); describe("Test genSecret", () => { + beforeAll(() => { + + }); + it("should be correct length", () => { let secret = genSecret(-1); expect(secret).toEqual(""); @@ -34,4 +34,11 @@ describe("Test genSecret", () => { expect(secret).toContain("A"); expect(secret).toContain("9"); }); + +}); + +describe("Test reset-password", () => { + it("should able to run", async () => { + await require("../extra/reset-password").main(); + }); });