req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}}
it('rejects a color not defined for the material',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/400000000000000000000001',
auth:{basic:'janedoe'},
httpStatus: 400,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',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:'put',
url:'/sample/400000000000000000000001',
auth:{basic:'janedoe'},
httpStatus: 400,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'000000000000000000000002',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:'put',
url:'/sample/400000000000000000000001',
auth:{basic:'janedoe'},
httpStatus: 400,
req:{number:'21',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',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:'put',
url:'/sample/400000000000000000000001',
auth:{basic:'janedoe'},
httpStatus: 400,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'000000000000000000000003',relation:'part to this sample'}]}},
res:{status:'Sample reference not available'}
});
});
it('rejects an invalid material id',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/400000000000000000000001',
auth:{basic:'janedoe'},
httpStatus: 400,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'10000000000h000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
res:{status:'Invalid body format',details:'"material_id" with value "10000000000h000000000001" fails to match the required pattern: /[0-9a-f]{24}/'}
});
});
it('rejects an invalid id',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/10000000000h000000000001',
auth:{basic:'janedoe'},
httpStatus: 404,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
});
});
it('rejects an API key',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/400000000000000000000001',
auth:{key:'janedoe'},
httpStatus: 401,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
});
});
it('rejects changes for samples from another user for a write user',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/400000000000000000000003',
auth:{basic:'janedoe'},
httpStatus: 403,
req:{}
});
});
it('accepts changes for samples from another user for a maintain/admin user',done=>{
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
});
});
it('returns 404 for an unknown sample',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/000000000000000000000001',
auth:{basic:'janedoe'},
httpStatus: 404,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}}
});
})
it('rejects unauthorized requests',done=>{
TestHelper.request(server,done,{
method:'put',
url:'/sample/400000000000000000000001',
httpStatus: 401,
req:{number:'10',type:'part',color:'signalviolet',batch:'114531',material_id:'100000000000000000000002',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
req:{number:'Rng172',color:'black',type:'granulate',batch:'1560237365',material_id:'100000000000000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}}
req:{number:'Rng172',color:'black',type:'granulate',batch:'1560237365',material_id:'100000000000000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}}
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}}}
NoteFieldModel.find({name:'not allowed for new applications'}).lean().exec((err,data)=>{
if(err)returndone(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'}]}},
req:{color:'black',type:'granulate',batch:'1560237365',material_id:'100000000000000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
req:{number:'Rng172',color:'black',batch:'1560237365',material_id:'100000000000000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
req:{number:'Rng172',color:'black',type:'granulate',material_id:'100000000000000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
req:{number:'Rng172',color:'black',type:'granulate',batch:'1560237365',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
req:{number:'Rng172',color:'black',type:'granulate',batch:'1560237365',material_id:'10000000000h000000000001',notes:{comment:'Testcomment',sample_references:[{id:'400000000000000000000003',relation:'part to this sample'}]}},
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'}]}}