Archived
2

validation for measurement

This commit is contained in:
VLE2FE 2020-05-29 11:28:35 +02:00
parent e342224b7f
commit ea81108251
3 changed files with 96 additions and 9 deletions

View File

@ -100,6 +100,29 @@
500: 500:
$ref: 'api.yaml#/components/responses/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: /measurement/new:
post: post:
summary: add measurement summary: add measurement
@ -129,4 +152,4 @@
403: 403:
$ref: 'api.yaml#/components/responses/403' $ref: 'api.yaml#/components/responses/403'
500: 500:
$ref: 'api.yaml#/components/responses/500' $ref: 'api.yaml#/components/responses/500'

View File

@ -3,7 +3,6 @@ import MeasurementModel from '../models/measurement';
import TestHelper from "../test/helper"; import TestHelper from "../test/helper";
import globals from '../globals'; import globals from '../globals';
// TODO: test unique material names and produced error code
describe('/measurement', () => { describe('/measurement', () => {
let server; 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', () => { describe('POST /measurement/new', () => {
it('returns the right measurement', done => { it('returns the right measurement', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {

View File

@ -81,14 +81,13 @@ router.delete('/measurement/' + IdValidate.parameter(), (req, res, next) => {
router.put('/measurement/restore/' + IdValidate.parameter(), (req, res, next) => { router.put('/measurement/restore/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
MeasurementModel.findByIdAndUpdate(req.params.id, {status: globals.status.new}).lean().exec((err, data) => { setStatus(globals.status.new, req, res, next);
if (err) return next(err); });
if (!data) { router.put('/measurement/validate/' + IdValidate.parameter(), (req, res, next) => {
return res.status(404).json({status: 'Not found'}); if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
}
res.json({status: 'OK'}); setStatus(globals.status.validated, req, res, next);
});
}); });
router.post('/measurement/new', async (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'); const {error, value} = ParametersValidate.input(measurement.values, templateData.parameters, 'null');
if (error) {res400(error, res); return false;} if (error) {res400(error, res); return false;}
return value || true; 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'});
});
} }