ProjectUploadControllerTests.coffee 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Uploads/ProjectUploadController.js"
  6. SandboxedModule = require('sandboxed-module')
  7. MockRequest = require "../helpers/MockRequest"
  8. MockResponse = require "../helpers/MockResponse"
  9. describe "ProjectUploadController", ->
  10. beforeEach ->
  11. @req = new MockRequest()
  12. @res = new MockResponse()
  13. @user_id = "user-id-123"
  14. @metrics =
  15. Timer: class Timer
  16. done: sinon.stub()
  17. @AuthenticationController =
  18. getLoggedInUserId: sinon.stub().returns(@user_id)
  19. @LockManager =
  20. runWithLock : sinon.spy((key, runner, callback) -> runner(callback))
  21. @ProjectUploadController = SandboxedModule.require modulePath, requires:
  22. "./ProjectUploadManager" : @ProjectUploadManager = {}
  23. "./FileSystemImportManager" : @FileSystemImportManager = {}
  24. "logger-sharelatex" : @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  25. "metrics-sharelatex": @metrics
  26. '../Authentication/AuthenticationController': @AuthenticationController
  27. "../../infrastructure/LockManager": @LockManager
  28. "fs" : @fs = {}
  29. describe "uploadProject", ->
  30. beforeEach ->
  31. @path = "/path/to/file/on/disk.zip"
  32. @name = "filename.zip"
  33. @req.files =
  34. qqfile:
  35. path: @path
  36. originalname: @name
  37. @req.session =
  38. user:
  39. _id: @user_id
  40. @project =
  41. _id: @project_id = "project-id-123"
  42. @fs.unlink = sinon.stub()
  43. describe "successfully", ->
  44. beforeEach ->
  45. @ProjectUploadManager.createProjectFromZipArchive =
  46. sinon.stub().callsArgWith(3, null, @project)
  47. @ProjectUploadController.uploadProject @req, @res
  48. it "should create a project owned by the logged in user", ->
  49. @ProjectUploadManager
  50. .createProjectFromZipArchive
  51. .calledWith(@user_id)
  52. .should.equal true
  53. it "should create a project with the same name as the zip archive", ->
  54. @ProjectUploadManager
  55. .createProjectFromZipArchive
  56. .calledWith(sinon.match.any, "filename", sinon.match.any)
  57. .should.equal true
  58. it "should create a project from the zip archive", ->
  59. @ProjectUploadManager
  60. .createProjectFromZipArchive
  61. .calledWith(sinon.match.any, sinon.match.any, @path)
  62. .should.equal true
  63. it "should return a successful response to the FileUploader client", ->
  64. expect(@res.body).to.deep.equal
  65. success: true
  66. project_id: @project_id
  67. it "should record the time taken to do the upload", ->
  68. @metrics.Timer::done.called.should.equal true
  69. it "should output a log line", ->
  70. @logger.log
  71. .calledWith(sinon.match.any, "uploaded project")
  72. .should.equal true
  73. it "should remove the uploaded file", ->
  74. @fs.unlink.calledWith(@path).should.equal true
  75. describe "when ProjectUploadManager.createProjectFromZipArchive fails", ->
  76. beforeEach ->
  77. @ProjectUploadManager.createProjectFromZipArchive =
  78. sinon.stub().callsArgWith(3, new Error("Something went wrong"), @project)
  79. @ProjectUploadController.uploadProject @req, @res
  80. it "should return a failed response to the FileUploader client", ->
  81. expect(@res.body).to.deep.equal
  82. success: false
  83. it "should output an error log line", ->
  84. @logger.error
  85. .calledWith(sinon.match.any, "error uploading project")
  86. .should.equal true
  87. describe "uploadFile", ->
  88. beforeEach ->
  89. @project_id = "project-id-123"
  90. @folder_id = "folder-id-123"
  91. @path = "/path/to/file/on/disk.png"
  92. @filename = "filename.png"
  93. @req.files =
  94. qqfile:
  95. path: @path
  96. originalname: @name
  97. @req.session =
  98. user:
  99. _id: @user_id
  100. @req.params =
  101. Project_id: @project_id
  102. @req.query =
  103. folder_id: @folder_id
  104. @fs.unlink = sinon.stub()
  105. describe "successfully", ->
  106. beforeEach ->
  107. @entity =
  108. _id : "1234"
  109. type: 'file'
  110. @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
  111. @ProjectUploadController.uploadFile @req, @res
  112. it "should take the lock", ->
  113. @LockManager.runWithLock.calledWith(@project_id).should.equal true
  114. it "should insert the file", ->
  115. @FileSystemImportManager.addEntity
  116. .calledWith(@user_id, @project_id, @folder_id, @name, @path)
  117. .should.equal true
  118. it "should return a successful response to the FileUploader client", ->
  119. expect(@res.body).to.deep.equal
  120. success: true
  121. entity_id: @entity._id
  122. entity_type: 'file'
  123. it "should output a log line", ->
  124. @logger.log
  125. .calledWith(sinon.match.any, "uploaded file")
  126. .should.equal true
  127. it "should time the request", ->
  128. @metrics.Timer::done.called.should.equal true
  129. it "should remove the uploaded file", ->
  130. @fs.unlink.calledWith(@path).should.equal true
  131. describe "when FileSystemImportManager.addEntity returns an error", ->
  132. beforeEach ->
  133. @FileSystemImportManager.addEntity = sinon.stub()
  134. .callsArgWith(6, new Error("Sorry something went wrong"))
  135. @ProjectUploadController.uploadFile @req, @res
  136. it "should return an unsuccessful response to the FileUploader client", ->
  137. expect(@res.body).to.deep.equal
  138. success: false
  139. it "should output an error log line", ->
  140. @logger.error
  141. .calledWith(sinon.match.any, "error uploading file")
  142. .should.equal true
  143. describe "with a bad request", ->
  144. beforeEach ->
  145. @req.files.qqfile.originalname = ""
  146. @ProjectUploadController.uploadFile @req, @res
  147. it "should return a a non success response", ->
  148. expect(@res.body).to.deep.equal
  149. success: false