Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

3.6 KB
Raw Blame
import { describe, expect, test } from "vitest";

import {
  throwIfCustomWidgetBoardDuplicationForbidden,
  throwIfCustomWidgetPlacementChangeForbidden,
} from "../custom-widget-placement-access";

const placement = {
  id: "widget-1",
  kind: "customApi",
  options: {
    definitionId: "definition-1",
    refreshInterval: 30,
    configuration: { server: "primary" },
  },
};

describe("Custom Widget placement access", () => {
  test("non-admins cannot add a Custom Widget placement", () => {
    expect(() =>
      throwIfCustomWidgetPlacementChangeForbidden({
        isAdmin: false,
        submittedItems: [{ ...placement, options: { ...placement.options, configurationVersion: 1 } }],
        storedItems: [],
      }),
    ).toThrowError("Only administrators can add or configure Custom Widgets");
  });

  test.each([
    ["definition", { ...placement, options: { ...placement.options, definitionId: "definition-2" } }],
    ["refresh interval", { ...placement, options: { ...placement.options, refreshInterval: 60 } }],
    [
      "option configuration",
      { ...placement, options: { ...placement.options, configuration: { server: "secondary" } } },
    ],
    ["widget kind", { ...placement, kind: "weather" }],
  ])("non-admins cannot change an existing Custom Widget %s", (_label, submitted) => {
    expect(() =>
      throwIfCustomWidgetPlacementChangeForbidden({
        isAdmin: false,
        submittedItems: [submitted],
        storedItems: [placement],
      }),
    ).toThrowError("Only administrators can add or configure Custom Widgets");
  });

  test("non-admins may move, resize and change presentation for an existing placement", () => {
    const submitted = {
      ...placement,
      layouts: [{ xOffset: 2, yOffset: 4, width: 6, height: 3 }],
      advancedOptions: { title: "Status", customCssClasses: ["wide"], borderColor: "#fff" },
    };
    expect(() =>
      throwIfCustomWidgetPlacementChangeForbidden({
        isAdmin: false,
        submittedItems: [submitted],
        storedItems: [placement],
      }),
    ).not.toThrow();
  });

  test("non-admins may remove an existing placement", () => {
    expect(() =>
      throwIfCustomWidgetPlacementChangeForbidden({
        isAdmin: false,
        submittedItems: [],
        storedItems: [placement],
      }),
    ).not.toThrow();
  });

  test("admins may create and reconfigure placements", () => {
    expect(() =>
      throwIfCustomWidgetPlacementChangeForbidden({
        isAdmin: true,
        submittedItems: [{ ...placement, options: { definitionId: "definition-2" } }],
        storedItems: [],
      }),
    ).not.toThrow();
  });

  test("duplicating a board with Custom Widgets requires admin", () => {
    expect(() => throwIfCustomWidgetBoardDuplicationForbidden(false, [placement])).toThrowError(
      "Only administrators can add or configure Custom Widgets",
    );
    expect(() => throwIfCustomWidgetBoardDuplicationForbidden(true, [placement])).not.toThrow();
    expect(() => throwIfCustomWidgetBoardDuplicationForbidden(false, [{ kind: "weather" }])).not.toThrow();
  });

  test("treats missing persisted v2 defaults as equivalent during presentation-only edits", () => {
    expect(() =>
      throwIfCustomWidgetPlacementChangeForbidden({
        isAdmin: false,
        submittedItems: [placement],
        storedItems: [
          {
            id: placement.id,
            kind: placement.kind,
            options: {
              definitionId: placement.options.definitionId,
              refreshInterval: placement.options.refreshInterval,
              configuration: placement.options.configuration,
            },
          },
        ],
      }),
    ).not.toThrow();
  });
});