Skip to content

Commit be8b974

Browse files
authored
Merge pull request #1560 from AndrewTr0612/issue1534
Fix unit route so not overwrite unspecified values - Team B
2 parents a35cdd0 + d5cf382 commit be8b974

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/server/routes/units.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,11 @@ router.post('/edit', adminAuthMiddleware('edit units'), async (req, res) => {
100100
// Suffix changes so some conversions and units need to be removed.
101101
await removeAdditionalConversionsAndUnits(unit, conn);
102102
}
103-
unit.name = req.body.name;
104-
unit.displayable = req.body.displayable;
105-
unit.identifier = req.body.identifier;
106-
unit.unitRepresent = req.body.unitRepresent;
107-
unit.typeOfUnit = req.body.typeOfUnit;
108-
unit.preferredDisplay = req.body.preferredDisplay;
109-
unit.secInRate = req.body.secInRate;
110-
unit.suffix = req.body.suffix;
111-
unit.note = req.body.note;
112-
unit.minVal = req.body.minVal;
113-
unit.maxVal = req.body.maxVal;
114-
unit.disableChecks = req.body.disableChecks;
103+
for (const key of Object.keys(unit)) {
104+
if (Object.hasOwn(req.body, key)) {
105+
unit[key] = req.body[key]
106+
}
107+
}
115108
// TODO Consider if this might be a better way.
116109
// Object.assign(unit, req.body);
117110
await unit.update(conn);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
const { mocha, expect, testDB, app, testUser } = require('../common');
6+
const chai = require('chai');
7+
const Unit = require('../../models/Unit');
8+
9+
mocha.describe('Units Route', () => {
10+
let token;
11+
12+
mocha.before(async () => {
13+
const res = await chai.request(app).post('/api/login')
14+
.send({ username: testUser.username, password: testUser.password });
15+
token = res.body.token;
16+
});
17+
18+
mocha.describe('Edit endpoint', () => {
19+
20+
mocha.it('returns 200 and updates the database for valid request', async () => {
21+
const conn = testDB.getConnection();
22+
const unit = new Unit(undefined, 'Unit', 'Unit Id', Unit.unitRepresentType.QUANTITY,
23+
1000, Unit.unitType.UNIT, 'Suffix', Unit.displayableType.ALL, true, 'Note');
24+
await unit.insert(conn);
25+
const beforeNote = unit.note;
26+
const res = await chai.request(app).post('/api/units/edit').set('token', token).send({
27+
id: unit.id,
28+
name: 'New name',
29+
identifier: unit.identifier,
30+
});
31+
expect(res).to.have.status(200);
32+
const updatedUnit = await Unit.getById(unit.id, conn);
33+
expect(updatedUnit.name).to.equal('New name');
34+
expect(updatedUnit.note).to.equal(beforeNote);
35+
});
36+
37+
});
38+
39+
});

0 commit comments

Comments
 (0)