ProjectEntityUpdateHandlerTests.coffee 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. chai = require('chai')
  2. assert = require('chai').assert
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/Features/Project/ProjectEntityUpdateHandler"
  6. sinon = require 'sinon'
  7. Errors = require "../../../../app/js/Features/Errors/Errors"
  8. SandboxedModule = require('sandboxed-module')
  9. ObjectId = require("mongoose").Types.ObjectId
  10. describe 'ProjectEntityUpdateHandler', ->
  11. project_id = '4eecb1c1bffa66588e0000a1'
  12. projectHistoryId = '123456'
  13. doc_id = '4eecb1c1bffa66588e0000a2'
  14. file_id = "4eecaffcbffa66588e000009"
  15. folder_id = "4eecaffcbffa66588e000008"
  16. rootFolderId = "4eecaffcbffa66588e000007"
  17. new_file_id = "4eecaffcbffa66588e000099"
  18. userId = 1234
  19. beforeEach ->
  20. @project =
  21. _id: project_id,
  22. name: 'project name'
  23. overleaf:
  24. history:
  25. id: projectHistoryId
  26. @fileUrl = 'filestore.example.com/file'
  27. @FileStoreHandler =
  28. uploadFileFromDisk: sinon.stub().yields(null, @fileUrl)
  29. copyFile: sinon.stub().yields(null, @fileUrl)
  30. @DocModel = class Doc
  31. constructor:(options)->
  32. {@name, @lines} = options
  33. @_id = doc_id
  34. @rev = 0
  35. @FileModel = class File
  36. constructor:(options)->
  37. {@name} = options
  38. # use a new id for replacement files
  39. if @name is 'dummy-upload-filename'
  40. @._id = new_file_id
  41. else
  42. @._id = file_id
  43. @rev = 0
  44. if options.linkedFileData?
  45. @linkedFileData = options.linkedFileData
  46. @docName = "doc-name"
  47. @docLines = ['1234','abc']
  48. @fileName = "something.jpg"
  49. @fileSystemPath = "somehintg"
  50. @linkedFileData = {provider: 'url'}
  51. @source = 'editor'
  52. @callback = sinon.stub()
  53. @ProjectEntityUpdateHandler = SandboxedModule.require modulePath, requires:
  54. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
  55. '../../models/Doc': Doc:@DocModel
  56. '../Docstore/DocstoreManager': @DocstoreManager = {}
  57. '../Errors/Errors': Errors
  58. '../../Features/DocumentUpdater/DocumentUpdaterHandler':@DocumentUpdaterHandler =
  59. updateProjectStructure: sinon.stub().yields()
  60. '../../models/File': File:@FileModel
  61. '../FileStore/FileStoreHandler':@FileStoreHandler
  62. "../../infrastructure/LockManager":@LockManager =
  63. runWithLock:
  64. sinon.spy((namespace, id, runner, callback) -> runner(callback))
  65. '../../models/Project': Project:@ProjectModel = {}
  66. "./ProjectGetter": @ProjectGetter = {}
  67. './ProjectLocator': @ProjectLocator = {}
  68. './ProjectUpdateHandler': @ProjectUpdater = {}
  69. './ProjectEntityHandler': @ProjectEntityHandler = {}
  70. './ProjectEntityMongoUpdateHandler': @ProjectEntityMongoUpdateHandler = {}
  71. '../ThirdPartyDataStore/TpdsUpdateSender':@TpdsUpdateSender =
  72. addFile: sinon.stub().yields()
  73. describe 'copyFileFromExistingProjectWithProject', ->
  74. beforeEach ->
  75. @oldProject_id = "123kljadas"
  76. @oldFileRef = {name:@fileName, _id:"oldFileRef"}
  77. @ProjectEntityMongoUpdateHandler._confirmFolder = sinon.stub().yields(folder_id)
  78. @ProjectEntityMongoUpdateHandler._putElement = sinon.stub().yields(null, {path:{fileSystem: @fileSystemPath}})
  79. @ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject @project, folder_id, @oldProject_id, @oldFileRef, userId, @callback
  80. it 'should copy the file in FileStoreHandler', ->
  81. @FileStoreHandler.copyFile
  82. .calledWith(@oldProject_id, @oldFileRef._id, project_id, file_id)
  83. .should.equal true
  84. it 'should put file into folder by calling put element', ->
  85. @ProjectEntityMongoUpdateHandler._putElement
  86. .calledWithMatch(@project, folder_id, { _id: file_id, name: @fileName }, "file")
  87. .should.equal true
  88. it 'should return doc and parent folder', ->
  89. @callback.calledWithMatch(null,{ _id: file_id, name: @fileName }, folder_id).should.equal true
  90. it 'should call third party data store if versioning is enabled', ->
  91. @TpdsUpdateSender.addFile.calledWith(
  92. project_id: project_id
  93. file_id: file_id
  94. path: @fileSystemPath
  95. rev: 0
  96. project_name: @project.name
  97. ).should.equal true
  98. it "should should send the change in project structure to the doc updater", ->
  99. changesMatcher = sinon.match (changes) =>
  100. { newFiles } = changes
  101. return false if newFiles.length != 1
  102. newFile = newFiles[0]
  103. newFile.file._id == file_id &&
  104. newFile.path == @fileSystemPath &&
  105. newFile.url == @fileUrl
  106. @DocumentUpdaterHandler.updateProjectStructure
  107. .calledWithMatch(project_id, projectHistoryId, userId, changesMatcher)
  108. .should.equal true
  109. describe 'copyFileFromExistingProjectWithProject, with linkedFileData', ->
  110. beforeEach ->
  111. @oldProject_id = "123kljadas"
  112. @oldFileRef = {
  113. _id:"oldFileRef",
  114. name:@fileName,
  115. linkedFileData: @linkedFileData
  116. }
  117. @ProjectEntityMongoUpdateHandler._confirmFolder = sinon.stub().yields(folder_id)
  118. @ProjectEntityMongoUpdateHandler._putElement = sinon.stub().yields(null, {path:{fileSystem: @fileSystemPath}})
  119. @ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject @project, folder_id, @oldProject_id, @oldFileRef, userId, @callback
  120. it 'should copy the file in FileStoreHandler', ->
  121. @FileStoreHandler.copyFile
  122. .calledWith(@oldProject_id, @oldFileRef._id, project_id, file_id)
  123. .should.equal true
  124. it 'should put file into folder by calling put element, with the linkedFileData', ->
  125. @ProjectEntityMongoUpdateHandler._putElement
  126. .calledWithMatch(
  127. @project,
  128. folder_id,
  129. { _id: file_id, name: @fileName, linkedFileData: @linkedFileData},
  130. "file"
  131. )
  132. .should.equal true
  133. describe 'updateDocLines', ->
  134. beforeEach ->
  135. @path = "/somewhere/something.tex"
  136. @doc = {
  137. _id: doc_id
  138. }
  139. @version = 42
  140. @ranges = {"mock":"ranges"}
  141. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields(null, @project)
  142. @ProjectLocator.findElement = sinon.stub().yields(null, @doc, {fileSystem: @path})
  143. @TpdsUpdateSender.addDoc = sinon.stub().yields()
  144. @ProjectUpdater.markAsUpdated = sinon.stub()
  145. @callback = sinon.stub()
  146. describe "when the doc has been modified", ->
  147. beforeEach ->
  148. @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
  149. @ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
  150. it "should get the project without doc lines", ->
  151. @ProjectGetter.getProjectWithoutDocLines
  152. .calledWith(project_id)
  153. .should.equal true
  154. it "should find the doc", ->
  155. @ProjectLocator.findElement
  156. .calledWith({
  157. project: @project
  158. type: "docs"
  159. element_id: doc_id
  160. })
  161. .should.equal true
  162. it "should update the doc in the docstore", ->
  163. @DocstoreManager.updateDoc
  164. .calledWith(project_id, doc_id, @docLines, @version, @ranges)
  165. .should.equal true
  166. it "should mark the project as updated", ->
  167. @ProjectUpdater.markAsUpdated
  168. .calledWith(project_id)
  169. .should.equal true
  170. it "should send the doc the to the TPDS", ->
  171. @TpdsUpdateSender.addDoc
  172. .calledWith({
  173. project_id: project_id
  174. project_name: @project.name
  175. doc_id: doc_id
  176. rev: @rev
  177. path: @path
  178. })
  179. .should.equal true
  180. it "should call the callback", ->
  181. @callback.called.should.equal true
  182. describe "when the doc has not been modified", ->
  183. beforeEach ->
  184. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  185. @ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
  186. it "should not mark the project as updated", ->
  187. @ProjectUpdater.markAsUpdated.called.should.equal false
  188. it "should not send the doc the to the TPDS", ->
  189. @TpdsUpdateSender.addDoc.called.should.equal false
  190. it "should call the callback", ->
  191. @callback.called.should.equal true
  192. describe "when the doc has been deleted", ->
  193. beforeEach ->
  194. @project.deletedDocs = [ _id: doc_id ]
  195. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields(null, @project)
  196. @ProjectLocator.findElement = sinon.stub().yields(new Errors.NotFoundError)
  197. @DocstoreManager.updateDoc = sinon.stub().yields()
  198. @ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
  199. it "should update the doc in the docstore", ->
  200. @DocstoreManager.updateDoc
  201. .calledWith(project_id, doc_id, @docLines, @version, @ranges)
  202. .should.equal true
  203. it "should not mark the project as updated", ->
  204. @ProjectUpdater.markAsUpdated.called.should.equal false
  205. it "should not send the doc the to the TPDS", ->
  206. @TpdsUpdateSender.addDoc.called.should.equal false
  207. it "should call the callback", ->
  208. @callback.called.should.equal true
  209. describe "when the doc is not related to the project", ->
  210. beforeEach ->
  211. @ProjectLocator.findElement = sinon.stub().yields()
  212. @ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
  213. it "should log out the error", ->
  214. @logger.error
  215. .calledWith(
  216. project_id: project_id
  217. doc_id: doc_id
  218. lines: @docLines
  219. "doc not found while updating doc lines"
  220. )
  221. .should.equal true
  222. it "should return a not found error", ->
  223. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  224. describe "when the project is not found", ->
  225. beforeEach ->
  226. @ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields()
  227. @ProjectEntityUpdateHandler.updateDocLines project_id, doc_id, @docLines, @version, @ranges, @callback
  228. it "should return a not found error", ->
  229. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  230. describe "setRootDoc", ->
  231. it "should call Project.update", ->
  232. rootDoc_id = "root-doc-id-123123"
  233. @ProjectModel.update = sinon.stub()
  234. @ProjectEntityUpdateHandler.setRootDoc project_id, rootDoc_id
  235. @ProjectModel.update
  236. .calledWith({_id : project_id}, {rootDoc_id})
  237. .should.equal true
  238. describe "unsetRootDoc", ->
  239. it "should call Project.update", ->
  240. @ProjectModel.update = sinon.stub()
  241. @ProjectEntityUpdateHandler.unsetRootDoc project_id
  242. @ProjectModel.update
  243. .calledWith({_id : project_id}, {$unset : {rootDoc_id: true}})
  244. .should.equal true
  245. describe 'addDoc', ->
  246. describe 'adding a doc', ->
  247. beforeEach ->
  248. @path = "/path/to/doc"
  249. @newDoc = _id: doc_id
  250. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory =
  251. withoutLock: sinon.stub().yields(null, @newDoc, folder_id, @path, @project)
  252. @ProjectEntityUpdateHandler.addDoc project_id, folder_id, @docName, @docLines, userId, @callback
  253. it "creates the doc without history", () ->
  254. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory.withoutLock
  255. .calledWith(project_id, folder_id, @docName, @docLines, userId)
  256. .should.equal true
  257. it "sends the change in project structure to the doc updater", () ->
  258. newDocs = [
  259. doc: @newDoc
  260. path: @path
  261. docLines: @docLines.join('\n')
  262. ]
  263. @DocumentUpdaterHandler.updateProjectStructure
  264. .calledWith(project_id, projectHistoryId, userId, {newDocs})
  265. .should.equal true
  266. describe 'adding a doc with an invalid name', ->
  267. beforeEach ->
  268. @path = "/path/to/doc"
  269. @newDoc = _id: doc_id
  270. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory =
  271. withoutLock: sinon.stub().yields(null, @newDoc, folder_id, @path, @project)
  272. @ProjectEntityUpdateHandler.addDoc project_id, folder_id, "*" + @docName, @docLines, userId, @callback
  273. it 'returns an error', ->
  274. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  275. @callback.calledWithMatch(errorMatcher)
  276. .should.equal true
  277. describe 'addFile', ->
  278. describe 'adding a file', ->
  279. beforeEach ->
  280. @path = "/path/to/file"
  281. @newFile = {_id: file_id, rev: 0, name: @fileName, linkedFileData: @linkedFileData}
  282. @TpdsUpdateSender.addFile = sinon.stub().yields()
  283. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  284. @ProjectEntityUpdateHandler.addFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  285. it "updates the file in the filestore", () ->
  286. @FileStoreHandler.uploadFileFromDisk
  287. .calledWith(project_id, file_id, @fileSystemPath)
  288. .should.equal true
  289. it "updates the file in mongo", () ->
  290. fileMatcher = sinon.match (file) =>
  291. file.name == @fileName
  292. @ProjectEntityMongoUpdateHandler.addFile
  293. .calledWithMatch(project_id, folder_id, fileMatcher)
  294. .should.equal true
  295. it "notifies the tpds", () ->
  296. @TpdsUpdateSender.addFile
  297. .calledWith({
  298. project_id: project_id
  299. project_name: @project.name
  300. file_id: file_id
  301. rev: 0
  302. path: @path
  303. })
  304. .should.equal true
  305. it "sends the change in project structure to the doc updater", () ->
  306. newFiles = [
  307. file: @newFile
  308. path: @path
  309. url: @fileUrl
  310. ]
  311. @DocumentUpdaterHandler.updateProjectStructure
  312. .calledWith(project_id, projectHistoryId, userId, {newFiles})
  313. .should.equal true
  314. describe 'adding a file with an invalid name', ->
  315. beforeEach ->
  316. @path = "/path/to/file"
  317. @newFile = {_id: file_id, rev: 0, name: @fileName, linkedFileData: @linkedFileData}
  318. @TpdsUpdateSender.addFile = sinon.stub().yields()
  319. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  320. @ProjectEntityUpdateHandler.addFile project_id, folder_id, "*" + @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  321. it 'returns an error', ->
  322. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  323. @callback.calledWithMatch(errorMatcher)
  324. .should.equal true
  325. describe 'replaceFile', ->
  326. beforeEach ->
  327. # replacement file now creates a new file object
  328. @newFileUrl = "new-file-url"
  329. @FileStoreHandler.uploadFileFromDisk = sinon.stub().yields(null, @newFileUrl)
  330. @newFile = _id: new_file_id, name: "dummy-upload-filename", rev: 0, linkedFileData: @linkedFileData
  331. @oldFile = _id: file_id
  332. @path = "/path/to/file"
  333. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  334. @ProjectEntityMongoUpdateHandler.replaceFileWithNew = sinon.stub().yields(null, @oldFile, @project, fileSystem: @path)
  335. @ProjectEntityUpdateHandler.replaceFile project_id, file_id, @fileSystemPath, @linkedFileData, userId, @callback
  336. it 'uploads a new version of the file', ->
  337. @FileStoreHandler.uploadFileFromDisk
  338. .calledWith(project_id, new_file_id, @fileSystemPath)
  339. .should.equal true
  340. it 'replaces the file in mongo', ->
  341. @ProjectEntityMongoUpdateHandler.replaceFileWithNew
  342. .calledWith(project_id, file_id, @newFile)
  343. .should.equal true
  344. it 'notifies the tpds', ->
  345. @TpdsUpdateSender.addFile
  346. .calledWith({
  347. project_id: project_id
  348. project_name: @project.name
  349. file_id: new_file_id
  350. rev: @newFile.rev + 1
  351. path: @path
  352. })
  353. .should.equal true
  354. it 'updates the project structure in the doc updater', ->
  355. oldFiles = [
  356. file: @oldFile
  357. path: @path
  358. ]
  359. newFiles = [
  360. file: @newFile
  361. path: @path
  362. url: @newFileUrl
  363. ]
  364. @DocumentUpdaterHandler.updateProjectStructure
  365. .calledWith(project_id, projectHistoryId, userId, {oldFiles, newFiles})
  366. .should.equal true
  367. describe 'addDocWithoutUpdatingHistory', ->
  368. describe 'adding a doc', ->
  369. beforeEach ->
  370. @path = "/path/to/doc"
  371. @project = _id: project_id, name: 'some project'
  372. @TpdsUpdateSender.addDoc = sinon.stub().yields()
  373. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  374. @ProjectEntityMongoUpdateHandler.addDoc = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  375. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory project_id, folder_id, @docName, @docLines, userId, @callback
  376. it "updates the doc in the docstore", () ->
  377. @DocstoreManager.updateDoc
  378. .calledWith(project_id, doc_id, @docLines, 0, {})
  379. .should.equal true
  380. it "updates the doc in mongo", () ->
  381. docMatcher = sinon.match (doc) =>
  382. doc.name == @docName
  383. @ProjectEntityMongoUpdateHandler.addDoc
  384. .calledWithMatch(project_id, folder_id, docMatcher)
  385. .should.equal true
  386. it "notifies the tpds", () ->
  387. @TpdsUpdateSender.addDoc
  388. .calledWith({
  389. project_id: project_id
  390. project_name: @project.name
  391. doc_id: doc_id
  392. rev: 0
  393. path: @path
  394. })
  395. .should.equal true
  396. it "should not should send the change in project structure to the doc updater", () ->
  397. @DocumentUpdaterHandler.updateProjectStructure
  398. .called
  399. .should.equal false
  400. describe 'adding a doc with an invalid name', ->
  401. beforeEach ->
  402. @path = "/path/to/doc"
  403. @project = _id: project_id, name: 'some project'
  404. @TpdsUpdateSender.addDoc = sinon.stub().yields()
  405. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  406. @ProjectEntityMongoUpdateHandler.addDoc = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  407. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory project_id, folder_id, "*" + @docName, @docLines, userId, @callback
  408. it 'returns an error', ->
  409. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  410. @callback.calledWithMatch(errorMatcher)
  411. .should.equal true
  412. describe 'addFileWithoutUpdatingHistory', ->
  413. describe 'adding a file', ->
  414. beforeEach ->
  415. @path = "/path/to/file"
  416. @project = _id: project_id, name: 'some project'
  417. @TpdsUpdateSender.addFile = sinon.stub().yields()
  418. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  419. @ProjectEntityUpdateHandler.addFileWithoutUpdatingHistory project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  420. it "updates the file in the filestore", () ->
  421. @FileStoreHandler.uploadFileFromDisk
  422. .calledWith(project_id, file_id, @fileSystemPath)
  423. .should.equal true
  424. it "updates the file in mongo", () ->
  425. fileMatcher = sinon.match (file) =>
  426. file.name == @fileName
  427. @ProjectEntityMongoUpdateHandler.addFile
  428. .calledWithMatch(project_id, folder_id, fileMatcher)
  429. .should.equal true
  430. it "notifies the tpds", () ->
  431. @TpdsUpdateSender.addFile
  432. .calledWith({
  433. project_id: project_id
  434. project_name: @project.name
  435. file_id: file_id
  436. rev: 0
  437. path: @path
  438. })
  439. .should.equal true
  440. it "should not should send the change in project structure to the doc updater", () ->
  441. @DocumentUpdaterHandler.updateProjectStructure
  442. .called
  443. .should.equal false
  444. describe 'adding a file with an invalid name', ->
  445. beforeEach ->
  446. @path = "/path/to/file"
  447. @project = _id: project_id, name: 'some project'
  448. @TpdsUpdateSender.addFile = sinon.stub().yields()
  449. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  450. @ProjectEntityUpdateHandler.addFileWithoutUpdatingHistory project_id, folder_id, "*" + @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  451. it 'returns an error', ->
  452. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  453. @callback.calledWithMatch(errorMatcher)
  454. .should.equal true
  455. describe 'upsertDoc', ->
  456. describe 'upserting into an invalid folder', ->
  457. beforeEach ->
  458. @ProjectLocator.findElement = sinon.stub().yields()
  459. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  460. it 'returns an error', ->
  461. errorMatcher = sinon.match.instanceOf(Error)
  462. @callback.calledWithMatch(errorMatcher)
  463. .should.equal true
  464. describe 'updating an existing doc', ->
  465. beforeEach ->
  466. @existingDoc = _id: doc_id, name: @docName
  467. @folder = _id: folder_id, docs: [@existingDoc]
  468. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  469. @DocumentUpdaterHandler.setDocument = sinon.stub().yields()
  470. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().yields()
  471. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  472. it 'tries to find the folder', ->
  473. @ProjectLocator.findElement
  474. .calledWith({project_id, element_id: folder_id, type: "folder"})
  475. .should.equal true
  476. it 'updates the doc contents', ->
  477. @DocumentUpdaterHandler.setDocument
  478. .calledWith(project_id, @existingDoc._id, userId, @docLines, @source)
  479. .should.equal true
  480. it 'flushes the doc contents', ->
  481. @DocumentUpdaterHandler.flushDocToMongo
  482. .calledWith(project_id, @existingDoc._id )
  483. .should.equal true
  484. it 'returns the doc', ->
  485. @callback.calledWith(null, @existingDoc, false)
  486. describe 'creating a new doc', ->
  487. beforeEach ->
  488. @folder = _id: folder_id, docs: []
  489. @newDoc = _id: doc_id
  490. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  491. @ProjectEntityUpdateHandler.addDoc = withoutLock: sinon.stub().yields(null, @newDoc)
  492. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  493. it 'tries to find the folder', ->
  494. @ProjectLocator.findElement
  495. .calledWith({project_id, element_id: folder_id, type: "folder"})
  496. .should.equal true
  497. it 'adds the doc', ->
  498. @ProjectEntityUpdateHandler.addDoc.withoutLock
  499. .calledWith(project_id, folder_id, @docName, @docLines, userId)
  500. .should.equal true
  501. it 'returns the doc', ->
  502. @callback.calledWith(null, @newDoc, true)
  503. describe 'upserting a new doc with an invalid name', ->
  504. beforeEach ->
  505. @folder = _id: folder_id, docs: []
  506. @newDoc = _id: doc_id
  507. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  508. @ProjectEntityUpdateHandler.addDoc = withoutLock: sinon.stub().yields(null, @newDoc)
  509. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, "*" + @docName, @docLines, @source, userId, @callback
  510. it 'returns an error', ->
  511. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  512. @callback.calledWithMatch(errorMatcher)
  513. .should.equal true
  514. describe 'upsertFile', ->
  515. describe 'upserting into an invalid folder', ->
  516. beforeEach ->
  517. @ProjectLocator.findElement = sinon.stub().yields()
  518. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  519. it 'returns an error', ->
  520. errorMatcher = sinon.match.instanceOf(Error)
  521. @callback.calledWithMatch(errorMatcher)
  522. .should.equal true
  523. describe 'updating an existing file', ->
  524. beforeEach ->
  525. @existingFile = _id: file_id, name: @fileName
  526. @folder = _id: folder_id, fileRefs: [@existingFile]
  527. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  528. @ProjectEntityUpdateHandler.replaceFile = mainTask: sinon.stub().yields(null, @newFile)
  529. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  530. it 'replaces the file', ->
  531. @ProjectEntityUpdateHandler.replaceFile.mainTask
  532. .calledWith(project_id, file_id, @fileSystemPath, @linkedFileData, userId)
  533. .should.equal true
  534. it 'returns the file', ->
  535. @callback.calledWith(null, @existingFile, false)
  536. describe 'creating a new file', ->
  537. beforeEach ->
  538. @folder = _id: folder_id, fileRefs: []
  539. @newFile = _id: file_id
  540. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  541. @ProjectEntityUpdateHandler.addFile = mainTask: sinon.stub().yields(null, @newFile)
  542. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  543. it 'tries to find the folder', ->
  544. @ProjectLocator.findElement
  545. .calledWith({project_id, element_id: folder_id, type: "folder"})
  546. .should.equal true
  547. it 'adds the file', ->
  548. @ProjectEntityUpdateHandler.addFile.mainTask
  549. .calledWith(project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId)
  550. .should.equal true
  551. it 'returns the file', ->
  552. @callback.calledWith(null, @newFile, true)
  553. describe 'upserting a new file with an invalid name', ->
  554. beforeEach ->
  555. @folder = _id: folder_id, fileRefs: []
  556. @newFile = _id: file_id
  557. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  558. @ProjectEntityUpdateHandler.addFile = mainTask: sinon.stub().yields(null, @newFile)
  559. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, '*' + @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  560. it 'returns an error', ->
  561. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  562. @callback.calledWithMatch(errorMatcher)
  563. .should.equal true
  564. describe 'upsertDocWithPath', ->
  565. describe 'upserting a doc', ->
  566. beforeEach ->
  567. @path = "/folder/doc.tex"
  568. @newFolders = [ 'mock-a', 'mock-b' ]
  569. @folder = _id: folder_id
  570. @doc = _id: doc_id
  571. @isNewDoc = true
  572. @ProjectEntityUpdateHandler.mkdirp =
  573. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  574. @ProjectEntityUpdateHandler.upsertDoc =
  575. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  576. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  577. it 'creates any necessary folders', ->
  578. @ProjectEntityUpdateHandler.mkdirp.withoutLock
  579. .calledWith(project_id, '/folder')
  580. .should.equal true
  581. it 'upserts the doc', ->
  582. @ProjectEntityUpdateHandler.upsertDoc.withoutLock
  583. .calledWith(project_id, @folder._id, 'doc.tex', @docLines, @source, userId)
  584. .should.equal true
  585. it 'calls the callback', ->
  586. @callback
  587. .calledWith(null, @doc, @isNewDoc, @newFolders, @folder)
  588. .should.equal true
  589. describe 'upserting a doc with an invalid path', ->
  590. beforeEach ->
  591. @path = "/*folder/doc.tex"
  592. @newFolders = [ 'mock-a', 'mock-b' ]
  593. @folder = _id: folder_id
  594. @doc = _id: doc_id
  595. @isNewDoc = true
  596. @ProjectEntityUpdateHandler.mkdirp =
  597. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  598. @ProjectEntityUpdateHandler.upsertDoc =
  599. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  600. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  601. it 'returns an error', ->
  602. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  603. @callback.calledWithMatch(errorMatcher)
  604. .should.equal true
  605. describe 'upserting a doc with an invalid name', ->
  606. beforeEach ->
  607. @path = "/folder/*doc.tex"
  608. @newFolders = [ 'mock-a', 'mock-b' ]
  609. @folder = _id: folder_id
  610. @doc = _id: doc_id
  611. @isNewDoc = true
  612. @ProjectEntityUpdateHandler.mkdirp =
  613. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  614. @ProjectEntityUpdateHandler.upsertDoc =
  615. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  616. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  617. it 'returns an error', ->
  618. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  619. @callback.calledWithMatch(errorMatcher)
  620. .should.equal true
  621. describe 'upsertFileWithPath', ->
  622. describe 'upserting a file', ->
  623. beforeEach ->
  624. @path = "/folder/file.png"
  625. @newFolders = [ 'mock-a', 'mock-b' ]
  626. @folder = _id: folder_id
  627. @file = _id: file_id
  628. @isNewFile = true
  629. @ProjectEntityUpdateHandler.mkdirp =
  630. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  631. @ProjectEntityUpdateHandler.upsertFile =
  632. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  633. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  634. it 'creates any necessary folders', ->
  635. @ProjectEntityUpdateHandler.mkdirp.withoutLock
  636. .calledWith(project_id, '/folder')
  637. .should.equal true
  638. it 'upserts the file', ->
  639. @ProjectEntityUpdateHandler.upsertFile.mainTask
  640. .calledWith(project_id, @folder._id, 'file.png', @fileSystemPath, @linkedFileData, userId)
  641. .should.equal true
  642. it 'calls the callback', ->
  643. @callback
  644. .calledWith(null, @file, @isNewFile, undefined, @newFolders, @folder)
  645. .should.equal true
  646. describe 'upserting a file with an invalid path', ->
  647. beforeEach ->
  648. @path = "/*folder/file.png"
  649. @newFolders = [ 'mock-a', 'mock-b' ]
  650. @folder = _id: folder_id
  651. @file = _id: file_id
  652. @isNewFile = true
  653. @ProjectEntityUpdateHandler.mkdirp =
  654. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  655. @ProjectEntityUpdateHandler.upsertFile =
  656. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  657. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  658. it 'returns an error', ->
  659. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  660. @callback.calledWithMatch(errorMatcher)
  661. .should.equal true
  662. describe 'upserting a file with an invalid name', ->
  663. beforeEach ->
  664. @path = "/folder/*file.png"
  665. @newFolders = [ 'mock-a', 'mock-b' ]
  666. @folder = _id: folder_id
  667. @file = _id: file_id
  668. @isNewFile = true
  669. @ProjectEntityUpdateHandler.mkdirp =
  670. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  671. @ProjectEntityUpdateHandler.upsertFile =
  672. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  673. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  674. it 'returns an error', ->
  675. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  676. @callback.calledWithMatch(errorMatcher)
  677. .should.equal true
  678. describe 'deleteEntity', ->
  679. beforeEach ->
  680. @path = '/path/to/doc.tex'
  681. @doc = _id: doc_id
  682. @projectBeforeDeletion = _id: project_id, name: 'project'
  683. @ProjectEntityMongoUpdateHandler.deleteEntity = sinon.stub().yields(null, @doc, {fileSystem: @path}, @projectBeforeDeletion)
  684. @ProjectEntityUpdateHandler._cleanUpEntity = sinon.stub().yields()
  685. @TpdsUpdateSender.deleteEntity = sinon.stub().yields()
  686. @ProjectEntityUpdateHandler.deleteEntity project_id, doc_id, 'doc', userId, @callback
  687. it 'deletes the entity in mongo', ->
  688. @ProjectEntityMongoUpdateHandler.deleteEntity
  689. .calledWith(project_id, doc_id, 'doc')
  690. .should.equal true
  691. it 'cleans up the doc in the docstore', ->
  692. @ProjectEntityUpdateHandler._cleanUpEntity
  693. .calledWith(@projectBeforeDeletion, @doc, 'doc', @path, userId)
  694. .should.equal true
  695. it 'it notifies the tpds', ->
  696. @TpdsUpdateSender.deleteEntity
  697. .calledWith({ project_id, @path, project_name: @projectBeforeDeletion.name })
  698. .should.equal true
  699. it 'retuns the entity_id', ->
  700. @callback.calledWith(null, doc_id).should.equal true
  701. describe 'deleteEntityWithPath', ->
  702. describe 'when the entity exists', ->
  703. beforeEach ->
  704. @doc = _id: doc_id
  705. @ProjectLocator.findElementByPath = sinon.stub().yields(null, @doc, 'doc')
  706. @ProjectEntityUpdateHandler.deleteEntity =
  707. withoutLock: sinon.stub().yields()
  708. @path = '/path/to/doc.tex'
  709. @ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
  710. it 'finds the entity', ->
  711. @ProjectLocator.findElementByPath
  712. .calledWith({project_id, @path})
  713. .should.equal true
  714. it 'deletes the entity', ->
  715. @ProjectEntityUpdateHandler.deleteEntity.withoutLock
  716. .calledWith(project_id, @doc._id, 'doc', userId, @callback)
  717. .should.equal true
  718. describe 'when the entity does not exist', ->
  719. beforeEach ->
  720. @ProjectLocator.findElementByPath = sinon.stub().yields()
  721. @path = '/doc.tex'
  722. @ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
  723. it 'returns an error', ->
  724. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  725. describe 'mkdirp', ->
  726. beforeEach ->
  727. @docPath = '/folder/doc.tex'
  728. @ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
  729. @ProjectEntityUpdateHandler.mkdirp project_id, @docPath, @callback
  730. it 'calls ProjectEntityMongoUpdateHandler', ->
  731. @ProjectEntityMongoUpdateHandler.mkdirp
  732. .calledWith(project_id, @docPath)
  733. .should.equal true
  734. describe 'addFolder', ->
  735. describe 'adding a folder', ->
  736. beforeEach ->
  737. @parentFolder_id = '123asdf'
  738. @folderName = 'new-folder'
  739. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  740. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  741. it 'calls ProjectEntityMongoUpdateHandler', ->
  742. @ProjectEntityMongoUpdateHandler.addFolder
  743. .calledWith(project_id, @parentFolder_id, @folderName)
  744. .should.equal true
  745. describe 'adding a folder with an invalid name', ->
  746. beforeEach ->
  747. @parentFolder_id = '123asdf'
  748. @folderName = '*new-folder'
  749. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  750. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  751. it 'returns an error', ->
  752. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  753. @callback.calledWithMatch(errorMatcher)
  754. .should.equal true
  755. describe 'moveEntity', ->
  756. beforeEach ->
  757. @project_name = 'project name'
  758. @startPath = '/a.tex'
  759. @endPath = '/folder/b.tex'
  760. @rev = 2
  761. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  762. @ProjectEntityMongoUpdateHandler.moveEntity = sinon.stub().yields(
  763. null, @project, @startPath, @endPath, @rev, @changes
  764. )
  765. @TpdsUpdateSender.moveEntity = sinon.stub()
  766. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  767. @ProjectEntityUpdateHandler.moveEntity project_id, doc_id, folder_id, 'doc', userId, @callback
  768. it 'moves the entity in mongo', ->
  769. @ProjectEntityMongoUpdateHandler.moveEntity
  770. .calledWith(project_id, doc_id, folder_id, 'doc')
  771. .should.equal true
  772. it 'notifies tpds', ->
  773. @TpdsUpdateSender.moveEntity
  774. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  775. .should.equal true
  776. it 'sends the changes in project structure to the doc updater', ->
  777. @DocumentUpdaterHandler.updateProjectStructure
  778. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  779. .should.equal true
  780. describe "renameEntity", ->
  781. describe 'renaming an entity', ->
  782. beforeEach ->
  783. @project_name = 'project name'
  784. @startPath = '/folder/a.tex'
  785. @endPath = '/folder/b.tex'
  786. @rev = 2
  787. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  788. @newDocName = 'b.tex'
  789. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  790. null, @project, @startPath, @endPath, @rev, @changes
  791. )
  792. @TpdsUpdateSender.moveEntity = sinon.stub()
  793. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  794. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  795. it 'moves the entity in mongo', ->
  796. @ProjectEntityMongoUpdateHandler.renameEntity
  797. .calledWith(project_id, doc_id, 'doc', @newDocName)
  798. .should.equal true
  799. it 'notifies tpds', ->
  800. @TpdsUpdateSender.moveEntity
  801. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  802. .should.equal true
  803. it 'sends the changes in project structure to the doc updater', ->
  804. @DocumentUpdaterHandler.updateProjectStructure
  805. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  806. .should.equal true
  807. describe 'renaming an entity to an invalid name', ->
  808. beforeEach ->
  809. @project_name = 'project name'
  810. @startPath = '/folder/a.tex'
  811. @endPath = '/folder/b.tex'
  812. @rev = 2
  813. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  814. @newDocName = '*b.tex'
  815. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  816. null, @project, @startPath, @endPath, @rev, @changes
  817. )
  818. @TpdsUpdateSender.moveEntity = sinon.stub()
  819. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  820. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  821. it 'returns an error', ->
  822. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  823. @callback.calledWithMatch(errorMatcher)
  824. .should.equal true
  825. describe "resyncProjectHistory", ->
  826. describe "a deleted project", ->
  827. beforeEach ->
  828. @ProjectGetter.getProject = sinon.stub().yields()
  829. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  830. it "should return an error", ->
  831. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  832. @callback.calledWith(error).should.equal true
  833. describe "a project without project-history enabled", ->
  834. beforeEach ->
  835. @project.overleaf = {}
  836. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  837. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  838. it "should return an error", ->
  839. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  840. @callback.calledWith(error).should.equal true
  841. describe "a project with project-history enabled", ->
  842. beforeEach ->
  843. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  844. docs = [
  845. doc: _id: doc_id
  846. path: 'main.tex'
  847. ]
  848. files = [
  849. file: _id: file_id
  850. path: 'universe.png'
  851. ]
  852. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().yields(null, docs, files)
  853. @FileStoreHandler._buildUrl = (project_id, file_id) ->
  854. "www.filestore.test/#{project_id}/#{file_id}"
  855. @DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
  856. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  857. it 'gets the project', ->
  858. @ProjectGetter.getProject
  859. .calledWith(project_id)
  860. .should.equal true
  861. it 'gets the entities for the project', ->
  862. @ProjectEntityHandler.getAllEntitiesFromProject
  863. .calledWith(@project)
  864. .should.equal true
  865. it 'tells the doc updater to sync the project', ->
  866. docs = [
  867. doc: doc_id
  868. path: 'main.tex'
  869. ]
  870. files = [
  871. file: file_id
  872. path: 'universe.png'
  873. url: "www.filestore.test/#{project_id}/#{file_id}"
  874. ]
  875. @DocumentUpdaterHandler.resyncProjectHistory
  876. .calledWith(project_id, projectHistoryId, docs, files)
  877. .should.equal true
  878. it 'calls the callback', ->
  879. @callback.called.should.equal true
  880. describe "_cleanUpEntity", ->
  881. beforeEach ->
  882. @entity_id = "4eecaffcbffa66588e000009"
  883. @FileStoreHandler.deleteFile = sinon.stub().yields()
  884. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  885. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  886. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  887. describe "a file", ->
  888. beforeEach (done) ->
  889. @path = "/file/system/path.png"
  890. @entity = _id: @entity_id
  891. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'file', @path, userId, done
  892. it "should insert the file into the deletedFiles array", ->
  893. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference
  894. .calledWith(@project._id, @entity)
  895. .should.equal true
  896. it "should not delete the file from FileStoreHandler", ->
  897. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal false
  898. it "should not attempt to delete from the document updater", ->
  899. @DocumentUpdaterHandler.deleteDoc.called.should.equal false
  900. it "should should send the update to the doc updater", ->
  901. oldFiles = [ file: @entity, path: @path ]
  902. @DocumentUpdaterHandler.updateProjectStructure
  903. .calledWith(project_id, projectHistoryId, userId, {oldFiles})
  904. .should.equal true
  905. describe "a doc", ->
  906. beforeEach (done) ->
  907. @path = "/file/system/path.tex"
  908. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  909. @entity = {_id: @entity_id}
  910. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'doc', @path, userId, done
  911. it "should clean up the doc", ->
  912. @ProjectEntityUpdateHandler._cleanUpDoc
  913. .calledWith(@project, @entity, @path, userId)
  914. .should.equal true
  915. it "should should send the update to the doc updater", ->
  916. oldDocs = [ doc: @entity, path: @path ]
  917. @DocumentUpdaterHandler.updateProjectStructure
  918. .calledWith(project_id, projectHistoryId, userId, {oldDocs})
  919. .should.equal true
  920. describe "a folder", ->
  921. beforeEach (done) ->
  922. @folder =
  923. folders: [
  924. name: "subfolder"
  925. fileRefs: [ @file1 = { _id: "file-id-1", name: "file-name-1"} ]
  926. docs: [ @doc1 = { _id: "doc-id-1", name: "doc-name-1" } ]
  927. folders: []
  928. ]
  929. fileRefs: [ @file2 = { _id: "file-id-2", name: "file-name-2" } ]
  930. docs: [ @doc2 = { _id: "doc-id-2", name: "doc-name-2" } ]
  931. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  932. @ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
  933. path = "/folder"
  934. @ProjectEntityUpdateHandler._cleanUpEntity @project, @folder, "folder", path, userId, done
  935. it "should clean up all sub files", ->
  936. @ProjectEntityUpdateHandler._cleanUpFile
  937. .calledWith(@project, @file1, "/folder/subfolder/file-name-1", userId)
  938. .should.equal true
  939. @ProjectEntityUpdateHandler._cleanUpFile
  940. .calledWith(@project, @file2, "/folder/file-name-2", userId)
  941. .should.equal true
  942. it "should clean up all sub docs", ->
  943. @ProjectEntityUpdateHandler._cleanUpDoc
  944. .calledWith(@project, @doc1, "/folder/subfolder/doc-name-1", userId)
  945. .should.equal true
  946. @ProjectEntityUpdateHandler._cleanUpDoc
  947. .calledWith(@project, @doc2, "/folder/doc-name-2", userId)
  948. .should.equal true
  949. it "should should send one update to the doc updater for all docs and files", ->
  950. oldFiles = [ {file: @file2, path: "/folder/file-name-2"}, {file: @file1, path: "/folder/subfolder/file-name-1"} ]
  951. oldDocs = [ {doc: @doc2, path: "/folder/doc-name-2"}, { doc: @doc1, path: "/folder/subfolder/doc-name-1"} ]
  952. @DocumentUpdaterHandler.updateProjectStructure
  953. .calledWith(project_id, projectHistoryId, userId, {oldFiles, oldDocs})
  954. .should.equal true
  955. describe "_cleanUpDoc", ->
  956. beforeEach ->
  957. @doc =
  958. _id: ObjectId()
  959. name: "test.tex"
  960. @path = "/path/to/doc"
  961. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  962. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference = sinon.stub().yields()
  963. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  964. @DocstoreManager.deleteDoc = sinon.stub().yields()
  965. @callback = sinon.stub()
  966. describe "when the doc is the root doc", ->
  967. beforeEach ->
  968. @project.rootDoc_id = @doc._id
  969. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  970. it "should unset the root doc", ->
  971. @ProjectEntityUpdateHandler.unsetRootDoc
  972. .calledWith(project_id)
  973. .should.equal true
  974. it "should delete the doc in the doc updater", ->
  975. @DocumentUpdaterHandler.deleteDoc
  976. .calledWith(project_id, @doc._id.toString())
  977. it "should insert the doc into the deletedDocs array", ->
  978. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference
  979. .calledWith(@project._id, @doc)
  980. .should.equal true
  981. it "should delete the doc in the doc store", ->
  982. @DocstoreManager.deleteDoc
  983. .calledWith(project_id, @doc._id.toString())
  984. .should.equal true
  985. it "should call the callback", ->
  986. @callback.called.should.equal true
  987. describe "when the doc is not the root doc", ->
  988. beforeEach ->
  989. @project.rootDoc_id = ObjectId()
  990. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  991. it "should not unset the root doc", ->
  992. @ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal false
  993. it "should call the callback", ->
  994. @callback.called.should.equal true