diff --git a/app/controllers/resources.js b/app/controllers/resources.js index b79d9450..4b92e723 100644 --- a/app/controllers/resources.js +++ b/app/controllers/resources.js @@ -64,8 +64,8 @@ class ResourcesController extends DefaultController { next(); } - static alloc(req, res) { - req.Resource.alloc((error) => { + static allocate(req, res) { + req.Resource.allocate((error) => { if (error) { res.status(500).json({error: error}); } else { diff --git a/app/routes/resources.js b/app/routes/resources.js index 5022a7bc..d20a36df 100644 --- a/app/routes/resources.js +++ b/app/routes/resources.js @@ -13,15 +13,8 @@ function Route(app) { .get(controller.find.bind(controller)) .post(controller.create.bind(controller)); - router.route('/api/v0/resources/allocation') - // .get( controller.list_allocs ) - .put(ResourceController.allocMultiple); - - // router.route('/api/v0/resources/allocation/:Alloc') - // .get( controller.list_alloc_resources ); - - router.route('/api/v0/resources/allocation/:Alloc/release') - .put(ResourceController.releaseMultiple); + router.route('/api/v0/resources/:Resource/allocate') + .put(ResourceController.allocate); router.route('/api/v0/resources/:Resource.:format?') .all(controller.all.bind(controller)) @@ -29,9 +22,6 @@ function Route(app) { .put(controller.update.bind(controller)) .delete(controller.remove.bind(controller)); - router.route('/api/v0/resources/:Resource/alloc') - .put(ResourceController.alloc); - router.route('/api/v0/resources/:Resource/release') .put(ResourceController.release); @@ -41,6 +31,17 @@ function Route(app) { router.route('/api/v0/resources/:Resource/route') .get(ResourceController.solveRoute); + // allocations api + router.route('/api/v0/allocations') + // .get( controller.list_allocs ) + .put(ResourceController.allocMultiple); + + // router.route('/api/v0/allocations/:Alloc') + // .get( controller.list_alloc_resources ); + + router.route('/api/v0/allocations/:Alloc/release') + .put(ResourceController.releaseMultiple); + app.use(router); } diff --git a/app/routes/users.js b/app/routes/users.js index a173ebf5..3c5265b2 100644 --- a/app/routes/users.js +++ b/app/routes/users.js @@ -37,7 +37,15 @@ function Route(app) { User.isEmpty() .then((empty) => { if (empty) { return createDefaultAdmin(); } - return Promise.resolve(); + return User + .find({name: 'admin'}).select('+password') + .then(([user]) => { + if (!user.password) { + logger.debug('Admin doesnt have password, set default one: "admin"'); + user.password = 'admin'; + return user.save(); + } + }); }) .catch(error => logger.warn(error)); diff --git a/package-lock.json b/package-lock.json index d18f4dd0..fa0e5d3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11778,8 +11778,7 @@ "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "requires": {} + "dev": true }, "agent-base": { "version": "6.0.2", @@ -13318,8 +13317,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/eslint-plugin-chai-expect/-/eslint-plugin-chai-expect-3.0.0.tgz", "integrity": "sha512-NS0YBcToJl+BRKBSMCwRs/oHJIX67fG5Gvb4tGked+9Wnd1/PzKijd82B2QVKcSSOwRe+pp4RAJ2AULeck4eQw==", - "dev": true, - "requires": {} + "dev": true }, "eslint-plugin-es": { "version": "3.0.1", @@ -14576,15 +14574,13 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/grunt-exec/-/grunt-exec-3.0.0.tgz", "integrity": "sha1-iB+GjWQJh4j92vIvol2FcqnWTcc=", - "dev": true, - "requires": {} + "dev": true }, "grunt-express-server": { "version": "0.5.4", "resolved": "https://registry.npmjs.org/grunt-express-server/-/grunt-express-server-0.5.4.tgz", "integrity": "sha512-Q9sTDOwxC46uviL1/LSXFflTMv+/WnwANtzxD7hwnEXaej79LS1AUZlWc/O5P+CWjKG69/1xJFn+CVP94fOQ6Q==", - "dev": true, - "requires": {} + "dev": true }, "grunt-known-options": { "version": "2.0.0", @@ -16583,8 +16579,7 @@ "mongoose-legacy-pluralize": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", - "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==", - "requires": {} + "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" }, "mongoose-query": { "version": "0.7.4", @@ -19621,8 +19616,7 @@ "ws": { "version": "8.2.3", "resolved": "https://registry.npmjs.org/ws/-/ws-8.2.3.tgz", - "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==", - "requires": {} + "integrity": "sha512-wBuoj1BDpC6ZQ1B7DWQBYVLphPWkm8i9Y0/3YdHjHKHiohOJ1ws+3OccDWtH+PoC9DZD5WOTrJvNbWvjS6JWaA==" }, "xml2js": { "version": "0.2.8", diff --git a/test/tests_api/resource.js b/test/tests_api/resource.js index 54ed876a..e3182ec1 100644 --- a/test/tests_api/resource.js +++ b/test/tests_api/resource.js @@ -27,6 +27,11 @@ const getResource = resourceId => superagent.get(`${api}/resources/${resourceId} return res.body; }); +function createResource({sn}) { + const body = {name: 'dev1', type: 'dut', hw: {sn}}; + return superagent.post(`${api}/resources`).send(body); +} + describe('Resource', function () { let resourceId; @@ -34,33 +39,27 @@ describe('Resource', function () { before(function () { authString = getTestUserToken(); }); + beforeEach(async function () { + const {body} = await createResource({sn: 'SerialNumber'}); + resourceId = body._id; + }); + afterEach(async function () { + await superagent.del(`${api}/resources/${resourceId}`); + resourceId = null; + }); - it('add resource', function () { - const body = { - name: 'dev1', - type: 'dut', - hw: { - sn: 'SerialNumber' - } - }; - return superagent.post(`${api}/resources`) - .send(body) - .end(function (error, res) { - expect(error).to.equal(null); - expect(res).to.be.a('Object'); - if (res.status === 300) { - logger.warn('Seems that your DB is not clean!'); - process.exit(1); - } - expect(res).to.have.property('status', 200); - expect(res.body).to.have.property('name'); - expect(res.body).to.have.property('type'); - expect(res.body).to.have.property('_id'); - expect(res.body._id).to.be.an('string'); - expect(res.body.name).to.equal('dev1'); - expect(res.body.type).to.equal('dut'); - resourceId = res.body._id; - }); + it('add resource', async function () { + const res = await createResource({sn: 'SerialNumber2'}) + expect(res).to.be.a('Object'); + expect(res).to.have.property('status', 200); + expect(res.body).to.have.property('name'); + expect(res.body).to.have.property('type'); + expect(res.body).to.have.property('_id'); + expect(res.body._id).to.be.an('string'); + expect(res.body.name).to.equal('dev1'); + expect(res.body.type).to.equal('dut'); + + await superagent.del(`${api}/resources/${res.body._id}`); }); it('get resource', function () { @@ -133,8 +132,10 @@ describe('Resource', function () { return getResource(resourceId) .then(doUpdate); }); - it('remove resource', function () { - return superagent.del(`${api}/resources/${resourceId}`) + it('remove resource', async function () { + const {body} = await createResource({sn: '1'}) + const {_id} = body + return superagent.del(`${api}/resources/${_id}`) .end(function (error, res) { expect(error).to.equal(null); expect(res).to.be.a('Object'); @@ -142,27 +143,7 @@ describe('Resource', function () { }); }); describe('events', function () { - before(function () { - const body = { - name: 'dev1', - type: 'dut', - hw: { - sn: 'SerialNumber' - } - }; - return superagent.post(`${api}/resources`) - .send(body) - .end(function (error, res) { - resourceId = res.body._id; - }); - }); - after(function () { - return superagent.del(`${api}/resources/${resourceId}`) - .end(function (error, res) { - expect(error).to.equal(null); - expect(res.status).to.be.equal(200); - }); - }); + it('events', function () { return superagent .get(`${api}/resources/${resourceId}/events`) @@ -192,4 +173,13 @@ describe('Resource', function () { }); }); }); + describe('allocation', function () { + it('allocate and release', async function () { + let url = `${api}/resources/${resourceId}/allocate`; + await superagent.put(url).send({}); + + url = `${api}/resources/${resourceId}/release`; + await superagent.put(url).send({}); + }); + }); }); diff --git a/test/unittests/controller/resources.js b/test/unittests/controller/resources.js index 45aff76a..3fce2182 100644 --- a/test/unittests/controller/resources.js +++ b/test/unittests/controller/resources.js @@ -1,11 +1,11 @@ /* eslint-disable func-names, prefer-arrow-callback, no-unused-expressions */ // Third party components -const chai = require('chai'); const sinon = require('sinon'); const Promise = require('bluebird'); // Local components +const chai = require('../../chai'); const {setup, reset, teardown} = require('../../utils/mongomock'); require('./../../../app/models/resource.js'); const ResourceController = require('./../../../app/controllers/resources.js'); @@ -207,10 +207,10 @@ describe('controllers/resources.js', function () { ]); }); - it('alloc', function () { + it('allocate', function () { // Allocate returns error const errorPromise = new Promise((resolve) => { - const req = {Resource: {alloc: (cb) => { + const req = {Resource: {allocate: (cb) => { cb(new Error('Cannot allocate this resource')); }}}; @@ -221,12 +221,12 @@ describe('controllers/resources.js', function () { expect(value).to.equal(500); }); - ResourceController.alloc(req, res); + ResourceController.allocate(req, res); }); // Allocate is successful const successPromise = new Promise((resolve) => { - const req = {Resource: {alloc: (cb) => { + const req = {Resource: {allocate: (cb) => { req.allocated = 'allocation'; cb(undefined, 'document'); }}}; @@ -236,7 +236,7 @@ describe('controllers/resources.js', function () { resolve(); }); - ResourceController.alloc(req, res); + ResourceController.allocate(req, res); }); // Resolve all promises