Archived
2

restore samples

This commit is contained in:
VLE2FE 2020-05-28 14:41:35 +02:00
parent 1c2631c6fb
commit 0fd0cc8da4
3 changed files with 91 additions and 1 deletions

View File

@ -119,6 +119,29 @@
500: 500:
$ref: 'api.yaml#/components/responses/500' $ref: 'api.yaml#/components/responses/500'
/sample/restore/{id}:
parameters:
- $ref: 'api.yaml#/components/parameters/Id'
put:
summary: restore sample
description: 'Auth: basic, levels: maintain, admin'
x-doc: status is set to 0
tags:
- /sample
security:
- BasicAuth: []
responses:
200:
$ref: 'api.yaml#/components/responses/Ok'
401:
$ref: 'api.yaml#/components/responses/401'
403:
$ref: 'api.yaml#/components/responses/403'
404:
$ref: 'api.yaml#/components/responses/404'
500:
$ref: 'api.yaml#/components/responses/500'
/sample/new: /sample/new:
post: post:
summary: add sample summary: add sample

View File

@ -14,7 +14,6 @@ import mongoose from 'mongoose';
// TODO: allow adding sample numbers for existing samples // TODO: allow adding sample numbers for existing samples
// TODO: Do not allow validation or measurement entry without condition // TODO: Do not allow validation or measurement entry without condition
// TODO: restore sample
describe('/sample', () => { describe('/sample', () => {
let server; let server;
@ -821,6 +820,61 @@ describe('/sample', () => {
}); });
}); });
describe('PUT /sample/restore/{id}', () => {
it('sets the status', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/sample/restore/400000000000000000000005',
auth: {basic: 'admin'},
httpStatus: 200,
req: {}
}).end((err, res) => {
if (err) return done (err);
should(res.body).be.eql({status: 'OK'});
SampleModel.findById('400000000000000000000005').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: '/sample/restore/400000000000000000000005',
auth: {key: 'admin'},
httpStatus: 401,
req: {}
});
});
it('rejects a write user', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/sample/restore/400000000000000000000005',
auth: {basic: 'janedoe'},
httpStatus: 403,
req: {}
});
});
it('returns 404 for an unknown sample', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/sample/restore/000000000000000000000005',
auth: {basic: 'admin'},
httpStatus: 404,
req: {}
});
});
it('rejects unauthorized requests', done => {
TestHelper.request(server, done, {
method: 'put',
url: '/sample/restore/400000000000000000000005',
httpStatus: 401,
req: {}
});
});
});
describe('POST /sample/new', () => { describe('POST /sample/new', () => {
it('returns the right sample', done => { it('returns the right sample', done => {
TestHelper.request(server, done, { TestHelper.request(server, done, {

View File

@ -163,6 +163,19 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
}); });
}); });
router.put('/sample/restore/' + IdValidate.parameter(), (req, res, next) => {
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
SampleModel.findByIdAndUpdate(req.params.id, {status: globals.status.new}).lean().exec((err, data) => {
if (err) return next(err);
if (!data) {
return res.status(404).json({status: 'Not found'});
}
res.json({status: 'OK'});
});
});
router.post('/sample/new', async (req, res, next) => { router.post('/sample/new', async (req, res, next) => {
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return; if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;