Преглед изворни кода

Merge pull request #5734 from overleaf/ab-handle-project-zip-not-existing

Handle case of not existing project ID when zipping projects for download

GitOrigin-RevId: 601b14ef0217c27dc50ef0442f7e81db35642b6d
Alf Eaton пре 4 година
родитељ
комит
a15e1bdc07

+ 7 - 0
services/web/app/src/Features/Downloads/ProjectZipStreamManager.js

@@ -23,6 +23,13 @@ module.exports = ProjectZipStreamManager = {
         if (error) {
         if (error) {
           return cb(error)
           return cb(error)
         }
         }
+        if (!project) {
+          logger.log(
+            { projectId },
+            'cannot append project to zip stream: project not found'
+          )
+          return cb()
+        }
         logger.log(
         logger.log(
           { projectId, name: project.name },
           { projectId, name: project.name },
           'appending project to zip stream'
           'appending project to zip stream'

+ 75 - 0
services/web/test/unit/src/Downloads/ProjectZipStreamManagerTests.js

@@ -120,6 +120,81 @@ describe('ProjectZipStreamManager', function () {
         )
         )
       })
       })
     })
     })
+
+    describe('with a project not existing', function () {
+      beforeEach(function (done) {
+        this.project_ids = ['project-1', 'wrong-id']
+        this.project_names = {
+          'project-1': 'Project One Name',
+        }
+        this.zip_streams = {
+          'project-1': new EventEmitter(),
+        }
+
+        this.ProjectZipStreamManager.createZipStreamForProject = (
+          project_id,
+          callback
+        ) => {
+          callback(null, this.zip_streams[project_id])
+          setTimeout(() => {
+            this.zip_streams[project_id].emit('end')
+          })
+        }
+        sinon.spy(this.ProjectZipStreamManager, 'createZipStreamForProject')
+
+        this.ProjectGetter.getProject = (project_id, fields, callback) => {
+          const name = this.project_names[project_id]
+          callback(null, name ? { name } : undefined)
+        }
+        sinon.spy(this.ProjectGetter, 'getProject')
+
+        this.ProjectZipStreamManager.createZipStreamForMultipleProjects(
+          this.project_ids,
+          this.callback
+        )
+
+        this.archive.finalize = () => done()
+      })
+
+      it('should create a zip archive', function () {
+        this.archiver.calledWith('zip').should.equal(true)
+      })
+
+      it('should return a stream before any processing is done', function () {
+        this.callback
+          .calledWith(sinon.match.falsy, this.archive)
+          .should.equal(true)
+        this.callback
+          .calledBefore(this.ProjectZipStreamManager.createZipStreamForProject)
+          .should.equal(true)
+      })
+
+      it('should get the names of each project', function () {
+        this.project_ids.map(project_id =>
+          this.ProjectGetter.getProject
+            .calledWith(project_id, { name: true })
+            .should.equal(true)
+        )
+      })
+
+      it('should get a zip stream only for the existing project', function () {
+        this.ProjectZipStreamManager.createZipStreamForProject
+          .calledWith('project-1')
+          .should.equal(true)
+        this.ProjectZipStreamManager.createZipStreamForProject
+          .calledWith('wrong-id')
+          .should.equal(false)
+      })
+
+      it('should only add the existing project to the zip', function () {
+        sinon.assert.calledOnce(this.archive.append)
+        this.archive.append
+          .calledWith(this.zip_streams['project-1'], {
+            name: this.project_names['project-1'] + '.zip',
+          })
+          .should.equal(true)
+      })
+    })
   })
   })
 
 
   describe('createZipStreamForProject', function () {
   describe('createZipStreamForProject', function () {