ProjectUploadControllerTests.coffee 5.9 KB

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