overleaf/services/web/test/unit/coffee/Uploads/ProjectUploadControllerTests.coffee

181 lines
5.4 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/Features/Uploads/ProjectUploadController.js"
SandboxedModule = require('sandboxed-module')
MockRequest = require "../helpers/MockRequest"
MockResponse = require "../helpers/MockResponse"
describe "ProjectUploadController", ->
beforeEach ->
@req = new MockRequest()
@res = new MockResponse()
@user_id = "user-id-123"
@metrics =
Timer: class Timer
done: sinon.stub()
2016-09-07 11:40:49 -04:00
@AuthenticationController =
getLoggedInUserId: sinon.stub().returns(@user_id)
2018-01-02 07:43:41 -05:00
@LockManager =
2018-01-09 11:00:08 -05:00
runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
2014-02-12 05:23:40 -05:00
@ProjectUploadController = SandboxedModule.require modulePath, requires:
"./ProjectUploadManager" : @ProjectUploadManager = {}
"./FileSystemImportManager" : @FileSystemImportManager = {}
"logger-sharelatex" : @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
"metrics-sharelatex": @metrics
2016-09-07 11:40:49 -04:00
'../Authentication/AuthenticationController': @AuthenticationController
2018-01-02 07:43:41 -05:00
"../../infrastructure/LockManager": @LockManager
2014-02-12 05:23:40 -05:00
"fs" : @fs = {}
2016-09-07 11:40:49 -04:00
2014-02-12 05:23:40 -05:00
describe "uploadProject", ->
beforeEach ->
@path = "/path/to/file/on/disk.zip"
@name = "filename.zip"
@req.files =
qqfile:
path: @path
originalname: @name
2014-02-12 05:23:40 -05:00
@req.session =
user:
_id: @user_id
@project =
_id: @project_id = "project-id-123"
@fs.unlink = sinon.stub()
describe "successfully", ->
beforeEach ->
@ProjectUploadManager.createProjectFromZipArchive =
sinon.stub().callsArgWith(3, null, @project)
@ProjectUploadController.uploadProject @req, @res
it "should create a project owned by the logged in user", ->
@ProjectUploadManager
.createProjectFromZipArchive
.calledWith(@user_id)
.should.equal true
it "should create a project with the same name as the zip archive", ->
@ProjectUploadManager
.createProjectFromZipArchive
.calledWith(sinon.match.any, "filename", sinon.match.any)
.should.equal true
2016-09-07 11:40:49 -04:00
2014-02-12 05:23:40 -05:00
it "should create a project from the zip archive", ->
@ProjectUploadManager
.createProjectFromZipArchive
.calledWith(sinon.match.any, sinon.match.any, @path)
.should.equal true
2016-09-07 11:40:49 -04:00
2014-02-12 05:23:40 -05:00
it "should return a successful response to the FileUploader client", ->
expect(@res.body).to.deep.equal
success: true
project_id: @project_id
it "should record the time taken to do the upload", ->
@metrics.Timer::done.called.should.equal true
it "should output a log line", ->
@logger.log
.calledWith(sinon.match.any, "uploaded project")
.should.equal true
it "should remove the uploaded file", ->
@fs.unlink.calledWith(@path).should.equal true
describe "when ProjectUploadManager.createProjectFromZipArchive fails", ->
beforeEach ->
@ProjectUploadManager.createProjectFromZipArchive =
sinon.stub().callsArgWith(3, new Error("Something went wrong"), @project)
@ProjectUploadController.uploadProject @req, @res
it "should return a failed response to the FileUploader client", ->
expect(@res.body).to.deep.equal
success: false
it "should output an error log line", ->
@logger.error
.calledWith(sinon.match.any, "error uploading project")
.should.equal true
describe "uploadFile", ->
beforeEach ->
@project_id = "project-id-123"
@folder_id = "folder-id-123"
@path = "/path/to/file/on/disk.png"
@filename = "filename.png"
@req.files =
qqfile:
path: @path
originalname: @name
2016-02-23 11:46:14 -05:00
@req.session =
user:
_id: @user_id
2014-02-12 05:23:40 -05:00
@req.params =
Project_id: @project_id
@req.query =
folder_id: @folder_id
@fs.unlink = sinon.stub()
describe "successfully", ->
beforeEach ->
@entity =
_id : "1234"
2017-06-12 05:09:33 -04:00
type: 'file'
2016-02-23 11:46:14 -05:00
@FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
2014-02-12 05:23:40 -05:00
@ProjectUploadController.uploadFile @req, @res
2018-01-02 07:43:41 -05:00
it "should take the lock", ->
2018-01-09 11:00:08 -05:00
@LockManager.runWithLock.calledWith(@project_id).should.equal true
2018-01-02 07:43:41 -05:00
2016-02-23 11:46:14 -05:00
it "should insert the file", ->
2014-02-12 05:23:40 -05:00
@FileSystemImportManager.addEntity
2016-02-23 11:46:14 -05:00
.calledWith(@user_id, @project_id, @folder_id, @name, @path)
2014-02-12 05:23:40 -05:00
.should.equal true
it "should return a successful response to the FileUploader client", ->
expect(@res.body).to.deep.equal
success: true
entity_id: @entity._id
2017-06-12 05:09:33 -04:00
entity_type: 'file'
2014-02-12 05:23:40 -05:00
it "should output a log line", ->
@logger.log
.calledWith(sinon.match.any, "uploaded file")
.should.equal true
it "should time the request", ->
@metrics.Timer::done.called.should.equal true
it "should remove the uploaded file", ->
@fs.unlink.calledWith(@path).should.equal true
describe "when FileSystemImportManager.addEntity returns an error", ->
beforeEach ->
@FileSystemImportManager.addEntity = sinon.stub()
2016-02-23 11:46:14 -05:00
.callsArgWith(6, new Error("Sorry something went wrong"))
2014-02-12 05:23:40 -05:00
@ProjectUploadController.uploadFile @req, @res
it "should return an unsuccessful response to the FileUploader client", ->
expect(@res.body).to.deep.equal
success: false
it "should output an error log line", ->
@logger.error
.calledWith(sinon.match.any, "error uploading file")
.should.equal true
describe "with a bad request", ->
beforeEach ->
@req.files.qqfile.originalname = ""
@ProjectUploadController.uploadFile @req, @res
it "should return a a non success response", ->
expect(@res.body).to.deep.equal
success: false