mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-29 15:31:16 -07:00
Chore: homepage tests (#6278)
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { screen } from "@testing-library/react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { renderWithProviders } from "test-utils/render-with-providers";
|
||||
import { findServiceBlockByLabel } from "test-utils/widget-assertions";
|
||||
|
||||
const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() }));
|
||||
vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI }));
|
||||
|
||||
import Component from "./component";
|
||||
|
||||
function expectBlockValue(container, label, value) {
|
||||
const block = findServiceBlockByLabel(container, label);
|
||||
expect(block, `missing block for ${label}`).toBeTruthy();
|
||||
expect(block.textContent).toContain(String(value));
|
||||
}
|
||||
|
||||
describe("widgets/unifi/component", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("renders placeholders when default site isn't available yet", () => {
|
||||
useWidgetAPI.mockReturnValue({ data: undefined, error: undefined });
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "unifi" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getByText("unifi.uptime")).toBeInTheDocument();
|
||||
expect(screen.getByText("unifi.wan")).toBeInTheDocument();
|
||||
expect(screen.getByText("unifi.lan_users")).toBeInTheDocument();
|
||||
expect(screen.getByText("unifi.wlan_users")).toBeInTheDocument();
|
||||
// 4 blocks if all are rendered.
|
||||
expect(container.querySelectorAll(".service-block")).toHaveLength(4);
|
||||
});
|
||||
|
||||
it("renders a site-not-found error when widget.site doesn't match", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: { data: [{ name: "default", desc: "Default", health: [] }] },
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
renderWithProviders(<Component service={{ widget: { type: "unifi", site: "Nope" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0);
|
||||
expect(screen.getByText("Site 'Nope' not found")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders uptime, wan and user counts when site data is present", () => {
|
||||
useWidgetAPI.mockReturnValue({
|
||||
data: {
|
||||
data: [
|
||||
{
|
||||
name: "default",
|
||||
desc: "Default",
|
||||
health: [
|
||||
{ subsystem: "wan", status: "ok", num_user: 0, num_adopted: 0, "gw_system-stats": { uptime: 86400 } },
|
||||
{ subsystem: "lan", status: "ok", num_user: 2, num_adopted: 5 },
|
||||
{ subsystem: "wlan", status: "ok", num_user: 3, num_adopted: 6 },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const { container } = renderWithProviders(<Component service={{ widget: { type: "unifi" } }} />, {
|
||||
settings: { hideErrors: false },
|
||||
});
|
||||
|
||||
// uptime includes unifi.days suffix.
|
||||
expect(findServiceBlockByLabel(container, "unifi.uptime")?.textContent).toContain("unifi.days");
|
||||
expectBlockValue(container, "unifi.wan", "unifi.up");
|
||||
expectBlockValue(container, "unifi.lan_users", 2);
|
||||
expectBlockValue(container, "unifi.wlan_users", 3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import createMockRes from "test-utils/create-mock-res";
|
||||
|
||||
const { httpProxy, getServiceWidget, getPrivateWidgetOptions, cache, cookieJar, logger } = vi.hoisted(() => {
|
||||
const store = new Map();
|
||||
return {
|
||||
httpProxy: vi.fn(),
|
||||
getServiceWidget: vi.fn(),
|
||||
getPrivateWidgetOptions: vi.fn(),
|
||||
cache: {
|
||||
get: vi.fn((k) => (store.has(k) ? store.get(k) : null)),
|
||||
put: vi.fn((k, v) => store.set(k, v)),
|
||||
del: vi.fn((k) => store.delete(k)),
|
||||
_reset: () => store.clear(),
|
||||
},
|
||||
cookieJar: {
|
||||
addCookieToJar: vi.fn(),
|
||||
setCookieHeader: vi.fn(),
|
||||
},
|
||||
logger: { debug: vi.fn(), error: vi.fn() },
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("memory-cache", () => ({
|
||||
default: cache,
|
||||
...cache,
|
||||
}));
|
||||
vi.mock("utils/logger", () => ({
|
||||
default: () => logger,
|
||||
}));
|
||||
vi.mock("utils/config/service-helpers", () => ({
|
||||
default: getServiceWidget,
|
||||
}));
|
||||
vi.mock("utils/config/widget-helpers", () => ({
|
||||
getPrivateWidgetOptions,
|
||||
}));
|
||||
vi.mock("utils/proxy/http", () => ({
|
||||
httpProxy,
|
||||
}));
|
||||
vi.mock("utils/proxy/cookie-jar", () => cookieJar);
|
||||
vi.mock("widgets/widgets", () => ({
|
||||
default: {
|
||||
unifi: {
|
||||
api: "{url}{prefix}/api/{endpoint}",
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
import unifiProxyHandler from "./proxy";
|
||||
|
||||
describe("widgets/unifi/proxy", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
cache._reset();
|
||||
});
|
||||
|
||||
it("auto-detects prefix, logs in on 401, and retries the request", async () => {
|
||||
getServiceWidget.mockResolvedValue({
|
||||
type: "unifi",
|
||||
url: "http://unifi",
|
||||
username: "u",
|
||||
password: "p",
|
||||
});
|
||||
|
||||
httpProxy
|
||||
// autodetect call -> csrf header indicates udmp prefix
|
||||
.mockResolvedValueOnce([200, "text/html", Buffer.from(""), { "x-csrf-token": "csrf" }])
|
||||
// initial api call -> unauthorized
|
||||
.mockResolvedValueOnce([401, "application/json", Buffer.from("nope"), { "x-csrf-token": "csrf2" }])
|
||||
// login -> ok
|
||||
.mockResolvedValueOnce([
|
||||
200,
|
||||
"application/json",
|
||||
Buffer.from(JSON.stringify({ meta: { rc: "ok" } })),
|
||||
{ "set-cookie": ["sid=1"] },
|
||||
])
|
||||
// retry api call -> ok
|
||||
.mockResolvedValueOnce([200, "application/json", Buffer.from("data"), {}]);
|
||||
|
||||
const req = { query: { group: "g", service: "svc", endpoint: "self", index: "0" } };
|
||||
const res = createMockRes();
|
||||
|
||||
await unifiProxyHandler(req, res);
|
||||
|
||||
expect(httpProxy).toHaveBeenCalledTimes(4);
|
||||
expect(httpProxy.mock.calls[1][0].toString()).toContain("/proxy/network/api/self");
|
||||
expect(cookieJar.addCookieToJar).toHaveBeenCalled();
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toEqual(Buffer.from("data"));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, it } from "vitest";
|
||||
|
||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||
|
||||
import widget from "./widget";
|
||||
|
||||
describe("unifi widget config", () => {
|
||||
it("exports a valid widget config", () => {
|
||||
expectWidgetConfigShape(widget);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user