diff --git a/api.py b/api.py index 72be04e1f8..9028ee4e25 100755 --- a/api.py +++ b/api.py @@ -37,6 +37,8 @@ If *nix: chmod +x api.py => ./api.py users python3 api.py lists BOARDID # Lists of BOARDID python3 api.py list BOARDID LISTID # Info of LISTID python3 api.py createlist BOARDID LISTTITLE # Create list + python3 api.py editlist BOARDID LISTID NEWLISTTITLE # Edit list title + python3 api.py editlistcolor BOARDID LISTID COLOR (Color available: `white`, `green`, `yellow`, `orange`, `red`, `purple`, `blue`, `sky`, `lime`, `pink`, `black`, `silver`, `peachpuff`, `crimson`, `plum`, `darkgreen`, `slateblue`, `magenta`, `gold`, `navy`, `gray`, `saddlebrown`, `paleturquoise`, `mistyrose`, `indigo`) # Edit list color python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION python3 api.py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION python3 api.py getcard BOARDID LISTID CARDID # Get card info @@ -432,6 +434,49 @@ if arguments == 5: if arguments >= 4: + if sys.argv[1] == 'editlist': + + # ------- EDIT LIST START ----------- + boardid = sys.argv[2] + listid = sys.argv[3] + newlisttitle = sys.argv[4] + edlist = wekanurl + apiboards + boardid + s + l + s + listid + print(edlist) + headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)} + put_data = {'title': '{}'.format(newlisttitle)} + body = requests.put(edlist, data=put_data, headers=headers) + print("=== EDIT LIST ===\n") + body = requests.get(edlist, headers=headers) + data2 = body.text.replace('}', "}\n") + print(data2) + # ------- EDIT LIST END ----------- + + if sys.argv[1] == 'editlistcolor': + + # ------- EDIT LIST COLOR START ----------- + boardid = sys.argv[2] + listid = sys.argv[3] + newcolor = sys.argv[4] + + valid_colors = ['white', 'green', 'yellow', 'orange', 'red', 'purple', 'blue', 'sky', 'lime', 'pink', 'black', + 'silver', 'peachpuff', 'crimson', 'plum', 'darkgreen', 'slateblue', 'magenta', 'gold', 'navy', + 'gray', 'saddlebrown', 'paleturquoise', 'mistyrose', 'indigo'] + + if newcolor not in valid_colors: + print("Invalid color. Choose a color from the list.") + sys.exit(1) + + edlist = wekanurl + apiboards + boardid + s + l + s + listid + print(edlist) + headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)} + put_data = {'color': '{}'.format(newcolor)} + body = requests.put(edlist, data=put_data, headers=headers) + print("=== EDIT LIST COLOR ===\n") + body = requests.get(edlist, headers=headers) + data2 = body.text.replace('}', "}\n") + print(data2) + # ------- EDIT LIST COLOR END ----------- + if sys.argv[1] == 'newuser': # ------- CREATE NEW USER START ----------- diff --git a/models/lib/listApiUpdate.js b/models/lib/listApiUpdate.js new file mode 100644 index 0000000000..e3158d2f70 --- /dev/null +++ b/models/lib/listApiUpdate.js @@ -0,0 +1,66 @@ +'use strict'; + +// Pure, Meteor-free helper for #5396: let REST-API clients edit a list's +// title and color (like cards can be edited) through +// PUT /api/boards/:boardId/lists/:listId. Kept CommonJS + dependency-free so +// the exact same field/validation logic runs on the server and in plain-Node +// unit tests (mirrors models/lib/boardSortReorder.js and +// models/lib/contrastColor.js). +// +// `normalizeColor` is injected (the server passes `normalizeListColor` from +// models/lists.js) so this module never imports anything Meteor/Mongo. That +// normalizer returns an allowed named palette color or a custom '#rrggbb' hex +// unchanged, and '' for anything else — so an unknown color is rejected here +// with a clear error instead of being silently stored as None. + +const MAX_TITLE_LENGTH = 1000; + +// Build the { $set } update document for a list PUT body. Returns either +// { set: {...} } the fields to update (may be empty when nothing given) +// { error: 'message' } when a provided field is invalid (e.g. a bad color) +// so the endpoint can answer with a clear 4xx instead of hanging or 500-ing. +function buildListPutUpdate(body, normalizeColor) { + const src = body || {}; + const set = {}; + + if ( + src.title !== undefined && + src.title !== null && + String(src.title) !== '' + ) { + const title = String(src.title); + set.title = + title.length > MAX_TITLE_LENGTH + ? title.substring(0, MAX_TITLE_LENGTH) + : title; + } + + if ( + src.color !== undefined && + src.color !== null && + String(src.color) !== '' + ) { + const normalize = + typeof normalizeColor === 'function' ? normalizeColor : c => c; + const color = normalize(String(src.color)); + if (!color) { + return { + error: + 'Invalid list color. Use a named palette color or a #rrggbb hex value.', + }; + } + set.color = color; + } + + if (Object.prototype.hasOwnProperty.call(src, 'starred')) { + set.starred = src.starred; + } + + if (src.wipLimit) { + set.wipLimit = src.wipLimit; + } + + return { set }; +} + +module.exports = { buildListPutUpdate, MAX_TITLE_LENGTH }; diff --git a/server/models/lists.js b/server/models/lists.js index 2d1a834b36..6c7cc55f0f 100644 --- a/server/models/lists.js +++ b/server/models/lists.js @@ -7,9 +7,10 @@ import { ReactiveCache } from '/imports/reactiveCache'; import Activities from '/models/activities'; import Boards from '/models/boards'; import Cards from '/models/cards'; -import Lists from '/models/lists'; +import Lists, { normalizeListColor } from '/models/lists'; import { ensureIndex } from '/server/lib/mongoStartup'; import { computeSortForIndex } from '/server/lib/utils'; +import { buildListPutUpdate } from '/models/lib/listApiUpdate'; const hasBoardWriteAccess = (userId, board) => { if (!userId || !board) { @@ -608,35 +609,23 @@ WebApp.handlers.put('/api/boards/:boardId/lists/:listId', async function(req, re return; } - if (req.body.title) { - const newTitle = req.body.title.length > 1000 ? req.body.title.substring(0, 1000) : req.body.title; - await Lists.direct.updateAsync( - { _id: paramListId, boardId: paramBoardId, archived: false }, - { $set: { title: newTitle } }, - ); - updated = true; + // #5396: edit a list's title/color (and starred/wipLimit) via the REST API, + // like cards can be edited. The color is validated with normalizeListColor + // (named palette color OR custom '#rrggbb' hex); a bad color is rejected + // with a clear 4xx rather than being silently stored as None. The pure + // field/validation logic lives in models/lib/listApiUpdate.js so it is unit + // testable without Meteor. + const { set, error } = buildListPutUpdate(req.body, normalizeListColor); + + if (error) { + sendJsonResult(res, { code: 400, data: { error } }); + return; } - if (req.body.color) { + if (Object.keys(set).length > 0) { await Lists.direct.updateAsync( { _id: paramListId, boardId: paramBoardId, archived: false }, - { $set: { color: req.body.color } }, - ); - updated = true; - } - - if (req.body.hasOwnProperty('starred')) { - await Lists.direct.updateAsync( - { _id: paramListId, boardId: paramBoardId, archived: false }, - { $set: { starred: req.body.starred } }, - ); - updated = true; - } - - if (req.body.wipLimit) { - await Lists.direct.updateAsync( - { _id: paramListId, boardId: paramBoardId, archived: false }, - { $set: { wipLimit: req.body.wipLimit } }, + { $set: set }, ); updated = true; } diff --git a/tests/listApiUpdate.test.cjs b/tests/listApiUpdate.test.cjs new file mode 100644 index 0000000000..326d29133a --- /dev/null +++ b/tests/listApiUpdate.test.cjs @@ -0,0 +1,116 @@ +'use strict'; + +// Plain-Node unit test (no Meteor) for the pure helper behind #5396: +// editing a list's title/color via the REST API +// (PUT /api/boards/:boardId/lists/:listId), like cards can be edited. +// Run: node tests/listApiUpdate.test.cjs +// +// The endpoint feeds the request body through buildListPutUpdate() together +// with the injected color normalizer (normalizeListColor on the server). The +// helper must: accept a title (truncated at 1000 chars) and a valid color +// (named palette color OR '#rrggbb' hex), REJECT an unknown color with a clear +// error (so the endpoint answers 4xx instead of storing None), and keep the +// existing starred/wipLimit fields working. + +const assert = require('assert'); +const { buildListPutUpdate, MAX_TITLE_LENGTH } = require('../models/lib/listApiUpdate'); + +// Stand-in for models/lists.js normalizeListColor: a small set of named colors +// plus '#rrggbb' hex are allowed; everything else normalizes to '' (rejected). +const NAMED = ['white', 'green', 'red', 'blue', 'silver']; +function normalizeColor(color) { + if (typeof color !== 'string') return ''; + if (NAMED.includes(color)) return color; + return /^#[0-9a-fA-F]{6}$/.test(color) ? color : ''; +} + +let passed = 0; +function test(name, fn) { + fn(); + passed += 1; + console.log(' ok -', name); +} + +// --- title ------------------------------------------------------------------- +test('accepts a new list title', () => { + const { set, error } = buildListPutUpdate({ title: 'Doing' }, normalizeColor); + assert.strictEqual(error, undefined); + assert.strictEqual(set.title, 'Doing'); +}); + +test('truncates an over-long title to 1000 chars', () => { + const { set } = buildListPutUpdate({ title: 'x'.repeat(2000) }, normalizeColor); + assert.strictEqual(set.title.length, MAX_TITLE_LENGTH); +}); + +test('ignores an empty title (nothing to set)', () => { + const { set, error } = buildListPutUpdate({ title: '' }, normalizeColor); + assert.strictEqual(error, undefined); + assert.ok(!('title' in set)); +}); + +// --- color: POSITIVE --------------------------------------------------------- +test('accepts a named palette color', () => { + const { set, error } = buildListPutUpdate({ color: 'green' }, normalizeColor); + assert.strictEqual(error, undefined); + assert.strictEqual(set.color, 'green'); +}); + +test('#5514: accepts a custom #rrggbb hex color', () => { + const { set, error } = buildListPutUpdate({ color: '#a1b2c3' }, normalizeColor); + assert.strictEqual(error, undefined); + assert.strictEqual(set.color, '#a1b2c3'); +}); + +// --- color: NEGATIVE (must 4xx, never hang / store None) --------------------- +test('rejects an unknown color with a clear error and no color set', () => { + const { set, error } = buildListPutUpdate({ color: 'notacolor' }, normalizeColor); + assert.ok(error, 'an unknown color must produce an error'); + assert.ok(/color/i.test(error)); + assert.strictEqual(set, undefined); +}); + +test('rejects a malformed hex color', () => { + const { error } = buildListPutUpdate({ color: '#zzzzzz' }, normalizeColor); + assert.ok(error); +}); + +test('a bad color is rejected even when a valid title is also given', () => { + const { set, error } = buildListPutUpdate({ title: 'ok', color: 'bogus' }, normalizeColor); + assert.ok(error); + assert.strictEqual(set, undefined); +}); + +// --- existing fields keep working ------------------------------------------- +test('keeps starred working (including the falsy false value)', () => { + assert.strictEqual(buildListPutUpdate({ starred: true }, normalizeColor).set.starred, true); + assert.strictEqual(buildListPutUpdate({ starred: false }, normalizeColor).set.starred, false); +}); + +test('keeps wipLimit working', () => { + assert.strictEqual(buildListPutUpdate({ wipLimit: 5 }, normalizeColor).set.wipLimit, 5); +}); + +test('sets multiple valid fields at once', () => { + const { set, error } = buildListPutUpdate( + { title: 'Review', color: 'blue', starred: true }, + normalizeColor, + ); + assert.strictEqual(error, undefined); + assert.deepStrictEqual(set, { title: 'Review', color: 'blue', starred: true }); +}); + +// --- empty / no-op body ------------------------------------------------------ +test('an empty body yields an empty set and no error (endpoint treats as no-op)', () => { + const { set, error } = buildListPutUpdate({}, normalizeColor); + assert.strictEqual(error, undefined); + assert.deepStrictEqual(set, {}); +}); + +test('a missing body does not throw', () => { + const { set, error } = buildListPutUpdate(undefined, normalizeColor); + assert.strictEqual(error, undefined); + assert.deepStrictEqual(set, {}); +}); + +console.log(`\n${passed} tests passed`);