import should from 'should/as-function'; import SampleModel from '../models/sample'; import NoteModel from '../models/note'; import NoteFieldModel from '../models/note_field'; import TestHelper from "../helpers/test"; describe('/sample', () => { let server; before(done => TestHelper.before(done)); beforeEach(done => server = TestHelper.beforeEach(server, done)); afterEach(done => TestHelper.afterEach(server, done)); describe('GET /samples', () => { it('returns all samples', done => { TestHelper.request(server, done, { method: 'get', url: '/samples', 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.samples.length); should(res.body).matchEach(material => { should(material).have.only.keys('_id', 'number', 'type', 'color', 'batch', 'material_id', 'note_id', 'user_id'); should(material).have.property('_id').be.type('string'); should(material).have.property('number').be.type('string'); should(material).have.property('type').be.type('string'); should(material).have.property('color').be.type('string'); should(material).have.property('batch').be.type('string'); should(material).have.property('material_id').be.type('string'); should(material).have.property('note_id'); should(material).have.property('user_id').be.type('string'); }); done(); }); }); it('works with an API key', done => { TestHelper.request(server, done, { method: 'get', url: '/samples', 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.samples.length); should(res.body).matchEach(material => { should(material).have.only.keys('_id', 'number', 'type', 'color', 'batch', 'material_id', 'note_id', 'user_id'); should(material).have.property('_id').be.type('string'); should(material).have.property('number').be.type('string'); should(material).have.property('type').be.type('string'); should(material).have.property('color').be.type('string'); should(material).have.property('batch').be.type('string'); should(material).have.property('material_id').be.type('string'); should(material).have.property('note_id'); should(material).have.property('user_id').be.type('string'); }); done(); }); }); it('rejects unauthorized requests', done => { TestHelper.request(server, done, { method: 'get', url: '/samples', httpStatus: 401 }); }); }); describe('POST /sample/new', () => { it('returns the right sample', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 200, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}} }).end((err, res) => { if (err) return done (err); should(res.body).have.only.keys('_id', 'number', 'color', 'type', 'batch', 'material_id', 'note_id', 'user_id'); should(res.body).have.property('_id').be.type('string'); should(res.body).have.property('number', 'Rng172'); should(res.body).have.property('color', 'black'); should(res.body).have.property('type', 'granulate'); should(res.body).have.property('batch', '1560237365'); should(res.body).have.property('material_id', '100000000000000000000001'); should(res.body).have.property('note_id').be.type('string'); should(res.body).have.property('user_id', '000000000000000000000002'); done(); }); }); it('stores the sample', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 200, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}} }).end(err => { if (err) return done (err); SampleModel.find({number: 'Rng172'}).lean().exec((err, data: any) => { if (err) return done (err); should(data).have.lengthOf(1); should(data[0]).have.only.keys('_id', 'number', 'color', 'type', 'batch', 'material_id', 'note_id', 'user_id', '__v'); should(data[0]).have.property('_id'); should(data[0]).have.property('number', 'Rng172'); should(data[0]).have.property('color', 'black'); should(data[0]).have.property('type', 'granulate'); should(data[0]).have.property('batch', '1560237365'); should(data[0].material_id.toString()).be.eql('100000000000000000000001'); should(data[0].user_id.toString()).be.eql('000000000000000000000002'); should(data[0]).have.property('note_id'); NoteModel.findById(data[0].note_id).lean().exec((err, data: any) => { if (err) return done (err); should(data).have.property('_id'); should(data).have.property('comment', 'Testcomment'); should(data).have.property('sample_references'); should(data.sample_references).have.lengthOf(1); should(data.sample_references[0].id.toString()).be.eql('400000000000000000000003'); should(data.sample_references[0]).have.property('relation', 'part to this sample'); done(); }); }) }); }); it('stores the custom fields', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 200, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [], custom_fields: {field1: 'a', field2: 'b', 'not allowed for new applications': true}}} }).end((err, res) => { if (err) return done (err); NoteModel.findById(res.body.note_id).lean().exec((err, data: any) => { if (err) return done(err); should(data).have.property('_id'); should(data).have.property('comment', 'Testcomment'); should(data).have.property('sample_references').have.lengthOf(0); should(data).have.property('custom_fields'); should(data.custom_fields).have.property('field1', 'a'); should(data.custom_fields).have.property('field2', 'b'); should(data.custom_fields).have.property('not allowed for new applications', true); NoteFieldModel.find({name: 'field1'}).lean().exec((err, data) => { if (err) return done(err); should(data).have.lengthOf(1); should(data[0]).have.property('qty', 1); NoteFieldModel.find({name: 'field2'}).lean().exec((err, data) => { if (err) return done(err); should(data).have.lengthOf(1); should(data[0]).have.property('qty', 1); NoteFieldModel.find({name: 'not allowed for new applications'}).lean().exec((err, data) => { if (err) return done(err); should(data).have.lengthOf(1); should(data[0]).have.property('qty', 3); done(); }); }); }); }); }); }); it('rejects a color not defined for the material', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'green', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Color not available for material'} }); }); it('rejects an unknown material id', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '000000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Material not available'} }); }); it('rejects a sample number in use', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: '1', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Sample number already taken'} }); }); it('rejects an invalid sample reference', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '000000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Sample reference not available'} }); }); it('rejects a missing color', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Invalid body format'} }); }); it('rejects a missing sample number', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Invalid body format'} }); }); it('rejects a missing type', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'black', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Invalid body format'} }); }); it('rejects a missing batch', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'black', type: 'granulate', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Invalid body format'} }); }); it('rejects a missing material id', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Invalid body format'} }); }); it('rejects an invalid material id', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'janedoe'}, httpStatus: 400, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '10000000000h000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}}, res: {status: 'Invalid body format'} }); }); it('rejects an API key', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {key: 'janedoe'}, httpStatus: 401, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}} }); }); it('rejects requests from a read user', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', auth: {basic: 'user'}, httpStatus: 403, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}} }); }); it('rejects unauthorized requests', done => { TestHelper.request(server, done, { method: 'post', url: '/sample/new', httpStatus: 401, req: {number: 'Rng172', color: 'black', type: 'granulate', batch: '1560237365', material_id: '100000000000000000000001', notes: {comment: 'Testcomment', sample_references: [{id: '400000000000000000000003', relation: 'part to this sample'}]}} }); }); }); describe('GET /sample/notes/fields', () => { it('returns all fields', done => { TestHelper.request(server, done, { method: 'get', url: '/sample/notes/fields', auth: {basic: 'user'}, httpStatus: 200 }).end((err, res) => { if (err) return done(err); const json = require('../test/db.json'); should(res.body).have.lengthOf(json.collections.note_fields.length); should(res.body).matchEach(material => { should(material).have.only.keys('name', 'qty'); should(material).have.property('qty').be.type('number'); }); done(); }); }); it('works with an API key', done => { TestHelper.request(server, done, { method: 'get', url: '/sample/notes/fields', auth: {key: 'user'}, httpStatus: 200 }).end((err, res) => { if (err) return done(err); const json = require('../test/db.json'); should(res.body).have.lengthOf(json.collections.note_fields.length); should(res.body).matchEach(material => { should(material).have.only.keys('name', 'qty'); should(material).have.property('qty').be.type('number'); }); done(); }); }); it('rejects unauthorized requests', done => { TestHelper.request(server, done, { method: 'get', url: '/sample/notes/fields', httpStatus: 401 }); }); }); });