387 lines
15 KiB
TypeScript
387 lines
15 KiB
TypeScript
|
import should from 'should/as-function';
|
||
|
import MaterialModel from '../models/material';
|
||
|
import TestHelper from "../helpers/test";
|
||
|
|
||
|
|
||
|
describe('/material', () => {
|
||
|
let server;
|
||
|
before(done => TestHelper.before(done));
|
||
|
beforeEach(done => server = TestHelper.beforeEach(server, done));
|
||
|
afterEach(done => TestHelper.afterEach(server, 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.users.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('number');
|
||
|
});
|
||
|
});
|
||
|
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.users.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('number');
|
||
|
});
|
||
|
});
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
it('rejects unauthorized requests', done => {
|
||
|
TestHelper.request(server, done, {
|
||
|
method: 'get',
|
||
|
url: '/materials',
|
||
|
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', mineral: 0, glass_fiber: 40, carbon_fiber: 0, numbers: [{color: 'black', number: 5514263423}]}
|
||
|
});
|
||
|
});
|
||
|
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', mineral: 0, glass_fiber: 0, carbon_fiber: 0, numbers: []}
|
||
|
});
|
||
|
});
|
||
|
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', mineral: 0, glass_fiber: 40, carbon_fiber: 0, numbers: [{color: 'black', number: 5514263423}]}
|
||
|
});
|
||
|
});
|
||
|
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', mineral: 0, glass_fiber: 35, carbon_fiber: 0, numbers: [{color: 'black', number: 5514212901}, {color: 'signalviolet', number: 5514612901}]}
|
||
|
,
|
||
|
}).end(err => {
|
||
|
if (err) return done(err);
|
||
|
MaterialModel.findById('100000000000000000000001').lean().exec((err, data) => {
|
||
|
if (err) return done(err);
|
||
|
should(data).be.eql({_id: '100000000000000000000002', name: 'UltramidTKR4355G7', supplier: 'BASF', group: 'PA6/6T', mineral: 0, glass_fiber: 35, carbon_fiber: 0, numbers: [{color: 'black', number: 5514212901}, {color: 'signalviolet', number: 5514612901}], __v: 0}
|
||
|
);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
it('rejects already existing material names', done => {
|
||
|
TestHelper.request(server, done, {
|
||
|
method: 'put',
|
||
|
url: '/material/100000000000000000000001',
|
||
|
auth: {basic: 'janedoe'},
|
||
|
httpStatus: 400,
|
||
|
req: {name: 'UltramidTKR4355G7'},
|
||
|
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: {mineral: 'x', glass_fiber: 'x', carbon_fiber: 'x', numbers: [{colorxx: 'black', number: 'xxx'}]},
|
||
|
res: {status: 'Invalid body format'}
|
||
|
});
|
||
|
});
|
||
|
it('rejects an invalid id', done => {
|
||
|
TestHelper.request(server, done, {
|
||
|
method: 'put',
|
||
|
url: '/material/10000000000000000000000x',
|
||
|
auth: {basic: 'admin'},
|
||
|
httpStatus: 400,
|
||
|
req: {},
|
||
|
res: {status: 'Invalid id'}
|
||
|
});
|
||
|
});
|
||
|
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('deletes the material', done => {
|
||
|
TestHelper.request(server, done, {
|
||
|
method: 'delete',
|
||
|
url: '/material/100000000000000000000001',
|
||
|
auth: {basic: 'janedoe'},
|
||
|
httpStatus: 200
|
||
|
}).end((err, res) => {
|
||
|
if (err) return done(err);
|
||
|
should(res.body).be.eql({status: 'OK'});
|
||
|
MaterialModel.findById('100000000000000000000001').lean().exec((err, data) => {
|
||
|
if (err) return done(err);
|
||
|
should(data).have.lengthOf(0);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
it('rejects deleting a material referenced by samples');
|
||
|
it('rejects an invalid id', done => {
|
||
|
TestHelper.request(server, done, {
|
||
|
method: 'delete',
|
||
|
url: '/material/10000000000000000000000x',
|
||
|
auth: {basic: 'admin'},
|
||
|
httpStatus: 400,
|
||
|
res: {status: 'Invalid id'}
|
||
|
});
|
||
|
});
|
||
|
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('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', mineral: 0, glass_fiber: 30, carbon_fiber: 0, numbers: [{color: 'black', number: 5515798402}]}
|
||
|
}).end((err, res) => {
|
||
|
if (err) return done (err);
|
||
|
console.log(res.body);
|
||
|
should(res.body).have.only.keys('_id', 'name', 'supplier', 'group', 'mineral', 'glass_fiber', 'carbon_fiber', '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).have.property('mineral', 0);
|
||
|
should(res.body).have.property('glass_fiber', 30);
|
||
|
should(res.body).have.property('carbon_fiber', 0);
|
||
|
should(res.body.numbers).matchEach(number => {
|
||
|
should(number).have.only.keys('color', 'number');
|
||
|
should(number).have.property('color', 'black');
|
||
|
should(number).have.property('number', 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', 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, data: any) => {
|
||
|
if (err) return done (err);
|
||
|
console.log(data[0]);
|
||
|
should(data[0]).have.only.keys('_id', 'name', 'supplier', 'group', 'mineral', 'glass_fiber', 'carbon_fiber', 'numbers', '__v');
|
||
|
should(data[0]).have.property('_id');
|
||
|
should(data[0]).have.property('name', 'Crastin CE 2510');
|
||
|
should(data[0]).have.property('supplier', 'Du Pont');
|
||
|
should(data[0]).have.property('group', 'PBT');
|
||
|
should(data[0]).have.property('mineral', '0');
|
||
|
should(data[0]).have.property('glass_fiber', '30');
|
||
|
should(data[0]).have.property('carbon_fiber', '0');
|
||
|
should(data[0].numbers).have.lengthOf(0);
|
||
|
done();
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
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', mineral: 0, glass_fiber: 40, carbon_fiber: 0, numbers: [{color: 'black', number: 5514263423}]},
|
||
|
res: {status: 'Material name already taken'}
|
||
|
});
|
||
|
});
|
||
|
it('rejects wrong material properties', 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', mineral: 'x', glass_fiber: 'x', carbon_fiber: 'x', numbers: [{colorxx: 'black', number: 'xxx'}]},
|
||
|
res: {status: 'Invalid body format'}
|
||
|
});
|
||
|
});
|
||
|
it('rejects incomplete material properties', done => {
|
||
|
TestHelper.request(server, done, {
|
||
|
method: 'post',
|
||
|
url: '/material/new',
|
||
|
auth: {basic: 'janedoe'},
|
||
|
httpStatus: 400,
|
||
|
req: {name: 'Crastin CE 2510'},
|
||
|
res: {status: 'Invalid body format'}
|
||
|
});
|
||
|
});
|
||
|
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', 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', 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', mineral: 0, glass_fiber: 30, carbon_fiber: 0, numbers: []}
|
||
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|