refactored user.ts
This commit is contained in:
@ -4,7 +4,6 @@ import NoteModel from '../models/note';
|
||||
import NoteFieldModel from '../models/note_field';
|
||||
import TestHelper from "../test/helper";
|
||||
|
||||
// TODO: think again which parameters are required at POST
|
||||
|
||||
describe('/sample', () => {
|
||||
let server;
|
||||
@ -23,16 +22,16 @@ describe('/sample', () => {
|
||||
if (err) return done(err);
|
||||
const json = require('../test/db.json');
|
||||
should(res.body).have.lengthOf(json.collections.samples.filter(e => e.status === 10).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');
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample).have.only.keys('_id', 'number', 'type', 'color', 'batch', 'material_id', 'note_id', 'user_id');
|
||||
should(sample).have.property('_id').be.type('string');
|
||||
should(sample).have.property('number').be.type('string');
|
||||
should(sample).have.property('type').be.type('string');
|
||||
should(sample).have.property('color').be.type('string');
|
||||
should(sample).have.property('batch').be.type('string');
|
||||
should(sample).have.property('material_id').be.type('string');
|
||||
should(sample).have.property('note_id');
|
||||
should(sample).have.property('user_id').be.type('string');
|
||||
});
|
||||
done();
|
||||
});
|
||||
@ -70,6 +69,94 @@ describe('/sample', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /samples/{group}', () => {
|
||||
it('returns all new samples', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples/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.samples.filter(e => e.status === 0).length);
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample).have.only.keys('_id', 'number', 'type', 'color', 'batch', 'material_id', 'note_id', 'user_id');
|
||||
should(sample).have.property('_id').be.type('string');
|
||||
should(sample).have.property('number').be.type('string');
|
||||
should(sample).have.property('type').be.type('string');
|
||||
should(sample).have.property('color').be.type('string');
|
||||
should(sample).have.property('batch').be.type('string');
|
||||
should(sample).have.property('material_id').be.type('string');
|
||||
should(sample).have.property('note_id');
|
||||
should(sample).have.property('user_id').be.type('string');
|
||||
SampleModel.findById(sample._id).lean().exec((err, data) => {
|
||||
should(data).have.property('status', 0);
|
||||
if (--asyncCounter === 0) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns all deleted samples', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples/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.samples.filter(e => e.status === -1).length);
|
||||
should(res.body).matchEach(sample => {
|
||||
should(sample).have.only.keys('_id', 'number', 'type', 'color', 'batch', 'material_id', 'note_id', 'user_id');
|
||||
should(sample).have.property('_id').be.type('string');
|
||||
should(sample).have.property('number').be.type('string');
|
||||
should(sample).have.property('type').be.type('string');
|
||||
should(sample).have.property('color').be.type('string');
|
||||
should(sample).have.property('batch').be.type('string');
|
||||
should(sample).have.property('material_id').be.type('string');
|
||||
should(sample).have.property('note_id');
|
||||
should(sample).have.property('user_id').be.type('string');
|
||||
SampleModel.findById(sample._id).lean().exec((err, data) => {
|
||||
should(data).have.property('status', -1);
|
||||
if (--asyncCounter === 0) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('rejects requests from a write user', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples/new',
|
||||
auth: {basic: 'janedoe'},
|
||||
httpStatus: 403
|
||||
});
|
||||
});
|
||||
it('rejects an API key', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples/new',
|
||||
auth: {key: 'admin'},
|
||||
httpStatus: 401
|
||||
});
|
||||
});
|
||||
it('rejects unauthorized requests', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/samples/new',
|
||||
httpStatus: 401
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('PUT /sample/{id}', () => {
|
||||
it('returns the right sample', done => {
|
||||
TestHelper.request(server, done, {
|
||||
@ -194,12 +281,10 @@ describe('/sample', () => {
|
||||
}).end(err => {
|
||||
if (err) return done(err);
|
||||
NoteFieldModel.findOne({name: 'not allowed for new applications'}).lean().exec((err, data) => {
|
||||
console.log(data);
|
||||
if (err) return done(err);
|
||||
should(data).have.property('qty', 1);
|
||||
NoteFieldModel.findOne({name: 'field1'}).lean().exec((err, data) => {
|
||||
if (err) return done(err);
|
||||
console.log(data);
|
||||
should(data).have.property('qty', 1);
|
||||
done();
|
||||
});
|
||||
@ -233,7 +318,6 @@ describe('/sample', () => {
|
||||
if (err) return done (err);
|
||||
NoteModel.findById(res.body.note_id).lean().exec((err, data) => {
|
||||
if (err) return done (err);
|
||||
console.log(data);
|
||||
should(data).not.be.null();
|
||||
should(data).have.property('comment', 'Stoff gesperrt');
|
||||
should(data).have.property('sample_references').have.lengthOf(0);
|
||||
@ -448,7 +532,6 @@ describe('/sample', () => {
|
||||
setTimeout(() => { // background action takes some time before we can check
|
||||
NoteModel.findById('500000000000000000000003').lean().exec((err, data: any) => {
|
||||
if (err) return done(err);
|
||||
console.log(data);
|
||||
should(data).have.property('sample_references').with.lengthOf(1);
|
||||
should(data.sample_references[0].id.toString()).be.eql('400000000000000000000003');
|
||||
should(data.sample_references[0]).have.property('relation', 'part to sample');
|
||||
|
Reference in New Issue
Block a user