ProjectUploadControllerTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. originalname: @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. originalname: @name
  91. @req.session =
  92. user:
  93. _id: @user_id
  94. @req.params =
  95. Project_id: @project_id
  96. @req.query =
  97. folder_id: @folder_id
  98. @fs.unlink = sinon.stub()
  99. describe "successfully", ->
  100. beforeEach ->
  101. @entity =
  102. _id : "1234"
  103. @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
  104. @ProjectUploadController.uploadFile @req, @res
  105. it "should insert the file", ->
  106. @FileSystemImportManager.addEntity
  107. .calledWith(@user_id, @project_id, @folder_id, @name, @path)
  108. .should.equal true
  109. it "should return a successful response to the FileUploader client", ->
  110. expect(@res.body).to.deep.equal
  111. success: true
  112. entity_id: @entity._id
  113. it "should output a log line", ->
  114. @logger.log
  115. .calledWith(sinon.match.any, "uploaded file")
  116. .should.equal true
  117. it "should time the request", ->
  118. @metrics.Timer::done.called.should.equal true
  119. it "should remove the uploaded file", ->
  120. @fs.unlink.calledWith(@path).should.equal true
  121. describe "when FileSystemImportManager.addEntity returns an error", ->
  122. beforeEach ->
  123. @FileSystemImportManager.addEntity = sinon.stub()
  124. .callsArgWith(6, new Error("Sorry something went wrong"))
  125. @ProjectUploadController.uploadFile @req, @res
  126. it "should return an unsuccessful response to the FileUploader client", ->
  127. expect(@res.body).to.deep.equal
  128. success: false
  129. it "should output an error log line", ->
  130. @logger.error
  131. .calledWith(sinon.match.any, "error uploading file")
  132. .should.equal true
  133. describe "with a bad request", ->
  134. beforeEach ->
  135. @req.files.qqfile.originalname = ""
  136. @ProjectUploadController.uploadFile @req, @res
  137. it "should return a a non success response", ->
  138. expect(@res.body).to.deep.equal
  139. success: false