From 72eeb5a9ee8fdb1441f0de8ebbada3d3cdbb7e0e Mon Sep 17 00:00:00 2001 From: David <33458145+davidmcpowell@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:10:54 +0000 Subject: [PATCH] Merge pull request #16936 from overleaf/dp-mongoose-callback-project-structure-tests Promisify ProjectStructureTests GitOrigin-RevId: c554ea42f107a52e792047a22da9aba9b4127644 --- .../acceptance/src/ProjectStructureTests.js | 407 ++++++------------ 1 file changed, 139 insertions(+), 268 deletions(-) diff --git a/services/web/test/acceptance/src/ProjectStructureTests.js b/services/web/test/acceptance/src/ProjectStructureTests.js index 9386e88bde..c1459e4343 100644 --- a/services/web/test/acceptance/src/ProjectStructureTests.js +++ b/services/web/test/acceptance/src/ProjectStructureTests.js @@ -6,7 +6,7 @@ const fs = require('fs') const { Project } = require('../../../app/src/models/Project') const ProjectGetter = require('../../../app/src/Features/Project/ProjectGetter') -const User = require('./helpers/User') +const User = require('./helpers/User').promises const MockDocStoreApiClass = require('./mocks/MockDocstoreApi') const MockDocUpdaterApiClass = require('./mocks/MockDocUpdaterApi') @@ -25,133 +25,92 @@ describe('ProjectStructureChanges', function () { owner.login(done) }) - function createExampleProject(owner, callback) { - owner.createProject( - 'example-project', - { template: 'example' }, - (error, projectId) => { - if (error) { - return callback(error) - } - - ProjectGetter.getProject(projectId, (error, project) => { - if (error) { - return callback(error) - } - const rootFolderId = project.rootFolder[0]._id.toString() - callback(null, projectId, rootFolderId) - }) - } - ) - } - - function createExampleDoc(owner, projectId, callback) { - ProjectGetter.getProject(projectId, (error, project) => { - if (error) { - return callback(error) - } - owner.request.post( - { - uri: `project/${projectId}/doc`, - json: { - name: 'new.tex', - parent_folder_id: project.rootFolder[0]._id, - }, - }, - (error, res, body) => { - if (error) { - return callback(error) - } - if (res.statusCode < 200 || res.statusCode >= 300) { - return callback(new Error(`failed to add doc ${res.statusCode}`)) - } - callback(null, body._id) - } - ) + async function createExampleProject(owner) { + const projectId = await owner.createProject('example-project', { + template: 'example', }) + + const project = await ProjectGetter.promises.getProject(projectId) + + const rootFolderId = project.rootFolder[0]._id.toString() + return { projectId, rootFolderId } } - function createExampleFolder(owner, projectId, callback) { - owner.request.post( - { - uri: `project/${projectId}/folder`, - json: { - name: 'foo', - }, + async function createExampleDoc(owner, projectId) { + const project = await ProjectGetter.promises.getProject(projectId) + + const { response, body } = await owner.doRequest('POST', { + uri: `project/${projectId}/doc`, + json: { + name: 'new.tex', + parent_folder_id: project.rootFolder[0]._id, }, - (error, res, body) => { - if (error) { - return callback(error) - } - if (res.statusCode < 200 || res.statusCode >= 300) { - return callback(new Error(`failed to add doc ${res.statusCode}`)) - } - callback(null, body._id) - } - ) - } + }) - function uploadExampleProject(owner, zipFilename, options, callback) { - if (typeof options === 'function') { - callback = options - options = {} + if (response.statusCode < 200 || response.statusCode >= 300) { + throw new Error(`failed to add doc ${response.statusCode}`) } + return body._id + } + + async function createExampleFolder(owner, projectId) { + const { response, body } = await owner.doRequest('POST', { + uri: `project/${projectId}/folder`, + json: { + name: 'foo', + }, + }) + + if (response.statusCode < 200 || response.statusCode >= 300) { + throw new Error(`failed to add doc ${response.statusCode}`) + } + + return body._id + } + + async function uploadExampleProject(owner, zipFilename, options = {}) { const zipFile = fs.createReadStream( Path.resolve(Path.join(__dirname, '..', 'files', zipFilename)) ) - owner.request.post( - { - uri: 'project/new/upload', - formData: { - name: zipFilename, - qqfile: zipFile, - }, + const { response, body } = await owner.doRequest('POST', { + uri: 'project/new/upload', + formData: { + name: zipFilename, + qqfile: zipFile, }, - (error, res, body) => { - if (error) { - return callback(error) - } - if ( - !options.allowBadStatus && - (res.statusCode < 200 || res.statusCode >= 300) - ) { - return new Error(`failed to upload project ${res.statusCode}`) - } - callback(null, JSON.parse(body).project_id, res) - } - ) + }) + + if ( + !options.allowBadStatus && + (response.statusCode < 200 || response.statusCode >= 300) + ) { + throw new Error(`failed to upload project ${response.statusCode}`) + } + + return { projectId: JSON.parse(body).project_id, response } } - function deleteItem(owner, projectId, type, itemId, callback) { - owner.deleteItemInProject(projectId, type, itemId, callback) + async function deleteItem(owner, projectId, type, itemId) { + return await owner.deleteItemInProject(projectId, type, itemId) } describe('uploading a project with a name', function () { let exampleProjectId const testProjectName = 'wombat' - beforeEach(function (done) { - uploadExampleProject( + beforeEach(async function () { + const { projectId } = await uploadExampleProject( owner, - 'test_project_with_name.zip', - (err, projectId) => { - if (err) { - return done(err) - } - exampleProjectId = projectId - done() - } + 'test_project_with_name.zip' ) + exampleProjectId = projectId }) - it('should set the project name from the zip contents', function (done) { - ProjectGetter.getProject(exampleProjectId, (error, project) => { - expect(error).not.to.exist - expect(project.name).to.equal(testProjectName) - done() - }) + it('should set the project name from the zip contents', async function () { + const project = await ProjectGetter.promises.getProject(exampleProjectId) + expect(project.name).to.equal(testProjectName) }) }) @@ -159,45 +118,31 @@ describe('ProjectStructureChanges', function () { let exampleProjectId const testProjectMatch = /^bad[^\\]+name$/ - beforeEach(function (done) { - uploadExampleProject( + beforeEach(async function () { + const { projectId } = await uploadExampleProject( owner, - 'test_project_with_invalid_name.zip', - (error, projectId) => { - if (error) { - return done(error) - } - exampleProjectId = projectId - done() - } + 'test_project_with_invalid_name.zip' ) + + exampleProjectId = projectId }) - it('should set the project name from the zip contents', function (done) { - ProjectGetter.getProject(exampleProjectId, (error, project) => { - expect(error).not.to.exist - expect(project.name).to.match(testProjectMatch) - done() - }) + it('should set the project name from the zip contents', async function () { + const project = await ProjectGetter.promises.getProject(exampleProjectId) + expect(project.name).to.match(testProjectMatch) }) }) describe('uploading an empty zipfile', function () { let res - beforeEach(function (done) { - uploadExampleProject( + beforeEach(async function () { + const { response } = await uploadExampleProject( owner, 'test_project_empty.zip', - { allowBadStatus: true }, - (err, projectId, response) => { - if (err) { - return done(err) - } - res = response - done() - } + { allowBadStatus: true } ) + res = response }) it('should fail with 422 error', function () { @@ -208,20 +153,14 @@ describe('ProjectStructureChanges', function () { describe('uploading a zipfile containing only empty directories', function () { let res - beforeEach(function (done) { - uploadExampleProject( + beforeEach(async function () { + const { response } = await uploadExampleProject( owner, 'test_project_with_empty_folder.zip', - { allowBadStatus: true }, - - (err, projectId, response) => { - if (err) { - return done(err) - } - res = response - done() - } + { allowBadStatus: true } ) + + res = response }) it('should fail with 422 error', function () { @@ -232,167 +171,99 @@ describe('ProjectStructureChanges', function () { describe('uploading a project with a shared top-level folder', function () { let exampleProjectId - beforeEach(function (done) { - uploadExampleProject( + beforeEach(async function () { + const { projectId } = await uploadExampleProject( owner, - 'test_project_with_shared_top_level_folder.zip', - (err, projectId) => { - if (err) { - return done(err) - } - exampleProjectId = projectId - done() - } + 'test_project_with_shared_top_level_folder.zip' ) + exampleProjectId = projectId }) - it('should not create the top-level folder', function (done) { - ProjectGetter.getProject(exampleProjectId, (error, project) => { - expect(error).not.to.exist - expect(project.rootFolder[0].folders.length).to.equal(0) - expect(project.rootFolder[0].docs.length).to.equal(2) - done() - }) + it('should not create the top-level folder', async function () { + const project = await ProjectGetter.promises.getProject(exampleProjectId) + expect(project.rootFolder[0].folders.length).to.equal(0) + expect(project.rootFolder[0].docs.length).to.equal(2) }) }) describe('uploading a project with backslashes in the path names', function () { let exampleProjectId - beforeEach(function (done) { - uploadExampleProject( + beforeEach(async function () { + const { projectId } = await uploadExampleProject( owner, - 'test_project_with_backslash_in_filename.zip', - (err, projectId) => { - if (err) { - return done(err) - } - exampleProjectId = projectId - done() - } + 'test_project_with_backslash_in_filename.zip' ) + exampleProjectId = projectId }) - it('should treat the backslash as a directory separator', function (done) { - ProjectGetter.getProject(exampleProjectId, (error, project) => { - expect(error).not.to.exist - expect(project.rootFolder[0].folders[0].name).to.equal('styles') - expect(project.rootFolder[0].folders[0].docs[0].name).to.equal('ao.sty') - done() - }) + it('should treat the backslash as a directory separator', async function () { + const project = await ProjectGetter.promises.getProject(exampleProjectId) + expect(project.rootFolder[0].folders[0].name).to.equal('styles') + expect(project.rootFolder[0].folders[0].docs[0].name).to.equal('ao.sty') }) }) describe('deleting docs', function () { - beforeEach(function (done) { - createExampleProject(owner, (err, projectId) => { - if (err) { - return done(err) - } - this.exampleProjectId = projectId - createExampleFolder(owner, projectId, (err, folderId) => { - if (err) { - return done(err) - } - this.exampleFolderId = folderId - createExampleDoc(owner, projectId, (err, docId) => { - if (err) { - return done(err) - } - this.exampleDocId = docId - MockDocUpdaterApi.reset() - ProjectGetter.getProject( - this.exampleProjectId, - (error, project) => { - if (error) { - throw error - } - this.project0 = project - done() - } - ) - }) - }) - }) + beforeEach(async function () { + const { projectId } = await createExampleProject(owner) + this.exampleProjectId = projectId + + const folderId = await createExampleFolder(owner, projectId) + this.exampleFolderId = folderId + + const docId = await createExampleDoc(owner, projectId) + this.exampleDocId = docId + + MockDocUpdaterApi.reset() + + const project = await ProjectGetter.promises.getProject( + this.exampleProjectId + ) + this.project0 = project }) - it('should pass the doc name to docstore', function (done) { - deleteItem( - owner, - this.exampleProjectId, - 'doc', - this.exampleDocId, - error => { - if (error) return done(error) - expect( - MockDocStoreApi.getDeletedDocs(this.exampleProjectId) - ).to.deep.equal([{ _id: this.exampleDocId, name: 'new.tex' }]) - done() - } - ) + it('should pass the doc name to docstore', async function () { + await deleteItem(owner, this.exampleProjectId, 'doc', this.exampleDocId) + + expect( + MockDocStoreApi.getDeletedDocs(this.exampleProjectId) + ).to.deep.equal([{ _id: this.exampleDocId, name: 'new.tex' }]) }) describe('when rootDoc_id matches doc being deleted', function () { - beforeEach(function (done) { - Project.updateOne( + beforeEach(async function () { + await Project.updateOne( { _id: this.exampleProjectId }, - { $set: { rootDoc_id: this.exampleDocId } }, - done - ) + { $set: { rootDoc_id: this.exampleDocId } } + ).exec() }) - it('should clear rootDoc_id', function (done) { - deleteItem( - owner, - this.exampleProjectId, - 'doc', - this.exampleDocId, - () => { - ProjectGetter.getProject( - this.exampleProjectId, - (error, project) => { - if (error) { - throw error - } - expect(project.rootDoc_id).to.be.undefined - done() - } - ) - } - ) + it('should clear rootDoc_id', async function () { + await deleteItem(owner, this.exampleProjectId, 'doc', this.exampleDocId) + const project = ProjectGetter.promises.getProject(this.exampleProjectId) + expect(project.rootDoc_id).to.be.undefined }) }) describe('when rootDoc_id does not match doc being deleted', function () { - beforeEach(function (done) { + beforeEach(async function () { this.exampleRootDocId = new ObjectId() - Project.updateOne( + await Project.updateOne( { _id: this.exampleProjectId }, - { $set: { rootDoc_id: this.exampleRootDocId } }, - done - ) + { $set: { rootDoc_id: this.exampleRootDocId } } + ).exec() }) - it('should not clear rootDoc_id', function (done) { - deleteItem( - owner, - this.exampleProjectId, - 'doc', - this.exampleDocId, - () => { - ProjectGetter.getProject( - this.exampleProjectId, - (error, project) => { - if (error) { - throw error - } - expect(project.rootDoc_id.toString()).to.equal( - this.exampleRootDocId.toString() - ) - done() - } - ) - } + it('should not clear rootDoc_id', async function () { + await deleteItem(owner, this.exampleProjectId, 'doc', this.exampleDocId) + + const project = await ProjectGetter.promises.getProject( + this.exampleProjectId + ) + + expect(project.rootDoc_id.toString()).to.equal( + this.exampleRootDocId.toString() ) }) })