diff --git a/api/measurement.yaml b/api/measurement.yaml index 3068d97..0c29e77 100644 --- a/api/measurement.yaml +++ b/api/measurement.yaml @@ -100,6 +100,29 @@ 500: $ref: 'api.yaml#/components/responses/500' +/measurement/validate/{id}: + parameters: + - $ref: 'api.yaml#/components/parameters/Id' + put: + summary: set measurement status to validated + description: 'Auth: basic, levels: maintain, admin' + x-doc: status is set to 10 + tags: + - /measurement + security: + - BasicAuth: [] + responses: + 200: + $ref: 'api.yaml#/components/responses/Ok' + 401: + $ref: 'api.yaml#/components/responses/401' + 403: + $ref: 'api.yaml#/components/responses/403' + 404: + $ref: 'api.yaml#/components/responses/404' + 500: + $ref: 'api.yaml#/components/responses/500' + /measurement/new: post: summary: add measurement @@ -129,4 +152,4 @@ 403: $ref: 'api.yaml#/components/responses/403' 500: - $ref: 'api.yaml#/components/responses/500' \ No newline at end of file + $ref: 'api.yaml#/components/responses/500' diff --git a/src/routes/measurement.spec.ts b/src/routes/measurement.spec.ts index af21400..8bef49b 100644 --- a/src/routes/measurement.spec.ts +++ b/src/routes/measurement.spec.ts @@ -3,7 +3,6 @@ import MeasurementModel from '../models/measurement'; import TestHelper from "../test/helper"; import globals from '../globals'; -// TODO: test unique material names and produced error code describe('/measurement', () => { let server; @@ -421,6 +420,61 @@ describe('/measurement', () => { }); }); + describe('PUT /measurement/validate/{id}', () => { + it('sets the status', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/validate/800000000000000000000003', + auth: {basic: 'admin'}, + httpStatus: 200, + req: {} + }).end((err, res) => { + if (err) return done (err); + should(res.body).be.eql({status: 'OK'}); + MeasurementModel.findById('800000000000000000000003').lean().exec((err, data: any) => { + if (err) return done(err); + should(data).have.property('status',globals.status.validated); + done(); + }); + }); + }); + it('rejects an API key', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/validate/800000000000000000000003', + auth: {key: 'admin'}, + httpStatus: 401, + req: {} + }); + }); + it('rejects a write user', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/validate/800000000000000000000003', + auth: {basic: 'janedoe'}, + httpStatus: 403, + req: {} + }); + }); + it('returns 404 for an unknown sample', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/validate/000000000000000000000003', + auth: {basic: 'admin'}, + httpStatus: 404, + req: {} + }); + }); + it('rejects unauthorized requests', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/validate/800000000000000000000003', + httpStatus: 401, + req: {} + }); + }); + }); + describe('POST /measurement/new', () => { it('returns the right measurement', done => { TestHelper.request(server, done, { diff --git a/src/routes/measurement.ts b/src/routes/measurement.ts index e7f6271..2b94060 100644 --- a/src/routes/measurement.ts +++ b/src/routes/measurement.ts @@ -81,14 +81,13 @@ router.delete('/measurement/' + IdValidate.parameter(), (req, res, next) => { router.put('/measurement/restore/' + IdValidate.parameter(), (req, res, next) => { if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; - MeasurementModel.findByIdAndUpdate(req.params.id, {status: globals.status.new}).lean().exec((err, data) => { - if (err) return next(err); + setStatus(globals.status.new, req, res, next); +}); - if (!data) { - return res.status(404).json({status: 'Not found'}); - } - res.json({status: 'OK'}); - }); +router.put('/measurement/validate/' + IdValidate.parameter(), (req, res, next) => { + if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; + + setStatus(globals.status.validated, req, res, next); }); router.post('/measurement/new', async (req, res, next) => { @@ -146,4 +145,15 @@ async function templateCheck (measurement, param, res, next) { // validate meas const {error, value} = ParametersValidate.input(measurement.values, templateData.parameters, 'null'); if (error) {res400(error, res); return false;} return value || true; +} + +function setStatus (status, req, res, next) { // set measurement status + MeasurementModel.findByIdAndUpdate(req.params.id, {status: status}).lean().exec((err, data) => { + if (err) return next(err); + + if (!data) { + return res.status(404).json({status: 'Not found'}); + } + res.json({status: 'OK'}); + }); } \ No newline at end of file