uptime-kuma/test/e2e.spec.js

313 lines
10 KiB
JavaScript
Raw Normal View History

2021-10-05 02:39:44 -07:00
// eslint-disable-next-line no-unused-vars
2021-10-06 00:25:00 -07:00
const { Page, Browser } = require("puppeteer");
2021-10-05 05:27:43 -07:00
const { sleep } = require("../src/util");
2021-10-06 00:25:00 -07:00
const axios = require("axios");
2021-10-05 02:39:44 -07:00
/**
* Set back the correct data type for page object
* @type {Page}
*/
page;
2021-10-03 03:16:55 -07:00
2021-10-06 00:25:00 -07:00
/**
* @type {Browser}
*/
browser;
2021-10-05 02:39:44 -07:00
2021-10-06 00:25:00 -07:00
beforeAll(async () => {
await page.setViewport({
width: 1280,
height: 720,
deviceScaleFactor: 1,
});
2021-10-03 03:16:55 -07:00
});
afterAll(() => {
2021-10-05 01:03:35 -07:00
2021-10-03 03:16:55 -07:00
});
2021-10-05 05:27:43 -07:00
const baseURL = "http://127.0.0.1:3002";
2021-10-05 02:39:44 -07:00
describe("Init", () => {
2021-10-03 03:16:55 -07:00
const title = "Uptime Kuma";
beforeAll(async () => {
2021-10-05 05:27:43 -07:00
await page.goto(baseURL);
2021-10-03 03:16:55 -07:00
});
it(`should be titled "${title}"`, async () => {
2021-10-08 00:11:50 -07:00
await expect(page.title()).resolves.toEqual(title);
2021-10-03 03:16:55 -07:00
});
2021-10-05 01:03:35 -07:00
2021-10-06 00:25:00 -07:00
// Setup Page
2021-10-05 02:39:44 -07:00
it("Setup", async () => {
// Create an Admin
await page.waitForSelector("#floatingInput");
await page.waitForSelector("#repeat");
await page.click("#floatingInput");
await page.type("#floatingInput", "admin");
await page.type("#floatingPassword", "admin123");
await page.type("#repeat", "admin123");
await page.click(".btn-primary[type=submit]");
2021-10-05 05:27:43 -07:00
await sleep(3000);
2021-10-05 02:39:44 -07:00
// Go to /setup again
2021-10-05 05:27:43 -07:00
await page.goto(baseURL + "/setup");
await sleep(3000);
2021-10-06 00:25:00 -07:00
let pathname = await page.evaluate(() => location.pathname);
2021-10-05 02:39:44 -07:00
expect(pathname).toEqual("/dashboard");
// Go to /
2021-10-05 05:27:43 -07:00
await page.goto(baseURL);
await page.waitForSelector("h1.mb-3");
2021-10-06 00:25:00 -07:00
pathname = await page.evaluate(() => location.pathname);
2021-10-05 02:39:44 -07:00
expect(pathname).toEqual("/dashboard");
});
it("should create monitor", async () => {
// Create monitor
await page.goto(baseURL + "/add");
await page.waitForSelector("#name");
await page.type("#name", "Myself");
await page.waitForSelector("#url");
await page.click("#url", { clickCount: 3 });
await page.keyboard.type(baseURL);
await page.keyboard.press("Enter");
await page.waitForFunction(() => {
const badge = document.querySelector("span.badge");
return badge && badge.innerText == "100%";
}, { timeout: 5000 });
});
2021-10-06 00:25:00 -07:00
// Settings Page
/*
2021-10-05 05:27:43 -07:00
describe("Settings", () => {
beforeEach(async () => {
2021-10-05 05:27:43 -07:00
await page.goto(baseURL + "/settings");
});
2021-10-05 02:39:44 -07:00
2021-10-05 05:27:43 -07:00
it("Change Language", async () => {
await page.goto(baseURL + "/settings/appearance");
2021-10-06 00:34:57 -07:00
await page.waitForSelector("#language");
2021-10-05 05:27:43 -07:00
await page.select("#language", "zh-HK");
let languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
2021-10-08 00:11:50 -07:00
expect(languageTitle).toEqual("語言");
2021-10-05 01:03:35 -07:00
2021-10-05 05:27:43 -07:00
await page.select("#language", "en");
languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
2021-10-08 00:11:50 -07:00
expect(languageTitle).toEqual("Language");
2021-10-05 05:27:43 -07:00
});
it("Change Theme", async () => {
await page.goto(baseURL + "/settings/appearance");
2021-10-06 00:25:00 -07:00
// Dark
await click(page, ".btn[for=btncheck2]");
await page.waitForSelector("div.dark");
await page.waitForSelector(".btn[for=btncheck1]");
2021-10-06 00:25:00 -07:00
2021-10-05 05:27:43 -07:00
// Light
2021-10-06 00:25:00 -07:00
await click(page, ".btn[for=btncheck1]");
2021-10-05 22:26:43 -07:00
await page.waitForSelector("div.light");
2021-10-06 00:25:00 -07:00
});
2021-10-05 05:27:43 -07:00
it("Change Heartbeat Bar Style", async () => {
await page.goto(baseURL + "/settings/appearance");
// Bottom
await click(page, ".btn[for=btncheck5]");
await page.waitForSelector("div.hp-bar-big");
// None
await click(page, ".btn[for=btncheck6]");
await page.waitForSelector("div.hp-bar-big", {
hidden: true,
timeout: 1000
});
});
2021-10-06 00:25:00 -07:00
// TODO: Timezone
it("Search Engine Visibility", async () => {
// Default
let res = await axios.get(baseURL + "/robots.txt");
2021-10-08 00:11:50 -07:00
expect(res.data).toContain("Disallow: /");
2021-10-06 00:25:00 -07:00
// Yes
await click(page, "#searchEngineIndexYes");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
2021-10-06 00:25:00 -07:00
res = await axios.get(baseURL + "/robots.txt");
2021-10-08 00:11:50 -07:00
expect(res.data).not.toContain("Disallow: /");
2021-10-06 00:25:00 -07:00
// No
await click(page, "#searchEngineIndexNo");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
2021-10-06 00:25:00 -07:00
res = await axios.get(baseURL + "/robots.txt");
2021-10-08 00:11:50 -07:00
expect(res.data).toContain("Disallow: /");
2021-10-06 00:25:00 -07:00
});
it("Entry Page", async () => {
const newPage = await browser.newPage();
// Default
await newPage.goto(baseURL);
await newPage.waitForSelector("h1.mb-3", { timeout: 3000 });
2021-10-06 00:25:00 -07:00
let pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
// Status Page
await click(page, "#entryPageNo");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
2021-10-06 00:25:00 -07:00
await newPage.goto(baseURL);
await newPage.waitForSelector("img.logo", { timeout: 3000 });
2021-10-06 00:25:00 -07:00
pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/status");
// Back to Dashboard
await click(page, "#entryPageYes");
await click(page, "form > div > .btn[type=submit]");
await sleep(1000);
2021-10-06 00:25:00 -07:00
await newPage.goto(baseURL);
await newPage.waitForSelector("h1.mb-3", { timeout: 3000 });
2021-10-06 00:25:00 -07:00
pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
await newPage.close();
});
it("Change Password (wrong current password)", async () => {
await page.goto(baseURL + "/settings/security");
2021-10-07 02:48:13 -07:00
await page.waitForSelector("#current-password");
2021-10-06 00:25:00 -07:00
await page.type("#current-password", "wrong_passw$$d");
await page.type("#new-password", "new_password123");
await page.type("#repeat-new-password", "new_password123");
2021-10-07 03:10:35 -07:00
// Save
await click(page, "form > div > .btn[type=submit]", 0);
await sleep(1000);
2021-10-07 03:10:35 -07:00
await click(page, "#logout-btn");
2021-10-06 00:25:00 -07:00
await login("admin", "new_password123");
let elementCount = await page.evaluate(() => document.querySelectorAll("#floatingPassword").length);
expect(elementCount).toEqual(1);
await login("admin", "admin123");
});
it("Change Password (wrong repeat)", async () => {
await page.goto(baseURL + "/settings/security");
2021-10-07 02:48:13 -07:00
await page.waitForSelector("#current-password");
2021-10-06 00:25:00 -07:00
await page.type("#current-password", "admin123");
await page.type("#new-password", "new_password123");
await page.type("#repeat-new-password", "new_password1234567898797898");
2021-10-07 03:10:35 -07:00
await click(page, "form > div > .btn[type=submit]", 0);
await sleep(1000);
2021-10-07 03:10:35 -07:00
await click(page, "#logout-btn");
2021-10-06 00:25:00 -07:00
await login("admin", "new_password123");
2021-10-07 03:10:35 -07:00
2021-10-06 00:25:00 -07:00
let elementCount = await page.evaluate(() => document.querySelectorAll("#floatingPassword").length);
expect(elementCount).toEqual(1);
await login("admin", "admin123");
await page.waitForSelector("#current-password");
let pathname = await page.evaluate(() => location.pathname);
expect(pathname).toEqual("/settings/security");
2021-10-05 05:27:43 -07:00
});
2021-10-06 00:25:00 -07:00
// TODO: 2FA
// TODO: Export Backup
// TODO: Import Backup
it("Should disable & enable auth", async () => {
await page.goto(baseURL + "/settings/security");
await click(page, "#disableAuth-btn");
await click(page, ".btn.btn-danger[data-bs-dismiss='modal']", 2); // Not a good way to do it
await page.waitForSelector("#enableAuth-btn", { timeout: 3000 });
await page.waitForSelector("#logout-btn", {
hidden: true,
timeout: 3000
});
const newPage = await browser.newPage();
await newPage.goto(baseURL);
await newPage.waitForSelector("span.badge", { timeout: 3000 });
newPage.close();
await click(page, "#enableAuth-btn");
await login("admin", "admin123");
await page.waitForSelector("#disableAuth-btn", { timeout: 3000 });
});
2021-10-06 00:25:00 -07:00
2021-11-16 18:52:14 -08:00
// it("Should clear all statistics", async () => {
// await page.goto(baseURL + "/settings/monitor-history");
// await click(page, "#clearAllStats-btn");
// await click(page, ".btn.btn-danger");
// await page.waitForFunction(() => {
// const badge = document.querySelector("span.badge");
// return badge && badge.innerText == "0%";
// }, { timeout: 3000 });
// });
2021-10-05 01:03:35 -07:00
});
*/
2021-10-05 05:27:43 -07:00
2021-10-06 00:25:00 -07:00
/*
* TODO
* Create Monitor - All type
* Edit Monitor
* Delete Monitor
*
* Create Notification (token problem, maybe hard to test)
*
*/
2021-10-05 05:27:43 -07:00
describe("Status Page", () => {
const title = "Uptime Kuma";
beforeAll(async () => {
await page.goto(baseURL + "/status");
});
it(`should be titled "${title}"`, async () => {
2021-10-08 00:11:50 -07:00
await expect(page.title()).resolves.toEqual(title);
2021-10-05 05:27:43 -07:00
});
2021-10-05 01:03:35 -07:00
});
2021-10-03 03:16:55 -07:00
});
2021-10-06 00:25:00 -07:00
async function login(username, password) {
await input(page, "#floatingInput", username);
await input(page, "#floatingPassword", password);
await page.click(".btn-primary[type=submit]");
2021-10-07 03:10:35 -07:00
await sleep(5000);
2021-10-06 00:25:00 -07:00
}
async function click(page, selector, elementIndex = 0) {
2021-10-07 03:10:35 -07:00
await page.waitForSelector(selector, {
timeout: 5000,
});
2021-10-06 00:25:00 -07:00
return await page.evaluate((s, i) => {
return document.querySelectorAll(s)[i].click();
}, selector, elementIndex);
}
async function input(page, selector, text) {
2021-10-07 03:10:35 -07:00
await page.waitForSelector(selector, {
timeout: 5000,
});
2021-10-06 00:25:00 -07:00
const element = await page.$(selector);
await element.click({ clickCount: 3 });
await page.keyboard.press("Backspace");
await page.type(selector, text);
}