From 52039317e0166f2799c7cd5ddf065d35eddfec6d Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Thu, 28 May 2020 14:54:52 +0200 Subject: [PATCH] restore materials --- api/material.yaml | 23 +++++++++++++++ src/routes/material.spec.ts | 57 +++++++++++++++++++++++++++++++++++-- src/routes/material.ts | 13 +++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/api/material.yaml b/api/material.yaml index 51af0ef..967071c 100644 --- a/api/material.yaml +++ b/api/material.yaml @@ -117,6 +117,29 @@ 500: $ref: 'api.yaml#/components/responses/500' +/material/restore/{id}: + parameters: + - $ref: 'api.yaml#/components/parameters/Id' + put: + summary: restore material + description: 'Auth: basic, levels: maintain, admin' + x-doc: status is set to 0 + tags: + - /material + 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' + /material/new: post: summary: add material diff --git a/src/routes/material.spec.ts b/src/routes/material.spec.ts index 330d5b7..56f094e 100644 --- a/src/routes/material.spec.ts +++ b/src/routes/material.spec.ts @@ -7,8 +7,6 @@ import globals from '../globals'; // TODO: color name must be unique to get color number // TODO: separate supplier/ material name into own collections -// TODO: restore material - describe('/material', () => { let server; before(done => TestHelper.before(done)); @@ -496,6 +494,61 @@ describe('/material', () => { }); }); + 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: {} + }); + }); + }); + describe('POST /material/new', () => { it('returns the right material', done => { TestHelper.request(server, done, { diff --git a/src/routes/material.ts b/src/routes/material.ts index ffba3ef..1711eb5 100644 --- a/src/routes/material.ts +++ b/src/routes/material.ts @@ -95,6 +95,19 @@ router.delete('/material/' + IdValidate.parameter(), (req, res, next) => { }); }); +router.put('/material/restore/' + IdValidate.parameter(), (req, res, next) => { + if (!req.auth(res, ['maintain', 'admin'], 'basic')) return; + + MaterialModel.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('/material/new', async (req, res, next) => { if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;