Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/server/routes/units.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,11 @@ router.post('/edit', adminAuthMiddleware('edit units'), async (req, res) => {
// Suffix changes so some conversions and units need to be removed.
await removeAdditionalConversionsAndUnits(unit, conn);
}
unit.name = req.body.name;
unit.displayable = req.body.displayable;
unit.identifier = req.body.identifier;
unit.unitRepresent = req.body.unitRepresent;
unit.typeOfUnit = req.body.typeOfUnit;
unit.preferredDisplay = req.body.preferredDisplay;
unit.secInRate = req.body.secInRate;
unit.suffix = req.body.suffix;
unit.note = req.body.note;
unit.minVal = req.body.minVal;
unit.maxVal = req.body.maxVal;
unit.disableChecks = req.body.disableChecks;
for (const key of Object.keys(unit)) {
if (Object.hasOwn(req.body, key)) {
unit[key] = req.body[key]
}
}
// TODO Consider if this might be a better way.
// Object.assign(unit, req.body);
await unit.update(conn);
Expand Down
39 changes: 39 additions & 0 deletions src/server/test/routes/unitsRouteTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const { mocha, expect, testDB, app, testUser } = require('../common');
const chai = require('chai');
const Unit = require('../../models/Unit');

mocha.describe('Units Route', () => {
let token;

mocha.before(async () => {
const res = await chai.request(app).post('/api/login')
.send({ username: testUser.username, password: testUser.password });
token = res.body.token;
});

mocha.describe('Edit endpoint', () => {

mocha.it('returns 200 and updates the database for valid request', async () => {
const conn = testDB.getConnection();
const unit = new Unit(undefined, 'Unit', 'Unit Id', Unit.unitRepresentType.QUANTITY,
1000, Unit.unitType.UNIT, 'Suffix', Unit.displayableType.ALL, true, 'Note');
await unit.insert(conn);
const beforeNote = unit.note;
const res = await chai.request(app).post('/api/units/edit').set('token', token).send({
id: unit.id,
name: 'New name',
identifier: unit.identifier,
});
expect(res).to.have.status(200);
const updatedUnit = await Unit.getById(unit.id, conn);
expect(updatedUnit.name).to.equal('New name');
expect(updatedUnit.note).to.equal(beforeNote);
});

});

});
Loading