ProjectUploadControllerTests.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. success: true,
  96. project_id: this.project_id,
  97. })
  98. })
  99. it('should record the time taken to do the upload', function () {
  100. return this.metrics.Timer.prototype.done.called.should.equal(true)
  101. })
  102. it('should remove the uploaded file', function () {
  103. return this.fs.unlink.calledWith(this.path).should.equal(true)
  104. })
  105. })
  106. describe('when ProjectUploadManager.createProjectFromZipArchive fails', function () {
  107. beforeEach(function () {
  108. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  109. .stub()
  110. .callsArgWith(3, new Error('Something went wrong'), this.project)
  111. return this.ProjectUploadController.uploadProject(this.req, this.res)
  112. })
  113. it('should return a failed response to the FileUploader client', function () {
  114. return expect(this.res.body).to.deep.equal(
  115. JSON.stringify({ success: false, error: 'upload_failed' })
  116. )
  117. })
  118. })
  119. describe('when ProjectUploadManager.createProjectFromZipArchive reports the file as invalid', function () {
  120. beforeEach(function () {
  121. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  122. .stub()
  123. .callsArgWith(
  124. 3,
  125. new ArchiveErrors.ZipContentsTooLargeError(),
  126. this.project
  127. )
  128. return this.ProjectUploadController.uploadProject(this.req, this.res)
  129. })
  130. it('should return the reported error to the FileUploader client', function () {
  131. expect(JSON.parse(this.res.body)).to.deep.equal({
  132. success: false,
  133. error: 'zip_contents_too_large',
  134. })
  135. })
  136. it("should return an 'unprocessable entity' status code", function () {
  137. return expect(this.res.statusCode).to.equal(422)
  138. })
  139. })
  140. })
  141. describe('uploadFile', function () {
  142. beforeEach(function () {
  143. this.project_id = 'project-id-123'
  144. this.folder_id = 'folder-id-123'
  145. this.path = '/path/to/file/on/disk.png'
  146. this.name = 'filename.png'
  147. this.req.file = {
  148. path: this.path,
  149. originalname: this.name,
  150. }
  151. this.req.session = {
  152. user: {
  153. _id: this.user_id,
  154. },
  155. }
  156. this.req.params = { Project_id: this.project_id }
  157. this.req.query = { folder_id: this.folder_id }
  158. return (this.fs.unlink = sinon.stub())
  159. })
  160. describe('successfully', function () {
  161. beforeEach(function () {
  162. this.entity = {
  163. _id: '1234',
  164. type: 'file',
  165. }
  166. this.FileSystemImportManager.addEntity = sinon
  167. .stub()
  168. .callsArgWith(6, null, this.entity)
  169. return this.ProjectUploadController.uploadFile(this.req, this.res)
  170. })
  171. it('should insert the file', function () {
  172. return this.FileSystemImportManager.addEntity
  173. .calledWith(
  174. this.user_id,
  175. this.project_id,
  176. this.folder_id,
  177. this.name,
  178. this.path
  179. )
  180. .should.equal(true)
  181. })
  182. it('should return a successful response to the FileUploader client', function () {
  183. return expect(this.res.body).to.deep.equal({
  184. success: true,
  185. entity_id: this.entity._id,
  186. entity_type: 'file',
  187. })
  188. })
  189. it('should time the request', function () {
  190. return this.metrics.Timer.prototype.done.called.should.equal(true)
  191. })
  192. it('should remove the uploaded file', function () {
  193. return this.fs.unlink.calledWith(this.path).should.equal(true)
  194. })
  195. })
  196. describe('when FileSystemImportManager.addEntity returns a generic error', function () {
  197. beforeEach(function () {
  198. this.FileSystemImportManager.addEntity = sinon
  199. .stub()
  200. .callsArgWith(6, new Error('Sorry something went wrong'))
  201. return this.ProjectUploadController.uploadFile(this.req, this.res)
  202. })
  203. it('should return an unsuccessful response to the FileUploader client', function () {
  204. return expect(this.res.body).to.deep.equal({
  205. success: false,
  206. })
  207. })
  208. })
  209. describe('when FileSystemImportManager.addEntity returns a too many files error', function () {
  210. beforeEach(function () {
  211. this.FileSystemImportManager.addEntity = sinon
  212. .stub()
  213. .callsArgWith(6, new Error('project_has_too_many_files'))
  214. return this.ProjectUploadController.uploadFile(this.req, this.res)
  215. })
  216. it('should return an unsuccessful response to the FileUploader client', function () {
  217. return expect(this.res.body).to.deep.equal({
  218. success: false,
  219. error: 'project_has_too_many_files',
  220. })
  221. })
  222. })
  223. describe('with an invalid filename', function () {
  224. beforeEach(function () {
  225. this.req.file.originalname = ''
  226. return this.ProjectUploadController.uploadFile(this.req, this.res)
  227. })
  228. it('should return a a non success response', function () {
  229. return expect(this.res.body).to.deep.equal({
  230. success: false,
  231. error: 'invalid_filename',
  232. })
  233. })
  234. })
  235. })
  236. })