uptime-kuma/test/e2e.spec.js

251 lines
7.6 KiB
JavaScript
Raw Normal View History

2021-10-05 10:39:44 +01:00
// eslint-disable-next-line no-unused-vars
2021-10-06 08:25:00 +01:00
const { Page, Browser } = require("puppeteer");
2021-10-05 13:27:43 +01:00
const { sleep } = require("../src/util");
2021-10-06 08:25:00 +01:00
const axios = require("axios");
2021-10-05 10:39:44 +01:00
/**
* Set back the correct data type for page object
* @type {Page}
*/
page;
2021-10-03 11:16:55 +01:00
2021-10-06 08:25:00 +01:00
/**
* @type {Browser}
*/
browser;
2021-10-05 10:39:44 +01:00
2021-10-06 08:25:00 +01:00
beforeAll(async () => {
await page.setViewport({
width: 1280,
height: 720,
deviceScaleFactor: 1,
});
2021-10-03 11:16:55 +01:00
});
afterAll(() => {
2021-10-05 09:03:35 +01:00
2021-10-03 11:16:55 +01:00
});
2021-10-05 13:27:43 +01:00
const baseURL = "http://127.0.0.1:3002";
2021-10-05 10:39:44 +01:00
describe("Init", () => {
2021-10-03 11:16:55 +01:00
const title = "Uptime Kuma";
beforeAll(async () => {
2021-10-05 13:27:43 +01:00
await page.goto(baseURL);
2021-10-03 11:16:55 +01:00
});
it(`should be titled "${title}"`, async () => {
2021-10-08 08:11:50 +01:00
await expect(page.title()).resolves.toEqual(title);
2021-10-03 11:16:55 +01:00
});
2021-10-05 09:03:35 +01:00
2021-10-06 08:25:00 +01:00
// Setup Page
2021-10-05 10:39:44 +01: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 13:27:43 +01:00
await sleep(3000);
2021-10-05 10:39:44 +01:00
// Go to /setup again
2021-10-05 13:27:43 +01:00
await page.goto(baseURL + "/setup");
await sleep(3000);
2021-10-06 08:25:00 +01:00
let pathname = await page.evaluate(() => location.pathname);
2021-10-05 10:39:44 +01:00
expect(pathname).toEqual("/dashboard");
// Go to /
2021-10-05 13:27:43 +01:00
await page.goto(baseURL);
2021-10-06 08:25:00 +01:00
await sleep(3000);
pathname = await page.evaluate(() => location.pathname);
2021-10-05 10:39:44 +01:00
expect(pathname).toEqual("/dashboard");
});
2021-10-06 08:25:00 +01:00
// Settings Page
2021-10-05 13:27:43 +01:00
describe("Settings", () => {
beforeAll(async () => {
await page.goto(baseURL + "/settings");
});
2021-10-05 10:39:44 +01:00
2021-10-05 13:27:43 +01:00
it("Change Language", async () => {
2021-10-06 08:34:57 +01:00
await page.waitForSelector("#language");
2021-10-05 13:27:43 +01:00
await page.select("#language", "zh-HK");
let languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
2021-10-08 08:11:50 +01:00
expect(languageTitle).toEqual("語言");
2021-10-05 09:03:35 +01:00
2021-10-05 13:27:43 +01:00
await page.select("#language", "en");
languageTitle = await page.evaluate(() => document.querySelector("[for=language]").innerText);
2021-10-08 08:11:50 +01:00
expect(languageTitle).toEqual("Language");
2021-10-05 13:27:43 +01:00
});
it("Change Theme", async () => {
2021-10-06 08:25:00 +01:00
await sleep(1000);
// Dark
await click(page, ".btn[for=btncheck2]");
await page.waitForSelector("div.dark");
await sleep(1000);
2021-10-05 13:27:43 +01:00
// Light
2021-10-06 08:25:00 +01:00
await click(page, ".btn[for=btncheck1]");
2021-10-06 06:26:43 +01:00
await page.waitForSelector("div.light");
2021-10-06 08:25:00 +01:00
});
2021-10-05 13:27:43 +01:00
2021-10-06 08:25:00 +01:00
// TODO: Heartbeat Bar Style
// TODO: Timezone
it("Search Engine Visibility", async () => {
// Default
let res = await axios.get(baseURL + "/robots.txt");
2021-10-08 08:11:50 +01:00
expect(res.data).toContain("Disallow: /");
2021-10-06 08:25:00 +01:00
// Yes
await click(page, "#searchEngineIndexYes");
await click(page, "form > div > .btn[type=submit]");
await sleep(2000);
res = await axios.get(baseURL + "/robots.txt");
2021-10-08 08:11:50 +01:00
expect(res.data).not.toContain("Disallow: /");
2021-10-06 08:25:00 +01:00
// No
await click(page, "#searchEngineIndexNo");
await click(page, "form > div > .btn[type=submit]");
await sleep(2000);
res = await axios.get(baseURL + "/robots.txt");
2021-10-08 08:11:50 +01:00
expect(res.data).toContain("Disallow: /");
2021-10-06 08:25:00 +01:00
});
it("Entry Page", async () => {
const newPage = await browser.newPage();
// Default
await newPage.goto(baseURL);
await sleep(3000);
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]");
2021-10-07 11:10:35 +01:00
await sleep(4000);
2021-10-06 08:25:00 +01:00
await newPage.goto(baseURL);
2021-10-07 11:10:35 +01:00
await sleep(4000);
2021-10-06 08:25:00 +01: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]");
2021-10-07 11:10:35 +01:00
await sleep(4000);
2021-10-06 08:25:00 +01:00
await newPage.goto(baseURL);
2021-10-07 11:10:35 +01:00
await sleep(4000);
2021-10-06 08:25:00 +01:00
pathname = await newPage.evaluate(() => location.pathname);
expect(pathname).toEqual("/dashboard");
await newPage.close();
});
it("Change Password (wrong current password)", async () => {
2021-10-07 10:48:13 +01:00
await page.goto(baseURL + "/settings");
await page.waitForSelector("#current-password");
2021-10-06 08:25:00 +01: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 11:10:35 +01:00
// Save
2021-10-06 08:25:00 +01:00
await click(page, "form > div > .btn[type=submit]", 1);
2021-10-07 11:10:35 +01:00
await sleep(4000);
await click(page, ".btn-danger.btn.me-2");
2021-10-06 08:25:00 +01: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 () => {
2021-10-07 10:48:13 +01:00
await page.goto(baseURL + "/settings");
await page.waitForSelector("#current-password");
2021-10-06 08:25:00 +01: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 11:10:35 +01:00
2021-10-06 08:25:00 +01:00
await click(page, "form > div > .btn[type=submit]", 1);
2021-10-07 11:10:35 +01:00
await sleep(4000);
await click(page, ".btn-danger.btn.me-2");
2021-10-06 08:25:00 +01:00
await login("admin", "new_password123");
2021-10-07 11:10:35 +01:00
2021-10-06 08:25:00 +01:00
let elementCount = await page.evaluate(() => document.querySelectorAll("#floatingPassword").length);
expect(elementCount).toEqual(1);
await login("admin", "admin123");
await sleep(3000);
2021-10-05 13:27:43 +01:00
});
2021-10-06 08:25:00 +01:00
// TODO: 2FA
// TODO: Export Backup
// TODO: Import Backup
// TODO: Disable Auth
// TODO: Clear Stats
2021-10-05 09:03:35 +01:00
});
2021-10-05 13:27:43 +01:00
2021-10-06 08:25:00 +01:00
/*
* TODO
* Create Monitor - All type
* Edit Monitor
* Delete Monitor
*
* Create Notification (token problem, maybe hard to test)
*
*/
2021-10-05 13:27:43 +01:00
describe("Status Page", () => {
const title = "Uptime Kuma";
beforeAll(async () => {
await page.goto(baseURL + "/status");
});
it(`should be titled "${title}"`, async () => {
2021-10-08 08:11:50 +01:00
await expect(page.title()).resolves.toEqual(title);
2021-10-05 13:27:43 +01:00
});
2021-10-05 09:03:35 +01:00
});
2021-10-03 11:16:55 +01:00
});
2021-10-06 08:25:00 +01: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 11:10:35 +01:00
await sleep(5000);
2021-10-06 08:25:00 +01:00
}
async function click(page, selector, elementIndex = 0) {
2021-10-07 11:10:35 +01:00
await page.waitForSelector(selector, {
timeout: 5000,
});
2021-10-06 08:25:00 +01:00
return await page.evaluate((s, i) => {
return document.querySelectorAll(s)[i].click();
}, selector, elementIndex);
}
async function input(page, selector, text) {
2021-10-07 11:10:35 +01:00
await page.waitForSelector(selector, {
timeout: 5000,
});
2021-10-06 08:25:00 +01:00
const element = await page.$(selector);
await element.click({ clickCount: 3 });
await page.keyboard.press("Backspace");
await page.type(selector, text);
}