ProjectUploadControllerTests.coffee 5.5 KB

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