diff --git a/api/sample.yaml b/api/sample.yaml index 9f52f77..67f25ac 100644 --- a/api/sample.yaml +++ b/api/sample.yaml @@ -119,6 +119,29 @@ 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: post: summary: add sample diff --git a/src/routes/sample.spec.ts b/src/routes/sample.spec.ts index 1bbfd5d..b90a722 100644 --- a/src/routes/sample.spec.ts +++ b/src/routes/sample.spec.ts @@ -14,7 +14,6 @@ import mongoose from 'mongoose'; // TODO: allow adding sample numbers for existing samples // TODO: Do not allow validation or measurement entry without condition -// TODO: restore sample describe('/sample', () => { 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', () => { it('returns the right sample', done => { TestHelper.request(server, done, { diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 23e786a..e8ed1f7 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -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) => { if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;