From aa7074f3039399d7a3bfcf3704bb28c9a911d7dd Mon Sep 17 00:00:00 2001 From: alexm Date: Fri, 27 May 2022 08:23:52 +0200 Subject: [PATCH] test(mdb): back test --- db/dump/fixtures.sql | 8 +- loopback/server/datasources.json | 20 ++--- modules/claim/front/photos/index.js | 2 +- .../mdbVersion/specs/filterValue.spec.js | 76 ------------------- .../methods/mdbVersion/specs/upload.spec.js | 27 +++++++ modules/mdb/back/methods/mdbVersion/upload.js | 48 ++++++++++-- modules/mdb/back/model-config.json | 4 +- modules/mdb/back/models/mdb-container.json | 2 +- modules/mdb/back/models/mdbVersion.json | 2 +- 9 files changed, 85 insertions(+), 104 deletions(-) delete mode 100644 modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js create mode 100644 modules/mdb/back/methods/mdbVersion/specs/upload.spec.js diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 087d66ac5..ca8791c21 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -2585,4 +2585,10 @@ INSERT INTO `vn`.`mdbBranch` (`name`) INSERT INTO `vn`.`mdbVersion` (`app`, `branchFk`, `version`) VALUES ('tpv', 'test', '1'), - ('lab', 'master', '2'); \ No newline at end of file + ('lab', 'master', '1'), + ('enc', 'test', '1'), + ('ent', 'master', '1'), + ('com', 'test', '1'), + ('eti', 'master', '1'), + ('ser', 'test', '1'), + ('lib', 'master', '1'); \ No newline at end of file diff --git a/loopback/server/datasources.json b/loopback/server/datasources.json index 73f3a7bf0..7d2a6c117 100644 --- a/loopback/server/datasources.json +++ b/loopback/server/datasources.json @@ -99,24 +99,14 @@ "video/mp4" ] }, - "mdbStorage": { - "name": "mdbStorage", + "accessStorage": { + "name": "accessStorage", "connector": "loopback-component-storage", "provider": "filesystem", - "root": "./storage/mdb", - "maxFileSize": "262144000", + "root": "./storage/access/branches", + "maxFileSize": "2621440000", "allowedContentTypes": [ - "application/x-7z-compressed", - "application/x-zip-compressed", - "application/x-rar-compressed", - "application/octet-stream", - "application/pdf", - "application/zip", - "application/rar", - "multipart/x-zip", - "image/png", - "image/jpeg", - "image/jpg" + "application/x-7z-compressed" ] } } \ No newline at end of file diff --git a/modules/claim/front/photos/index.js b/modules/claim/front/photos/index.js index 62e439a91..5cde18f52 100644 --- a/modules/claim/front/photos/index.js +++ b/modules/claim/front/photos/index.js @@ -71,7 +71,7 @@ class Controller extends Section { } create() { - const query = `claims/${this.claim.id}/uploadFile`; + const query = `mdbVersions/upload`; const options = { method: 'POST', url: query, diff --git a/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js b/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js deleted file mode 100644 index 9e5cf2da2..000000000 --- a/modules/mdb/back/methods/mdbVersion/specs/filterValue.spec.js +++ /dev/null @@ -1,76 +0,0 @@ -const models = require('vn-loopback/server/server').models; - -describe('tag filterValue()', () => { - const colorTagId = 1; - it('should return a list of color values', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(5); - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - - it('should return the values matching color "Blue"', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {where: {value: 'Blue'}, limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(2); - expect(result[0].value).toEqual('Blue'); - expect(result[1].value).toEqual('Blue/Silver'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - - it('should return the values matching color "Blue/Silver"', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {where: {value: 'Blue/Silver'}, limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(1); - expect(result[0].value).toEqual('Blue/Silver'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); - - it('should return the values matching color "Silver"', async() => { - const tx = await models.Item.beginTransaction({}); - const options = {transaction: tx}; - - try { - const filter = {where: {value: 'Silver'}, limit: 5}; - const result = await models.Tag.filterValue(colorTagId, filter, options); - - expect(result.length).toEqual(2); - expect(result[0].value).toEqual('Silver'); - expect(result[1].value).toEqual('Blue/Silver'); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - throw e; - } - }); -}); diff --git a/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js b/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js new file mode 100644 index 000000000..f62886760 --- /dev/null +++ b/modules/mdb/back/methods/mdbVersion/specs/upload.spec.js @@ -0,0 +1,27 @@ +const models = require('vn-loopback/server/server').models; + +fdescribe('Mdb upload()', () => { + it(`should return an error for a user without enough privileges`, async() => { + const tx = await models.MdbVersion.beginTransaction({}); + + let error; + + try { + const options = {transaction: tx}; + const currentUserId = 1101; + const appName = 'tpv'; + const newVersion = '123'; + const branch = 'test'; + const ctx = {req: {accessToken: {userId: currentUserId}}}; + + await models.MdbVersion.upload(ctx, appName, newVersion, branch, options); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + error = e; + } + + expect(error.message).toEqual(`You don't have enough privileges`); + }); +}); diff --git a/modules/mdb/back/methods/mdbVersion/upload.js b/modules/mdb/back/methods/mdbVersion/upload.js index 96b8347b2..d5725edbe 100644 --- a/modules/mdb/back/methods/mdbVersion/upload.js +++ b/modules/mdb/back/methods/mdbVersion/upload.js @@ -1,3 +1,6 @@ +const fs = require('fs-extra'); +const path = require('path'); + module.exports = Self => { Self.remoteMethodCtx('upload', { description: 'Returns a list of tag values', @@ -34,17 +37,48 @@ module.exports = Self => { Self.upload = async(ctx, appName, newVersion, branch, options) => { const models = Self.app.models; const myOptions = {}; + console.log(ctx); - const moduleFile = ctx.req; - if (!moduleFile) return; + const TempContainer = models.TempContainer; + const AccessContainer = models.AccessContainer; + const fileOptions = {}; + + let tx; if (typeof options == 'object') Object.assign(myOptions, options); - const newMdb = await models.mdbVersion.create({ - app: appName, - version: newVersion, - branchFk: branch - }, myOptions); + if (!myOptions.transaction) { + tx = await Self.beginTransaction({}); + myOptions.transaction = tx; + } + + try { + const tempContainer = await TempContainer.container('access'); + const uploaded = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions); + const files = Object.values(uploaded.files).map(file => { + return file[0]; + }); + const uploadedFile = files[0]; + const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name); + const srcFile = path.join(file.client.root, file.container, file.name); + const accessContainer = await AccessContainer.container(branch); + const destinationFile = path.join(accessContainer.client.root, branch, `${appName}${newVersion}.7z`); + + await fs.move(srcFile, destinationFile, { + overwrite: true + }); + + await models.MdbVersion.create({ + app: appName, + version: newVersion, + branchFk: branch + }, myOptions); + + if (tx) await tx.commit(); + } catch (e) { + if (tx) await tx.rollback(); + throw e; + } }; }; diff --git a/modules/mdb/back/model-config.json b/modules/mdb/back/model-config.json index fbb7e1a33..d5be8de87 100644 --- a/modules/mdb/back/model-config.json +++ b/modules/mdb/back/model-config.json @@ -5,7 +5,7 @@ "MdbVersion": { "dataSource": "vn" }, - "MdbContainer": { - "dataSource": "mdbStorage" + "AccessContainer": { + "dataSource": "accessStorage" } } diff --git a/modules/mdb/back/models/mdb-container.json b/modules/mdb/back/models/mdb-container.json index 9d8052d80..a927b30f1 100644 --- a/modules/mdb/back/models/mdb-container.json +++ b/modules/mdb/back/models/mdb-container.json @@ -1,5 +1,5 @@ { - "name": "MdbContainer", + "name": "AccessContainer", "base": "Container", "acls": [{ "accessType": "*", diff --git a/modules/mdb/back/models/mdbVersion.json b/modules/mdb/back/models/mdbVersion.json index dc17531d6..0fd86e4a5 100644 --- a/modules/mdb/back/models/mdbVersion.json +++ b/modules/mdb/back/models/mdbVersion.json @@ -19,7 +19,7 @@ "branch": { "type": "belongsTo", "model": "MdbBranch", - "foreignKey": "name" + "foreignKey": "branchFk" } } } \ No newline at end of file