1086 lines
44 KiB
TypeScript
1086 lines
44 KiB
TypeScript
import should from 'should/as-function';
|
|
import MaterialModel from '../models/material';
|
|
import MaterialGroupModel from '../models/material_groups';
|
|
import MaterialSupplierModel from '../models/material_suppliers';
|
|
import TestHelper from "../test/helper";
|
|
import globals from '../globals';
|
|
|
|
|
|
describe('/material', () => {
|
|
let server;
|
|
before(done => TestHelper.before(done));
|
|
beforeEach(done => server = TestHelper.beforeEach(server, done));
|
|
afterEach(done => TestHelper.afterEach(server, done));
|
|
after(done => TestHelper.after(done));
|
|
|
|
describe('GET /materials', () => {
|
|
it('returns all materials', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/materials',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.materials.filter(e => e.status === 'validated').length);
|
|
should(res.body).matchEach(material => {
|
|
should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'properties', '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.properties).have.property('material_template').be.type('string');
|
|
should(material.numbers).be.instanceof(Array);
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('works with an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/materials',
|
|
auth: {key: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.materials.filter(e => e.status === 'validated').length);
|
|
should(res.body).matchEach(material => {
|
|
should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'properties', '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.properties).have.property('material_template').be.type('string');
|
|
should(material.numbers).be.instanceof(Array);
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('allows filtering by state', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/materials?status=new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.materials.filter(e => e.status === 'new').length);
|
|
should(res.body).matchEach(material => {
|
|
should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'properties', '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.properties).have.property('material_template').be.type('string');
|
|
should(material.numbers).be.instanceof(Array);
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('rejects an invalid state name', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/materials?status=xxx',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
res: {status: 'Invalid body format', details: '"status" must be one of [validated, new, all]'}
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/materials',
|
|
httpStatus: 401
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('GET /materials/{state}', () => {
|
|
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 ==='new').length);
|
|
should(res.body).matchEach(material => {
|
|
should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'properties', '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.properties).have.property('material_template').be.type('string');
|
|
should(material.numbers).be.instanceof(Array);
|
|
MaterialModel.findById(material._id).lean().exec((err, data) => {
|
|
should(data).have.property('status','new');
|
|
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 ==='deleted').length);
|
|
should(res.body).matchEach(material => {
|
|
should(material).have.only.keys('_id', 'name', 'supplier', 'group', 'properties', '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.properties).have.property('material_template').be.type('string');
|
|
should(material.numbers).be.instanceof(Array);
|
|
MaterialModel.findById(material._id).lean().exec((err, data) => {
|
|
should(data).have.property('status','deleted');
|
|
if (--asyncCounter === 0) {
|
|
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, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
res: {_id: '100000000000000000000001', name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423', '5514263422']}
|
|
});
|
|
});
|
|
it('returns the right material for an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000003',
|
|
auth: {key: 'admin'},
|
|
httpStatus: 200,
|
|
res: {_id: '100000000000000000000003', name: 'PA GF 50 black (2706)', supplier: 'Akro-Plastic', group: 'PA66+PA6I/6T', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 0, carbon_fiber: 0}, numbers: []}
|
|
});
|
|
});
|
|
it('returns a material with a color without number', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000007',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
res: {_id: '100000000000000000000007', name: 'Ultramid A4H', supplier: 'BASF', group: 'PA66', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 0, carbon_fiber: 0}, numbers: []}
|
|
});
|
|
});
|
|
it('returns a deleted material for a dev/admin user', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000008',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 200,
|
|
res: {_id: '100000000000000000000008', name: 'Latamid 66 H 2 G 30', supplier: 'LATI', group: 'PA66', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: ['5513943509']}
|
|
});
|
|
});
|
|
it('returns 403 for a write user when requesting a deleted material', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000008',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 403
|
|
});
|
|
});
|
|
it('rejects an invalid id', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/10000000000000000000000x',
|
|
auth: {key: 'admin'},
|
|
httpStatus: 404
|
|
});
|
|
});
|
|
it('rejects an unknown id', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000111',
|
|
auth: {key: 'janedoe'},
|
|
httpStatus: 404
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/100000000000000000000001',
|
|
httpStatus: 401
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('PUT /material/{id}', () => {
|
|
it('returns the right material', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {},
|
|
res: {_id: '100000000000000000000001', name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423', '5514263422']}
|
|
});
|
|
});
|
|
it('keeps unchanged properties', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423', '5514263422']}
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
should(res.body).be.eql({_id: '100000000000000000000001', name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423', '5514263422']});
|
|
MaterialModel.findById('100000000000000000000001').lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('status','validated');
|
|
MaterialGroupModel.find({name: 'PA46'}).lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.lengthOf(1);
|
|
should(data[0]._id.toString()).be.eql('900000000000000000000001');
|
|
MaterialSupplierModel.find({name: 'DSM'}).lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.lengthOf(1);
|
|
should(data[0]._id.toString()).be.eql('110000000000000000000001');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
it('keeps only one unchanged property', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'Stanyl TW 200 F8'}
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
should(res.body).be.eql({_id: '100000000000000000000001', name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423', '5514263422']});
|
|
MaterialModel.findById('100000000000000000000001').lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('status','validated');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
it('keeps unchanged properties', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}}
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
should(res.body).be.eql({_id: '100000000000000000000001', name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423', '5514263422']});
|
|
MaterialModel.findById('100000000000000000000001').lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('status','validated');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
it('changes the given properties', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'UltramidTKR4355G7_2', supplier: 'BASF', group: 'PA6/6T', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 35, carbon_fiber: 0}, numbers: ['5514212901', '5514612901']}
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
should(res.body).be.eql({_id: '100000000000000000000001', name: 'UltramidTKR4355G7_2', supplier: 'BASF', group: 'PA6/6T', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 35, carbon_fiber: 0}, numbers: ['5514212901', '5514612901']});
|
|
MaterialModel.findById('100000000000000000000001').lean().exec((err, data:any) => {
|
|
if (err) return done(err);
|
|
data._id = data._id.toString();
|
|
data.group_id = data.group_id.toString();
|
|
data.supplier_id = data.supplier_id.toString();
|
|
should(data).be.eql({_id: '100000000000000000000001', name: 'UltramidTKR4355G7_2', supplier_id: '110000000000000000000002', group_id: '900000000000000000000002', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 35, carbon_fiber: 0}, numbers: ['5514212901', '5514612901'], status: 'new', __v: 0});
|
|
MaterialGroupModel.find({name: 'PA6/6T'}).lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.lengthOf(1);
|
|
should(data[0]._id.toString()).be.eql('900000000000000000000002');
|
|
MaterialSupplierModel.find({name: 'BASF'}).lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.lengthOf(1);
|
|
should(data[0]._id.toString()).be.eql('110000000000000000000002');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
it('creates a changelog', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'UltramidTKR4355G7_2', supplier: 'BASF', group: 'PA6/6T', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 35, carbon_fiber: 0}, numbers: ['5514212901', '5514612901']},
|
|
log: {
|
|
collection: 'materials',
|
|
dataAdd: {
|
|
group_id: '900000000000000000000002',
|
|
supplier_id: '110000000000000000000002',
|
|
status: 'new'
|
|
},
|
|
dataIgn: ['supplier', 'group']
|
|
}
|
|
});
|
|
});
|
|
it('rejects already existing material names', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Ultramid T KR 4355 G7'},
|
|
res: {status: 'Material name already taken'}
|
|
});
|
|
});
|
|
it('rejects wrong material properties', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '130000000000000000000003', mineral: 'x', glass_fiber: 0, carbon_fiber: 0}},
|
|
res: {status: 'Invalid body format', details: '"mineral" must be a number'}
|
|
});
|
|
});
|
|
it('rejects an invalid id', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/10000000000000000000000x',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 404,
|
|
req: {},
|
|
});
|
|
});
|
|
it('rejects not specified properties parameters', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 0, carbon_fiber: 0, x: 55}},
|
|
res: {status: 'Invalid body format', details: '"x" is not allowed'}
|
|
});
|
|
});
|
|
it('rejects a properties parameter not in the value range', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000009',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '130000000000000000000002', stickiness: 'xx'}},
|
|
res: {status: 'Invalid body format', details: '"stickiness" must be one of [not so sticky, medium, very sticky]'}
|
|
});
|
|
});
|
|
it('rejects a properties parameter below minimum range', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: -5, carbon_fiber: 0}},
|
|
res: {status: 'Invalid body format', details: '"glass_fiber" must be greater than or equal to 0'}
|
|
});
|
|
});
|
|
it('rejects a properties parameter above maximum range', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 0, carbon_fiber: 105}},
|
|
res: {status: 'Invalid body format', details: '"carbon_fiber" must be less than or equal to 100'}
|
|
});
|
|
});
|
|
it('rejects an invalid material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '1300000000000h0000000001', mineral: 0, glass_fiber: 0, carbon_fiber: 0}},
|
|
res: {status: 'Material template not available'}
|
|
});
|
|
});
|
|
it('rejects an unknown material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '100000000000000000000001', mineral: 0, glass_fiber: 0, carbon_fiber: 0}},
|
|
res: {status: 'Material template not available'}
|
|
});
|
|
});
|
|
it('rejects an old version of a material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {properties: {material_template: '130000000000000000000001', glass_fiber: 0}},
|
|
res: {status: 'Old template version not allowed'}
|
|
});
|
|
});
|
|
it('allows keeping an old version of a material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000010',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {properties: {material_template: '130000000000000000000001', glass_fiber: 5}},
|
|
res: {_id: '100000000000000000000010', name: 'Latamid 66 G 40', numbers: ['5513943509'], supplier: 'LATI', group: 'PA66', properties: {material_template: '130000000000000000000001', glass_fiber: 5}}
|
|
});
|
|
});
|
|
it('rejects editing a deleted material', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000008',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 403,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000002',
|
|
auth: {key: 'admin'},
|
|
httpStatus: 401,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects requests from a read user', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000002',
|
|
auth: {basic: 'user'},
|
|
httpStatus: 403,
|
|
req: {}
|
|
});
|
|
});
|
|
it('returns 404 for an unknown material', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000111',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 404,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/100000000000000000000001',
|
|
httpStatus: 401,
|
|
req: {}
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('DELETE /material/{id}', () => {
|
|
it('sets the status to deleted', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000002',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
should(res.body).be.eql({status: 'OK'});
|
|
MaterialModel.findById('100000000000000000000002').lean().exec((err, data: any) => {
|
|
if (err) return done(err);
|
|
data._id = data._id.toString();
|
|
data.group_id = data.group_id.toString();
|
|
data.supplier_id = data.supplier_id.toString();
|
|
data.properties.material_template = data.properties.material_template.toString();
|
|
should(data).be.eql({_id: '100000000000000000000002', name: 'Ultramid T KR 4355 G7', supplier_id: '110000000000000000000002', group_id: '900000000000000000000002', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 35, carbon_fiber: 0}, numbers: ['5514212901', '5514612901'], status: 'deleted', __v: 0}
|
|
);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
it('creates a changelog', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000002',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
log: {
|
|
collection: 'materials',
|
|
dataAdd: { status: 'deleted'}
|
|
}
|
|
});
|
|
});
|
|
it('rejects deleting a material referenced by samples', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000004',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
res: {status: 'Material still in use'}
|
|
})
|
|
});
|
|
it('rejects an invalid id', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/10000000000000000000000x',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 404
|
|
});
|
|
});
|
|
it('rejects an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000002',
|
|
auth: {key: 'admin'},
|
|
httpStatus: 401
|
|
});
|
|
});
|
|
it('rejects requests from a read user', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000002',
|
|
auth: {basic: 'user'},
|
|
httpStatus: 403
|
|
});
|
|
});
|
|
it('returns 404 for an unknown id', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000111',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 404
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'delete',
|
|
url: '/material/100000000000000000000001',
|
|
httpStatus: 401
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('PUT /material/restore/{id}', () => {
|
|
it('sets the status', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/restore/100000000000000000000008',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 200,
|
|
req: {}
|
|
}).end((err, res) => {
|
|
if (err) return done (err);
|
|
should(res.body).be.eql({status: 'OK'});
|
|
MaterialModel.findById('100000000000000000000008').lean().exec((err, data: any) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('status','new');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
it('creates a changelog', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/restore/100000000000000000000008',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 200,
|
|
req: {},
|
|
log: {
|
|
collection: 'materials',
|
|
dataAdd: {
|
|
status: 'new'
|
|
}
|
|
}
|
|
});
|
|
});
|
|
it('rejects an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/restore/100000000000000000000008',
|
|
auth: {key: 'admin'},
|
|
httpStatus: 401,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects a write user', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/restore/100000000000000000000008',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 403,
|
|
req: {}
|
|
});
|
|
});
|
|
it('returns 404 for an unknown sample', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/restore/000000000000000000000008',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 404,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/restore/100000000000000000000008',
|
|
httpStatus: 401,
|
|
req: {}
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('PUT /material/validate/{id}', () => {
|
|
it('sets the status', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/validate/100000000000000000000007',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 200,
|
|
req: {}
|
|
}).end((err, res) => {
|
|
if (err) return done (err);
|
|
should(res.body).be.eql({status: 'OK'});
|
|
MaterialModel.findById('100000000000000000000007').lean().exec((err, data: any) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('status','validated');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
it('creates a changelog', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/validate/100000000000000000000007',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 200,
|
|
req: {},
|
|
log: {
|
|
collection: 'materials',
|
|
dataAdd: {
|
|
status: 'validated'
|
|
}
|
|
}
|
|
});
|
|
});
|
|
it('rejects an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/validate/100000000000000000000007',
|
|
auth: {key: 'admin'},
|
|
httpStatus: 401,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects a write user', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/validate/100000000000000000000007',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 403,
|
|
req: {}
|
|
});
|
|
});
|
|
it('returns 404 for an unknown sample', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/validate/000000000000000000000007',
|
|
auth: {basic: 'admin'},
|
|
httpStatus: 404,
|
|
req: {}
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'put',
|
|
url: '/material/validate/100000000000000000000007',
|
|
httpStatus: 401,
|
|
req: {}
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('POST /material/new', () => {
|
|
it('returns the right material', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: ['5515798402']}
|
|
}).end((err, res) => {
|
|
if (err) return done (err);
|
|
should(res.body).have.only.keys('_id', 'name', 'supplier', 'group', 'properties', 'numbers');
|
|
should(res.body).have.property('_id').be.type('string');
|
|
should(res.body).have.property('name', 'Crastin CE 2510');
|
|
should(res.body).have.property('supplier', 'Du Pont');
|
|
should(res.body).have.property('group', 'PBT');
|
|
should(res.body.properties).have.property('material_template', '130000000000000000000003');
|
|
should(res.body.properties).have.property('mineral', 0);
|
|
should(res.body.properties).have.property('glass_fiber', 30);
|
|
should(res.body.properties).have.property('carbon_fiber', 0);
|
|
should(res.body).have.property('numbers', ['5515798402']);
|
|
done();
|
|
});
|
|
});
|
|
it('stores the material', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: []}
|
|
}).end(err => {
|
|
if (err) return done (err);
|
|
MaterialModel.find({name: 'Crastin CE 2510'}).lean().exec((err, materialData: any) => {
|
|
if (err) return done (err);
|
|
should(materialData).have.lengthOf(1);
|
|
should(materialData[0]).have.only.keys('_id', 'name', 'supplier_id', 'group_id', 'properties', 'numbers', 'status', '__v');
|
|
should(materialData[0]).have.property('name', 'Crastin CE 2510');
|
|
should(materialData[0].properties).have.property('material_template', '130000000000000000000003');
|
|
should(materialData[0].properties).have.property('mineral', 0);
|
|
should(materialData[0].properties).have.property('glass_fiber', 30);
|
|
should(materialData[0].properties).have.property('carbon_fiber', 0);
|
|
should(materialData[0]).have.property('status','new');
|
|
should(materialData[0].numbers).have.lengthOf(0);
|
|
MaterialGroupModel.findById(materialData[0].group_id).lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('name', 'PBT')
|
|
MaterialSupplierModel.findById(materialData[0].supplier_id).lean().exec((err, data) => {
|
|
if (err) return done(err);
|
|
should(data).have.property('name', 'Du Pont');
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
it('creates a changelog', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: []},
|
|
log: {
|
|
collection: 'materials',
|
|
dataAdd: {status: 'new'},
|
|
dataIgn: ['group_id', 'supplier_id', 'group', 'supplier']
|
|
}
|
|
});
|
|
});
|
|
it('rejects already existing material names', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Stanyl TW 200 F8', supplier: 'DSM', group: 'PA46', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 40, carbon_fiber: 0}, numbers: ['5514263423']},
|
|
res: {status: 'Material name already taken'}
|
|
});
|
|
});
|
|
it('rejects a missing name', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"name" is required'}
|
|
});
|
|
});
|
|
it('rejects a missing supplier', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"supplier" is required'}
|
|
});
|
|
});
|
|
it('rejects a missing group', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"group" is required'}
|
|
});
|
|
});
|
|
it('rejects a missing mineral property', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', glass_fiber: 30, carbon_fiber: 0}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"mineral" is required'}
|
|
});
|
|
});
|
|
it('rejects a missing glass_fiber property', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, carbon_fiber: 0}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"glass_fiber" is required'}
|
|
});
|
|
});
|
|
it('rejects a missing carbon_fiber property', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"carbon_fiber" is required'}
|
|
});
|
|
});
|
|
it('rejects a missing numbers array', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}},
|
|
res: {status: 'Invalid body format', details: '"numbers" is required'}
|
|
});
|
|
});
|
|
it('rejects not specified properties parameters', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, carbon_fiber: 0, glass_fiber: 30, x: 47}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"x" is not allowed'}
|
|
});
|
|
});
|
|
it('rejects a properties parameter not in the value range', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Glue2', supplier: 'BASF', group: 'Glue', properties: {material_template: '130000000000000000000002', stickiness: 'not so much'}, numbers: []},
|
|
res: {status: 'Invalid body format', details: '"stickiness" must be one of [not so sticky, medium, very sticky]'}
|
|
});
|
|
});
|
|
it('rejects a properties parameter below minimum range', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', glass_fiber: -0.3}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"glass_fiber" must be greater than or equal to 0'}
|
|
});
|
|
});
|
|
it('rejects a properties parameter above maximum range', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', glass_fiber: 100.001}, numbers: ['5515798402']},
|
|
res: {status: 'Invalid body format', details: '"glass_fiber" must be less than or equal to 100'}
|
|
});
|
|
});
|
|
it('rejects an invalid material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000h00000000000003', glass_fiber: 30}, numbers: ['5515798402']},
|
|
res: {status: 'Material template not available'}
|
|
});
|
|
});
|
|
it('rejects an unknown material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '100000000000000000000003', glass_fiber: 30}, numbers: ['5515798402']},
|
|
res: {status: 'Material template not available'}
|
|
});
|
|
});
|
|
it('rejects an old version of a material template', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 400,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000001', glass_fiber: 30}, numbers: ['5515798402']},
|
|
res: {status: 'Old template version not allowed'}
|
|
});
|
|
});
|
|
it('rejects an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {key: 'janedoe'},
|
|
httpStatus: 401,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: []}
|
|
});
|
|
});
|
|
it('rejects requests from a read user', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
auth: {basic: 'user'},
|
|
httpStatus: 403,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: []}
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'post',
|
|
url: '/material/new',
|
|
httpStatus: 401,
|
|
req: {name: 'Crastin CE 2510', supplier: 'Du Pont', group: 'PBT', properties: {material_template: '130000000000000000000003', mineral: 0, glass_fiber: 30, carbon_fiber: 0}, numbers: []}
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('GET /material/groups', () => {
|
|
it('returns all groups', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/groups',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.material_groups.length);
|
|
should(res.body[0]).be.eql(json.collections.material_groups[0].name);
|
|
should(res.body).matchEach(group => {
|
|
should(group).be.type('string');
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('works with an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/groups',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.material_groups.length);
|
|
should(res.body[0]).be.eql(json.collections.material_groups[0].name);
|
|
should(res.body).matchEach(group => {
|
|
should(group).be.type('string');
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/groups',
|
|
httpStatus: 401
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('GET /material/suppliers', () => {
|
|
it('returns all suppliers', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/suppliers',
|
|
auth: {basic: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.material_suppliers.length);
|
|
should(res.body[0]).be.eql(json.collections.material_suppliers[0].name);
|
|
should(res.body).matchEach(supplier => {
|
|
should(supplier).be.type('string');
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('works with an API key', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/suppliers',
|
|
auth: {key: 'janedoe'},
|
|
httpStatus: 200
|
|
}).end((err, res) => {
|
|
if (err) return done(err);
|
|
const json = require('../test/db.json');
|
|
should(res.body).have.lengthOf(json.collections.material_suppliers.length);
|
|
should(res.body[0]).be.eql(json.collections.material_suppliers[0].name);
|
|
should(res.body).matchEach(supplier => {
|
|
should(supplier).be.type('string');
|
|
});
|
|
done();
|
|
});
|
|
});
|
|
it('rejects unauthorized requests', done => {
|
|
TestHelper.request(server, done, {
|
|
method: 'get',
|
|
url: '/material/suppliers',
|
|
httpStatus: 401
|
|
});
|
|
});
|
|
});
|
|
}); |