mirror of
https://github.com/gethomepage/homepage.git
synced 2026-05-18 19:40:58 +08:00
Fix: allow empty data for ntfy widget (#6653)
This commit is contained in:
@@ -8,6 +8,13 @@ export default function validateWidgetData(widget, endpoint, data) {
|
|||||||
let dataParsed = data;
|
let dataParsed = data;
|
||||||
let error;
|
let error;
|
||||||
let mapping;
|
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)) {
|
if (Buffer.isBuffer(data)) {
|
||||||
try {
|
try {
|
||||||
dataParsed = JSON.parse(data);
|
dataParsed = JSON.parse(data);
|
||||||
@@ -23,15 +30,11 @@ export default function validateWidgetData(widget, endpoint, data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (dataParsed && Object.entries(dataParsed).length) {
|
if (dataParsed && Object.entries(dataParsed).length) {
|
||||||
const mappings = widgets[widget.type]?.mappings;
|
mapping?.validate?.forEach((key) => {
|
||||||
if (mappings) {
|
if (dataParsed[key] === undefined) {
|
||||||
mapping = Object.values(mappings).find((m) => m.endpoint === endpoint);
|
valid = false;
|
||||||
mapping?.validate?.forEach((key) => {
|
}
|
||||||
if (dataParsed[key] === undefined) {
|
});
|
||||||
valid = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!valid) {
|
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(() => ({
|
const { loggerError } = vi.hoisted(() => ({
|
||||||
loggerError: vi.fn(),
|
loggerError: vi.fn(),
|
||||||
@@ -18,6 +18,10 @@ vi.mock("widgets/widgets", () => ({
|
|||||||
endpoint: "foo",
|
endpoint: "foo",
|
||||||
validate: ["a", "b"],
|
validate: ["a", "b"],
|
||||||
},
|
},
|
||||||
|
empty: {
|
||||||
|
endpoint: "empty",
|
||||||
|
allowEmpty: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -26,6 +30,10 @@ vi.mock("widgets/widgets", () => ({
|
|||||||
import validateWidgetData from "./validate-widget-data";
|
import validateWidgetData from "./validate-widget-data";
|
||||||
|
|
||||||
describe("utils/proxy/validate-widget-data", () => {
|
describe("utils/proxy/validate-widget-data", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
it("returns false when buffer JSON cannot be parsed", () => {
|
it("returns false when buffer JSON cannot be parsed", () => {
|
||||||
expect(validateWidgetData({ type: "test" }, "foo", Buffer.from("not json"))).toBe(false);
|
expect(validateWidgetData({ type: "test" }, "foo", Buffer.from("not json"))).toBe(false);
|
||||||
expect(loggerError).toHaveBeenCalled();
|
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(validateWidgetData({ type: "test" }, "foo", Buffer.from(JSON.stringify({ a: 1 })))).toBe(false);
|
||||||
expect(loggerError).toHaveBeenCalled();
|
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();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
|
import { asJson } from "utils/proxy/api-helpers";
|
||||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||||
|
|
||||||
|
const noMessages = {
|
||||||
|
title: null,
|
||||||
|
message: null,
|
||||||
|
priority: 3,
|
||||||
|
time: null,
|
||||||
|
tags: [],
|
||||||
|
};
|
||||||
|
|
||||||
const widget = {
|
const widget = {
|
||||||
api: "{url}/{endpoint}",
|
api: "{url}/{endpoint}",
|
||||||
proxyHandler: credentialedProxyHandler,
|
proxyHandler: credentialedProxyHandler,
|
||||||
@@ -7,6 +16,14 @@ const widget = {
|
|||||||
mappings: {
|
mappings: {
|
||||||
messages: {
|
messages: {
|
||||||
endpoint: "{topic}/json?poll=1&since=latest",
|
endpoint: "{topic}/json?poll=1&since=latest",
|
||||||
|
allowEmpty: true,
|
||||||
|
map: (data) => {
|
||||||
|
if (Buffer.isBuffer(data) && data.length === 0) {
|
||||||
|
return noMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
return asJson(data);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
import { expectWidgetConfigShape } from "test-utils/widget-config";
|
||||||
|
|
||||||
@@ -8,4 +8,18 @@ describe("ntfy widget config", () => {
|
|||||||
it("exports a valid widget config", () => {
|
it("exports a valid widget config", () => {
|
||||||
expectWidgetConfigShape(widget);
|
expectWidgetConfigShape(widget);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("maps an empty latest message response to the no messages state", () => {
|
||||||
|
expect(widget.mappings.messages.map(Buffer.from(""))).toEqual({
|
||||||
|
title: null,
|
||||||
|
message: null,
|
||||||
|
priority: 3,
|
||||||
|
time: null,
|
||||||
|
tags: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses latest message responses", () => {
|
||||||
|
expect(widget.mappings.messages.map(Buffer.from('{"message":"hello"}'))).toEqual({ message: "hello" });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user