Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/measurement.spec.ts

790 lines
31 KiB
TypeScript
Raw Normal View History

2020-05-12 12:15:36 +02:00
import should from 'should/as-function';
import MeasurementModel from '../models/measurement';
import TestHelper from "../test/helper";
import globals from '../globals';
2020-05-12 12:15:36 +02:00
2020-05-12 12:15:36 +02:00
describe('/measurement', () => {
let server;
before(done => TestHelper.before(done));
beforeEach(done => server = TestHelper.beforeEach(server, done));
afterEach(done => TestHelper.afterEach(server, done));
2020-05-28 11:47:51 +02:00
after(done => TestHelper.after(done));
2020-05-12 12:15:36 +02:00
2020-05-28 11:47:51 +02:00
describe('GET /measurement/{id}', () => {
2020-05-12 12:15:36 +02:00
it('returns the right measurement', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
2020-05-27 17:03:03 +02:00
res: {_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}, measurement_template: '300000000000000000000001'}
2020-05-12 12:15:36 +02:00
});
});
it('returns the measurement for an API key', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/800000000000000000000001',
auth: {key: 'janedoe'},
httpStatus: 200,
2020-05-27 17:03:03 +02:00
res: {_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}, measurement_template: '300000000000000000000001'}
});
});
it('returns deleted measurements for a maintain/admin user', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/800000000000000000000004',
auth: {basic: 'admin'},
httpStatus: 200,
res: {_id: '800000000000000000000004', sample_id: '400000000000000000000003', values: {val1: 1}, measurement_template: '300000000000000000000003'}
});
});
it('rejects requests for deleted measurements from a write user', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/800000000000000000000004',
auth: {basic: 'janedoe'},
httpStatus: 403
2020-05-12 12:15:36 +02:00
});
});
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/8000000000h0000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/000000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'get',
url: '/measurement/800000000000000000000001',
httpStatus: 401
});
});
});
2020-05-12 17:15:36 +02:00
describe('PUT /measurement/{id}', () => {
it('returns the right measurement', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {},
2020-05-27 17:03:03 +02:00
res: {_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}, measurement_template: '300000000000000000000001'}
2020-05-12 17:15:36 +02:00
});
});
it('keeps unchanged values', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}}
}).end((err, res) => {
if (err) return done(err);
2020-05-27 17:03:03 +02:00
should(res.body).be.eql({_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[3997.12558,98.00555],[3995.08519,98.03253],[3993.04480,98.02657]]}, measurement_template: '300000000000000000000001'});
2020-05-12 17:15:36 +02:00
MeasurementModel.findById('800000000000000000000001').lean().exec((err, data: any) => {
2020-05-13 09:56:44 +02:00
if (err) return done(err);
should(data).have.property('status',globals.status.validated);
2020-05-13 09:56:44 +02:00
done();
});
});
});
it('keeps only one unchanged value', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {'weight %': 0.5}}
}).end((err, res) => {
if (err) return done(err);
2020-05-27 17:03:03 +02:00
should(res.body).be.eql({_id: '800000000000000000000002', sample_id: '400000000000000000000002', values: {'weight %': 0.5, 'standard deviation': 0.2}, measurement_template: '300000000000000000000002'});
2020-05-13 09:56:44 +02:00
MeasurementModel.findById('800000000000000000000002').lean().exec((err, data: any) => {
if (err) return done(err);
should(data).have.property('status',globals.status.validated);
2020-05-12 17:15:36 +02:00
done();
});
});
});
it('changes the given values', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {dpt: [[1,2],[3,4],[5,6]]}}
}).end((err, res) => {
if (err) return done(err);
2020-05-27 17:03:03 +02:00
should(res.body).be.eql({_id: '800000000000000000000001', sample_id: '400000000000000000000001', values: {dpt: [[1,2],[3,4],[5,6]]}, measurement_template: '300000000000000000000001'});
2020-05-12 17:15:36 +02:00
MeasurementModel.findById('800000000000000000000001').lean().exec((err, data: any) => {
2020-05-27 17:03:03 +02:00
should(data).have.only.keys('_id', 'sample_id', 'values', 'measurement_template', 'status', '__v');
should(data.sample_id.toString()).be.eql('400000000000000000000001');
2020-05-12 17:15:36 +02:00
should(data.measurement_template.toString()).be.eql('300000000000000000000001');
should(data).have.property('status',globals.status.new);
2020-05-12 17:15:36 +02:00
should(data).have.property('values');
should(data.values).have.property('dpt', [[1,2],[3,4],[5,6]]);
done();
});
});
});
2020-06-05 08:50:06 +02:00
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {dpt: [[1,2],[3,4],[5,6]]}},
log: {
collection: 'measurements',
dataAdd: {
measurement_template: '300000000000000000000001',
sample_id: '400000000000000000000001',
status: 0
}
}
});
});
2020-05-12 17:15:36 +02:00
it('allows changing only one value', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {'weight %': 0.9}},
2020-05-27 17:03:03 +02:00
res: {_id: '800000000000000000000002', sample_id: '400000000000000000000002', values: {'weight %': 0.9, 'standard deviation': 0.2}, measurement_template: '300000000000000000000002'}
});
});
it('allows keeping empty values empty', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000005',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {values: {'weight %': 0.9}},
res: {_id: '800000000000000000000005', sample_id: '400000000000000000000002', values: {'weight %': 0.9, 'standard deviation': null}, measurement_template: '300000000000000000000002'}
2020-05-12 17:15:36 +02:00
});
});
it('rejects not specified values', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3, xx: 44}},
res: {status: 'Invalid body format', details: '"xx" is not allowed'}
});
});
it('rejects a value not in the value range', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000003',
auth: {basic: 'admin'},
httpStatus: 400,
req: {values: {val1: 4}},
2020-05-27 17:03:03 +02:00
res: {status: 'Invalid body format', details: '"val1" must be one of [1, 2, 3, null]'}
2020-05-12 17:15:36 +02:00
});
});
it('rejects a value below minimum range', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {values: {'weight %': -1, 'standard deviation': 0.3}},
res: {status: 'Invalid body format', details: '"weight %" must be larger than or equal to 0'}
});
});
it('rejects a value above maximum range', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {values: {'weight %': 0.9, 'standard deviation': 3}},
res: {status: 'Invalid body format', details: '"standard deviation" must be less than or equal to 0.5'}
});
});
it('rejects a new measurement template', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3}, measurement_template: '300000000000000000000001'},
res: {status: 'Invalid body format', details: '"measurement_template" is not allowed'}
});
});
2020-05-27 17:03:03 +02:00
it('rejects a new sample id', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3}, sample_id: '400000000000000000000002'},
res: {status: 'Invalid body format', details: '"sample_id" is not allowed'}
});
});
2020-05-12 17:15:36 +02:00
it('rejects editing a measurement for a write user who did not create this measurement', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000003',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {values: {val1: 2}}
});
});
it('accepts editing a measurement of another user for a maintain/admin user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'admin'},
httpStatus: 200,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3}},
2020-05-27 17:03:03 +02:00
res: {_id: '800000000000000000000002', sample_id: '400000000000000000000002', values: {'weight %': 0.9, 'standard deviation': 0.3}, measurement_template: '300000000000000000000002'}
2020-05-12 17:15:36 +02:00
});
});
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000h00000000000002',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/000000000000000000000002',
auth: {basic: 'janedoe'},
httpStatus: 404
});
});
2020-05-28 13:16:15 +02:00
it('rejects editing a deleted measurement', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000004',
auth: {basic: 'admin'},
httpStatus: 403,
req: {}
});
});
2020-05-12 17:15:36 +02:00
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {key: 'janedoe'},
httpStatus: 401,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3}},
});
});
it('rejects requests from a read user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
auth: {basic: 'user'},
httpStatus: 403,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3}},
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/800000000000000000000002',
httpStatus: 401,
req: {values: {'weight %': 0.9, 'standard deviation': 0.3}},
});
});
});
2020-05-12 17:37:01 +02:00
describe('DELETE /measurement/{id}', () => {
it('sets the status to deleted', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
2020-06-05 08:50:06 +02:00
httpStatus: 200
2020-05-12 17:37:01 +02:00
}).end((err, res) => {
if (err) return done(err);
should(res.body).be.eql({status: 'OK'});
MeasurementModel.findById('800000000000000000000001').lean().exec((err, data) => {
if (err) return done(err);
should(data).have.property('status',globals.status.deleted);
2020-05-12 17:37:01 +02:00
done();
});
});
});
2020-06-05 08:50:06 +02:00
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 200,
log: {
collection: 'measurements',
dataAdd: {
status: -1
}
}
});
});
2020-05-12 17:37:01 +02:00
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000001',
auth: {key: 'janedoe'},
httpStatus: 401,
});
});
it('rejects requests from a read user', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000001',
auth: {basic: 'user'},
httpStatus: 403,
});
});
it('rejects deleting a measurement for a write user who did not create this measurement', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000003',
auth: {basic: 'janedoe'},
httpStatus: 403,
});
});
it('accepts deleting a measurement of another user for a maintain/admin user', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000001',
auth: {basic: 'admin'},
httpStatus: 200,
res: {status: 'OK'}
});
});
it('rejects an invalid id', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000h00000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404,
});
});
it('rejects an unknown id', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/000000000000000000000001',
auth: {basic: 'janedoe'},
httpStatus: 404,
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'delete',
url: '/measurement/800000000000000000000001',
httpStatus: 401,
});
});
});
2020-05-28 15:03:49 +02:00
describe('PUT /measurement/restore/{id}', () => {
it('sets the status', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/restore/800000000000000000000004',
auth: {basic: 'admin'},
httpStatus: 200,
req: {}
}).end((err, res) => {
if (err) return done (err);
should(res.body).be.eql({status: 'OK'});
MeasurementModel.findById('800000000000000000000004').lean().exec((err, data: any) => {
if (err) return done(err);
should(data).have.property('status',globals.status.new);
done();
});
});
});
2020-06-05 08:50:06 +02:00
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/restore/800000000000000000000004',
auth: {basic: 'admin'},
httpStatus: 200,
req: {},
log: {
collection: 'measurements',
dataAdd: {
status: 0
}
}
});
});
2020-05-28 15:03:49 +02:00
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/restore/800000000000000000000004',
auth: {key: 'admin'},
httpStatus: 401,
req: {}
});
});
it('rejects a write user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/restore/800000000000000000000004',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {}
});
});
it('returns 404 for an unknown sample', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/restore/000000000000000000000004',
auth: {basic: 'admin'},
httpStatus: 404,
req: {}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/restore/800000000000000000000004',
httpStatus: 401,
req: {}
});
});
});
2020-05-29 11:28:35 +02:00
describe('PUT /measurement/validate/{id}', () => {
it('sets the status', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/validate/800000000000000000000003',
auth: {basic: 'admin'},
httpStatus: 200,
req: {}
}).end((err, res) => {
if (err) return done (err);
should(res.body).be.eql({status: 'OK'});
MeasurementModel.findById('800000000000000000000003').lean().exec((err, data: any) => {
if (err) return done(err);
should(data).have.property('status',globals.status.validated);
done();
});
});
});
2020-06-05 08:50:06 +02:00
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/validate/800000000000000000000003',
auth: {basic: 'admin'},
httpStatus: 200,
req: {},
log: {
collection: 'measurements',
dataAdd: {
status: 10
}
}
});
});
2020-05-29 11:28:35 +02:00
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/validate/800000000000000000000003',
auth: {key: 'admin'},
httpStatus: 401,
req: {}
});
});
it('rejects a write user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/validate/800000000000000000000003',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {}
});
});
it('returns 404 for an unknown sample', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/validate/000000000000000000000003',
auth: {basic: 'admin'},
httpStatus: 404,
req: {}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/measurement/validate/800000000000000000000003',
httpStatus: 401,
req: {}
});
});
});
2020-05-12 12:15:36 +02:00
describe('POST /measurement/new', () => {
it('returns the right measurement', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 200,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
}).end((err, res) => {
if (err) return done(err);
2020-05-27 17:03:03 +02:00
should(res.body).have.only.keys('_id', 'sample_id', 'values', 'measurement_template');
2020-05-12 12:15:36 +02:00
should(res.body).have.property('_id').be.type('string');
2020-05-27 17:03:03 +02:00
should(res.body).have.property('sample_id', '400000000000000000000001');
2020-05-12 12:15:36 +02:00
should(res.body).have.property('measurement_template', '300000000000000000000002');
should(res.body).have.property('values');
should(res.body.values).have.property('weight %', 0.8);
should(res.body.values).have.property('standard deviation', 0.1);
done();
});
});
it('stores the measurement', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 200,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
}).end((err, res) => {
if (err) return done(err);
MeasurementModel.findById(res.body._id).lean().exec((err, data: any) => {
2020-05-12 17:15:36 +02:00
if (err) return done(err);
2020-05-27 17:03:03 +02:00
should(data).have.only.keys('_id', 'sample_id', 'values', 'measurement_template', 'status', '__v');
should(data.sample_id.toString()).be.eql('400000000000000000000001');
2020-05-12 12:15:36 +02:00
should(data.measurement_template.toString()).be.eql('300000000000000000000002');
2020-05-12 12:26:26 +02:00
should(data).have.property('status', 0);
2020-05-12 12:15:36 +02:00
should(data).have.property('values');
should(data.values).have.property('weight %', 0.8);
should(data.values).have.property('standard deviation', 0.1);
done();
});
});
});
2020-06-05 08:50:06 +02:00
it('creates a changelog', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 200,
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'},
log: {
collection: 'measurements',
dataAdd: {
status: 0
}
}
});
});
2020-05-27 17:03:03 +02:00
it('rejects an invalid sample id', done => {
2020-05-12 12:15:36 +02:00
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000h00000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'},
res: {status: 'Invalid body format', details: '"sample_id" with value "400000000000h00000000001" fails to match the required pattern: /[0-9a-f]{24}/'}
2020-05-12 12:15:36 +02:00
});
});
2020-05-27 17:03:03 +02:00
it('rejects a sample id not available', done => {
2020-05-12 12:15:36 +02:00
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '000000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'},
res: {status: 'Sample id not available'}
2020-05-12 12:15:36 +02:00
});
});
it('rejects an invalid measurement_template id', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '30000000000h000000000002'},
2020-05-12 12:15:36 +02:00
res: {status: 'Invalid body format', details: '"measurement_template" with value "30000000000h000000000002" fails to match the required pattern: /[0-9a-f]{24}/'}
});
});
it('rejects a measurement_template not available', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '000000000000000000000002'},
2020-05-12 12:15:36 +02:00
res: {status: 'Measurement template not available'}
});
});
it('rejects not specified values', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1, xx: 44}, measurement_template: '300000000000000000000002'},
2020-05-12 12:15:36 +02:00
res: {status: 'Invalid body format', details: '"xx" is not allowed'}
});
});
2020-05-27 17:03:03 +02:00
it('accepts missing values', done => {
2020-05-12 12:15:36 +02:00
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
2020-05-27 17:03:03 +02:00
httpStatus: 200,
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8}, measurement_template: '300000000000000000000002'}
}).end((err, res) => {
if (err) return done(err);
should(res.body).have.only.keys('_id', 'sample_id', 'values', 'measurement_template');
should(res.body).have.property('_id').be.type('string');
should(res.body).have.property('sample_id', '400000000000000000000001');
should(res.body).have.property('measurement_template', '300000000000000000000002');
should(res.body).have.property('values');
should(res.body.values).have.property('weight %', 0.8);
should(res.body.values).have.property('standard deviation', null);
done();
2020-05-12 12:15:36 +02:00
});
});
it('rejects no values', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000001', values: {}, measurement_template: '300000000000000000000002'},
res: {status: 'At least one value is required'}
});
});
2020-05-12 12:15:36 +02:00
it('rejects a value not in the value range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000001', values: {val2: 5}, measurement_template: '300000000000000000000004'},
res: {status: 'Invalid body format', details: '"val2" must be one of [1, 2, 3, 4, null]'}
2020-05-12 12:15:36 +02:00
});
});
it('rejects a value below minimum range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': -1, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'},
2020-05-12 12:15:36 +02:00
res: {status: 'Invalid body format', details: '"weight %" must be larger than or equal to 0'}
});
});
it('rejects a value above maximum range', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 2}, measurement_template: '300000000000000000000002'},
2020-05-12 12:15:36 +02:00
res: {status: 'Invalid body format', details: '"standard deviation" must be less than or equal to 0.5'}
});
});
2020-05-27 17:03:03 +02:00
it('rejects a missing sample id', done => {
2020-05-12 12:15:36 +02:00
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'},
2020-05-27 17:03:03 +02:00
res: {status: 'Invalid body format', details: '"sample_id" is required'}
2020-05-12 12:15:36 +02:00
});
});
it('rejects a missing measurement_template', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}},
2020-05-12 12:15:36 +02:00
res: {status: 'Invalid body format', details: '"measurement_template" is required'}
});
});
it('rejects adding a measurement to the sample of another user for a write user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 403,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000003', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
});
});
it('accepts adding a measurement to the sample of another user for a maintain/admin user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'admin'},
httpStatus: 200,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
}).end((err, res) => {
if (err) return done(err);
2020-05-27 17:03:03 +02:00
should(res.body).have.only.keys('_id', 'sample_id', 'values', 'measurement_template');
2020-05-12 12:15:36 +02:00
should(res.body).have.property('_id').be.type('string');
2020-05-27 17:03:03 +02:00
should(res.body).have.property('sample_id', '400000000000000000000001');
2020-05-12 12:15:36 +02:00
should(res.body).have.property('measurement_template', '300000000000000000000002');
should(res.body).have.property('values');
should(res.body.values).have.property('weight %', 0.8);
should(res.body.values).have.property('standard deviation', 0.1);
done();
});
});
it('rejects an old version of a measurement template', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'janedoe'},
httpStatus: 400,
req: {sample_id: '400000000000000000000001', values: {val1: 2}, measurement_template: '300000000000000000000003'},
res: {status: 'Old template version not allowed'}
});
});
2020-05-12 12:15:36 +02:00
it('rejects an API key', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {key: 'janedoe'},
httpStatus: 401,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
});
});
it('rejects requests from a read user', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
auth: {basic: 'user'},
httpStatus: 403,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'post',
url: '/measurement/new',
httpStatus: 401,
2020-05-27 17:03:03 +02:00
req: {sample_id: '400000000000000000000001', values: {'weight %': 0.8, 'standard deviation': 0.1}, measurement_template: '300000000000000000000002'}
2020-05-12 12:15:36 +02:00
});
});
});
});