/materials/new|deleted
This commit is contained in:
		@@ -5,6 +5,7 @@ import mongoSanitize from 'mongo-sanitize';
 | 
			
		||||
import api from './api';
 | 
			
		||||
import db from './db';
 | 
			
		||||
 | 
			
		||||
// TODO: overall commenting/documentation review
 | 
			
		||||
 | 
			
		||||
// tell if server is running in debug or production environment
 | 
			
		||||
console.info(process.env.NODE_ENV === 'production' ? '===== PRODUCTION =====' : process.env.NODE_ENV === 'test' ? '' :'===== DEVELOPMENT =====');
 | 
			
		||||
 
 | 
			
		||||
@@ -76,6 +76,101 @@ describe('/material', () => {
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  describe('GET /materials/{group}', () => {
 | 
			
		||||
    it('returns all new materials', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials/new',
 | 
			
		||||
        auth: {basic: 'admin'},
 | 
			
		||||
        httpStatus: 200
 | 
			
		||||
      }).end((err, res) => {
 | 
			
		||||
        if (err) return done(err);
 | 
			
		||||
        const json = require('../test/db.json');
 | 
			
		||||
        let asyncCounter = res.body.length;
 | 
			
		||||
        should(res.body).have.lengthOf(json.collections.materials.filter(e => e.status === 0).length);
 | 
			
		||||
        should(res.body).matchEach(material => {
 | 
			
		||||
          should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'mineral', 'glass_fiber', 'carbon_fiber', 'numbers');
 | 
			
		||||
          should(material).have.property('_id').be.type('string');
 | 
			
		||||
          should(material).have.property('name').be.type('string');
 | 
			
		||||
          should(material).have.property('supplier').be.type('string');
 | 
			
		||||
          should(material).have.property('group').be.type('string');
 | 
			
		||||
          should(material).have.property('mineral').be.type('number');
 | 
			
		||||
          should(material).have.property('glass_fiber').be.type('number');
 | 
			
		||||
          should(material).have.property('carbon_fiber').be.type('number');
 | 
			
		||||
          should(material.numbers).matchEach(number => {
 | 
			
		||||
            should(number).have.only.keys('color', 'number');
 | 
			
		||||
            should(number).have.property('color').be.type('string');
 | 
			
		||||
            should(number).have.property('number').be.type('string');
 | 
			
		||||
          });
 | 
			
		||||
          MaterialModel.findById(material._id).lean().exec((err, data) => {
 | 
			
		||||
            should(data).have.property('status', 0);
 | 
			
		||||
            if (--asyncCounter === 0) {
 | 
			
		||||
              done();
 | 
			
		||||
            }
 | 
			
		||||
          });
 | 
			
		||||
        });
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('returns all deleted materials', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials/deleted',
 | 
			
		||||
        auth: {basic: 'admin'},
 | 
			
		||||
        httpStatus: 200
 | 
			
		||||
      }).end((err, res) => {
 | 
			
		||||
        if (err) return done(err);
 | 
			
		||||
        const json = require('../test/db.json');
 | 
			
		||||
        let asyncCounter = res.body.length;
 | 
			
		||||
        should(res.body).have.lengthOf(json.collections.materials.filter(e => e.status === -1).length);
 | 
			
		||||
        should(res.body).matchEach(material => {
 | 
			
		||||
          should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'mineral', 'glass_fiber', 'carbon_fiber', 'numbers');
 | 
			
		||||
          should(material).have.property('_id').be.type('string');
 | 
			
		||||
          should(material).have.property('name').be.type('string');
 | 
			
		||||
          should(material).have.property('supplier').be.type('string');
 | 
			
		||||
          should(material).have.property('group').be.type('string');
 | 
			
		||||
          should(material).have.property('mineral').be.type('number');
 | 
			
		||||
          should(material).have.property('glass_fiber').be.type('number');
 | 
			
		||||
          should(material).have.property('carbon_fiber').be.type('number');
 | 
			
		||||
          should(material.numbers).matchEach(number => {
 | 
			
		||||
            should(number).have.only.keys('color', 'number');
 | 
			
		||||
            should(number).have.property('color').be.type('string');
 | 
			
		||||
            should(number).have.property('number').be.type('string');
 | 
			
		||||
          });
 | 
			
		||||
          MaterialModel.findById(material._id).lean().exec((err, data) => {
 | 
			
		||||
            should(data).have.property('status', -1);
 | 
			
		||||
            if (--asyncCounter === 0) {
 | 
			
		||||
              done();
 | 
			
		||||
            }
 | 
			
		||||
          });
 | 
			
		||||
        });
 | 
			
		||||
        done();
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('rejects requests from a write user', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials/new',
 | 
			
		||||
        auth: {basic: 'janedoe'},
 | 
			
		||||
        httpStatus: 403
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('rejects an API key', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials/deleted',
 | 
			
		||||
        auth: {key: 'admin'},
 | 
			
		||||
        httpStatus: 401
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
    it('rejects unauthorized requests', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
        method: 'get',
 | 
			
		||||
        url: '/materials/new',
 | 
			
		||||
        httpStatus: 401
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  describe('GET /material/{id}', () => {
 | 
			
		||||
    it('returns the right material', done => {
 | 
			
		||||
      TestHelper.request(server, done, {
 | 
			
		||||
 
 | 
			
		||||
@@ -21,6 +21,23 @@ router.get('/materials', (req, res, next) => {
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/materials/:group(new|deleted)', (req, res, next) => {
 | 
			
		||||
  if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
 | 
			
		||||
 | 
			
		||||
  let status;
 | 
			
		||||
  switch (req.params.group) {
 | 
			
		||||
    case 'new': status = 0;
 | 
			
		||||
      break;
 | 
			
		||||
    case 'deleted': status = -1;
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  MaterialModel.find({status: status}).lean().exec((err, data) => {
 | 
			
		||||
    if (err) return next(err);
 | 
			
		||||
    console.log(data);
 | 
			
		||||
    res.json(_.compact(data.map(e => MaterialValidate.output(e))));  // validate all and filter null values from validation errors
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
router.get('/material/' + IdValidate.parameter(), (req, res, next) => {
 | 
			
		||||
  if (!req.auth(res, ['read', 'write', 'maintain', 'dev', 'admin'], 'all')) return;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -230,7 +230,7 @@
 | 
			
		||||
            "number": ""
 | 
			
		||||
          }
 | 
			
		||||
        ],
 | 
			
		||||
        "status": 10,
 | 
			
		||||
        "status": 0,
 | 
			
		||||
        "__v": 0
 | 
			
		||||
      }
 | 
			
		||||
    ],
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user