validation for sample
This commit is contained in:
		@@ -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, {
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user