mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
PUT /api/boards/:boardId/labels only wrote an HTTP response inside
`if (hasOwnProperty(req.body, "label"))` and read req.body.label.color/.name
unconditionally. Two consequences:
- A body without a `label` key fell through the `if` and NEVER sent a response,
so the request hung until the client timed out (the reported "timeout").
- A body where `label` was a bare string (e.g. { "label": "labelName" }, the
exact reported payload) produced name === undefined / color === undefined and
pushed a schema-invalid label via Boards.direct, returning 200 for garbage.
- The catch returned { code: 200, data: error }, masking real failures as
success (same anti-pattern already fixed for the board create/delete handlers).
The handler now returns a 404 for an unknown board, delegates input handling to
a new pure, Meteor-free helper models/lib/restLabel.js (buildBoardLabel), and
always sends a JSON response: 2xx for a well-formed create (documented contract
unchanged), 400 with a clear message for bad input, or the real error status in
the catch. buildBoardLabel accepts { label: { name, color } } (color defaulted
when absent, rejected when unknown) and, for convenience, a bare string taken as
the label name. Added tests/restLabel.test.cjs covering these cases.
97 lines
4.0 KiB
JavaScript
97 lines
4.0 KiB
JavaScript
'use strict';
|
|
|
|
// Plain-Node unit test (no Meteor) for the REST board-label input helper.
|
|
// Run: node tests/restLabel.test.cjs
|
|
//
|
|
// Regression guard for #5510 (adding a board label via the REST API fails with an
|
|
// internal server error / timeout). The old PUT /api/boards/:boardId/labels handler
|
|
// only responded inside `if (hasOwnProperty(req.body, 'label'))`, so a body without a
|
|
// `label` key produced NO response and the request hung; and a body where `label` was
|
|
// a bare string produced name === undefined / color === undefined and pushed a
|
|
// schema-invalid label. buildBoardLabel must always return a definite outcome:
|
|
// a valid { ok:true, name, color } doc for good input, or { ok:false, status:400, ... }
|
|
// for bad input — never throwing, never leaving the caller without something to send.
|
|
|
|
const assert = require('assert');
|
|
const { buildBoardLabel, DEFAULT_LABEL_COLOR } = require('../models/lib/restLabel');
|
|
|
|
// A representative slice of LABEL_COLORS (config/const.js ALLOWED_COLORS).
|
|
const COLORS = ['white', 'green', 'yellow', 'orange', 'red', 'blue'];
|
|
|
|
let passed = 0;
|
|
function test(name, fn) {
|
|
fn();
|
|
passed += 1;
|
|
console.log(' ok -', name);
|
|
}
|
|
|
|
// --- documented success contract (must be unchanged) ------------------------
|
|
test('valid { name, color } object builds the expected label doc', () => {
|
|
const r = buildBoardLabel({ name: 'Bug', color: 'red' }, COLORS);
|
|
assert.deepStrictEqual(r, { ok: true, name: 'Bug', color: 'red' });
|
|
});
|
|
|
|
// --- missing color is defaulted, not rejected/thrown ------------------------
|
|
test('object with a name but no color defaults the color sensibly', () => {
|
|
const r = buildBoardLabel({ name: 'Bug' }, COLORS);
|
|
assert.strictEqual(r.ok, true);
|
|
assert.strictEqual(r.name, 'Bug');
|
|
assert.strictEqual(r.color, DEFAULT_LABEL_COLOR);
|
|
assert.ok(COLORS.includes(r.color), 'defaulted color must be an allowed color');
|
|
});
|
|
|
|
test('empty-string color is treated as absent and defaulted', () => {
|
|
const r = buildBoardLabel({ name: 'Bug', color: '' }, COLORS);
|
|
assert.strictEqual(r.ok, true);
|
|
assert.strictEqual(r.color, DEFAULT_LABEL_COLOR);
|
|
});
|
|
|
|
// --- the exact reported payload: { "label": "labelName" } -------------------
|
|
test('#5510: bare string label is taken as the name (no throw, valid doc)', () => {
|
|
const r = buildBoardLabel('labelName', COLORS);
|
|
assert.strictEqual(r.ok, true);
|
|
assert.strictEqual(r.name, 'labelName');
|
|
assert.strictEqual(r.color, DEFAULT_LABEL_COLOR);
|
|
});
|
|
|
|
// --- missing label / empty body: rejected cleanly, never a hang -------------
|
|
test('#5510: missing label (undefined) is rejected with 400, not left to hang', () => {
|
|
const r = buildBoardLabel(undefined, COLORS);
|
|
assert.strictEqual(r.ok, false);
|
|
assert.strictEqual(r.status, 400);
|
|
assert.ok(typeof r.error === 'string' && r.error.length > 0);
|
|
});
|
|
|
|
test('null label is rejected with 400', () => {
|
|
const r = buildBoardLabel(null, COLORS);
|
|
assert.strictEqual(r.ok, false);
|
|
assert.strictEqual(r.status, 400);
|
|
});
|
|
|
|
// --- explicit but unknown color is rejected, not silently stored ------------
|
|
test('unknown color is rejected with 400', () => {
|
|
const r = buildBoardLabel({ name: 'Bug', color: 'chartreuse' }, COLORS);
|
|
assert.strictEqual(r.ok, false);
|
|
assert.strictEqual(r.status, 400);
|
|
assert.ok(/chartreuse/.test(r.error));
|
|
});
|
|
|
|
// --- non-string/non-object label is rejected -------------------------------
|
|
test('numeric label value is rejected with 400', () => {
|
|
const r = buildBoardLabel(42, COLORS);
|
|
assert.strictEqual(r.ok, false);
|
|
assert.strictEqual(r.status, 400);
|
|
});
|
|
|
|
// --- nothing ever throws ----------------------------------------------------
|
|
test('never throws for a range of odd inputs', () => {
|
|
const inputs = [undefined, null, '', 'x', 0, 42, true, [], {}, { color: 'red' }];
|
|
inputs.forEach(v => {
|
|
assert.doesNotThrow(() => buildBoardLabel(v, COLORS));
|
|
const r = buildBoardLabel(v, COLORS);
|
|
assert.ok(r && typeof r.ok === 'boolean', 'result must always have a boolean ok');
|
|
});
|
|
});
|
|
|
|
console.log(`\n${passed} tests passed.`);
|