ProjectUploadControllerTests.coffee 5.4 KB

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