Skip to content

armgit / dashy

Public Imported from GitHub
0

πŸš€ A self-hostable personal dashboard built for you. Includes status-checking, widgets, themes, icon packs, a UI editor and tons more!

✨ REST API for dashboard management (#2011)

Alicia Sykes1500839ef513
diff --git a/.env b/.env
index 20f181d4..86a53066 100644
--- a/.env
+++ b/.env
@@ -67,6 +67,9 @@
# Set to 'true' to disable automatic backups before each config save
# DISABLE_CONFIG_BACKUPS=true
+# Set to 'true' to enable the REST API for reading / writing config (see docs/api.md)
+# ENABLE_API=true
+
# Setup any other user defined vars by prepending VITE_APP_ to the var name
# VITE_APP_pihole_ip=http://your.pihole.ip
# VITE_APP_pihole_key=your_pihole_secret_key
diff --git a/docs/api.md b/docs/api.md
new file mode 100644
index 00000000..36d98014
--- /dev/null
+++ b/docs/api.md
@@ -0,0 +1,94 @@
+# REST API
+
+Dashy includes an optional REST API, for reading and writing your config programmatically β€” from the command line, scripts or third-party applications. It covers whole config files, as well as individual sections and items.
+
+> [!NOTE]
+> The API is served by Dashy's Node server, so it's available with Docker and bare-metal deployments, but not on static hosting providers (Netlify, Vercel, EdgeOne, CDN).
+
+## Enabling the API
+
+The API is disabled by default. To enable it, set the `ENABLE_API` environmental variable to `true`. For example, with Docker Compose:
+
+```yaml
+environment:
+ - ENABLE_API=true
+```
+
+Or with `docker run`, pass `-e ENABLE_API=true`. While disabled, all `/api/*` requests return a 404.
+
+## Authentication
+
+The API uses Dashy's existing [server-side authentication](/docs/authentication.md). Read endpoints require any authenticated user, and write endpoints require an admin. If no auth is configured, the API is open β€” the same as Dashy's other endpoints.
+
+```bash
+# With HTTP Basic Auth (ENABLE_HTTP_AUTH or BASIC_AUTH_USERNAME / BASIC_AUTH_PASSWORD)
+curl -u alice:hunter2 http://localhost:8080/api/config
+
+# With OIDC / Keycloak, pass your ID token
+curl -H 'Authorization: Bearer <id-token>' http://localhost:8080/api/config
+```
+
+## Endpoints
+
+`:filename` is any YAML config file in your user-data directory (e.g. `conf.yml`, or a sub-page like `home-lab.yml`). `:key` is one of the top-level config keys: `pageInfo`, `appConfig`, `sections` or `pages`.
+
+**Method** | **Path** | **Description**
+--- | --- | ---
+`GET` | `/api/config` | List config files
+`GET` | `/api/config/:filename` | Get a full config file, as JSON
+`PUT` | `/api/config/:filename` | Replace a full config file
+`GET` | `/api/config/:filename/:key` | Get a top-level key
+`PUT` | `/api/config/:filename/:key` | Replace a top-level key
+`POST` | `/api/config/:filename/sections` | Add a section (`name` required)
+`GET` | `/api/config/:filename/sections/:sid` | Get a section
+`PATCH` | `/api/config/:filename/sections/:sid` | Update fields on a section
+`DELETE` | `/api/config/:filename/sections/:sid` | Delete a section
+`GET` | `/api/config/:filename/sections/:sid/items` | List a section's items
+`POST` | `/api/config/:filename/sections/:sid/items` | Add an item (`title` required)
+`GET` | `/api/config/:filename/sections/:sid/items/:iid` | Get an item
+`PATCH` | `/api/config/:filename/sections/:sid/items/:iid` | Update fields on an item
+`DELETE` | `/api/config/:filename/sections/:sid/items/:iid` | Delete an item
+
+All bodies are JSON. Errors return `{ "success": false, "message": "..." }` with an appropriate status code (400 bad input, 401/403 auth, 404 not found).
+
+### Addressing Sections and Items
+
+`:sid` and `:iid` can be either a zero-based index (`0`, `1`, ...) or an exact match on the section's `name` / item's `title` (URL-encoded). If multiple entries share a name, the first match wins. A section literally named `2` can only be addressed by index.
+
+### Updating
+
+`PATCH` does a shallow merge: only the fields you send are changed, but nested values (like a section's `items` array) are replaced wholesale if included. `PUT` replaces the target entirely.
+
+## Examples
+
+```bash
+# List config files
+curl http://localhost:8080/api/config
+
+# Get your main config as JSON
+curl http://localhost:8080/api/config/conf.yml
+
+# Add an item to the first section
+curl -X POST -H 'Content-Type: application/json' \
+ -d '{"title": "Grafana", "url": "https://grafana.local", "icon": "hl-grafana"}' \
+ http://localhost:8080/api/config/conf.yml/sections/0/items
+
+# Rename a section
+curl -X PATCH -H 'Content-Type: application/json' \
+ -d '{"name": "Monitoring"}' \
+ 'http://localhost:8080/api/config/conf.yml/sections/Old%20Name'
+
+# Update the theme
+curl -X PUT -H 'Content-Type: application/json' \
+ -d '{"theme": "nord-frost"}' \
+ http://localhost:8080/api/config/conf.yml/appConfig
+```
+
+After modifying your config, refresh the page to see changes.
+
+## Limitations & Notes
+
+- Writes re-serialize the YAML file, so comments, anchors and custom formatting are discarded (the same applies to saving via the UI). A timestamped backup is saved to `user-data/config-backups/` before every write, unless `DISABLE_CONFIG_BACKUPS=true`
+- Writes to `conf.yml` are validated against [the schema](https://github.com/Lissy93/dashy/blob/master/src/utils/config/ConfigSchema.json) and rejected if invalid. Sub-page files are not schema-validated, since they may contain only a subset of fields
+- Config files are capped at 256 KB
+- Concurrent writes are last-write-wins; there is no locking or optimistic concurrency
diff --git a/docs/configuring.md b/docs/configuring.md
index bf1c5369..0561cc36 100644
--- a/docs/configuring.md
+++ b/docs/configuring.md
@@ -10,7 +10,7 @@ All app configuration is specified in [`/user-data/conf.yml`](https://github.com
- From the UI, under the config menu there is a JSON editor, with built-in validation, documentation and advanced options
- **UI Visual Editor** _(3/5 reliability, 5/5 usability)_
- From the UI, enter the Interactive Edit Mode, then click any part of the page to edit. Changes are previewed live, and then saved to disk
-- **REST API** _(Coming soon)_
+- **REST API** _(see [API docs](/docs/api.md))_
- Programmatically edit config either through the command line, using a script or a third-party application
## Tips
diff --git a/docs/readme.md b/docs/readme.md
index 1192cbaf..12f4c15e 100644
--- a/docs/readme.md
+++ b/docs/readme.md
@@ -25,6 +25,7 @@
- [Icons](/docs/icons.md) - Outline of all available icon types for sections and items, with examples
- [Language Switching](/docs/multi-language-support.md) - Details on how to switch language, or add a new locale
- [Pages and Sections](/docs/pages-and-sections.md) - Multi-page support, sections, items and sub-items
+- [REST API](/docs/api.md) - Programmatically read and update your config over HTTP
- [Status Indicators](/docs/status-indicators.md) - Using Dashy to monitor uptime and status of your apps/services and hosts
- [Searching & Shortcuts](/docs/searching.md) - Searching, launching methods + keyboard shortcuts
- [Theming](/docs/theming.md) - Complete guide to applying, writing and modifying themes + styles
diff --git a/services/api/config-files.js b/services/api/config-files.js
new file mode 100644
index 00000000..c12bd2ea
--- /dev/null
+++ b/services/api/config-files.js
@@ -0,0 +1,102 @@
+/**
+ * File-system + YAML helpers for the REST API.
+ * Reads config files from USER_DATA_DIR, and reuses save-config.js for
+ * writes, so backups, size limits and filename validation stay in one place
+ */
+const fsPromises = require('fs').promises;
+const path = require('path');
+const yaml = require('js-yaml');
+const Ajv = require('ajv');
+
+const saveConfig = require('../save-config');
+const schema = require('../../src/utils/config/ConfigSchema.json');
+
+const rootDir = path.join(__dirname, '..', '..');
+
+// Same rules as save-config.js: no path separators, control chars or ..
+const SAFE_FILENAME = /^(?!\.+$)[^\\/\0\r\n]+\.ya?ml$/i;
+
+const validateSchema = new Ajv({ strict: false, allowUnionTypes: true, allErrors: true })
+ .compile(schema);
+
+/* An error with an associated HTTP status code, for the router to render */
+class ApiError extends Error {
+ constructor(message, status = 400) {
+ super(message);
+ this.status = status;
+ }
+}
+
+const userDataDir = () => path.resolve(rootDir, process.env.USER_DATA_DIR || 'user-data');
+
+/* Returns the validated basename of a config filename, or throws a 400 */
+const safeFilename = (filename) => {
+ const base = path.basename(String(filename));
+ if (!SAFE_FILENAME.test(base)) {
+ throw new ApiError('Invalid filename: must be a basename ending in .yml or .yaml');
+ }
+ return base;
+};
+
+/* Lists all YAML config files in the user data directory */
+const listConfigFiles = async () => {
+ const entries = await fsPromises.readdir(userDataDir(), { withFileTypes: true });
+ return entries
+ .filter((entry) => entry.isFile() && /\.ya?ml$/i.test(entry.name))
+ .map((entry) => entry.name)
+ .sort();
+};
+
+/* Reads and parses a config file, returning it as a plain object */
+const readConfig = async (filename) => {
+ const base = safeFilename(filename);
+ let raw;
+ try {
+ raw = await fsPromises.readFile(path.join(userDataDir(), base), 'utf8');
+ } catch (e) {
+ if (e.code === 'ENOENT') throw new ApiError(`${base} not found`, 404);
+ throw new ApiError(`Could not read ${base}`, 500);
+ }
+ let parsed;
+ try {
+ parsed = yaml.load(raw);
+ } catch (e) {
+ throw new ApiError(`Could not parse ${base}: ${e.reason || e.message}`, 500);
+ }
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
+ throw new ApiError(`${base} is not a valid config (expected a YAML mapping)`);
+ }
+ return parsed;
+};
+
+/* Serializes and writes a config, via save-config for backups + size limits.
+ The root conf.yml is validated against the schema; sub-pages are not,
+ since they may contain only a subset of fields (e.g. just pageInfo) */
+const writeConfig = async (filename, config) => {
+ const base = safeFilename(filename);
+ if (base === 'conf.yml' && !validateSchema(config)) {
+ const issues = (validateSchema.errors || []).slice(0, 5)
+ .map((e) => `${e.instancePath || '/'} ${e.message}`).join('; ');
+ throw new ApiError(`Config does not conform to schema: ${issues}`);
+ }
+ const result = await new Promise((resolve) => {
+ saveConfig({ config: yaml.dump(config, { noRefs: true }), filename: base }, (jsonStr) => {
+ resolve(JSON.parse(jsonStr));
+ });
+ });
+ if (!result.success) throw new ApiError(result.message);
+ return result.message;
+};
+
+/* Resolves a section/item identifier (numeric index, or match on keyField)
+ to an array index, returning -1 when not found */
+const resolveIndex = (arr, id, keyField) => {
+ if (/^\d+$/.test(id)) {
+ return Number(id) < arr.length ? Number(id) : -1;
+ }
+ return arr.findIndex((entry) => entry && entry[keyField] === id);
+};
+
+module.exports = {
+ ApiError, safeFilename, listConfigFiles, readConfig, writeConfig, resolveIndex,
+};
diff --git a/services/api/index.js b/services/api/index.js
new file mode 100644
index 00000000..a8723399
--- /dev/null
+++ b/services/api/index.js
@@ -0,0 +1,183 @@
+/**
+ * Opt-in REST API for reading + writing config files (see docs/api.md).
+ * Disabled unless ENABLE_API=true. Mounted at /api by app.js, which passes
+ * in its auth middleware: reads require any user, writes require an admin
+ */
+const express = require('express');
+
+const {
+ ApiError, safeFilename, listConfigFiles, readConfig, writeConfig, resolveIndex,
+} = require('./config-files');
+
+/* The editable top-level config keys (matches ConfigSchema.json) */
+const TOP_LEVEL_KEYS = ['pageInfo', 'appConfig', 'sections', 'pages'];
+
+/* Renders errors raised before route handlers (e.g. malformed JSON or
+ oversized bodies from express.json) as JSON, instead of Express's HTML */
+const apiErrorHandler = (err, req, res, next) => {
+ if (res.headersSent) return next(err);
+ return res.status(err.status || 400).json({ success: false, message: err.message });
+};
+
+/* Responds with a 404 unless the API has been explicitly enabled */
+const apiEnabledGate = (req, res, next) => {
+ if (process.env.ENABLE_API === 'true') return next();
+ return res.status(404).json({
+ success: false,
+ message: 'API not enabled. Set ENABLE_API=true to use the REST API.',
+ });
+};
+
+/* Throws a 400 unless the request body is a plain object */
+const requireObjectBody = (body, what) => {
+ if (!body || typeof body !== 'object' || Array.isArray(body)) {
+ throw new ApiError(`Request body must be ${what}`);
+ }
+};
+
+/* Throws a 400 if key isn't one of the editable top-level config keys */
+const checkKey = (key) => {
+ if (!TOP_LEVEL_KEYS.includes(key)) {
+ throw new ApiError(`Invalid key '${key}', must be one of: ${TOP_LEVEL_KEYS.join(', ')}`);
+ }
+};
+
+/* Returns parent[key] as an array, creating it if requested + missing */
+const getArray = (parent, key, create) => {
+ if (parent[key] === undefined && create) parent[key] = [];
+ const arr = parent[key] === undefined ? [] : parent[key];
+ if (!Array.isArray(arr)) throw new ApiError(`'${key}' is not a list`);
+ return arr;
+};
+
+/* Locates a section by index or name, throwing a 404 when not found */
+const findSection = (config, sid) => {
+ const sections = getArray(config, 'sections');
+ const index = resolveIndex(sections, sid, 'name');
+ if (index === -1) throw new ApiError(`Section '${sid}' not found`, 404);
+ return { sections, index, section: sections[index] };
+};
+
+/* Locates an item within a section by index or title */
+const findItem = (section, iid) => {
+ const items = getArray(section, 'items');
+ const index = resolveIndex(items, iid, 'title');
+ if (index === -1) throw new ApiError(`Item '${iid}' not found`, 404);
+ return { items, index, item: items[index] };
+};
+
+const createApiRouter = ({ requireAuth, requireAdmin, onConfigSaved }) => {
+ const router = express.Router();
+
+ /* Wraps an async handler, rendering thrown ApiErrors as JSON */
+ const route = (handler) => async (req, res) => {
+ try {
+ await handler(req, res);
+ } catch (e) {
+ res.status(e.status || 500).json({ success: false, message: e.message });
+ }
+ };
+
+ /* Read-modify-write handler: applies mutate() to the parsed config, writes
+ it back, and responds with the save message + anything mutate returned */
+ const update = (mutate, status = 200) => route(async (req, res) => {
+ const config = await readConfig(req.params.filename);
+ const result = mutate(req, config);
+ const message = await writeConfig(req.params.filename, config);
+ if (onConfigSaved) onConfigSaved(safeFilename(req.params.filename), config);
+ res.status(status).json({ success: true, message, ...result });
+ });
+
+ router.get('/config', requireAuth, route(async (req, res) => {
+ res.json({ success: true, files: await listConfigFiles() });
+ }));
+
+ router.get('/config/:filename', requireAuth, route(async (req, res) => {
+ res.json(await readConfig(req.params.filename));
+ }));
+
+ router.put('/config/:filename', requireAdmin, route(async (req, res) => {
+ requireObjectBody(req.body, 'a config object');
+ const message = await writeConfig(req.params.filename, req.body);
+ if (onConfigSaved) onConfigSaved(safeFilename(req.params.filename), req.body);
+ res.json({ success: true, message });
+ }));
+
+ router.post('/config/:filename/sections', requireAdmin, update((req, config) => {
+ requireObjectBody(req.body, 'a section object');
+ if (!req.body.name) throw new ApiError("Section must have a 'name'");
+ const sections = getArray(config, 'sections', true);
+ sections.push(req.body);
+ return { index: sections.length - 1, section: req.body };
+ }, 201));
+
+ router.get('/config/:filename/sections/:sid', requireAuth, route(async (req, res) => {
+ const config = await readConfig(req.params.filename);
+ res.json(findSection(config, req.params.sid).section);
+ }));
+
+ router.patch('/config/:filename/sections/:sid', requireAdmin, update((req, config) => {
+ requireObjectBody(req.body, 'a partial section object');
+ const { section } = findSection(config, req.params.sid);
+ Object.assign(section, req.body);
+ return { section };
+ }));
+
+ router.delete('/config/:filename/sections/:sid', requireAdmin, update((req, config) => {
+ const { sections, index } = findSection(config, req.params.sid);
+ sections.splice(index, 1);
+ }));
+
+ router.get('/config/:filename/sections/:sid/items', requireAuth, route(async (req, res) => {
+ const config = await readConfig(req.params.filename);
+ res.json(getArray(findSection(config, req.params.sid).section, 'items'));
+ }));
+
+ router.post('/config/:filename/sections/:sid/items', requireAdmin, update((req, config) => {
+ requireObjectBody(req.body, 'an item object');
+ if (!req.body.title) throw new ApiError("Item must have a 'title'");
+ const items = getArray(findSection(config, req.params.sid).section, 'items', true);
+ items.push(req.body);
+ return { index: items.length - 1, item: req.body };
+ }, 201));
+
+ router.get('/config/:filename/sections/:sid/items/:iid', requireAuth, route(async (req, res) => {
+ const config = await readConfig(req.params.filename);
+ const { section } = findSection(config, req.params.sid);
+ res.json(findItem(section, req.params.iid).item);
+ }));
+
+ router.patch('/config/:filename/sections/:sid/items/:iid', requireAdmin, update((req, config) => {
+ requireObjectBody(req.body, 'a partial item object');
+ const { section } = findSection(config, req.params.sid);
+ const { item } = findItem(section, req.params.iid);
+ Object.assign(item, req.body);
+ return { item };
+ }));
+
+ router.delete('/config/:filename/sections/:sid/items/:iid', requireAdmin, update((req, config) => {
+ const { section } = findSection(config, req.params.sid);
+ const { items, index } = findItem(section, req.params.iid);
+ items.splice(index, 1);
+ }));
+
+ router.get('/config/:filename/:key', requireAuth, route(async (req, res) => {
+ checkKey(req.params.key);
+ const config = await readConfig(req.params.filename);
+ if (config[req.params.key] === undefined) {
+ throw new ApiError(`'${req.params.key}' not present in config`, 404);
+ }
+ res.json(config[req.params.key]);
+ }));
+
+ router.put('/config/:filename/:key', requireAdmin, update((req, config) => {
+ checkKey(req.params.key);
+ config[req.params.key] = req.body;
+ }));
+
+ router.use((req, res) => res.status(404).json({ success: false, message: 'Not found' }));
+
+ return router;
+};
+
+module.exports = { apiEnabledGate, apiErrorHandler, createApiRouter };
diff --git a/services/app.js b/services/app.js
index b46d3eb3..fd24b874 100644
--- a/services/app.js
+++ b/services/app.js
@@ -31,6 +31,7 @@ const systemInfo = require('./system-info'); // Basic system info, for resource
const sslServer = require('./ssl-server'); // TLS-enabled web server
const corsProxy = require('./cors-proxy'); // Enables API requests to CORS-blocked services
const getUser = require('./get-user'); // Enables server side user lookup
+const { apiEnabledGate, apiErrorHandler, createApiRouter } = require('./api'); // Opt-in REST API
const { loadOidcSettings, createOidcMiddleware, maybeBootstrapConfig } = require('./auth-oidc');
@@ -44,6 +45,7 @@ const ENDPOINTS = {
systemInfo: '/system-info',
corsProxy: '/cors-proxy',
getUser: '/get-user',
+ api: '/api',
};
/* Read package version once at startup, so healthcheck never touches the disk per-request */
@@ -298,6 +300,13 @@ const app = express()
safeEnd(res, errBody(e));
}
}))
+ // REST API for reading / writing config files (no-op 404 unless ENABLE_API=true)
+ .use(ENDPOINTS.api, apiEnabledGate, protectConfig, createApiRouter({
+ requireAuth,
+ requireAdmin,
+ onConfigSaved: (filename, newConf) => { if (filename === 'conf.yml') config = newConf; },
+ }))
+ .use(ENDPOINTS.api, apiErrorHandler)
// Middleware to serve any .yml/.yaml files in USER_DATA_DIR with optional protection
// Note: returns stripped version if auth configured but not yet authenticated
.get(/\.ya?ml$/i, bootstrapAuth, (req, res) => {
diff --git a/tests/server/api-auth.test.js b/tests/server/api-auth.test.js
new file mode 100644
index 00000000..a7352c58
--- /dev/null
+++ b/tests/server/api-auth.test.js
@@ -0,0 +1,57 @@
+// @vitest-environment node
+import { describe, it, expect, afterAll } from 'vitest';
+import request from 'supertest';
+import fs from 'fs';
+import os from 'os';
+import path from 'path';
+
+// Auth strategy is chosen when the app module loads, so env must be set first
+const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dashy-api-auth-'));
+process.env.USER_DATA_DIR = tmpDir;
+process.env.ENABLE_API = 'true';
+process.env.BASIC_AUTH_USERNAME = 'admin';
+process.env.BASIC_AUTH_PASSWORD = 'test-pass';
+fs.writeFileSync(path.join(tmpDir, 'conf.yml'), 'pageInfo:\n title: Test\nsections: []\n');
+
+afterAll(() => {
+ fs.rmSync(tmpDir, { recursive: true, force: true });
+ delete process.env.ENABLE_API;
+ delete process.env.BASIC_AUTH_USERNAME;
+ delete process.env.BASIC_AUTH_PASSWORD;
+});
+
+const app = require('../../services/app');
+
+describe('API auth', () => {
+ it('rejects unauthenticated requests', async () => {
+ const res = await request(app).get('/api/config');
+ expect(res.status).toBe(401);
+ });
+
+ it('rejects incorrect credentials', async () => {
+ const res = await request(app).get('/api/config').auth('admin', 'wrong');
+ expect(res.status).toBe(401);
+ });
+
+ it('allows authenticated reads', async () => {
+ const res = await request(app).get('/api/config').auth('admin', 'test-pass');
+ expect(res.status).toBe(200);
+ expect(res.body.files).toContain('conf.yml');
+ });
+
+ it('allows authenticated writes', async () => {
+ const res = await request(app).put('/api/config/conf.yml')
+ .auth('admin', 'test-pass')
+ .send({ pageInfo: { title: 'Updated' }, sections: [] });
+ expect(res.status).toBe(200);
+ expect(res.body.success).toBe(true);
+ });
+
+ it('responds with the disabled message before any auth challenge', async () => {
+ process.env.ENABLE_API = 'false';
+ const res = await request(app).get('/api/config');
+ expect(res.status).toBe(404);
+ expect(res.headers['www-authenticate']).toBeUndefined();
+ process.env.ENABLE_API = 'true';
+ });
+});
diff --git a/tests/server/api.test.js b/tests/server/api.test.js
new file mode 100644
index 00000000..9a38c351
--- /dev/null
+++ b/tests/server/api.test.js
@@ -0,0 +1,282 @@
+// @vitest-environment node
+import { describe, it, expect, beforeEach, afterAll } from 'vitest';
+import request from 'supertest';
+import fs from 'fs';
+import os from 'os';
+import path from 'path';
+import yaml from 'js-yaml';
+
+// Isolate writes to a temp dir so the real user-data/conf.yml is never touched
+const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dashy-api-'));
+process.env.USER_DATA_DIR = tmpDir;
+process.env.ENABLE_API = 'true';
+afterAll(() => {
+ fs.rmSync(tmpDir, { recursive: true, force: true });
+ delete process.env.ENABLE_API;
+});
+
+const app = require('../../services/app');
+
+const seedConf = `pageInfo:
+ title: Test Dashboard
+sections:
+ - name: Section One
+ items:
+ - title: Item A
+ url: https://example.com/a
+ - title: Item B
+ - name: Section Two
+`;
+
+const readDisk = (file) => yaml.load(fs.readFileSync(path.join(tmpDir, file), 'utf8'));
+
+beforeEach(() => {
+ fs.writeFileSync(path.join(tmpDir, 'conf.yml'), seedConf);
+ fs.writeFileSync(path.join(tmpDir, 'page2.yml'), 'pageInfo:\n title: Page Two\n');
+});
+
+describe('API enablement gate', () => {
+ it('returns 404 when ENABLE_API is not true', async () => {
+ process.env.ENABLE_API = 'false';
+ const res = await request(app).get('/api/config');
+ expect(res.status).toBe(404);
+ expect(res.body.message).toContain('API not enabled');
+ process.env.ENABLE_API = 'true';
+ });
+
+ it('returns 404 when ENABLE_API is unset', async () => {
+ delete process.env.ENABLE_API;
+ const res = await request(app).get('/api/config');
+ expect(res.status).toBe(404);
+ process.env.ENABLE_API = 'true';
+ });
+
+ it('returns 404 for unknown API routes', async () => {
+ const res = await request(app).get('/api/nonsense');
+ expect(res.status).toBe(404);
+ expect(res.body.message).toBe('Not found');
+ });
+});
+
+describe('List and read config', () => {
+ it('lists config files, excluding backups', async () => {
+ const res = await request(app).get('/api/config');
+ expect(res.status).toBe(200);
+ expect(res.body.files).toContain('conf.yml');
+ expect(res.body.files).toContain('page2.yml');
+ expect(res.body.files).not.toContain('config-backups');
+ });
+
+ it('returns a config file as JSON', async () => {
+ const res = await request(app).get('/api/config/conf.yml');
+ expect(res.status).toBe(200);
+ expect(res.body.pageInfo.title).toBe('Test Dashboard');
+ expect(res.body.sections).toHaveLength(2);
+ });
+
+ it('404s for a missing file', async () => {
+ const res = await request(app).get('/api/config/nope.yml');
+ expect(res.status).toBe(404);
+ });
+
+ it('rejects path traversal', async () => {
+ const res = await request(app).get('/api/config/..%2F..%2Fetc%2Fpasswd');
+ expect(res.status).toBe(400);
+ });
+
+ it('rejects non-yaml filenames', async () => {
+ const res = await request(app).get('/api/config/evil.txt');
+ expect(res.status).toBe(400);
+ });
+
+ it('returns a top-level key', async () => {
+ const res = await request(app).get('/api/config/conf.yml/pageInfo');
+ expect(res.status).toBe(200);
+ expect(res.body.title).toBe('Test Dashboard');
+ });
+
+ it('rejects a non-whitelisted key', async () => {
+ const res = await request(app).get('/api/config/conf.yml/banana');
+ expect(res.status).toBe(400);
+ });
+
+ it('404s for an absent key', async () => {
+ const res = await request(app).get('/api/config/conf.yml/appConfig');
+ expect(res.status).toBe(404);
+ });
+
+ it('500s for unparseable YAML', async () => {
+ fs.writeFileSync(path.join(tmpDir, 'broken.yml'), 'foo: [unclosed');
+ const res = await request(app).get('/api/config/broken.yml');
+ expect(res.status).toBe(500);
+ });
+});
+
+describe('Replace config and keys', () => {
+ it('replaces a whole config file and makes a backup', async () => {
+ const res = await request(app).put('/api/config/conf.yml')
+ .send({ pageInfo: { title: 'Replaced' }, sections: [] });
+ expect(res.status).toBe(200);
+ expect(res.body.success).toBe(true);
+ expect(readDisk('conf.yml').pageInfo.title).toBe('Replaced');
+ const backups = fs.readdirSync(path.join(tmpDir, 'config-backups'));
+ expect(backups.some((f) => f.startsWith('conf-'))).toBe(true);
+ });
+
+ it('rejects schema-invalid conf.yml writes', async () => {
+ const res = await request(app).put('/api/config/conf.yml')
+ .send({ sections: 'not-a-list' });
+ expect(res.status).toBe(400);
+ expect(res.body.message).toContain('schema');
+ expect(readDisk('conf.yml').sections).toHaveLength(2);
+ });
+
+ it('accepts a pageInfo-only sub-page write', async () => {
+ const res = await request(app).put('/api/config/page2.yml')
+ .send({ pageInfo: { title: 'Updated Page' } });
+ expect(res.status).toBe(200);
+ expect(readDisk('page2.yml').pageInfo.title).toBe('Updated Page');
+ });
+
+ it('rejects oversized configs', async () => {
+ const res = await request(app).put('/api/config/conf.yml')
+ .send({ sections: [{ name: 'x'.repeat(300 * 1024) }] });
+ expect(res.status).toBe(400);
+ expect(res.body.message).toContain('256');
+ });
+
+ it('replaces a top-level key', async () => {
+ const res = await request(app).put('/api/config/conf.yml/pageInfo')
+ .send({ title: 'New Title' });
+ expect(res.status).toBe(200);
+ expect(readDisk('conf.yml').pageInfo.title).toBe('New Title');
+ });
+});
+
+describe('Sections CRUD', () => {
+ it('adds a section', async () => {
+ const res = await request(app).post('/api/config/conf.yml/sections')
+ .send({ name: 'Section Three' });
+ expect(res.status).toBe(201);
+ expect(res.body.index).toBe(2);
+ expect(readDisk('conf.yml').sections).toHaveLength(3);
+ });
+
+ it('rejects a section without a name', async () => {
+ const res = await request(app).post('/api/config/conf.yml/sections').send({});
+ expect(res.status).toBe(400);
+ });
+
+ it('gets a section by index', async () => {
+ const res = await request(app).get('/api/config/conf.yml/sections/0');
+ expect(res.body.name).toBe('Section One');
+ });
+
+ it('gets a section by name', async () => {
+ const res = await request(app).get('/api/config/conf.yml/sections/Section%20Two');
+ expect(res.body.name).toBe('Section Two');
+ });
+
+ it('patches a section, leaving other fields untouched', async () => {
+ const res = await request(app).patch('/api/config/conf.yml/sections/0')
+ .send({ icon: 'fas fa-rocket' });
+ expect(res.status).toBe(200);
+ const section = readDisk('conf.yml').sections[0];
+ expect(section.icon).toBe('fas fa-rocket');
+ expect(section.items).toHaveLength(2);
+ });
+
+ it('deletes a section', async () => {
+ const res = await request(app).delete('/api/config/conf.yml/sections/1');
+ expect(res.status).toBe(200);
+ expect(readDisk('conf.yml').sections).toHaveLength(1);
+ });
+
+ it('404s for an unknown section', async () => {
+ expect((await request(app).get('/api/config/conf.yml/sections/99')).status).toBe(404);
+ expect((await request(app).get('/api/config/conf.yml/sections/Nope')).status).toBe(404);
+ });
+
+ it('400s when sections is not a list', async () => {
+ fs.writeFileSync(path.join(tmpDir, 'scalar.yml'), 'sections: just-a-string\n');
+ const res = await request(app).get('/api/config/scalar.yml/sections/0');
+ expect(res.status).toBe(400);
+ });
+});
+
+describe('Items CRUD', () => {
+ it('lists items, defaulting to empty for a section without any', async () => {
+ expect((await request(app).get('/api/config/conf.yml/sections/0/items')).body).toHaveLength(2);
+ expect((await request(app).get('/api/config/conf.yml/sections/1/items')).body).toEqual([]);
+ });
+
+ it('adds an item, creating the items array if missing', async () => {
+ const res = await request(app).post('/api/config/conf.yml/sections/1/items')
+ .send({ title: 'New Item' });
+ expect(res.status).toBe(201);
+ expect(readDisk('conf.yml').sections[1].items[0].title).toBe('New Item');
+ });
+
+ it('rejects an item without a title', async () => {
+ const res = await request(app).post('/api/config/conf.yml/sections/0/items').send({});
+ expect(res.status).toBe(400);
+ });
+
+ it('gets an item by index and by title', async () => {
+ expect((await request(app).get('/api/config/conf.yml/sections/0/items/1')).body.title).toBe('Item B');
+ expect((await request(app).get('/api/config/conf.yml/sections/0/items/Item%20A')).body.title).toBe('Item A');
+ });
+
+ it('patches an item', async () => {
+ const res = await request(app).patch('/api/config/conf.yml/sections/0/items/0')
+ .send({ url: 'https://example.com/new' });
+ expect(res.status).toBe(200);
+ const item = readDisk('conf.yml').sections[0].items[0];
+ expect(item.url).toBe('https://example.com/new');
+ expect(item.title).toBe('Item A');
+ });
+
+ it('deletes an item', async () => {
+ const res = await request(app).delete('/api/config/conf.yml/sections/0/items/0');
+ expect(res.status).toBe(200);
+ expect(readDisk('conf.yml').sections[0].items).toHaveLength(1);
+ });
+
+ it('404s for an unknown item', async () => {
+ const res = await request(app).get('/api/config/conf.yml/sections/0/items/99');
+ expect(res.status).toBe(404);
+ });
+});
+
+describe('Robustness', () => {
+ it('returns JSON for malformed request bodies', async () => {
+ const res = await request(app).post('/api/config/conf.yml/sections')
+ .set('Content-Type', 'application/json').send('{not json');
+ expect(res.status).toBe(400);
+ expect(res.body.success).toBe(false);
+ });
+
+ it('returns JSON for bodies over the 1mb parser limit', async () => {
+ const res = await request(app).put('/api/config/conf.yml')
+ .set('Content-Type', 'application/json')
+ .send(`{"sections": [{"name": "${'x'.repeat(1100 * 1024)}"}]}`);
+ expect(res.status).toBe(413);
+ expect(res.body.success).toBe(false);
+ });
+
+
+ it('does not write when the existing file is unparseable', async () => {
+ fs.writeFileSync(path.join(tmpDir, 'broken.yml'), 'foo: [unclosed');
+ const res = await request(app).patch('/api/config/broken.yml/sections/0').send({ name: 'x' });
+ expect(res.status).toBe(500);
+ expect(fs.readFileSync(path.join(tmpDir, 'broken.yml'), 'utf8')).toBe('foo: [unclosed');
+ });
+
+ it('handles concurrent writes without corrupting the file', async () => {
+ const patch = (icon) => request(app)
+ .patch('/api/config/conf.yml/sections/0').send({ icon });
+ const results = await Promise.all(['a', 'b', 'c', 'd', 'e'].map(patch));
+ results.forEach((res) => expect(res.status).toBe(200));
+ expect(() => readDisk('conf.yml')).not.toThrow();
+ });
+});