validation for sample
This commit is contained in:
		@@ -142,6 +142,31 @@
 | 
				
			|||||||
      500:
 | 
					      500:
 | 
				
			||||||
        $ref: 'api.yaml#/components/responses/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:
 | 
					/sample/new:
 | 
				
			||||||
  post:
 | 
					  post:
 | 
				
			||||||
    summary: add sample
 | 
					    summary: add sample
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,7 +12,6 @@ import mongoose from 'mongoose';
 | 
				
			|||||||
// TODO: filter by not completely filled/no measurements
 | 
					// TODO: filter by not completely filled/no measurements
 | 
				
			||||||
// TODO: write script for data import
 | 
					// TODO: write script for data import
 | 
				
			||||||
// TODO: allow adding sample numbers for existing samples
 | 
					// TODO: allow adding sample numbers for existing samples
 | 
				
			||||||
// TODO: Do not allow validation or measurement entry without condition
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe('/sample', () => {
 | 
					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', () => {
 | 
					  describe('POST /sample/new', () => {
 | 
				
			||||||
    it('returns the right sample', done => {
 | 
					    it('returns the right sample', done => {
 | 
				
			||||||
      TestHelper.request(server, done, {
 | 
					      TestHelper.request(server, done, {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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) => {
 | 
					router.post('/sample/new', async (req, res, next) => {
 | 
				
			||||||
  if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
 | 
					  if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -407,6 +407,17 @@
 | 
				
			|||||||
        "status": 10,
 | 
					        "status": 10,
 | 
				
			||||||
        "measurement_template": {"$oid":"300000000000000000000002"},
 | 
					        "measurement_template": {"$oid":"300000000000000000000002"},
 | 
				
			||||||
        "__v": 0
 | 
					        "__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": [
 | 
					    "condition_templates": [
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user