From d93b2ad748fb8606d6b999d9c55bde8b646f7878 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Fri, 29 May 2020 12:22:01 +0200 Subject: [PATCH] validation for sample --- api/sample.yaml | 25 +++++++++++++ src/routes/sample.spec.ts | 76 ++++++++++++++++++++++++++++++++++++++- src/routes/sample.ts | 28 +++++++++++++++ src/test/db.json | 11 ++++++ 4 files changed, 139 insertions(+), 1 deletion(-) diff --git a/api/sample.yaml b/api/sample.yaml index 00e35ff..d074172 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -142,6 +142,31 @@ 500: $ref: 'api.yaml#/components/responses/500' +/sample/validate/{id}: + parameters: + - $ref: 'api.yaml#/components/parameters/Id' + put: + summary: set sample status to validated + description: 'Auth: basic, levels: maintain, admin' + x-doc: status is set to 10 + tags: + - /sample + security: + - BasicAuth: [] + responses: + 200: + $ref: 'api.yaml#/components/responses/Ok' + 400: + $ref: 'api.yaml#/components/responses/400' + 401: + $ref: 'api.yaml#/components/responses/401' + 403: + $ref: 'api.yaml#/components/responses/403' + 404: + $ref: 'api.yaml#/components/responses/404' + 500: + $ref: 'api.yaml#/components/responses/500' + /sample/new: post: summary: add sample diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index 11a5641..f2fdd36 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -12,7 +12,6 @@ import mongoose from 'mongoose'; // TODO: filter by not completely filled/no measurements // TODO: write script for data import // TODO: allow adding sample numbers for existing samples -// TODO: Do not allow validation or measurement entry without condition describe('/sample', () => { @@ -875,6 +874,81 @@ describe('/sample', () => { }); }); + describe('PUT /sample/validate/{id}', () => { + it('sets the status', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/400000000000000000000003', + auth: {basic: 'admin'}, + httpStatus: 200, + req: {} + }).end((err, res) => { + if (err) return done (err); + should(res.body).be.eql({status: 'OK'}); + SampleModel.findById('400000000000000000000003').lean().exec((err, data: any) => { + if (err) return done(err); + should(data).have.property('status',globals.status.validated); + done(); + }); + }); + }); + it('rejects validating a sample without condition', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/400000000000000000000006', + auth: {basic: 'admin'}, + httpStatus: 400, + req: {}, + res: {status: 'Sample without condition cannot be valid'} + }); + }); + it('rejects validating a sample without measurements', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/400000000000000000000004', + auth: {basic: 'admin'}, + httpStatus: 400, + req: {}, + res: {status: 'Sample without measurements cannot be valid'} + }); + }); + it('rejects an API key', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/400000000000000000000003', + auth: {key: 'admin'}, + httpStatus: 401, + req: {} + }); + }); + it('rejects a write user', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/400000000000000000000003', + auth: {basic: 'janedoe'}, + httpStatus: 403, + req: {} + }); + }); + it('returns 404 for an unknown sample', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/000000000000000000000003', + auth: {basic: 'admin'}, + httpStatus: 404, + req: {} + }); + }); + it('rejects unauthorized requests', done => { + TestHelper.request(server, done, { + method: 'put', + url: '/sample/validate/400000000000000000000003', + httpStatus: 401, + req: {} + }); + }); + }); + describe('POST /sample/new', () => { it('returns the right sample', done => { TestHelper.request(server, done, { diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 65c0a86..e741c4a 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -182,6 +182,34 @@ router.put('/sample/restore/' + IdValidate.parameter(), (req, res, next) => { }); }); +router.put('/sample/validate/' + IdValidate.parameter(), (req, res, next) => { + if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; + + SampleModel.findById(req.params.id).lean().exec((err, data: any) => { + if (err) return next(err); + + if (!data) { + return res.status(404).json({status: 'Not found'}); + } + if (Object.keys(data.condition).length === 0) { + return res.status(400).json({status: 'Sample without condition cannot be valid'}); + } + + MeasurementModel.find({sample_id: mongoose.Types.ObjectId(req.params.id)}).lean().exec((err, data) => { + if (err) return next(err); + + if (data.length === 0) { + return res.status(400).json({status: 'Sample without measurements cannot be valid'}); + } + + SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.validated}).lean().exec(err => { + if (err) return next(err); + res.json({status: 'OK'}); + }); + }); + }); +}); + router.post('/sample/new', async (req, res, next) => { if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return; diff --git a/src/test/db.json b/src/test/db.json index b65c0ec..0d49876 100644 --- a/src/test/db.json +++ b/src/test/db.json @@ -407,6 +407,17 @@ "status": 10, "measurement_template": {"$oid":"300000000000000000000002"}, "__v": 0 + }, + { + "_id": {"$oid":"800000000000000000000006"}, + "sample_id": {"$oid":"400000000000000000000006"}, + "values": { + "weight %": 0.5, + "standard deviation":null + }, + "status": 0, + "measurement_template": {"$oid":"300000000000000000000002"}, + "__v": 0 } ], "condition_templates": [