Archived
2

added /measurement/sample/{id} route

This commit is contained in:
VLE2FE 2020-08-21 14:01:01 +02:00
parent 47955ec572
commit 8d894fa27e
4 changed files with 110 additions and 3 deletions

View File

@ -77,6 +77,40 @@
500:
$ref: 'api.yaml#/components/responses/500'
/measurement/sample/{id}:
parameters:
- $ref: 'api.yaml#/components/parameters/Id'
get:
summary: all measurements of the given sample
description: 'Auth: basic, levels: dev, admin'
tags:
- /measurement
security:
- BasicAuth: []
responses:
200:
description: measurement details
content:
application/json:
schema:
type: array
items:
allOf:
- $ref: 'api.yaml#/components/schemas/Measurement'
properties:
status:
type: string
description: can be deleted/new/validated
example: new
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/restore/{id}:
parameters:
- $ref: 'api.yaml#/components/parameters/Id'

View File

@ -404,6 +404,60 @@ describe('/measurement', () => {
});
});
describe('GET /measurement/sample/{id}', () => {
it('returns the right measurements', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/sample/400000000000000000000003',
auth: {basic: 'admin'},
httpStatus: 200,
res: [
{_id: '800000000000000000000003', sample_id: '400000000000000000000003', values: {val1: 1}, measurement_template: '300000000000000000000003', status: 'new'},
{_id: '800000000000000000000004', sample_id: '400000000000000000000003', values: {val1: 1}, measurement_template: '300000000000000000000003', status: 'deleted'}
]
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/sample/400000000000000000000003',
auth: {key: 'admin'},
httpStatus: 401
});
});
it('rejects a write user', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/sample/400000000000000000000003',
auth: {basic: 'janedoe'},
httpStatus: 403
});
});
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/sample/4000000000h0000000000003',
auth: {basic: 'admin'},
httpStatus: 404
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/sample/000000000000000000000003',
auth: {basic: 'admin'},
httpStatus: 404
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/sample/400000000000000000000003',
httpStatus: 401
});
});
});
describe('PUT /measurement/restore/{id}', () => {
it('sets the status', done => {
TestHelper.request(server, done, {

View File

@ -10,6 +10,7 @@ import res400 from './validate/res400';
import ParametersValidate from './validate/parameters';
import db from '../db';
import globals from '../globals';
import mongoose from "mongoose";
const router = express.Router();
@ -82,6 +83,19 @@ router.delete('/measurement/' + IdValidate.parameter(), (req, res, next) => {
});
});
router.get('/measurement/sample/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;
MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data: any) => {
if (err) return next(err);
if (!data.length) {
return res.status(404).json({status: 'Not found'});
}
res.json(_.compact(data.map(e => MeasurementValidate.output(e, req, true))));
});
});
router.put('/measurement/restore/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['dev', 'admin'], 'basic')) return;

View File

@ -35,18 +35,23 @@ export default class MeasurementValidate {
}
}
static output (data, req) { // validate output and strip unwanted properties, returns null if not valid
// validate output and strip unwanted properties, returns null if not valid
static output (data, req, status = false) {
data = IdValidate.stringify(data);
// spectral data not allowed for read/write users
if (['dev', 'admin'].indexOf(req.authDetails.level) < 0 && data.values[globals.spectrum.dpt]) {
delete data.values[globals.spectrum.dpt];
}
const {value, error} = Joi.object({
const validation: any = {
_id: IdValidate.get(),
sample_id: IdValidate.get(),
values: this.measurement.values,
measurement_template: IdValidate.get()
}).validate(data, {stripUnknown: true});
};
if (status) {
validation.status = Joi.string().valid(...Object.values(globals.status));
}
const {value, error} = Joi.object(validation).validate(data, {stripUnknown: true});
return error !== undefined? null : value;
}