ProjectUploadControllerTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. @ProjectUploadController = SandboxedModule.require modulePath, requires:
  20. "./ProjectUploadManager" : @ProjectUploadManager = {}
  21. "./FileSystemImportManager" : @FileSystemImportManager = {}
  22. "logger-sharelatex" : @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  23. "metrics-sharelatex": @metrics
  24. '../Authentication/AuthenticationController': @AuthenticationController
  25. "fs" : @fs = {}
  26. describe "uploadProject", ->
  27. beforeEach ->
  28. @path = "/path/to/file/on/disk.zip"
  29. @name = "filename.zip"
  30. @req.files =
  31. qqfile:
  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
  79. success: false
  80. it "should output an error log line", ->
  81. @logger.error
  82. .calledWith(sinon.match.any, "error uploading project")
  83. .should.equal true
  84. describe "uploadFile", ->
  85. beforeEach ->
  86. @project_id = "project-id-123"
  87. @folder_id = "folder-id-123"
  88. @path = "/path/to/file/on/disk.png"
  89. @filename = "filename.png"
  90. @req.files =
  91. qqfile:
  92. path: @path
  93. originalname: @name
  94. @req.session =
  95. user:
  96. _id: @user_id
  97. @req.params =
  98. Project_id: @project_id
  99. @req.query =
  100. folder_id: @folder_id
  101. @fs.unlink = sinon.stub()
  102. describe "successfully", ->
  103. beforeEach ->
  104. @entity =
  105. _id : "1234"
  106. @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
  107. @ProjectUploadController.uploadFile @req, @res
  108. it "should insert the file", ->
  109. @FileSystemImportManager.addEntity
  110. .calledWith(@user_id, @project_id, @folder_id, @name, @path)
  111. .should.equal true
  112. it "should return a successful response to the FileUploader client", ->
  113. expect(@res.body).to.deep.equal
  114. success: true
  115. entity_id: @entity._id
  116. it "should output a log line", ->
  117. @logger.log
  118. .calledWith(sinon.match.any, "uploaded file")
  119. .should.equal true
  120. it "should time the request", ->
  121. @metrics.Timer::done.called.should.equal true
  122. it "should remove the uploaded file", ->
  123. @fs.unlink.calledWith(@path).should.equal true
  124. describe "when FileSystemImportManager.addEntity returns an error", ->
  125. beforeEach ->
  126. @FileSystemImportManager.addEntity = sinon.stub()
  127. .callsArgWith(6, new Error("Sorry something went wrong"))
  128. @ProjectUploadController.uploadFile @req, @res
  129. it "should return an unsuccessful response to the FileUploader client", ->
  130. expect(@res.body).to.deep.equal
  131. success: false
  132. it "should output an error log line", ->
  133. @logger.error
  134. .calledWith(sinon.match.any, "error uploading file")
  135. .should.equal true
  136. describe "with a bad request", ->
  137. beforeEach ->
  138. @req.files.qqfile.originalname = ""
  139. @ProjectUploadController.uploadFile @req, @res
  140. it "should return a a non success response", ->
  141. expect(@res.body).to.deep.equal
  142. success: false