ProjectUploadControllerTests.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS206: Consider reworking classes to avoid initClass
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath =
  17. '../../../../app/src/Features/Uploads/ProjectUploadController.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. const MockRequest = require('../helpers/MockRequest')
  20. const MockResponse = require('../helpers/MockResponse')
  21. const ArchiveErrors = require('../../../../app/src/Features/Uploads/ArchiveErrors')
  22. describe('ProjectUploadController', function () {
  23. beforeEach(function () {
  24. let Timer
  25. this.req = new MockRequest()
  26. this.res = new MockResponse()
  27. this.user_id = 'user-id-123'
  28. this.metrics = {
  29. Timer: (Timer = (function () {
  30. Timer = class Timer {
  31. static initClass() {
  32. this.prototype.done = sinon.stub()
  33. }
  34. }
  35. Timer.initClass()
  36. return Timer
  37. })()),
  38. }
  39. this.SessionManager = {
  40. getLoggedInUserId: sinon.stub().returns(this.user_id),
  41. }
  42. return (this.ProjectUploadController = SandboxedModule.require(modulePath, {
  43. requires: {
  44. multer: sinon.stub(),
  45. '@overleaf/settings': { path: {} },
  46. './ProjectUploadManager': (this.ProjectUploadManager = {}),
  47. './FileSystemImportManager': (this.FileSystemImportManager = {}),
  48. '@overleaf/metrics': this.metrics,
  49. '../Authentication/SessionManager': this.SessionManager,
  50. './ArchiveErrors': ArchiveErrors,
  51. fs: (this.fs = {}),
  52. },
  53. }))
  54. })
  55. describe('uploadProject', function () {
  56. beforeEach(function () {
  57. this.path = '/path/to/file/on/disk.zip'
  58. this.name = 'filename.zip'
  59. this.req.file = {
  60. path: this.path,
  61. originalname: this.name,
  62. }
  63. this.req.session = {
  64. user: {
  65. _id: this.user_id,
  66. },
  67. }
  68. this.project = { _id: (this.project_id = 'project-id-123') }
  69. return (this.fs.unlink = sinon.stub())
  70. })
  71. describe('successfully', function () {
  72. beforeEach(function () {
  73. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  74. .stub()
  75. .callsArgWith(3, null, this.project)
  76. return this.ProjectUploadController.uploadProject(this.req, this.res)
  77. })
  78. it('should create a project owned by the logged in user', function () {
  79. return this.ProjectUploadManager.createProjectFromZipArchive
  80. .calledWith(this.user_id)
  81. .should.equal(true)
  82. })
  83. it('should create a project with the same name as the zip archive', function () {
  84. return this.ProjectUploadManager.createProjectFromZipArchive
  85. .calledWith(sinon.match.any, 'filename', sinon.match.any)
  86. .should.equal(true)
  87. })
  88. it('should create a project from the zip archive', function () {
  89. return this.ProjectUploadManager.createProjectFromZipArchive
  90. .calledWith(sinon.match.any, sinon.match.any, this.path)
  91. .should.equal(true)
  92. })
  93. it('should return a successful response to the FileUploader client', function () {
  94. return expect(this.res.body).to.deep.equal(
  95. JSON.stringify({
  96. success: true,
  97. project_id: this.project_id,
  98. })
  99. )
  100. })
  101. it('should record the time taken to do the upload', function () {
  102. return this.metrics.Timer.prototype.done.called.should.equal(true)
  103. })
  104. it('should remove the uploaded file', function () {
  105. return this.fs.unlink.calledWith(this.path).should.equal(true)
  106. })
  107. })
  108. describe('when ProjectUploadManager.createProjectFromZipArchive fails', function () {
  109. beforeEach(function () {
  110. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  111. .stub()
  112. .callsArgWith(3, new Error('Something went wrong'), this.project)
  113. return this.ProjectUploadController.uploadProject(this.req, this.res)
  114. })
  115. it('should return a failed response to the FileUploader client', function () {
  116. return expect(this.res.body).to.deep.equal(
  117. JSON.stringify({ success: false, error: 'upload_failed' })
  118. )
  119. })
  120. })
  121. describe('when ProjectUploadManager.createProjectFromZipArchive reports the file as invalid', function () {
  122. beforeEach(function () {
  123. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  124. .stub()
  125. .callsArgWith(
  126. 3,
  127. new ArchiveErrors.ZipContentsTooLargeError(),
  128. this.project
  129. )
  130. return this.ProjectUploadController.uploadProject(this.req, this.res)
  131. })
  132. it('should return the reported error to the FileUploader client', function () {
  133. expect(JSON.parse(this.res.body)).to.deep.equal({
  134. success: false,
  135. error: 'zip_contents_too_large',
  136. })
  137. })
  138. it("should return an 'unprocessable entity' status code", function () {
  139. return expect(this.res.statusCode).to.equal(422)
  140. })
  141. })
  142. })
  143. describe('uploadFile', function () {
  144. beforeEach(function () {
  145. this.project_id = 'project-id-123'
  146. this.folder_id = 'folder-id-123'
  147. this.path = '/path/to/file/on/disk.png'
  148. this.name = 'filename.png'
  149. this.req.file = {
  150. path: this.path,
  151. originalname: this.name,
  152. }
  153. this.req.session = {
  154. user: {
  155. _id: this.user_id,
  156. },
  157. }
  158. this.req.params = { Project_id: this.project_id }
  159. this.req.query = { folder_id: this.folder_id }
  160. return (this.fs.unlink = sinon.stub())
  161. })
  162. describe('successfully', function () {
  163. beforeEach(function () {
  164. this.entity = {
  165. _id: '1234',
  166. type: 'file',
  167. }
  168. this.FileSystemImportManager.addEntity = sinon
  169. .stub()
  170. .callsArgWith(6, null, this.entity)
  171. return this.ProjectUploadController.uploadFile(this.req, this.res)
  172. })
  173. it('should insert the file', function () {
  174. return this.FileSystemImportManager.addEntity
  175. .calledWith(
  176. this.user_id,
  177. this.project_id,
  178. this.folder_id,
  179. this.name,
  180. this.path
  181. )
  182. .should.equal(true)
  183. })
  184. it('should return a successful response to the FileUploader client', function () {
  185. return expect(this.res.body).to.deep.equal(
  186. JSON.stringify({
  187. success: true,
  188. entity_id: this.entity._id,
  189. entity_type: 'file',
  190. })
  191. )
  192. })
  193. it('should time the request', function () {
  194. return this.metrics.Timer.prototype.done.called.should.equal(true)
  195. })
  196. it('should remove the uploaded file', function () {
  197. return this.fs.unlink.calledWith(this.path).should.equal(true)
  198. })
  199. })
  200. describe('when FileSystemImportManager.addEntity returns a generic error', function () {
  201. beforeEach(function () {
  202. this.FileSystemImportManager.addEntity = sinon
  203. .stub()
  204. .callsArgWith(6, new Error('Sorry something went wrong'))
  205. return this.ProjectUploadController.uploadFile(this.req, this.res)
  206. })
  207. it('should return an unsuccessful response to the FileUploader client', function () {
  208. return expect(this.res.body).to.deep.equal(
  209. JSON.stringify({
  210. success: false,
  211. })
  212. )
  213. })
  214. })
  215. describe('when FileSystemImportManager.addEntity returns a too many files error', function () {
  216. beforeEach(function () {
  217. this.FileSystemImportManager.addEntity = sinon
  218. .stub()
  219. .callsArgWith(6, new Error('project_has_too_many_files'))
  220. return this.ProjectUploadController.uploadFile(this.req, this.res)
  221. })
  222. it('should return an unsuccessful response to the FileUploader client', function () {
  223. return expect(this.res.body).to.deep.equal(
  224. JSON.stringify({
  225. success: false,
  226. error: 'project_has_too_many_files',
  227. })
  228. )
  229. })
  230. })
  231. describe('with an invalid filename', function () {
  232. beforeEach(function () {
  233. this.req.file.originalname = ''
  234. return this.ProjectUploadController.uploadFile(this.req, this.res)
  235. })
  236. it('should return a a non success response', function () {
  237. return expect(this.res.body).to.deep.equal(
  238. JSON.stringify({
  239. success: false,
  240. error: 'invalid_filename',
  241. })
  242. )
  243. })
  244. })
  245. })
  246. })