From d924ee5a8cfc9d1f7644ff4af9de2d5f36c71522 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 28 May 2020 15:03:49 +0200 Subject: [PATCH] restore measurements --- api/measurement.yaml | 23 ++++++++++++++ src/routes/measurement.spec.ts | 57 ++++++++++++++++++++++++++++++++-- src/routes/measurement.ts | 13 ++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/api/measurement.yaml b/api/measurement.yaml index 9116a8c..3068d97 100644 --- a/api/measurement.yaml +++ b/api/measurement.yaml @@ -77,6 +77,29 @@ 500: $ref: 'api.yaml#/components/responses/500' +/measurement/restore/{id}: + parameters: + - $ref: 'api.yaml#/components/parameters/Id' + put: + summary: restore measurement + description: 'Auth: basic, levels: maintain, admin' + x-doc: status is set to 0 + 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 diff --git a/src/routes/measurement.spec.ts b/src/routes/measurement.spec.ts index 113847f..c27bf63 100644 --- a/src/routes/measurement.spec.ts +++ b/src/routes/measurement.spec.ts @@ -3,8 +3,6 @@ import MeasurementModel from '../models/measurement'; import TestHelper from "../test/helper"; import globals from '../globals'; -// TODO: restore measurements for m/a - describe('/measurement', () => { let server; @@ -367,6 +365,61 @@ describe('/measurement', () => { }); }); + describe('PUT /measurement/restore/{id}', () => { + it('sets the status', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/restore/800000000000000000000004', + auth: {basic: 'admin'}, + httpStatus: 200, + req: {} + }).end((err, res) => { + if (err) return done (err); + should(res.body).be.eql({status: 'OK'}); + MeasurementModel.findById('800000000000000000000004').lean().exec((err, data: any) => { + if (err) return done(err); + should(data).have.property('status',globals.status.new); + done(); + }); + }); + }); + it('rejects an API key', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/restore/800000000000000000000004', + auth: {key: 'admin'}, + httpStatus: 401, + req: {} + }); + }); + it('rejects a write user', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/restore/800000000000000000000004', + auth: {basic: 'janedoe'}, + httpStatus: 403, + req: {} + }); + }); + it('returns 404 for an unknown sample', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/restore/000000000000000000000004', + auth: {basic: 'admin'}, + httpStatus: 404, + req: {} + }); + }); + it('rejects unauthorized requests', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/measurement/restore/800000000000000000000004', + 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 ab9d50e..e7f6271 100644 --- a/src/routes/measurement.ts +++ b/src/routes/measurement.ts @@ -78,6 +78,19 @@ 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); + + if (!data) { + return res.status(404).json({status: 'Not found'}); + } + res.json({status: 'OK'}); + }); +}); + router.post('/measurement/new', async (req, res, next) => { if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;