Merge pull request #3537 from overleaf/jpa-helper-for-project-actions

[misc] move some helper functions from ProjectStructureTests into User

GitOrigin-RevId: d5d1bbf9aa31a9f248e03b1fa3deb0378dce7a7b
This commit is contained in:
Miguel Serrano 2021-01-11 12:58:16 +01:00 committed by Copybot
parent 32f133dd41
commit d7d989ca1b
2 changed files with 119 additions and 88 deletions

View file

@ -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(

View file

@ -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') {