mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-26 22:11:20 -07:00
Fix: allow empty data for ntfy widget (#6653)
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
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
This commit is contained in:
@@ -8,6 +8,13 @@ export default function validateWidgetData(widget, endpoint, data) {
|
||||
let dataParsed = data;
|
||||
let error;
|
||||
let mapping;
|
||||
const mappings = widgets[widget.type]?.mappings;
|
||||
if (mappings) {
|
||||
mapping = Object.values(mappings).find((m) => m.endpoint === endpoint);
|
||||
}
|
||||
|
||||
if (mapping?.allowEmpty && Buffer.isBuffer(data) && data.length === 0) return true;
|
||||
|
||||
if (Buffer.isBuffer(data)) {
|
||||
try {
|
||||
dataParsed = JSON.parse(data);
|
||||
@@ -23,15 +30,11 @@ export default function validateWidgetData(widget, endpoint, data) {
|
||||
}
|
||||
|
||||
if (dataParsed && Object.entries(dataParsed).length) {
|
||||
const mappings = widgets[widget.type]?.mappings;
|
||||
if (mappings) {
|
||||
mapping = Object.values(mappings).find((m) => m.endpoint === endpoint);
|
||||
mapping?.validate?.forEach((key) => {
|
||||
if (dataParsed[key] === undefined) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
mapping?.validate?.forEach((key) => {
|
||||
if (dataParsed[key] === undefined) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { loggerError } = vi.hoisted(() => ({
|
||||
loggerError: vi.fn(),
|
||||
@@ -18,6 +18,10 @@ vi.mock("widgets/widgets", () => ({
|
||||
endpoint: "foo",
|
||||
validate: ["a", "b"],
|
||||
},
|
||||
empty: {
|
||||
endpoint: "empty",
|
||||
allowEmpty: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -26,6 +30,10 @@ vi.mock("widgets/widgets", () => ({
|
||||
import validateWidgetData from "./validate-widget-data";
|
||||
|
||||
describe("utils/proxy/validate-widget-data", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("returns false when buffer JSON cannot be parsed", () => {
|
||||
expect(validateWidgetData({ type: "test" }, "foo", Buffer.from("not json"))).toBe(false);
|
||||
expect(loggerError).toHaveBeenCalled();
|
||||
@@ -41,4 +49,9 @@ describe("utils/proxy/validate-widget-data", () => {
|
||||
expect(validateWidgetData({ type: "test" }, "foo", Buffer.from(JSON.stringify({ a: 1 })))).toBe(false);
|
||||
expect(loggerError).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("allows empty buffer responses for mappings that explicitly allow them", () => {
|
||||
expect(validateWidgetData({ type: "test" }, "empty", Buffer.from(""))).toBe(true);
|
||||
expect(loggerError).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user