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 example: kf
range: range:
type: object 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: example:
min: 0 min: 0
max: 2 max: 2

View File

@ -176,6 +176,7 @@ async function templateCheck (measurement, param, res, next) {
// validate values // validate values
const {error, value} = ParametersValidate.input(measurement.values, templateData.parameters, 'null'); const {error, value} = ParametersValidate.input(measurement.values, templateData.parameters, 'null');
console.log(error);
if (error) {res400(error, res); return false;} if (error) {res400(error, res); return false;}
return value || true; 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'} 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 => { it('rejects an invalid condition template', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {
method: 'put', method: 'put',
@ -1985,8 +2007,8 @@ describe('/sample', () => {
url: '/sample/new', url: '/sample/new',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 400, 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'}]}}, 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: 'Condition template not available'} res: {status: 'Invalid body format', details: '"xxx" is not allowed'}
}); });
}); });
it('rejects missing condition parameters', done => { it('rejects missing condition parameters', done => {
@ -1995,8 +2017,8 @@ describe('/sample', () => {
url: '/sample/new', url: '/sample/new',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 400, 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'}]}}, 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: 'Condition template not available'} res: { status: 'Invalid body format', details: '"weeks" is required'}
}); });
}); });
it('rejects condition parameters not in the value range', done => { it('rejects condition parameters not in the value range', done => {
@ -2005,8 +2027,8 @@ describe('/sample', () => {
url: '/sample/new', url: '/sample/new',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 400, 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'}]}}, 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: 'Condition template not available'} res: {status: 'Invalid body format', details: '"material" must be one of [copper, hot air]'}
}); });
}); });
it('rejects a condition parameter below minimum range', done => { it('rejects a condition parameter below minimum range', done => {
@ -2015,8 +2037,8 @@ describe('/sample', () => {
url: '/sample/new', url: '/sample/new',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 400, 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'}]}}, 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: 'Condition template not available'} res: {status: 'Invalid body format', details: '"weeks" must be greater than or equal to 1'}
}); });
}); });
it('rejects a condition parameter above maximum range', done => { it('rejects a condition parameter above maximum range', done => {
@ -2025,8 +2047,30 @@ describe('/sample', () => {
url: '/sample/new', url: '/sample/new',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 400, 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'}]}}, 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: 'Condition template not available'} 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 => { 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 // validate treatment template, returns false if invalid, otherwise template data
async function conditionCheck (condition, param, res, next, checkVersion = true) { 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 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'}); res.status(400).json({status: 'Condition template not available'});
return false; return false;
} }
@ -713,6 +717,7 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
.lean().exec().catch(err => next(err)) as any; .lean().exec().catch(err => next(err)) as any;
if (conditionData instanceof Error) return false; if (conditionData instanceof Error) return false;
if (!conditionData) { // template not found if (!conditionData) { // template not found
console.log('B');
res.status(400).json({status: 'Condition template not available'}); res.status(400).json({status: 'Condition template not available'});
return false; return false;
} }
@ -731,6 +736,7 @@ async function conditionCheck (condition, param, res, next, checkVersion = true)
// validate parameters // validate parameters
const {error, value: ignore} = const {error, value: ignore} =
ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param); ParametersValidate.input(_.omit(condition, 'condition_template'), conditionData.parameters, param);
console.log(error);
if (error) {res400(error, res); return false;} if (error) {res400(error, res); return false;}
return conditionData; return conditionData;
} }

View File

@ -1,7 +1,6 @@
import should from 'should/as-function'; import should from 'should/as-function';
import _ from 'lodash'; import _ from 'lodash';
import TemplateConditionModel from '../models/condition_template'; import TemplateConditionModel from '../models/condition_template';
import TemplateMeasurementModel from '../models/measurement_template';
import TestHelper from "../test/helper"; import TestHelper from "../test/helper";
@ -63,7 +62,7 @@ describe('/template', () => {
url: '/template/condition/200000000000000000000001', url: '/template/condition/200000000000000000000001',
auth: {basic: 'janedoe'}, auth: {basic: 'janedoe'},
httpStatus: 200, 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 => { it('rejects an API key', done => {
@ -99,7 +98,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {}, 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 => { it('keeps unchanged properties', done => {
@ -108,8 +107,8 @@ describe('/template', () => {
url: '/template/condition/200000000000000000000001', url: '/template/condition/200000000000000000000001',
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {name: 'heat treatment', 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}}]} 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 => { it('keeps only one unchanged property', done => {
@ -119,7 +118,7 @@ describe('/template', () => {
auth: {basic: 'admin'}, auth: {basic: 'admin'},
httpStatus: 200, httpStatus: 200,
req: {name: 'heat treatment'}, 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 => { it('changes the given properties', done => {
@ -223,6 +222,19 @@ describe('/template', () => {
done(); 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 => { it('supports empty ranges', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {
method: 'put', method: 'put',
@ -514,394 +526,7 @@ describe('/template', () => {
}); });
}); });
}); });
// other methods should be covered by condition tests
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}}]}
});
});
});
}); });
describe('/template/material', () => { 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) { static input (data, parameters, param) {
let joiObject = {}; let joiObject = {};
parameters.forEach(parameter => { parameters.forEach(parameter => {
if (parameter.range.hasOwnProperty('values')) { // append right validation method according to parameter switch (parameter.range.type) {
joiObject[parameter.name] = Joi.alternatives() case 'number': joiObject[parameter.name] = Joi.number();
.try(Joi.string().max(128), Joi.number(), Joi.boolean()) break;
.valid(...parameter.range.values); case 'boolean': joiObject[parameter.name] = Joi.boolean();
} break;
else if (parameter.range.hasOwnProperty('min') && parameter.range.hasOwnProperty('max')) { case 'array': joiObject[parameter.name] = Joi.array();
break;
joiObject[parameter.name] = Joi.number() case 'string': joiObject[parameter.name] = Joi.string().max(128);
.min(parameter.range.min) break; // min or max implicitly define the value to be a number
.max(parameter.range.max); default: if (parameter.range.hasOwnProperty('min') || parameter.range.hasOwnProperty('max')) {
} joiObject[parameter.name] = Joi.number();
else if (parameter.range.hasOwnProperty('min')) { }
joiObject[parameter.name] = Joi.number() else {
.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:
joiObject[parameter.name] = Joi.string().max(128); joiObject[parameter.name] = Joi.string().max(128);
break; }
}
} }
else { if (parameter.range.hasOwnProperty('min')) {
joiObject[parameter.name] = Joi.alternatives() joiObject[parameter.name] = joiObject[parameter.name].min(parameter.range.min)
.try(Joi.string().max(128), Joi.number(), Joi.boolean());
} }
if (param === 'new') { if (parameter.range.hasOwnProperty('max')) {
joiObject[parameter.name] = joiObject[parameter.name].required() 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) joiObject[parameter.name] = joiObject[parameter.name].allow(null)
} }
}); });

View File

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

View File

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