Files
homepage/src/widgets/qbittorrent/proxy.test.js
T
headscarf737andshamoon 84c7e51269
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Chore: simplify qbittorrent api key handling (#6888)
Co-authored-by: shamoon <4887959+shamoon@users.noreply.github.com>
2026-07-21 16:14:39 +00:00

104 lines
3.5 KiB
JavaScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import createMockRes from "test-utils/create-mock-res";
const { httpProxy, getServiceWidget, logger } = vi.hoisted(() => ({
httpProxy: vi.fn(),
getServiceWidget: vi.fn(),
logger: {
debug: vi.fn(),
error: vi.fn(),
},
}));
vi.mock("utils/logger", () => ({
default: () => logger,
}));
vi.mock("utils/config/service-helpers", () => ({
default: getServiceWidget,
}));
vi.mock("utils/proxy/http", () => ({
httpProxy,
}));
import qbittorrentProxyHandler from "./proxy";
describe("widgets/qbittorrent/proxy", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("logs in and retries after a 403 response", async () => {
getServiceWidget.mockResolvedValue({ url: "http://qb", username: "u", password: "p" });
httpProxy
.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([200, "text/plain", Buffer.from("Ok.")])
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
const req = { query: { group: "g", service: "svc", endpoint: "torrents/info", index: "0" } };
const res = createMockRes();
await qbittorrentProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(3);
expect(httpProxy.mock.calls[1][0]).toBe("http://qb/api/v2/auth/login");
expect(res.statusCode).toBe(200);
expect(res.body).toEqual(Buffer.from("data"));
});
it("accepts qBittorrent 5.2.0 no-content login responses", async () => {
getServiceWidget.mockResolvedValue({ url: "http://qb", username: "u", password: "p" });
httpProxy
.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([204, null, Buffer.from("")])
.mockResolvedValueOnce([200, "application/json", Buffer.from("data")]);
const req = { query: { group: "g", service: "svc", endpoint: "torrents/info", index: "0" } };
const res = createMockRes();
await qbittorrentProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(3);
expect(httpProxy.mock.calls[1][0]).toBe("http://qb/api/v2/auth/login");
expect(res.statusCode).toBe(200);
expect(res.body).toEqual(Buffer.from("data"));
});
it("returns 401 when login succeeds but response body is not Ok.", async () => {
getServiceWidget.mockResolvedValue({ url: "http://qb", username: "u", password: "p" });
httpProxy
.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")])
.mockResolvedValueOnce([200, "text/plain", Buffer.from("Denied")]);
const req = { query: { group: "g", service: "svc", endpoint: "torrents/info", index: "0" } };
const res = createMockRes();
await qbittorrentProxyHandler(req, res);
expect(res.statusCode).toBe(401);
expect(res.body).toEqual(Buffer.from("Denied"));
});
it("uses an API key on the WebAPI request without attempting login", async () => {
getServiceWidget.mockResolvedValue({ url: "http://qb", key: "abc123" });
httpProxy.mockResolvedValueOnce([403, "application/json", Buffer.from("nope")]);
const req = { query: { group: "g", service: "svc", endpoint: "torrents/info", index: "0" } };
const res = createMockRes();
await qbittorrentProxyHandler(req, res);
expect(httpProxy).toHaveBeenCalledTimes(1);
expect(httpProxy.mock.calls[0][0].toString()).toBe("http://qb/api/v2/torrents/info");
expect(httpProxy.mock.calls[0][1].headers.Authorization).toBe("Bearer abc123");
expect(res.statusCode).toBe(403);
expect(res.body).toEqual(Buffer.from("nope"));
});
});