ProjectUploadControllerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. this.ProjectLocator = {
  43. promises: {},
  44. }
  45. this.EditorController = {
  46. promises: {},
  47. }
  48. return (this.ProjectUploadController = SandboxedModule.require(modulePath, {
  49. requires: {
  50. multer: sinon.stub(),
  51. '@overleaf/settings': { path: {} },
  52. './ProjectUploadManager': (this.ProjectUploadManager = {}),
  53. './FileSystemImportManager': (this.FileSystemImportManager = {}),
  54. '@overleaf/metrics': this.metrics,
  55. '../Authentication/SessionManager': this.SessionManager,
  56. './ArchiveErrors': ArchiveErrors,
  57. '../Project/ProjectLocator': this.ProjectLocator,
  58. '../Editor/EditorController': this.EditorController,
  59. fs: (this.fs = {}),
  60. },
  61. }))
  62. })
  63. describe('uploadProject', function () {
  64. beforeEach(function () {
  65. this.path = '/path/to/file/on/disk.zip'
  66. this.name = 'filename.zip'
  67. this.req.file = {
  68. path: this.path,
  69. }
  70. this.req.body = {
  71. name: this.name,
  72. }
  73. this.req.session = {
  74. user: {
  75. _id: this.user_id,
  76. },
  77. }
  78. this.project = { _id: (this.project_id = 'project-id-123') }
  79. return (this.fs.unlink = sinon.stub())
  80. })
  81. describe('successfully', function () {
  82. beforeEach(function () {
  83. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  84. .stub()
  85. .callsArgWith(3, null, this.project)
  86. return this.ProjectUploadController.uploadProject(this.req, this.res)
  87. })
  88. it('should create a project owned by the logged in user', function () {
  89. return this.ProjectUploadManager.createProjectFromZipArchive
  90. .calledWith(this.user_id)
  91. .should.equal(true)
  92. })
  93. it('should create a project with the same name as the zip archive', function () {
  94. return this.ProjectUploadManager.createProjectFromZipArchive
  95. .calledWith(sinon.match.any, 'filename', sinon.match.any)
  96. .should.equal(true)
  97. })
  98. it('should create a project from the zip archive', function () {
  99. return this.ProjectUploadManager.createProjectFromZipArchive
  100. .calledWith(sinon.match.any, sinon.match.any, this.path)
  101. .should.equal(true)
  102. })
  103. it('should return a successful response to the FileUploader client', function () {
  104. return expect(this.res.body).to.deep.equal(
  105. JSON.stringify({
  106. success: true,
  107. project_id: this.project_id,
  108. })
  109. )
  110. })
  111. it('should record the time taken to do the upload', function () {
  112. return this.metrics.Timer.prototype.done.called.should.equal(true)
  113. })
  114. it('should remove the uploaded file', function () {
  115. return this.fs.unlink.calledWith(this.path).should.equal(true)
  116. })
  117. })
  118. describe('when ProjectUploadManager.createProjectFromZipArchive fails', function () {
  119. beforeEach(function () {
  120. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  121. .stub()
  122. .callsArgWith(3, new Error('Something went wrong'), this.project)
  123. return this.ProjectUploadController.uploadProject(this.req, this.res)
  124. })
  125. it('should return a failed response to the FileUploader client', function () {
  126. return expect(this.res.body).to.deep.equal(
  127. JSON.stringify({ success: false, error: 'upload_failed' })
  128. )
  129. })
  130. })
  131. describe('when ProjectUploadManager.createProjectFromZipArchive reports the file as invalid', function () {
  132. beforeEach(function () {
  133. this.ProjectUploadManager.createProjectFromZipArchive = sinon
  134. .stub()
  135. .callsArgWith(
  136. 3,
  137. new ArchiveErrors.ZipContentsTooLargeError(),
  138. this.project
  139. )
  140. return this.ProjectUploadController.uploadProject(this.req, this.res)
  141. })
  142. it('should return the reported error to the FileUploader client', function () {
  143. expect(JSON.parse(this.res.body)).to.deep.equal({
  144. success: false,
  145. error: 'zip_contents_too_large',
  146. })
  147. })
  148. it("should return an 'unprocessable entity' status code", function () {
  149. return expect(this.res.statusCode).to.equal(422)
  150. })
  151. })
  152. })
  153. describe('uploadFile', function () {
  154. beforeEach(function () {
  155. this.project_id = 'project-id-123'
  156. this.folder_id = 'folder-id-123'
  157. this.path = '/path/to/file/on/disk.png'
  158. this.name = 'filename.png'
  159. this.req.file = {
  160. path: this.path,
  161. }
  162. this.req.body = {
  163. name: this.name,
  164. }
  165. this.req.session = {
  166. user: {
  167. _id: this.user_id,
  168. },
  169. }
  170. this.req.params = { Project_id: this.project_id }
  171. this.req.query = { folder_id: this.folder_id }
  172. return (this.fs.unlink = sinon.stub())
  173. })
  174. describe('successfully', function () {
  175. beforeEach(function () {
  176. this.entity = {
  177. _id: '1234',
  178. type: 'file',
  179. }
  180. this.FileSystemImportManager.addEntity = sinon
  181. .stub()
  182. .callsArgWith(6, null, this.entity)
  183. return this.ProjectUploadController.uploadFile(this.req, this.res)
  184. })
  185. it('should insert the file', function () {
  186. return this.FileSystemImportManager.addEntity
  187. .calledWith(
  188. this.user_id,
  189. this.project_id,
  190. this.folder_id,
  191. this.name,
  192. this.path
  193. )
  194. .should.equal(true)
  195. })
  196. it('should return a successful response to the FileUploader client', function () {
  197. return expect(this.res.body).to.deep.equal(
  198. JSON.stringify({
  199. success: true,
  200. entity_id: this.entity._id,
  201. entity_type: 'file',
  202. })
  203. )
  204. })
  205. it('should time the request', function () {
  206. return this.metrics.Timer.prototype.done.called.should.equal(true)
  207. })
  208. it('should remove the uploaded file', function () {
  209. return this.fs.unlink.calledWith(this.path).should.equal(true)
  210. })
  211. })
  212. describe('with folder structure', function () {
  213. beforeEach(function (done) {
  214. this.entity = {
  215. _id: '1234',
  216. type: 'file',
  217. }
  218. this.FileSystemImportManager.addEntity = sinon
  219. .stub()
  220. .callsArgWith(6, null, this.entity)
  221. this.ProjectLocator.promises.findElement = sinon.stub().resolves({
  222. path: { fileSystem: '/test' },
  223. })
  224. this.EditorController.promises.mkdirp = sinon.stub().resolves({
  225. lastFolder: { _id: 'folder-id' },
  226. })
  227. this.req.body.relativePath = 'foo/bar/' + this.name
  228. this.res.json = data => {
  229. expect(data.success).to.be.true
  230. done()
  231. }
  232. this.ProjectUploadController.uploadFile(this.req, this.res)
  233. })
  234. it('should insert the file', function () {
  235. this.ProjectLocator.promises.findElement.should.be.calledOnceWithExactly(
  236. {
  237. project_id: this.project_id,
  238. element_id: this.folder_id,
  239. type: 'folder',
  240. }
  241. )
  242. this.EditorController.promises.mkdirp.should.be.calledWith(
  243. this.project_id,
  244. '/test/foo/bar'
  245. )
  246. this.FileSystemImportManager.addEntity.should.be.calledOnceWith(
  247. this.user_id,
  248. this.project_id,
  249. 'folder-id',
  250. this.name,
  251. this.path
  252. )
  253. })
  254. })
  255. describe('when FileSystemImportManager.addEntity returns a generic error', function () {
  256. beforeEach(function () {
  257. this.FileSystemImportManager.addEntity = sinon
  258. .stub()
  259. .callsArgWith(6, new Error('Sorry something went wrong'))
  260. return this.ProjectUploadController.uploadFile(this.req, this.res)
  261. })
  262. it('should return an unsuccessful response to the FileUploader client', function () {
  263. return expect(this.res.body).to.deep.equal(
  264. JSON.stringify({
  265. success: false,
  266. })
  267. )
  268. })
  269. })
  270. describe('when FileSystemImportManager.addEntity returns a too many files error', function () {
  271. beforeEach(function () {
  272. this.FileSystemImportManager.addEntity = sinon
  273. .stub()
  274. .callsArgWith(6, new Error('project_has_too_many_files'))
  275. return this.ProjectUploadController.uploadFile(this.req, this.res)
  276. })
  277. it('should return an unsuccessful response to the FileUploader client', function () {
  278. return expect(this.res.body).to.deep.equal(
  279. JSON.stringify({
  280. success: false,
  281. error: 'project_has_too_many_files',
  282. })
  283. )
  284. })
  285. })
  286. describe('with an invalid filename', function () {
  287. beforeEach(function () {
  288. this.req.body.name = ''
  289. return this.ProjectUploadController.uploadFile(this.req, this.res)
  290. })
  291. it('should return a a non success response', function () {
  292. return expect(this.res.body).to.deep.equal(
  293. JSON.stringify({
  294. success: false,
  295. error: 'invalid_filename',
  296. })
  297. )
  298. })
  299. })
  300. })
  301. })