ProjectUploadControllerTests.js 8.7 KB

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