ProjectUploadControllerTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.file =
  31. path: @path
  32. originalname: @name
  33. @req.session =
  34. user:
  35. _id: @user_id
  36. @project =
  37. _id: @project_id = "project-id-123"
  38. @fs.unlink = sinon.stub()
  39. describe "successfully", ->
  40. beforeEach ->
  41. @ProjectUploadManager.createProjectFromZipArchive =
  42. sinon.stub().callsArgWith(3, null, @project)
  43. @ProjectUploadController.uploadProject @req, @res
  44. it "should create a project owned by the logged in user", ->
  45. @ProjectUploadManager
  46. .createProjectFromZipArchive
  47. .calledWith(@user_id)
  48. .should.equal true
  49. it "should create a project with the same name as the zip archive", ->
  50. @ProjectUploadManager
  51. .createProjectFromZipArchive
  52. .calledWith(sinon.match.any, "filename", sinon.match.any)
  53. .should.equal true
  54. it "should create a project from the zip archive", ->
  55. @ProjectUploadManager
  56. .createProjectFromZipArchive
  57. .calledWith(sinon.match.any, sinon.match.any, @path)
  58. .should.equal true
  59. it "should return a successful response to the FileUploader client", ->
  60. expect(@res.body).to.deep.equal
  61. success: true
  62. project_id: @project_id
  63. it "should record the time taken to do the upload", ->
  64. @metrics.Timer::done.called.should.equal true
  65. it "should output a log line", ->
  66. @logger.log
  67. .calledWith(sinon.match.any, "uploaded project")
  68. .should.equal true
  69. it "should remove the uploaded file", ->
  70. @fs.unlink.calledWith(@path).should.equal true
  71. describe "when ProjectUploadManager.createProjectFromZipArchive fails", ->
  72. beforeEach ->
  73. @ProjectUploadManager.createProjectFromZipArchive =
  74. sinon.stub().callsArgWith(3, new Error("Something went wrong"), @project)
  75. @ProjectUploadController.uploadProject @req, @res
  76. it "should return a failed response to the FileUploader client", ->
  77. expect(@res.body).to.deep.equal
  78. success: false
  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 "uploadFile", ->
  84. beforeEach ->
  85. @project_id = "project-id-123"
  86. @folder_id = "folder-id-123"
  87. @path = "/path/to/file/on/disk.png"
  88. @name = "filename.png"
  89. @req.file =
  90. path: @path
  91. originalname: @name
  92. @req.session =
  93. user:
  94. _id: @user_id
  95. @req.params =
  96. Project_id: @project_id
  97. @req.query =
  98. folder_id: @folder_id
  99. @fs.unlink = sinon.stub()
  100. describe "successfully", ->
  101. beforeEach ->
  102. @entity =
  103. _id : "1234"
  104. type: 'file'
  105. @FileSystemImportManager.addEntity = sinon.stub().callsArgWith(6, null, @entity)
  106. @ProjectUploadController.uploadFile @req, @res
  107. it "should insert the file", ->
  108. @FileSystemImportManager.addEntity
  109. .calledWith(@user_id, @project_id, @folder_id, @name, @path)
  110. .should.equal true
  111. it "should return a successful response to the FileUploader client", ->
  112. expect(@res.body).to.deep.equal
  113. success: true
  114. entity_id: @entity._id
  115. entity_type: 'file'
  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.file.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