ProjectUploadControllerTests.mjs 10 KB

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