2020-04-29 12:10:27 +02:00
import should from 'should/as-function' ;
2020-05-15 14:55:01 +02:00
import _ from 'lodash' ;
2020-04-29 12:10:27 +02:00
import MaterialModel from '../models/material' ;
2020-05-08 09:58:12 +02:00
import TestHelper from "../test/helper" ;
2020-05-27 14:31:17 +02:00
import globals from '../globals' ;
2020-05-13 17:28:18 +02:00
2020-05-26 09:07:01 +02:00
// TODO: separate supplier/ material name into own collections
2020-04-29 12:10:27 +02:00
describe ( '/material' , ( ) = > {
let server ;
before ( done = > TestHelper . before ( done ) ) ;
beforeEach ( done = > server = TestHelper . beforeEach ( server , done ) ) ;
afterEach ( done = > TestHelper . afterEach ( server , done ) ) ;
2020-05-28 11:47:51 +02:00
after ( done = > TestHelper . after ( done ) ) ;
2020-04-29 12:10:27 +02:00
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' ) ;
2020-05-27 14:31:17 +02:00
should ( res . body ) . have . lengthOf ( json . collections . materials . filter ( e = > e . status === globals . status . validated ) . length ) ;
2020-04-29 12:10:27 +02:00
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' ) ;
2020-05-15 14:55:01 +02:00
should ( number ) . have . property ( 'number' ) . be . type ( 'string' ) ;
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
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' ) ;
2020-05-27 14:31:17 +02:00
should ( res . body ) . have . lengthOf ( json . collections . materials . filter ( e = > e . status === globals . status . validated ) . length ) ;
2020-04-29 12:10:27 +02:00
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' ) ;
2020-05-15 14:55:01 +02:00
should ( number ) . have . property ( 'number' ) . be . type ( 'string' ) ;
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
done ( ) ;
} ) ;
} ) ;
it ( 'rejects unauthorized requests' , done = > {
TestHelper . request ( server , done , {
method : 'get' ,
url : '/materials' ,
httpStatus : 401
} ) ;
} ) ;
} ) ;
2020-05-28 17:05:23 +02:00
describe ( 'GET /materials/{state}' , ( ) = > {
2020-05-18 10:43:26 +02:00
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 ;
2020-05-27 14:31:17 +02:00
should ( res . body ) . have . lengthOf ( json . collections . materials . filter ( e = > e . status === globals . status . new ) . length ) ;
2020-05-18 10:43:26 +02:00
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 ) = > {
2020-05-27 14:31:17 +02:00
should ( data ) . have . property ( 'status' , globals . status . new ) ;
2020-05-18 10:43:26 +02:00
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 ;
2020-05-27 14:31:17 +02:00
should ( res . body ) . have . lengthOf ( json . collections . materials . filter ( e = > e . status === globals . status . deleted ) . length ) ;
2020-05-18 10:43:26 +02:00
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 ) = > {
2020-05-27 14:31:17 +02:00
should ( data ) . have . property ( 'status' , globals . status . deleted ) ;
2020-05-18 10:43:26 +02:00
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
} ) ;
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
describe ( 'GET /material/{id}' , ( ) = > {
it ( 'returns the right material' , done = > {
TestHelper . request ( server , done , {
method : 'get' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 200 ,
2020-05-15 14:55:01 +02:00
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' } , { color : 'natural' , number : '5514263422' } ] }
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
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 : [ ] }
} ) ;
} ) ;
2020-05-15 14:55:01 +02:00
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' , mineral : 0 , glass_fiber : 0 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '' } ] }
} ) ;
} ) ;
2020-05-28 14:11:19 +02:00
it ( 'returns a deleted material for a maintain/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' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'blue' , number : '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
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
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 : { } ,
2020-05-15 14:55:01 +02:00
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' } , { color : 'natural' , number : '5514263422' } ] }
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
2020-05-04 15:48:07 +02:00
it ( 'keeps unchanged properties' , done = > {
2020-04-29 16:09:31 +02:00
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 200 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Stanyl TW 200 F8' , supplier : 'DSM' , group : 'PA46' , mineral : 0 , glass_fiber : 40 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5514263423' } , { color : 'natural' , number : '5514263422' } ] }
2020-05-13 14:18:15 +02:00
} ) . end ( ( err , res ) = > {
if ( err ) return done ( err ) ;
2020-05-15 14:55:01 +02:00
should ( res . body ) . be . eql ( { _id : '100000000000000000000001' , name : 'Stanyl TW 200 F8' , supplier : 'DSM' , group : 'PA46' , mineral : 0 , glass_fiber : 40 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5514263423' } , { color : 'natural' , number : '5514263422' } ] } ) ;
2020-05-13 14:18:15 +02:00
MaterialModel . findById ( '100000000000000000000001' ) . lean ( ) . exec ( ( err , data ) = > {
if ( err ) return done ( err ) ;
2020-05-27 14:31:17 +02:00
should ( data ) . have . property ( 'status' , globals . status . validated ) ;
2020-05-13 14:18:15 +02:00
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 ) ;
2020-05-15 14:55:01 +02:00
should ( res . body ) . be . eql ( { _id : '100000000000000000000001' , name : 'Stanyl TW 200 F8' , supplier : 'DSM' , group : 'PA46' , mineral : 0 , glass_fiber : 40 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5514263423' } , { color : 'natural' , number : '5514263422' } ] } ) ;
2020-05-13 14:18:15 +02:00
MaterialModel . findById ( '100000000000000000000001' ) . lean ( ) . exec ( ( err , data ) = > {
if ( err ) return done ( err ) ;
2020-05-27 14:31:17 +02:00
should ( data ) . have . property ( 'status' , globals . status . validated ) ;
2020-05-13 14:18:15 +02:00
done ( ) ;
} ) ;
2020-04-29 16:09:31 +02:00
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
it ( 'changes the given properties' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 200 ,
2020-05-15 14:55:01 +02:00
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' } ] }
2020-04-29 12:10:27 +02:00
,
2020-05-04 15:48:07 +02:00
} ) . end ( ( err , res ) = > {
2020-04-29 12:10:27 +02:00
if ( err ) return done ( err ) ;
2020-05-15 14:55:01 +02:00
should ( res . body ) . be . eql ( { _id : '100000000000000000000001' , 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' } ] } ) ;
2020-04-29 16:09:31 +02:00
MaterialModel . findById ( '100000000000000000000001' ) . lean ( ) . exec ( ( err , data :any ) = > {
2020-04-29 12:10:27 +02:00
if ( err ) return done ( err ) ;
2020-05-11 13:05:54 +02:00
data . _id = data . _id . toString ( ) ;
2020-04-29 16:09:31 +02:00
data . numbers = data . numbers . map ( e = > { return { color : e.color , number : e . number } } ) ;
2020-05-15 14:55:01 +02:00
should ( data ) . be . eql ( { _id : '100000000000000000000001' , 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' } ] , status : 0 , __v : 0 } ) ;
2020-04-29 12:10:27 +02:00
done ( ) ;
} ) ;
} ) ;
} ) ;
2020-05-15 14:55:01 +02:00
it ( 'accepts a color without number' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000007' ,
auth : { basic : 'janedoe' } ,
httpStatus : 200 ,
req : { numbers : [ { color : 'black' , number : '' } , { color : 'natural' , number : '' } ] } ,
res : { _id : '100000000000000000000007' , name : 'Ultramid A4H' , supplier : 'BASF' , group : 'PA66' , mineral : 0 , glass_fiber : 0 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '' } , { color : 'natural' , number : '' } ] }
} ) ;
} )
2020-04-29 12:10:27 +02:00
it ( 'rejects already existing material names' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-04-29 16:09:31 +02:00
req : { name : 'Ultramid T KR 4355 G7' } ,
2020-04-29 12:10:27 +02:00
res : { status : 'Material name already taken' }
} ) ;
} ) ;
2020-05-07 21:55:29 +02:00
it ( 'rejects a wrong mineral property' , done = > {
2020-04-29 12:10:27 +02:00
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-05-07 21:55:29 +02:00
req : { mineral : 'x' } ,
res : { status : 'Invalid body format' , details : '"mineral" must be a number' }
} ) ;
} ) ;
it ( 'rejects a wrong glass_fiber property' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
req : { glass_fiber : 'x' } ,
res : { status : 'Invalid body format' , details : '"glass_fiber" must be a number' }
} ) ;
} ) ;
it ( 'rejects a wrong carbon_fiber property' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
req : { carbon_fiber : 'x' } ,
res : { status : 'Invalid body format' , details : '"carbon_fiber" must be a number' }
} ) ;
} ) ;
it ( 'rejects a wrong color name property' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000001' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-05-15 14:55:01 +02:00
req : { numbers : [ { colorxx : 'black' , number : '55' } ] } ,
2020-05-07 21:55:29 +02:00
res : { status : 'Invalid body format' , details : '"numbers[0].color" is required' }
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
it ( 'rejects an invalid id' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/10000000000000000000000x' ,
auth : { basic : 'admin' } ,
2020-04-29 16:09:31 +02:00
httpStatus : 404 ,
2020-04-29 12:10:27 +02:00
req : { } ,
} ) ;
} ) ;
2020-05-28 14:11:19 +02:00
it ( 'rejects editing a deleted material' , done = > {
TestHelper . request ( server , done , {
method : 'put' ,
url : '/material/100000000000000000000008' ,
auth : { basic : 'janedoe' } ,
httpStatus : 403 ,
req : { }
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
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}' , ( ) = > {
2020-05-13 14:18:15 +02:00
it ( 'sets the status to deleted' , done = > {
2020-04-29 12:10:27 +02:00
TestHelper . request ( server , done , {
method : 'delete' ,
2020-05-08 09:58:12 +02:00
url : '/material/100000000000000000000002' ,
2020-04-29 12:10:27 +02:00
auth : { basic : 'janedoe' } ,
httpStatus : 200
} ) . end ( ( err , res ) = > {
if ( err ) return done ( err ) ;
should ( res . body ) . be . eql ( { status : 'OK' } ) ;
2020-05-13 14:18:15 +02:00
MaterialModel . findById ( '100000000000000000000002' ) . lean ( ) . exec ( ( err , data : any ) = > {
2020-04-29 12:10:27 +02:00
if ( err ) return done ( err ) ;
2020-05-13 14:18:15 +02:00
data . _id = data . _id . toString ( ) ;
data . numbers = data . numbers . map ( e = > { return { color : e.color , number : e . number } } ) ;
2020-05-15 14:55:01 +02:00
should ( data ) . be . eql ( { _id : '100000000000000000000002' , name : 'Ultramid T KR 4355 G7' , supplier : 'BASF' , group : 'PA6/6T' , mineral : 0 , glass_fiber : 35 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5514212901' } , { color : 'signalviolet' , number : '5514612901' } ] , status : - 1 , __v : 0 }
2020-05-13 14:18:15 +02:00
) ;
2020-04-29 12:10:27 +02:00
done ( ) ;
} ) ;
} ) ;
} ) ;
2020-05-08 09:58:12 +02:00
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' }
} )
} ) ;
2020-04-29 12:10:27 +02:00
it ( 'rejects an invalid id' , done = > {
TestHelper . request ( server , done , {
method : 'delete' ,
url : '/material/10000000000000000000000x' ,
auth : { basic : 'admin' } ,
2020-04-29 16:09:31 +02:00
httpStatus : 404
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
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
} ) ;
} ) ;
} ) ;
2020-05-28 14:54:52 +02:00
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' , globals . status . new ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
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 : { }
} ) ;
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
describe ( 'POST /material/new' , ( ) = > {
it ( 'returns the right material' , done = > {
TestHelper . request ( server , done , {
method : 'post' ,
url : '/material/new' ,
auth : { basic : 'janedoe' } ,
httpStatus : 200 ,
2020-05-15 14:56:42 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , group : 'PBT' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '05515798402' } ] }
2020-04-29 12:10:27 +02:00
} ) . end ( ( err , res ) = > {
if ( err ) return done ( err ) ;
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' ) ;
2020-05-15 14:56:42 +02:00
should ( number ) . have . property ( 'number' , '05515798402' ) ;
2020-04-29 12:10:27 +02:00
} ) ;
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 ) ;
2020-05-06 14:39:04 +02:00
should ( data ) . have . lengthOf ( 1 ) ;
2020-05-13 14:18:15 +02:00
should ( data [ 0 ] ) . have . only . keys ( '_id' , 'name' , 'supplier' , 'group' , 'mineral' , 'glass_fiber' , 'carbon_fiber' , 'numbers' , 'status' , '__v' ) ;
2020-04-29 12:10:27 +02:00
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' ) ;
2020-05-27 14:31:17 +02:00
should ( data [ 0 ] ) . have . property ( 'status' , globals . status . new ) ;
2020-04-29 12:10:27 +02:00
should ( data [ 0 ] . numbers ) . have . lengthOf ( 0 ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
2020-05-15 14:55:01 +02:00
it ( 'accepts a color without number' , 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 : '' } ] }
} ) . end ( ( err , res ) = > {
if ( err ) return done ( err ) ;
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' , '' ) ;
} ) ;
MaterialModel . find ( { name : 'Crastin CE 2510' } ) . lean ( ) . exec ( ( err , data : any ) = > {
if ( err ) return done ( err ) ;
should ( data ) . have . lengthOf ( 1 ) ;
should ( data [ 0 ] ) . have . only . keys ( '_id' , 'name' , 'supplier' , 'group' , 'mineral' , 'glass_fiber' , 'carbon_fiber' , 'numbers' , 'status' , '__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' ) ;
2020-05-27 14:31:17 +02:00
should ( data [ 0 ] ) . have . property ( 'status' , globals . status . new ) ;
2020-05-15 14:55:01 +02:00
should ( _ . omit ( data [ 0 ] . numbers [ 0 ] , '_id' ) ) . be . eql ( { color : 'black' , number : '' } ) ;
done ( ) ;
} ) ;
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
it ( 'rejects already existing material names' , done = > {
TestHelper . request ( server , done , {
method : 'post' ,
url : '/material/new' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Stanyl TW 200 F8' , supplier : 'DSM' , group : 'PA46' , mineral : 0 , glass_fiber : 40 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5514263423' } ] } ,
2020-04-29 12:10:27 +02:00
res : { status : 'Material name already taken' }
} ) ;
} ) ;
2020-05-07 21:55:29 +02:00
it ( 'rejects a missing name' , done = > {
TestHelper . request ( server , done , {
method : 'post' ,
url : '/material/new' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-05-15 14:55:01 +02:00
req : { supplier : 'Du Pont' , group : 'PBT' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
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 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Crastin CE 2510' , group : 'PBT' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
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 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
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 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , group : 'PBT' , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
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 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , group : 'PBT' , mineral : 0 , carbon_fiber : 0 , numbers : [ { color : 'black' , number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
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 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , group : 'PBT' , mineral : 0 , glass_fiber : 30 , numbers : [ { color : 'black' , number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
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' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 } ,
res : { status : 'Invalid body format' , details : '"numbers" is required' }
} ) ;
} ) ;
it ( 'rejects a missing color name' , done = > {
2020-04-29 12:10:27 +02:00
TestHelper . request ( server , done , {
method : 'post' ,
url : '/material/new' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-05-15 14:55:01 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , group : 'PBT' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { number : '5515798402' } ] } ,
2020-05-07 21:55:29 +02:00
res : { status : 'Invalid body format' , details : '"numbers[0].color" is required' }
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
2020-05-07 21:55:29 +02:00
it ( 'rejects a missing color number' , done = > {
2020-04-29 12:10:27 +02:00
TestHelper . request ( server , done , {
method : 'post' ,
url : '/material/new' ,
auth : { basic : 'janedoe' } ,
httpStatus : 400 ,
2020-05-07 21:55:29 +02:00
req : { name : 'Crastin CE 2510' , supplier : 'Du Pont' , group : 'PBT' , mineral : 0 , glass_fiber : 30 , carbon_fiber : 0 , numbers : [ { color : 'black' } ] } ,
res : { status : 'Invalid body format' , details : '"numbers[0].number" is required' }
2020-04-29 12:10:27 +02:00
} ) ;
} ) ;
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 : [ ] }
} ) ;
} ) ;
} ) ;
2020-05-28 17:05:23 +02:00
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
} ) ;
} ) ;
} ) ;
2020-04-29 12:10:27 +02:00
} ) ;