Archived
2

added required parameter range

This commit is contained in:
VLE2FE 2020-08-24 15:24:31 +02:00
parent 2c250e0111
commit 6702fb78c4
8 changed files with 122 additions and 447 deletions

View File

@ -176,6 +176,8 @@ Template:
example: kf
range:
type: object
description: keys can be min or max to define number boundaries, values to define allowed values, type,
being string, number, boolean or array and required to make the parameter required
example:
min: 0
max: 2

View File

@ -176,6 +176,7 @@ async function templateCheck (measurement, param, res, next) {
// validate values
const {error, value} = ParametersValidate.input(measurement.values, templateData.parameters, 'null');
console.log(error);
if (error) {res400(error, res); return false;}
return value || true;
}

View File

@ -1148,6 +1148,28 @@ describe('/sample', () => {
res: {status: 'Invalid body format', details: '"weeks" must be less than or equal to 10'}
});
});
it('rejects a missing condition parameter marked as required', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/sample/400000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {condition: {material: 'copper', condition_template: '200000000000000000000001'}},
res: {status: 'Invalid body format', details: '"weeks" is required'}
});
});
it('accepts a missing condition parameter not marked as required', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/sample/400000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {condition: {weeks: 10, condition_template: '200000000000000000000001'}}
}).end(err => {
if (err) return done(err);
done();
});
});
it('rejects an invalid condition template', done => {
TestHelper.request(server, done, {
method: 'put',
@ -1985,8 +2007,8 @@ describe('/sample', () => {
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', weeks: 3, xxx: 23, condition_template: '20000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Condition template not available'}
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', weeks: 3, xxx: 23, condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Invalid body format', details: '"xxx" is not allowed'}
});
});
it('rejects missing condition parameters', done => {
@ -1995,8 +2017,8 @@ describe('/sample', () => {
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', condition_template: '20000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Condition template not available'}
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: { status: 'Invalid body format', details: '"weeks" is required'}
});
});
it('rejects condition parameters not in the value range', done => {
@ -2005,8 +2027,8 @@ describe('/sample', () => {
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'xxx', weeks: 3, condition_template: '20000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Condition template not available'}
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'xxx', weeks: 3, condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Invalid body format', details: '"material" must be one of [copper, hot air]'}
});
});
it('rejects a condition parameter below minimum range', done => {
@ -2015,8 +2037,8 @@ describe('/sample', () => {
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', weeks: 0, condition_template: '20000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Condition template not available'}
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', weeks: 0, condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Invalid body format', details: '"weeks" must be greater than or equal to 1'}
});
});
it('rejects a condition parameter above maximum range', done => {
@ -2025,8 +2047,30 @@ describe('/sample', () => {
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', weeks: 11, condition_template: '20000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Condition template not available'}
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', weeks: 11, condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Invalid body format', details: '"weeks" must be less than or equal to 10'}
});
});
it('rejects a missing condition parameter marked as required', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: {material: 'copper', condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
res: {status: 'Invalid body format', details: '"weeks" is required'}
});
});
it('accepts a missing condition parameter not marked as required', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/sample/new',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {color: 'black', type: 'as-delivered/raw', batch: '1560237365', condition: { weeks: 10, condition_template: '200000000000000000000001'}, material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{sample_id: '400000000000000000000003', relation: 'part to this sample'}]}},
}).end(err => {
if (err) return done(err);
done();
});
});
it('rejects a condition without condition template', done => {

View File

@ -705,7 +705,11 @@ async function materialCheck (sample, res, next) {
// validate treatment template, returns false if invalid, otherwise template data
async function conditionCheck (condition, param, res, next, checkVersion = true) {
console.log(condition);
console.log(condition.condition_template);
console.log(IdValidate.valid(condition.condition_template));
if (!condition.condition_template || !IdValidate.valid(condition.condition_template)) { // template id not found
console.log('A');
res.status(400).json({status: 'Condition template not available'});
return false;
}
@ -713,6 +717,7 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
.lean().exec().catch(err => next(err)) as any;
if (conditionData instanceof Error) return false;
if (!conditionData) { // template not found
console.log('B');
res.status(400).json({status: 'Condition template not available'});
return false;
}
@ -731,6 +736,7 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
// validate parameters
const {error, value: ignore} =
ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param);
console.log(error);
if (error) {res400(error, res); return false;}
return conditionData;
}

View File

@ -1,7 +1,6 @@
import should from 'should/as-function';
import _ from 'lodash';
import TemplateConditionModel from '../models/condition_template';
import TemplateMeasurementModel from '../models/measurement_template';
import TestHelper from "../test/helper";
@ -63,7 +62,7 @@ describe('/template', () => {
url: '/template/condition/200000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]}
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10, required: true}}]}
});
});
it('rejects an API key', done => {
@ -99,7 +98,7 @@ describe('/template', () => {
auth: {basic: 'admin'},
httpStatus: 200,
req: {},
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]}
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10, required: true}}]}
});
});
it('keeps unchanged properties', done => {
@ -108,8 +107,8 @@ describe('/template', () => {
url: '/template/condition/200000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'heat treatment', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]},
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]}
req: {name: 'heat treatment', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10, required: true}}]},
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10, required: true}}]}
});
});
it('keeps only one unchanged property', done => {
@ -119,7 +118,7 @@ describe('/template', () => {
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'heat treatment'},
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10}}]}
res: {_id: '200000000000000000000001', name: 'heat treatment', version: 1, first_id: '200000000000000000000001', parameters: [{name: 'material', range: {values: ['copper', 'hot air']}}, {name: 'weeks', range: {min: 1, max: 10, required: true}}]}
});
});
it('changes the given properties', done => {
@ -223,6 +222,19 @@ describe('/template', () => {
done();
});
});
it('supports required ranges', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/condition/200000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {parameters: [{name: 'time', range: {required: true}}]}
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'heat treatment', version: 2, first_id: '200000000000000000000001', parameters: [{name: 'time', range: {required: true}}]});
done();
});
});
it('supports empty ranges', done => {
TestHelper.request(server, done, {
method: 'put',
@ -514,394 +526,7 @@ describe('/template', () => {
});
});
});
describe('GET /template/measurement/id', () => {
it('returns the right measurement template', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]}
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/template/measurement/300000000000000000000001',
auth: {key: 'janedoe'},
httpStatus: 401
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/template/measurement/000000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/template/measurement/300000000000000000000001',
httpStatus: 401
});
});
});
describe('PUT /template/measurement/{name}', () => {
it('returns the right measurement template', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {},
res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]}
});
});
it('keeps unchanged properties', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'spectrum', parameters: [{name: 'dpt', range: { type: 'array'}}, {name: 'device', range: {}}]},
res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]}
});
});
it('keeps only one unchanged property', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'spectrum'},
res: {_id: '300000000000000000000001', name: 'spectrum', version: 1, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]}
});
});
it('changes the given properties', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]}
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]});
TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => {
if (err) return done(err);
should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v');
should(data.first_id.toString()).be.eql('300000000000000000000001');
should(data).have.property('name', 'IR spectrum');
should(data).have.property('version', 2);
should(data).have.property('parameters').have.lengthOf(1);
should(data.parameters[0]).have.property('name', 'data point table');
should(data.parameters[0]).have.property('range');
should(data.parameters[0].range).have.property('min', 0);
should(data.parameters[0].range).have.property('max', 1000);
done();
});
});
});
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]},
log: {
collection: 'measurement_templates',
dataAdd: {
first_id: '300000000000000000000001',
version: 2
}
}
});
});
it('allows changing only one property', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'IR spectrum'},
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'IR spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {type: 'array'}}, {name: 'device', range: {}}]});
TemplateMeasurementModel.findById(res.body._id).lean().exec((err, data:any) => {
if (err) return done(err);
should(data).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v');
should(data.first_id.toString()).be.eql('300000000000000000000001');
should(data).have.property('name', 'IR spectrum');
should(data).have.property('version', 2);
should(data).have.property('parameters').have.lengthOf(2);
should(data.parameters[0]).have.property('name', 'dpt');
should(data.parameters[0]).have.property('range');
should(data.parameters[0].range).have.property('type', 'array');
should(data.parameters[1]).have.property('name', 'device');
should(data.parameters[1]).have.property('range');
done();
});
});
});
it('supports values ranges', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {parameters: [{name: 'dpt', range: {values: [1, 2, 5]}}]}
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {values: [1, 2, 5]}}]});
done();
});
});
it('supports min max ranges', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]}
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt', range: {min: 0, max: 1000}}]});
done();
});
});
it('supports array type ranges', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
req: {parameters: [{name: 'dpt2', range: {type: 'array'}}]}
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'spectrum', version: 2, first_id: '300000000000000000000001', parameters: [{name: 'dpt2', range: {type: 'array'}}]});
done();
});
});
it('supports empty ranges', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000002',
auth: {basic: 'admin'},
httpStatus: 200,
req: {parameters: [{name: 'weight %', range: {}}]}
}).end((err, res) => {
if (err) return done(err);
should(_.omit(res.body, '_id')).be.eql({name: 'kf', version: 2, first_id: '300000000000000000000002', parameters: [{name: 'weight %', range: {}}]});
done();
});
});
it('rejects not specified parameters', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 400,
req: {parameters: [{name: 'dpt'}], range: {xx: 33}},
res: {status: 'Invalid body format', details: '"parameters[0].range" is required'}
});
});
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/3000000000h0000000000001',
auth: {basic: 'admin'},
httpStatus: 404,
req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]},
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/000000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 404,
req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]},
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {key: 'admin'},
httpStatus: 401,
req: {}
});
});
it('rejects requests from a write user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/template/measurement/300000000000000000000001',
httpStatus: 401,
req: {}
});
});
});
describe('POST /template/measurement/new', () => {
it('returns the right measurement template', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]}
}).end((err, res) => {
if (err) return done(err);
should(res.body).have.only.keys('_id', 'name', 'version', 'first_id', 'parameters');
should(res.body).have.property('name', 'vz');
should(res.body).have.property('version', 1);
should(res.body._id).be.eql(res.body.first_id);
should(res.body).have.property('parameters').have.lengthOf(1);
should(res.body.parameters[0]).have.property('name', 'vz');
should(res.body.parameters[0]).have.property('range');
should(res.body.parameters[0].range).have.property('min', 1);
done();
});
});
it('stores the template', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]}
}).end(err => {
if (err) return done(err);
TemplateMeasurementModel.find({name: 'vz'}).lean().exec((err, data:any) => {
if (err) return done(err);
should(data).have.lengthOf(1);
should(data[0]).have.only.keys('_id', 'first_id', 'name', 'version', 'parameters', '__v');
should(data[0].first_id.toString()).be.eql(data[0]._id.toString());
should(data[0]).have.property('name', 'vz');
should(data[0]).have.property('version', 1);
should(data[0]).have.property('parameters').have.lengthOf(1);
should(data[0].parameters[0]).have.property('name', 'vz');
should(data[0].parameters[0]).have.property('range');
should(data[0].parameters[0].range).have.property('min', 1);
done();
});
});
});
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 200,
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]},
log: {
collection: 'measurement_templates',
dataAdd: {version: 1},
dataIgn: ['first_id']
}
});
});
it('rejects a missing name', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 400,
req: {parameters: [{name: 'data point table', range: {min: 0, max: 1000}}]},
res: {status: 'Invalid body format', details: '"name" is required'}
});
});
it('rejects missing parameters', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 400,
req: {name: 'IR spectrum'},
res: {status: 'Invalid body format', details: '"parameters" is required'}
});
});
it('rejects a missing parameter name', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 400,
req: {name: 'IR spectrum', parameters: [{range: {min: 0, max: 1000}}]},
res: {status: 'Invalid body format', details: '"parameters[0].name" is required'}
});
});
it('rejects a missing parameter range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 400,
req: {name: 'IR spectrum', parameters: [{name: 'data point table'}]},
res: {status: 'Invalid body format', details: '"parameters[0].range" is required'}
});
});
it('rejects a an invalid parameter range property', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 400,
req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {xx: 0}}]},
res: {status: 'Invalid body format', details: '"parameters[0].range.xx" is not allowed'}
});
});
it('rejects wrong properties', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'admin'},
httpStatus: 400,
req: {name: 'IR spectrum', parameters: [{name: 'data point table', range: {}}], xx: 35},
res: {status: 'Invalid body format', details: '"xx" is not allowed'}
});
});
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {key: 'admin'},
httpStatus: 401,
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]}
});
});
it('rejects requests from a write user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/template/measurement/new',
httpStatus: 401,
req: {name: 'vz', parameters: [{name: 'vz', range: {min: 1}}]}
});
});
});
// other methods should be covered by condition tests
});
describe('/template/material', () => {
@ -946,6 +571,6 @@ describe('/template', () => {
});
});
});
// other methods should be covered by measurement and condition tests
// other methods should be covered by condition tests
});
});

View File

@ -5,43 +5,35 @@ export default class ParametersValidate {
static input (data, parameters, param) {
let joiObject = {};
parameters.forEach(parameter => {
if (parameter.range.hasOwnProperty('values')) { // append right validation method according to parameter
joiObject[parameter.name] = Joi.alternatives()
.try(Joi.string().max(128), Joi.number(), Joi.boolean())
.valid(...parameter.range.values);
}
else if (parameter.range.hasOwnProperty('min') && parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = Joi.number()
.min(parameter.range.min)
.max(parameter.range.max);
}
else if (parameter.range.hasOwnProperty('min')) {
joiObject[parameter.name] = Joi.number()
.min(parameter.range.min);
}
else if (parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = Joi.number()
.max(parameter.range.max);
}
else if (parameter.range.hasOwnProperty('type')) {
switch (parameter.range.type) {
case 'array':
joiObject[parameter.name] = Joi.array();
break;
default:
switch (parameter.range.type) {
case 'number': joiObject[parameter.name] = Joi.number();
break;
case 'boolean': joiObject[parameter.name] = Joi.boolean();
break;
case 'array': joiObject[parameter.name] = Joi.array();
break;
case 'string': joiObject[parameter.name] = Joi.string().max(128);
break; // min or max implicitly define the value to be a number
default: if (parameter.range.hasOwnProperty('min') || parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = Joi.number();
}
else {
joiObject[parameter.name] = Joi.string().max(128);
break;
}
}
}
else {
joiObject[parameter.name] = Joi.alternatives()
.try(Joi.string().max(128), Joi.number(), Joi.boolean());
if (parameter.range.hasOwnProperty('min')) {
joiObject[parameter.name] = joiObject[parameter.name].min(parameter.range.min)
}
if (param === 'new') {
joiObject[parameter.name] = joiObject[parameter.name].required()
if (parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = joiObject[parameter.name].max(parameter.range.max)
}
else if (param === 'null') {
if (parameter.range.hasOwnProperty('values')) {
joiObject[parameter.name] = joiObject[parameter.name].valid(...parameter.range.values);
}
if (parameter.range.hasOwnProperty('required') && parameter.range.required) {
joiObject[parameter.name] = joiObject[parameter.name].required();
}
if (param === 'null') {
joiObject[parameter.name] = joiObject[parameter.name].allow(null)
}
});

View File

@ -28,13 +28,12 @@ export default class TemplateValidate {
max: Joi.number(),
type: Joi.string()
.valid('array')
.valid('string', 'number', 'boolean', 'array'),
required: Joi.boolean()
})
.oxor('values', 'min')
.oxor('values', 'max')
.oxor('type', 'values')
.oxor('type', 'min')
.oxor('type', 'max')
.required()
})
)

View File

@ -514,7 +514,8 @@
"name": "weeks",
"range": {
"min": 1,
"max": 10
"max": 10,
"required": true
}
}
],
@ -537,7 +538,9 @@
"parameters": [
{
"name": "p1",
"range": {}
"range": {
"type": "number"
}
}
],
"__v": 0
@ -672,21 +675,24 @@
"name": "glass_fiber",
"range": {
"min": 0,
"max": 100
"max": 100,
"required": true
}
},
{
"name": "carbon_fiber",
"range": {
"min": 0,
"max": 100
"max": 100,
"required": true
}
},
{
"name": "mineral",
"range": {
"min": 0,
"max": 100
"max": 100,
"required": true
}
}
],