Merge pull request #37 in ~VLE2FE/definma-api from develop to master
* commit '9297b63a286e760142f2153a68757f8330ad4796': fixed double unwinding with spectrum filter added required parameter range added notes.comment field and filter
This commit is contained in:
commit
500f490ace
@ -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
|
||||
|
70
data_import/spectrum-fix.js
Normal file
70
data_import/spectrum-fix.js
Normal file
@ -0,0 +1,70 @@
|
||||
const axios = require('axios');
|
||||
|
||||
|
||||
const host = 'http://localhost:3000';
|
||||
// const host = 'https://definma-api.apps.de1.bosch-iot-cloud.com';
|
||||
|
||||
let errors = [];
|
||||
|
||||
async function fix() {
|
||||
let res = await axios({
|
||||
method: 'get',
|
||||
url: host + '/samples?status[]=new&status[]=validated&status[]=deleted&fields[]=_id&fields[]=number',
|
||||
auth: {
|
||||
username: 'admin',
|
||||
password: 'Abc123!#'
|
||||
}
|
||||
}).catch(err => {
|
||||
if (err.response) {
|
||||
console.error(err.response.data);
|
||||
errors.push(`Could not fetch samples: ${JSON.stringify(err.response.data)}`);
|
||||
}
|
||||
});
|
||||
console.log(res.data);
|
||||
// for all samples
|
||||
for (let sampleIndex in res.data) {
|
||||
console.log(`SAMPLE ${sampleIndex}/${res.data.length}`);
|
||||
const measurements = await axios({ // get all measurements
|
||||
method: 'get',
|
||||
url: host + '/measurement/sample/' + res.data[sampleIndex]._id,
|
||||
auth: {
|
||||
username: 'admin',
|
||||
password: 'Abc123!#'
|
||||
}
|
||||
}).catch(err => {
|
||||
if (err.response) {
|
||||
console.error(err.response.data);
|
||||
errors.push(`Could not fetch measurements: ${JSON.stringify(err.response.data)}`);
|
||||
}
|
||||
});
|
||||
|
||||
if (measurements && measurements.data) { // found measurements
|
||||
for (let measurementIndex in measurements.data) {
|
||||
console.log(`${measurementIndex}/${measurements.data.length}`);
|
||||
const measurement = measurements.data[measurementIndex];
|
||||
if (measurement.values.hasOwnProperty('dpt')) { // is spectrum
|
||||
await axios({ // get all measurements
|
||||
method: 'put',
|
||||
url: host + '/measurement/' + measurement._id,
|
||||
auth: {
|
||||
username: 'admin',
|
||||
password: 'Abc123!#'
|
||||
},
|
||||
data: {values: {dpt: measurement.values.dpt
|
||||
.filter(e => e.length === 2)
|
||||
.map(e => e.map(el => parseFloat(el)))
|
||||
}}
|
||||
}).catch(err => {
|
||||
if (err.response) {
|
||||
console.error(err.response.data);
|
||||
errors.push(`Could not save measurements: ${JSON.stringify(err.response.data)}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log(errors);
|
||||
}
|
||||
|
||||
fix();
|
@ -14,7 +14,8 @@
|
||||
"start-local": "node dist/index.js",
|
||||
"loadDev": "node dist/test/loadDev.js",
|
||||
"coverage": "tsc && nyc --reporter=html --reporter=text mocha dist/**/**.spec.js --timeout 5000",
|
||||
"import": "node data_import/import.js"
|
||||
"import": "node data_import/import.js",
|
||||
"spectrum-fix": "node data_import/spectrum-fix.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import TestHelper from "../test/helper";
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
|
||||
|
||||
describe('/sample', () => {
|
||||
let server;
|
||||
before(done => TestHelper.before(done));
|
||||
@ -255,6 +254,24 @@ describe('/sample', () => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('unwinds only once when filtering for spectrum measurements and including the dpt field', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=measurements.spectrum.device&fields=measurements.spectrum.dpt&filters[]=%7B%22mode%22%3A%22eq%22%2C%22field%22%3A%22measurements.spectrum.device%22%2C%22values%22%3A%5B%22Alpha%20II%22%5D%7D',
|
||||
auth: {basic: 'admin'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
const json = require('../test/db.json');
|
||||
should(res.body).have.lengthOf(json.collections.measurements.filter(e => e.values.device === 'Alpha II' && e.status !== 'deleted').length);
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample).have.only.keys('number', 'spectrum');
|
||||
should(sample.spectrum).have.only.keys('device', 'dpt');
|
||||
should(sample.spectrum).have.property('device', 'Alpha II');
|
||||
});
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('adds the status if specified', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
@ -289,7 +306,7 @@ describe('/sample', () => {
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
should(res.body.filter(e => e.spectrum.dpt)).have.lengthOf(2);
|
||||
should(res.body.filter(e => e.spectrum.dpt)).have.lengthOf(3);
|
||||
should(res.body[0].spectrum).have.property('dpt', [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]);
|
||||
should(res.body[1].spectrum).have.property('dpt', [[3996.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]);
|
||||
done();
|
||||
@ -402,6 +419,45 @@ describe('/sample', () => {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('filters for empty comments', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=notes.comment&filters[]=%7B%22mode%22%3A%22in%22%2C%22field%22%3A%22notes.comment%22%2C%22values%22%3A%5Bnull%2C%22%22%5D%7D',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
const json = require('../test/db.json');
|
||||
should(res.body).have.lengthOf(
|
||||
json.collections.samples
|
||||
.filter(e => e.status !== 'deleted')
|
||||
.filter(e => e.note_id === null || json.collections.notes.find(el => el._id.toString() == e.note_id.toString()).comment === '')
|
||||
.length
|
||||
);
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample.notes.comment).be.equalOneOf(null, '');
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns comment fields', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples?status[]=new&status[]=validated&fields[]=number&fields[]=notes.comment',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200
|
||||
}).end((err, res) => {
|
||||
if (err) return done(err);
|
||||
console.log(res.body);
|
||||
const json = require('../test/db.json');
|
||||
should(res.body).have.lengthOf(json.collections.samples.filter(e => e.status !== 'deleted').length);
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample).have.only.keys('number', 'notes');
|
||||
should(sample.notes).have.only.keys('comment');
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('rejects returning spectral data for a write user', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
@ -744,7 +800,7 @@ describe('/sample', () => {
|
||||
url: '/sample/400000000000000000000001',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200,
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {device: 'Alpha II'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000009', sample_id: '400000000000000000000001', values: {device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
});
|
||||
});
|
||||
it ('returns spectral data for an admin user', done => {
|
||||
@ -753,7 +809,7 @@ describe('/sample', () => {
|
||||
url: '/sample/400000000000000000000001',
|
||||
auth: {basic: 'admin'},
|
||||
httpStatus: 200,
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[ 3997.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]],device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {dpt: [[ 3996.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]], device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[ 3997.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]],device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {dpt: [[ 3996.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]], device: 'Alpha II'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000009', sample_id: '400000000000000000000001', values: {dpt: [[ 3996.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]], device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
});
|
||||
});
|
||||
it('returns a deleted sample for a dev/admin user', done => {
|
||||
@ -1109,6 +1165,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',
|
||||
@ -1462,7 +1540,7 @@ describe('/sample', () => {
|
||||
url: '/sample/number/1',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 200,
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {device: 'Alpha II'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000009', sample_id: '400000000000000000000001', values: {device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
});
|
||||
});
|
||||
it ('returns spectral data for an admin user', done => {
|
||||
@ -1471,7 +1549,7 @@ describe('/sample', () => {
|
||||
url: '/sample/number/1',
|
||||
auth: {basic: 'admin'},
|
||||
httpStatus: 200,
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[ 3997.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]],device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {dpt: [[ 3996.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]], device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
res: {_id: '400000000000000000000001', number: '1', type: 'as-delivered/raw', color: 'black', batch: '', condition: {material: 'copper', weeks: 3, condition_template: '200000000000000000000001'}, material: {numbers: ['5513933405'], _id: '100000000000000000000004', name: 'Schulamid 66 GF 25 H', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 25, carbon_fiber: 0}, group: 'PA66', supplier: 'Schulmann'}, user: 'janedoe', notes: {}, measurements: [{_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[ 3997.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]],device: 'Alpha I'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000007', sample_id: '400000000000000000000001', values: {dpt: [[ 3996.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]], device: 'Alpha II'}, measurement_template: '300000000000000000000001'}, {_id: '800000000000000000000009', sample_id: '400000000000000000000001', values: {dpt: [[ 3996.12558, 98.00555 ], [ 3995.08519, 98.03253 ], [ 3993.0448, 98.02657 ]], device: 'Alpha II'}, measurement_template: '300000000000000000000001'}], status: 'validated'}
|
||||
});
|
||||
});
|
||||
it('returns 403 for a write user when requesting a deleted sample', done => {
|
||||
@ -1946,8 +2024,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 => {
|
||||
@ -1956,8 +2034,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 => {
|
||||
@ -1966,8 +2044,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 => {
|
||||
@ -1976,8 +2054,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 => {
|
||||
@ -1986,8 +2064,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 => {
|
||||
|
@ -228,13 +228,20 @@ router.get('/samples', async (req, res, next) => {
|
||||
if (measurementTemplates.length < measurementFilterFields.length) {
|
||||
return res.status(400).json({status: 'Invalid body format', details: 'Measurement key not found'});
|
||||
}
|
||||
queryPtr.push({$lookup: {
|
||||
from: 'measurements', let: {sId: '$_id'},
|
||||
pipeline: [{$match: {$expr: {$and: [
|
||||
const pipeline: any[] = [{$match: {$expr: {$and: [
|
||||
{$eq: ['$sample_id', '$$sId']},
|
||||
{$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
|
||||
]}}}
|
||||
],
|
||||
];
|
||||
if (measurementFilterFields.indexOf(globals.spectrum.spectrum) >= 0) { // filter out dpts
|
||||
pipeline.push(
|
||||
{$project: {'values.device': true, measurement_template: true}},
|
||||
{$addFields: {'values._id': '$_id'}}
|
||||
);
|
||||
}
|
||||
queryPtr.push({$lookup: {
|
||||
from: 'measurements', let: {sId: '$_id'},
|
||||
pipeline: pipeline,
|
||||
as: 'measurements'
|
||||
}});
|
||||
const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => {
|
||||
@ -255,6 +262,11 @@ router.get('/samples', async (req, res, next) => {
|
||||
); // measurement filters
|
||||
}
|
||||
|
||||
if (sortFilterKeys.find(e => e === 'notes.comment')) {
|
||||
addNotes(queryPtr);
|
||||
addFilterQueries(queryPtr, filters.filters.filter(e => e.field === 'notes.comment'));
|
||||
}
|
||||
|
||||
// count total number of items before $skip and $limit, only works when from-id is not specified and spectra are not
|
||||
// included
|
||||
if (!filters.fields.find(e =>
|
||||
@ -279,11 +291,8 @@ router.get('/samples', async (req, res, next) => {
|
||||
&& e !== filters.sort[0] // field was not in sort
|
||||
);
|
||||
|
||||
if (fieldsToAdd.find(e => e === 'notes')) { // add notes
|
||||
queryPtr.push(
|
||||
{$lookup: {from: 'notes', localField: 'note_id', foreignField: '_id', as: 'notes'}},
|
||||
{$addFields: {notes: { $arrayElemAt: ['$notes', 0]}}}
|
||||
);
|
||||
if (fieldsToAdd.find(e => /^notes(\..+|$)/m.test(e))) { // add notes
|
||||
addNotes(queryPtr);
|
||||
}
|
||||
|
||||
if (fieldsToAdd.find(e => /material\./.test(e)) && !materialAdded) { // add material, was not added already
|
||||
@ -321,10 +330,18 @@ router.get('/samples', async (req, res, next) => {
|
||||
}
|
||||
// use different lookup methods with and without dpt for the best performance
|
||||
if (fieldsToAdd.find(e => e.indexOf(globals.spectrum.spectrum + '.' + globals.spectrum.dpt) >= 0)) { // with dpt
|
||||
// spectrum was already used for filters
|
||||
if (sortFilterKeys.find(e => new RegExp('measurements\\.' + globals.spectrum.spectrum).test(e))) {
|
||||
queryPtr.push(
|
||||
{$lookup: {from: 'measurements', localField: 'spectrum._id', foreignField: '_id', as: 'measurements'}}
|
||||
);
|
||||
}
|
||||
else {
|
||||
queryPtr.push(
|
||||
{$lookup: {from: 'measurements', localField: '_id', foreignField: 'sample_id', as: 'measurements'}}
|
||||
);
|
||||
}
|
||||
}
|
||||
else {
|
||||
queryPtr.push({$lookup: {
|
||||
from: 'measurements', let: {sId: '$_id'},
|
||||
@ -729,6 +746,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;
|
||||
}
|
||||
@ -857,6 +875,17 @@ function addMeasurements(queryPtr, templates) {
|
||||
);
|
||||
}
|
||||
|
||||
function addNotes(queryPtr) { // add note fields with default, if no notes are found
|
||||
queryPtr.push(
|
||||
{$lookup: {from: 'notes', localField: 'note_id', foreignField: '_id', as: 'notes'}},
|
||||
{$addFields: {notes: {$cond: [
|
||||
{'$arrayElemAt': ['$notes', 0]},
|
||||
{'$arrayElemAt': ['$notes', 0]},
|
||||
{comment: null, sample_references: []}
|
||||
]}}}
|
||||
);
|
||||
}
|
||||
|
||||
function dateToOId (date) { // convert date to ObjectId
|
||||
return mongoose.Types.ObjectId(Math.floor(date / 1000).toString(16) + '0000000000000000');
|
||||
}
|
||||
|
@ -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
|
||||
});
|
||||
});
|
@ -65,7 +65,7 @@ router.put('/user:username([/](?!key|new).?*|/?)', async (req, res, next) => {
|
||||
await UserModel.findOneAndUpdate({name: username}, user, {new: true}).log(req).lean().exec( (err, data:any) => {
|
||||
if (err) return next(err);
|
||||
if (data) {
|
||||
if (data.mail !== oldUserData.email) { // mail address was changed, send notice to old address
|
||||
if (data.email !== oldUserData.email) { // mail address was changed, send notice to old address
|
||||
Mail.send(oldUserData.email, 'Email change in your DeFinMa database account',
|
||||
'Hi, <br><br> Your email address of your DeFinMa account was changed to ' + data.mail +
|
||||
'<br><br>If you actually did this, just delete this email.' +
|
||||
|
@ -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();
|
||||
case 'number': joiObject[parameter.name] = Joi.number();
|
||||
break;
|
||||
default:
|
||||
joiObject[parameter.name] = Joi.string().max(128);
|
||||
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.alternatives()
|
||||
.try(Joi.string().max(128), Joi.number(), Joi.boolean());
|
||||
joiObject[parameter.name] = Joi.string().max(128);
|
||||
}
|
||||
if (param === 'new') {
|
||||
joiObject[parameter.name] = joiObject[parameter.name].required()
|
||||
}
|
||||
else if (param === 'null') {
|
||||
if (parameter.range.hasOwnProperty('min')) {
|
||||
joiObject[parameter.name] = joiObject[parameter.name].min(parameter.range.min)
|
||||
}
|
||||
if (parameter.range.hasOwnProperty('max')) {
|
||||
joiObject[parameter.name] = joiObject[parameter.name].max(parameter.range.max)
|
||||
}
|
||||
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)
|
||||
}
|
||||
});
|
||||
|
@ -28,7 +28,8 @@ export default class SampleValidate {
|
||||
notes: Joi.object({
|
||||
comment: Joi.string()
|
||||
.max(512)
|
||||
.allow(''),
|
||||
.allow('')
|
||||
.allow(null),
|
||||
|
||||
sample_references: Joi.array()
|
||||
.items(Joi.object({
|
||||
@ -78,6 +79,7 @@ export default class SampleValidate {
|
||||
'batch',
|
||||
'added',
|
||||
'status',
|
||||
'notes.comment',
|
||||
'material.name',
|
||||
'material.supplier',
|
||||
'material.group',
|
||||
@ -196,6 +198,7 @@ export default class SampleValidate {
|
||||
data.filters[i] = decodeURIComponent(data.filters[i]);
|
||||
}
|
||||
catch (ignore) {}
|
||||
console.log(data.filters[i]);
|
||||
data.filters[i] = JSON.parse(data.filters[i]);
|
||||
data.filters[i].values = data.filters[i].values.map(e => { // validate filter values
|
||||
if (e === null) { // null values are always allowed
|
||||
@ -223,11 +226,14 @@ export default class SampleValidate {
|
||||
});
|
||||
field = 'value';
|
||||
}
|
||||
else if (field === 'notes.comment') {
|
||||
field = 'comment';
|
||||
validator = this.sample.notes
|
||||
}
|
||||
else {
|
||||
validator = Joi.object(this.sample);
|
||||
}
|
||||
const {value, error} = validator.validate({[field]: e});
|
||||
console.log(error);
|
||||
if (error) throw error; // reject invalid values
|
||||
return value[field];
|
||||
});
|
||||
@ -260,7 +266,7 @@ export default class SampleValidate {
|
||||
new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm')
|
||||
).messages({'string.pattern.base': 'Invalid filter field name'}),
|
||||
values: Joi.array().items(Joi.alternatives().try(
|
||||
Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date().iso(), Joi.object(), null
|
||||
Joi.string().max(128).allow(''), Joi.number(), Joi.boolean(), Joi.date().iso(), Joi.object(), null
|
||||
)).min(1)
|
||||
})).default([])
|
||||
}).with('to-page', 'page-size').validate(data);
|
||||
|
@ -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()
|
||||
})
|
||||
)
|
||||
|
@ -492,6 +492,21 @@
|
||||
"status": "deleted",
|
||||
"measurement_template": {"$oid":"300000000000000000000001"},
|
||||
"__v": 0
|
||||
},
|
||||
{
|
||||
"_id": {"$oid":"800000000000000000000009"},
|
||||
"sample_id": {"$oid":"400000000000000000000001"},
|
||||
"values": {
|
||||
"dpt": [
|
||||
[3996.12558,98.00555],
|
||||
[3995.08519,98.03253],
|
||||
[3993.04480,98.02657]
|
||||
],
|
||||
"device": "Alpha II"
|
||||
},
|
||||
"status": "validated",
|
||||
"measurement_template": {"$oid":"300000000000000000000001"},
|
||||
"__v": 0
|
||||
}
|
||||
],
|
||||
"condition_templates": [
|
||||
@ -514,7 +529,8 @@
|
||||
"name": "weeks",
|
||||
"range": {
|
||||
"min": 1,
|
||||
"max": 10
|
||||
"max": 10,
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
],
|
||||
@ -537,7 +553,9 @@
|
||||
"parameters": [
|
||||
{
|
||||
"name": "p1",
|
||||
"range": {}
|
||||
"range": {
|
||||
"type": "number"
|
||||
}
|
||||
}
|
||||
],
|
||||
"__v": 0
|
||||
@ -672,21 +690,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
|
||||
}
|
||||
}
|
||||
],
|
||||
|
Reference in New Issue
Block a user