From d7d989ca1b9013c24d6b47f6b13f26d387f83bfc Mon Sep 17 00:00:00 2001 From: Miguel Serrano Date: Mon, 11 Jan 2021 12:58:16 +0100 Subject: [PATCH] Merge pull request #3537 from overleaf/jpa-helper-for-project-actions [misc] move some helper functions from ProjectStructureTests into User GitOrigin-RevId: d5d1bbf9aa31a9f248e03b1fa3deb0378dce7a7b --- .../acceptance/src/ProjectStructureTests.js | 99 ++-------------- .../web/test/acceptance/src/helpers/User.js | 108 ++++++++++++++++++ 2 files changed, 119 insertions(+), 88 deletions(-) diff --git a/services/web/test/acceptance/src/ProjectStructureTests.js b/services/web/test/acceptance/src/ProjectStructureTests.js index fff37acc9a..4087efcb8b 100644 --- a/services/web/test/acceptance/src/ProjectStructureTests.js +++ b/services/web/test/acceptance/src/ProjectStructureTests.js @@ -97,47 +97,21 @@ describe('ProjectStructureChanges', function() { contentType, callback ) { - const imageFile = fs.createReadStream( - Path.resolve(Path.join(__dirname, '..', 'files', file)) - ) - - owner.request.post( - { - uri: `project/${projectId}/upload`, - qs: { - folder_id: folderId - }, - formData: { - qqfile: { - value: imageFile, - options: { - filename: name, - contentType: contentType - } - } - } - }, - (error, res, body) => { - if (error) { - return callback(error) - } - if (res.statusCode < 200 || res.statusCode >= 300) { - return callback(new Error(`failed to upload file ${res.statusCode}`)) - } - - callback(null, JSON.parse(body).entity_id) - } + owner.uploadFileInProject( + projectId, + folderId, + file, + name, + contentType, + callback ) } function uploadExampleFile(owner, projectId, folderId, callback) { - uploadFile( - owner, + owner.uploadExampleFileInProject( projectId, folderId, '1pixel.png', - '1pixel.png', - 'image/png', callback ) } @@ -175,66 +149,15 @@ describe('ProjectStructureChanges', function() { } function moveItem(owner, projectId, type, itemId, folderId, callback) { - owner.request.post( - { - uri: `project/${projectId}/${type}/${itemId}/move`, - json: { - folder_id: folderId - } - }, - (error, res) => { - if (error) { - return callback(error) - } - if (res.statusCode < 200 || res.statusCode >= 300) { - return callback(new Error(`failed to move ${type} ${res.statusCode}`)) - } - - callback() - } - ) + owner.moveItemInProject(projectId, type, itemId, folderId, callback) } function renameItem(owner, projectId, type, itemId, name, callback) { - owner.request.post( - { - uri: `project/${projectId}/${type}/${itemId}/rename`, - json: { - name: name - } - }, - (error, res) => { - if (error) { - return callback(error) - } - if (res.statusCode < 200 || res.statusCode >= 300) { - return callback( - new Error(`failed to rename ${type} ${res.statusCode}`) - ) - } - - callback() - } - ) + owner.renameItemInProject(projectId, type, itemId, name, callback) } function deleteItem(owner, projectId, type, itemId, callback) { - owner.request.delete( - { - uri: `project/${projectId}/${type}/${itemId}` - }, - (error, res) => { - if (error) { - return callback(error) - } - if (res.statusCode < 200 || res.statusCode >= 300) { - return callback( - new Error(`failed to delete folder ${res.statusCode}`) - ) - } - callback() - } - ) + owner.deleteItemInProject(projectId, type, itemId, callback) } function verifyVersionIncremented( diff --git a/services/web/test/acceptance/src/helpers/User.js b/services/web/test/acceptance/src/helpers/User.js index 0401486e75..e1f7fa64ce 100644 --- a/services/web/test/acceptance/src/helpers/User.js +++ b/services/web/test/acceptance/src/helpers/User.js @@ -5,6 +5,8 @@ const UserModel = require('../../../../app/src/models/User').User const UserUpdater = require('../../../../app/src/Features/User/UserUpdater') const AuthenticationManager = require('../../../../app/src/Features/Authentication/AuthenticationManager') const { promisify } = require('util') +const fs = require('fs') +const Path = require('path') let count = 0 @@ -401,6 +403,112 @@ class User { }) } + uploadFileInProject(projectId, folderId, file, name, contentType, callback) { + const imageFile = fs.createReadStream( + Path.resolve(Path.join(__dirname, '..', '..', 'files', file)) + ) + + this.request.post( + { + uri: `project/${projectId}/upload`, + qs: { + folder_id: String(folderId) + }, + formData: { + qqfile: { + value: imageFile, + options: { + filename: name, + contentType: contentType + } + } + } + }, + (error, res, body) => { + if (error) { + return callback(error) + } + if (res.statusCode < 200 || res.statusCode >= 300) { + return callback(new Error(`failed to upload file ${res.statusCode}`)) + } + + callback(null, JSON.parse(body).entity_id) + } + ) + } + + uploadExampleFileInProject(projectId, folderId, name, callback) { + this.uploadFileInProject( + projectId, + folderId, + '1pixel.png', + name, + 'image/png', + callback + ) + } + + moveItemInProject(projectId, type, itemId, folderId, callback) { + this.request.post( + { + uri: `project/${projectId}/${type}/${itemId}/move`, + json: { + folder_id: folderId + } + }, + (error, res) => { + if (error) { + return callback(error) + } + if (res.statusCode < 200 || res.statusCode >= 300) { + return callback(new Error(`failed to move ${type} ${res.statusCode}`)) + } + + callback() + } + ) + } + + renameItemInProject(projectId, type, itemId, name, callback) { + this.request.post( + { + uri: `project/${projectId}/${type}/${itemId}/rename`, + json: { + name: name + } + }, + (error, res) => { + if (error) { + return callback(error) + } + if (res.statusCode < 200 || res.statusCode >= 300) { + return callback( + new Error(`failed to rename ${type} ${res.statusCode}`) + ) + } + + callback() + } + ) + } + + deleteItemInProject(projectId, type, itemId, callback) { + this.request.delete( + `project/${projectId}/${type}/${itemId}`, + (error, res) => { + if (error) { + return callback(error) + } + if (res.statusCode < 200 || res.statusCode >= 300) { + return callback( + new Error(`failed to delete ${type} ${res.statusCode}`) + ) + } + callback() + } + ) + } + addUserToProject(projectId, user, privileges, callback) { let updateOp if (privileges === 'readAndWrite') {