ProjectEntityUpdateHandlerTests.coffee 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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._id, @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._id, @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 = name:@docName, lines:undefined, _id: doc_id, rev: 0
  250. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  251. @TpdsUpdateSender.addDoc = sinon.stub().yields()
  252. @ProjectEntityMongoUpdateHandler.addDoc = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  253. @ProjectEntityUpdateHandler.addDoc project_id, doc_id, @docName, @docLines, userId, @callback
  254. it "creates the doc without history", () ->
  255. @DocstoreManager.updateDoc
  256. .calledWith(project_id, doc_id, @docLines, 0, {})
  257. .should.equal true
  258. it "sends the change in project structure to the doc updater", () ->
  259. newDocs = [
  260. doc: @newDoc
  261. path: @path
  262. docLines: @docLines.join('\n')
  263. ]
  264. @DocumentUpdaterHandler.updateProjectStructure
  265. .calledWith(project_id, projectHistoryId, userId, {newDocs})
  266. .should.equal true
  267. describe 'adding a doc with an invalid name', ->
  268. beforeEach ->
  269. @path = "/path/to/doc"
  270. @newDoc = _id: doc_id
  271. @ProjectEntityUpdateHandler.addDoc project_id, folder_id, "*" + @docName, @docLines, userId, @callback
  272. it 'returns an error', ->
  273. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  274. @callback.calledWithMatch(errorMatcher)
  275. .should.equal true
  276. describe 'addFile', ->
  277. describe 'adding a file', ->
  278. beforeEach ->
  279. @path = "/path/to/file"
  280. @newFile = {_id: file_id, rev: 0, name: @fileName, linkedFileData: @linkedFileData}
  281. @TpdsUpdateSender.addFile = sinon.stub().yields()
  282. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  283. @ProjectEntityUpdateHandler.addFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  284. it "updates the file in the filestore", () ->
  285. @FileStoreHandler.uploadFileFromDisk
  286. .calledWith(project_id, file_id, @fileSystemPath)
  287. .should.equal true
  288. it "updates the file in mongo", () ->
  289. fileMatcher = sinon.match (file) =>
  290. file.name == @fileName
  291. @ProjectEntityMongoUpdateHandler.addFile
  292. .calledWithMatch(project_id, folder_id, fileMatcher)
  293. .should.equal true
  294. it "notifies the tpds", () ->
  295. @TpdsUpdateSender.addFile
  296. .calledWith({
  297. project_id: project_id
  298. project_name: @project.name
  299. file_id: file_id
  300. rev: 0
  301. path: @path
  302. })
  303. .should.equal true
  304. it "sends the change in project structure to the doc updater", () ->
  305. newFiles = [
  306. file: @newFile
  307. path: @path
  308. url: @fileUrl
  309. ]
  310. @DocumentUpdaterHandler.updateProjectStructure
  311. .calledWith(project_id, projectHistoryId, userId, {newFiles})
  312. .should.equal true
  313. describe 'adding a file with an invalid name', ->
  314. beforeEach ->
  315. @path = "/path/to/file"
  316. @newFile = {_id: file_id, rev: 0, name: @fileName, linkedFileData: @linkedFileData}
  317. @TpdsUpdateSender.addFile = sinon.stub().yields()
  318. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  319. @ProjectEntityUpdateHandler.addFile project_id, folder_id, "*" + @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  320. it 'returns an error', ->
  321. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  322. @callback.calledWithMatch(errorMatcher)
  323. .should.equal true
  324. describe 'replaceFile', ->
  325. beforeEach ->
  326. # replacement file now creates a new file object
  327. @newFileUrl = "new-file-url"
  328. @FileStoreHandler.uploadFileFromDisk = sinon.stub().yields(null, @newFileUrl)
  329. @newFile = _id: new_file_id, name: "dummy-upload-filename", rev: 0, linkedFileData: @linkedFileData
  330. @oldFile = _id: file_id, rev: 3
  331. @path = "/path/to/file"
  332. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  333. @ProjectEntityMongoUpdateHandler.replaceFileWithNew = sinon.stub().yields(null, @oldFile, @project, fileSystem: @path)
  334. @ProjectEntityUpdateHandler.replaceFile project_id, file_id, @fileSystemPath, @linkedFileData, userId, @callback
  335. it 'uploads a new version of the file', ->
  336. @FileStoreHandler.uploadFileFromDisk
  337. .calledWith(project_id, new_file_id, @fileSystemPath)
  338. .should.equal true
  339. it 'replaces the file in mongo', ->
  340. @ProjectEntityMongoUpdateHandler.replaceFileWithNew
  341. .calledWith(project_id, file_id, @newFile)
  342. .should.equal true
  343. it 'notifies the tpds', ->
  344. @TpdsUpdateSender.addFile
  345. .calledWith({
  346. project_id: project_id
  347. project_name: @project.name
  348. file_id: new_file_id
  349. rev: @oldFile.rev + 1
  350. path: @path
  351. })
  352. .should.equal true
  353. it 'updates the project structure in the doc updater', ->
  354. oldFiles = [
  355. file: @oldFile
  356. path: @path
  357. ]
  358. newFiles = [
  359. file: @newFile
  360. path: @path
  361. url: @newFileUrl
  362. ]
  363. @DocumentUpdaterHandler.updateProjectStructure
  364. .calledWith(project_id, projectHistoryId, userId, {oldFiles, newFiles})
  365. .should.equal true
  366. describe 'upsertDoc', ->
  367. describe 'upserting into an invalid folder', ->
  368. beforeEach ->
  369. @ProjectLocator.findElement = sinon.stub().yields()
  370. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  371. it 'returns an error', ->
  372. errorMatcher = sinon.match.instanceOf(Error)
  373. @callback.calledWithMatch(errorMatcher)
  374. .should.equal true
  375. describe 'updating an existing doc', ->
  376. beforeEach ->
  377. @existingDoc = _id: doc_id, name: @docName
  378. @folder = _id: folder_id, docs: [@existingDoc]
  379. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  380. @DocumentUpdaterHandler.setDocument = sinon.stub().yields()
  381. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().yields()
  382. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  383. it 'tries to find the folder', ->
  384. @ProjectLocator.findElement
  385. .calledWith({project_id, element_id: folder_id, type: "folder"})
  386. .should.equal true
  387. it 'updates the doc contents', ->
  388. @DocumentUpdaterHandler.setDocument
  389. .calledWith(project_id, @existingDoc._id, userId, @docLines, @source)
  390. .should.equal true
  391. it 'flushes the doc contents', ->
  392. @DocumentUpdaterHandler.flushDocToMongo
  393. .calledWith(project_id, @existingDoc._id )
  394. .should.equal true
  395. it 'returns the doc', ->
  396. @callback.calledWith(null, @existingDoc, false)
  397. describe 'creating a new doc', ->
  398. beforeEach ->
  399. @folder = _id: folder_id, docs: []
  400. @newDoc = _id: doc_id
  401. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  402. @ProjectEntityUpdateHandler.addDoc = withoutLock: sinon.stub().yields(null, @newDoc)
  403. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  404. it 'tries to find the folder', ->
  405. @ProjectLocator.findElement
  406. .calledWith({project_id, element_id: folder_id, type: "folder"})
  407. .should.equal true
  408. it 'adds the doc', ->
  409. @ProjectEntityUpdateHandler.addDoc.withoutLock
  410. .calledWith(project_id, folder_id, @docName, @docLines, userId)
  411. .should.equal true
  412. it 'returns the doc', ->
  413. @callback.calledWith(null, @newDoc, true)
  414. describe 'upserting a new doc with an invalid name', ->
  415. beforeEach ->
  416. @folder = _id: folder_id, docs: []
  417. @newDoc = _id: doc_id
  418. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  419. @ProjectEntityUpdateHandler.addDoc = withoutLock: sinon.stub().yields(null, @newDoc)
  420. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, "*" + @docName, @docLines, @source, userId, @callback
  421. it 'returns an error', ->
  422. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  423. @callback.calledWithMatch(errorMatcher)
  424. .should.equal true
  425. describe 'upsertFile', ->
  426. describe 'upserting into an invalid folder', ->
  427. beforeEach ->
  428. @ProjectLocator.findElement = sinon.stub().yields()
  429. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  430. it 'returns an error', ->
  431. errorMatcher = sinon.match.instanceOf(Error)
  432. @callback.calledWithMatch(errorMatcher)
  433. .should.equal true
  434. describe 'updating an existing file', ->
  435. beforeEach ->
  436. @existingFile = _id: file_id, name: @fileName
  437. @folder = _id: folder_id, fileRefs: [@existingFile]
  438. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  439. @ProjectEntityUpdateHandler.replaceFile = mainTask: sinon.stub().yields(null, @newFile)
  440. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  441. it 'replaces the file', ->
  442. @ProjectEntityUpdateHandler.replaceFile.mainTask
  443. .calledWith(project_id, file_id, @fileSystemPath, @linkedFileData, userId)
  444. .should.equal true
  445. it 'returns the file', ->
  446. @callback.calledWith(null, @existingFile, false)
  447. describe 'creating a new file', ->
  448. beforeEach ->
  449. @folder = _id: folder_id, fileRefs: []
  450. @newFile = _id: file_id
  451. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  452. @ProjectEntityUpdateHandler.addFile = mainTask: sinon.stub().yields(null, @newFile)
  453. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  454. it 'tries to find the folder', ->
  455. @ProjectLocator.findElement
  456. .calledWith({project_id, element_id: folder_id, type: "folder"})
  457. .should.equal true
  458. it 'adds the file', ->
  459. @ProjectEntityUpdateHandler.addFile.mainTask
  460. .calledWith(project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId)
  461. .should.equal true
  462. it 'returns the file', ->
  463. @callback.calledWith(null, @newFile, true)
  464. describe 'upserting a new file with an invalid name', ->
  465. beforeEach ->
  466. @folder = _id: folder_id, fileRefs: []
  467. @newFile = _id: file_id
  468. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  469. @ProjectEntityUpdateHandler.addFile = mainTask: sinon.stub().yields(null, @newFile)
  470. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, '*' + @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  471. it 'returns an error', ->
  472. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  473. @callback.calledWithMatch(errorMatcher)
  474. .should.equal true
  475. describe 'upsertDocWithPath', ->
  476. describe 'upserting a doc', ->
  477. beforeEach ->
  478. @path = "/folder/doc.tex"
  479. @newFolders = [ 'mock-a', 'mock-b' ]
  480. @folder = _id: folder_id
  481. @doc = _id: doc_id
  482. @isNewDoc = true
  483. @ProjectEntityUpdateHandler.mkdirp =
  484. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  485. @ProjectEntityUpdateHandler.upsertDoc =
  486. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  487. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  488. it 'creates any necessary folders', ->
  489. @ProjectEntityUpdateHandler.mkdirp.withoutLock
  490. .calledWith(project_id, '/folder')
  491. .should.equal true
  492. it 'upserts the doc', ->
  493. @ProjectEntityUpdateHandler.upsertDoc.withoutLock
  494. .calledWith(project_id, @folder._id, 'doc.tex', @docLines, @source, userId)
  495. .should.equal true
  496. it 'calls the callback', ->
  497. @callback
  498. .calledWith(null, @doc, @isNewDoc, @newFolders, @folder)
  499. .should.equal true
  500. describe 'upserting a doc with an invalid path', ->
  501. beforeEach ->
  502. @path = "/*folder/doc.tex"
  503. @newFolders = [ 'mock-a', 'mock-b' ]
  504. @folder = _id: folder_id
  505. @doc = _id: doc_id
  506. @isNewDoc = true
  507. @ProjectEntityUpdateHandler.mkdirp =
  508. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  509. @ProjectEntityUpdateHandler.upsertDoc =
  510. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  511. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  512. it 'returns an error', ->
  513. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  514. @callback.calledWithMatch(errorMatcher)
  515. .should.equal true
  516. describe 'upserting a doc with an invalid name', ->
  517. beforeEach ->
  518. @path = "/folder/*doc.tex"
  519. @newFolders = [ 'mock-a', 'mock-b' ]
  520. @folder = _id: folder_id
  521. @doc = _id: doc_id
  522. @isNewDoc = true
  523. @ProjectEntityUpdateHandler.mkdirp =
  524. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  525. @ProjectEntityUpdateHandler.upsertDoc =
  526. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  527. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  528. it 'returns an error', ->
  529. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  530. @callback.calledWithMatch(errorMatcher)
  531. .should.equal true
  532. describe 'upsertFileWithPath', ->
  533. describe 'upserting a file', ->
  534. beforeEach ->
  535. @path = "/folder/file.png"
  536. @newFolders = [ 'mock-a', 'mock-b' ]
  537. @folder = _id: folder_id
  538. @file = _id: file_id
  539. @isNewFile = true
  540. @ProjectEntityUpdateHandler.mkdirp =
  541. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  542. @ProjectEntityUpdateHandler.upsertFile =
  543. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  544. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  545. it 'creates any necessary folders', ->
  546. @ProjectEntityUpdateHandler.mkdirp.withoutLock
  547. .calledWith(project_id, '/folder')
  548. .should.equal true
  549. it 'upserts the file', ->
  550. @ProjectEntityUpdateHandler.upsertFile.mainTask
  551. .calledWith(project_id, @folder._id, 'file.png', @fileSystemPath, @linkedFileData, userId)
  552. .should.equal true
  553. it 'calls the callback', ->
  554. @callback
  555. .calledWith(null, @file, @isNewFile, undefined, @newFolders, @folder)
  556. .should.equal true
  557. describe 'upserting a file with an invalid path', ->
  558. beforeEach ->
  559. @path = "/*folder/file.png"
  560. @newFolders = [ 'mock-a', 'mock-b' ]
  561. @folder = _id: folder_id
  562. @file = _id: file_id
  563. @isNewFile = true
  564. @ProjectEntityUpdateHandler.mkdirp =
  565. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  566. @ProjectEntityUpdateHandler.upsertFile =
  567. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  568. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  569. it 'returns an error', ->
  570. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  571. @callback.calledWithMatch(errorMatcher)
  572. .should.equal true
  573. describe 'upserting a file with an invalid name', ->
  574. beforeEach ->
  575. @path = "/folder/*file.png"
  576. @newFolders = [ 'mock-a', 'mock-b' ]
  577. @folder = _id: folder_id
  578. @file = _id: file_id
  579. @isNewFile = true
  580. @ProjectEntityUpdateHandler.mkdirp =
  581. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  582. @ProjectEntityUpdateHandler.upsertFile =
  583. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  584. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  585. it 'returns an error', ->
  586. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  587. @callback.calledWithMatch(errorMatcher)
  588. .should.equal true
  589. describe 'deleteEntity', ->
  590. beforeEach ->
  591. @path = '/path/to/doc.tex'
  592. @doc = _id: doc_id
  593. @projectBeforeDeletion = _id: project_id, name: 'project'
  594. @ProjectEntityMongoUpdateHandler.deleteEntity = sinon.stub().yields(null, @doc, {fileSystem: @path}, @projectBeforeDeletion)
  595. @ProjectEntityUpdateHandler._cleanUpEntity = sinon.stub().yields()
  596. @TpdsUpdateSender.deleteEntity = sinon.stub().yields()
  597. @ProjectEntityUpdateHandler.deleteEntity project_id, doc_id, 'doc', userId, @callback
  598. it 'deletes the entity in mongo', ->
  599. @ProjectEntityMongoUpdateHandler.deleteEntity
  600. .calledWith(project_id, doc_id, 'doc')
  601. .should.equal true
  602. it 'cleans up the doc in the docstore', ->
  603. @ProjectEntityUpdateHandler._cleanUpEntity
  604. .calledWith(@projectBeforeDeletion, @doc, 'doc', @path, userId)
  605. .should.equal true
  606. it 'it notifies the tpds', ->
  607. @TpdsUpdateSender.deleteEntity
  608. .calledWith({ project_id, @path, project_name: @projectBeforeDeletion.name })
  609. .should.equal true
  610. it 'retuns the entity_id', ->
  611. @callback.calledWith(null, doc_id).should.equal true
  612. describe 'deleteEntityWithPath', ->
  613. describe 'when the entity exists', ->
  614. beforeEach ->
  615. @doc = _id: doc_id
  616. @ProjectLocator.findElementByPath = sinon.stub().yields(null, @doc, 'doc')
  617. @ProjectEntityUpdateHandler.deleteEntity =
  618. withoutLock: sinon.stub().yields()
  619. @path = '/path/to/doc.tex'
  620. @ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
  621. it 'finds the entity', ->
  622. @ProjectLocator.findElementByPath
  623. .calledWith({project_id, @path})
  624. .should.equal true
  625. it 'deletes the entity', ->
  626. @ProjectEntityUpdateHandler.deleteEntity.withoutLock
  627. .calledWith(project_id, @doc._id, 'doc', userId, @callback)
  628. .should.equal true
  629. describe 'when the entity does not exist', ->
  630. beforeEach ->
  631. @ProjectLocator.findElementByPath = sinon.stub().yields()
  632. @path = '/doc.tex'
  633. @ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
  634. it 'returns an error', ->
  635. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  636. describe 'mkdirp', ->
  637. beforeEach ->
  638. @docPath = '/folder/doc.tex'
  639. @ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
  640. @ProjectEntityUpdateHandler.mkdirp project_id, @docPath, @callback
  641. it 'calls ProjectEntityMongoUpdateHandler', ->
  642. @ProjectEntityMongoUpdateHandler.mkdirp
  643. .calledWith(project_id, @docPath)
  644. .should.equal true
  645. describe 'mkdirpWithExactCase', ->
  646. beforeEach ->
  647. @docPath = '/folder/doc.tex'
  648. @ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
  649. @ProjectEntityUpdateHandler.mkdirpWithExactCase project_id, @docPath, @callback
  650. it 'calls ProjectEntityMongoUpdateHandler', ->
  651. @ProjectEntityMongoUpdateHandler.mkdirp
  652. .calledWith(project_id, @docPath, {exactCaseMatch: true})
  653. .should.equal true
  654. describe 'addFolder', ->
  655. describe 'adding a folder', ->
  656. beforeEach ->
  657. @parentFolder_id = '123asdf'
  658. @folderName = 'new-folder'
  659. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  660. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  661. it 'calls ProjectEntityMongoUpdateHandler', ->
  662. @ProjectEntityMongoUpdateHandler.addFolder
  663. .calledWith(project_id, @parentFolder_id, @folderName)
  664. .should.equal true
  665. describe 'adding a folder with an invalid name', ->
  666. beforeEach ->
  667. @parentFolder_id = '123asdf'
  668. @folderName = '*new-folder'
  669. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  670. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  671. it 'returns an error', ->
  672. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  673. @callback.calledWithMatch(errorMatcher)
  674. .should.equal true
  675. describe 'moveEntity', ->
  676. beforeEach ->
  677. @project_name = 'project name'
  678. @startPath = '/a.tex'
  679. @endPath = '/folder/b.tex'
  680. @rev = 2
  681. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  682. @ProjectEntityMongoUpdateHandler.moveEntity = sinon.stub().yields(
  683. null, @project, @startPath, @endPath, @rev, @changes
  684. )
  685. @TpdsUpdateSender.moveEntity = sinon.stub()
  686. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  687. @ProjectEntityUpdateHandler.moveEntity project_id, doc_id, folder_id, 'doc', userId, @callback
  688. it 'moves the entity in mongo', ->
  689. @ProjectEntityMongoUpdateHandler.moveEntity
  690. .calledWith(project_id, doc_id, folder_id, 'doc')
  691. .should.equal true
  692. it 'notifies tpds', ->
  693. @TpdsUpdateSender.moveEntity
  694. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  695. .should.equal true
  696. it 'sends the changes in project structure to the doc updater', ->
  697. @DocumentUpdaterHandler.updateProjectStructure
  698. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  699. .should.equal true
  700. describe "renameEntity", ->
  701. describe 'renaming an entity', ->
  702. beforeEach ->
  703. @project_name = 'project name'
  704. @startPath = '/folder/a.tex'
  705. @endPath = '/folder/b.tex'
  706. @rev = 2
  707. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  708. @newDocName = 'b.tex'
  709. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  710. null, @project, @startPath, @endPath, @rev, @changes
  711. )
  712. @TpdsUpdateSender.moveEntity = sinon.stub()
  713. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  714. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  715. it 'moves the entity in mongo', ->
  716. @ProjectEntityMongoUpdateHandler.renameEntity
  717. .calledWith(project_id, doc_id, 'doc', @newDocName)
  718. .should.equal true
  719. it 'notifies tpds', ->
  720. @TpdsUpdateSender.moveEntity
  721. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  722. .should.equal true
  723. it 'sends the changes in project structure to the doc updater', ->
  724. @DocumentUpdaterHandler.updateProjectStructure
  725. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  726. .should.equal true
  727. describe 'renaming an entity to an invalid name', ->
  728. beforeEach ->
  729. @project_name = 'project name'
  730. @startPath = '/folder/a.tex'
  731. @endPath = '/folder/b.tex'
  732. @rev = 2
  733. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  734. @newDocName = '*b.tex'
  735. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  736. null, @project, @startPath, @endPath, @rev, @changes
  737. )
  738. @TpdsUpdateSender.moveEntity = sinon.stub()
  739. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  740. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  741. it 'returns an error', ->
  742. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  743. @callback.calledWithMatch(errorMatcher)
  744. .should.equal true
  745. describe "resyncProjectHistory", ->
  746. describe "a deleted project", ->
  747. beforeEach ->
  748. @ProjectGetter.getProject = sinon.stub().yields()
  749. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  750. it "should return an error", ->
  751. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  752. @callback.calledWith(error).should.equal true
  753. describe "a project without project-history enabled", ->
  754. beforeEach ->
  755. @project.overleaf = {}
  756. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  757. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  758. it "should return an error", ->
  759. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  760. @callback.calledWith(error).should.equal true
  761. describe "a project with project-history enabled", ->
  762. beforeEach ->
  763. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  764. docs = [
  765. doc: _id: doc_id
  766. path: 'main.tex'
  767. ]
  768. files = [
  769. file: _id: file_id
  770. path: 'universe.png'
  771. ]
  772. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().yields(null, docs, files)
  773. @FileStoreHandler._buildUrl = (project_id, file_id) ->
  774. "www.filestore.test/#{project_id}/#{file_id}"
  775. @DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
  776. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  777. it 'gets the project', ->
  778. @ProjectGetter.getProject
  779. .calledWith(project_id)
  780. .should.equal true
  781. it 'gets the entities for the project', ->
  782. @ProjectEntityHandler.getAllEntitiesFromProject
  783. .calledWith(@project)
  784. .should.equal true
  785. it 'tells the doc updater to sync the project', ->
  786. docs = [
  787. doc: doc_id
  788. path: 'main.tex'
  789. ]
  790. files = [
  791. file: file_id
  792. path: 'universe.png'
  793. url: "www.filestore.test/#{project_id}/#{file_id}"
  794. ]
  795. @DocumentUpdaterHandler.resyncProjectHistory
  796. .calledWith(project_id, projectHistoryId, docs, files)
  797. .should.equal true
  798. it 'calls the callback', ->
  799. @callback.called.should.equal true
  800. describe "_cleanUpEntity", ->
  801. beforeEach ->
  802. @entity_id = "4eecaffcbffa66588e000009"
  803. @FileStoreHandler.deleteFile = sinon.stub().yields()
  804. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  805. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  806. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  807. describe "a file", ->
  808. beforeEach (done) ->
  809. @path = "/file/system/path.png"
  810. @entity = _id: @entity_id
  811. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'file', @path, userId, done
  812. it "should insert the file into the deletedFiles array", ->
  813. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference
  814. .calledWith(@project._id, @entity)
  815. .should.equal true
  816. it "should not delete the file from FileStoreHandler", ->
  817. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal false
  818. it "should not attempt to delete from the document updater", ->
  819. @DocumentUpdaterHandler.deleteDoc.called.should.equal false
  820. it "should should send the update to the doc updater", ->
  821. oldFiles = [ file: @entity, path: @path ]
  822. @DocumentUpdaterHandler.updateProjectStructure
  823. .calledWith(project_id, projectHistoryId, userId, {oldFiles})
  824. .should.equal true
  825. describe "a doc", ->
  826. beforeEach (done) ->
  827. @path = "/file/system/path.tex"
  828. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  829. @entity = {_id: @entity_id}
  830. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'doc', @path, userId, done
  831. it "should clean up the doc", ->
  832. @ProjectEntityUpdateHandler._cleanUpDoc
  833. .calledWith(@project, @entity, @path, userId)
  834. .should.equal true
  835. it "should should send the update to the doc updater", ->
  836. oldDocs = [ doc: @entity, path: @path ]
  837. @DocumentUpdaterHandler.updateProjectStructure
  838. .calledWith(project_id, projectHistoryId, userId, {oldDocs})
  839. .should.equal true
  840. describe "a folder", ->
  841. beforeEach (done) ->
  842. @folder =
  843. folders: [
  844. name: "subfolder"
  845. fileRefs: [ @file1 = { _id: "file-id-1", name: "file-name-1"} ]
  846. docs: [ @doc1 = { _id: "doc-id-1", name: "doc-name-1" } ]
  847. folders: []
  848. ]
  849. fileRefs: [ @file2 = { _id: "file-id-2", name: "file-name-2" } ]
  850. docs: [ @doc2 = { _id: "doc-id-2", name: "doc-name-2" } ]
  851. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  852. @ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
  853. path = "/folder"
  854. @ProjectEntityUpdateHandler._cleanUpEntity @project, @folder, "folder", path, userId, done
  855. it "should clean up all sub files", ->
  856. @ProjectEntityUpdateHandler._cleanUpFile
  857. .calledWith(@project, @file1, "/folder/subfolder/file-name-1", userId)
  858. .should.equal true
  859. @ProjectEntityUpdateHandler._cleanUpFile
  860. .calledWith(@project, @file2, "/folder/file-name-2", userId)
  861. .should.equal true
  862. it "should clean up all sub docs", ->
  863. @ProjectEntityUpdateHandler._cleanUpDoc
  864. .calledWith(@project, @doc1, "/folder/subfolder/doc-name-1", userId)
  865. .should.equal true
  866. @ProjectEntityUpdateHandler._cleanUpDoc
  867. .calledWith(@project, @doc2, "/folder/doc-name-2", userId)
  868. .should.equal true
  869. it "should should send one update to the doc updater for all docs and files", ->
  870. oldFiles = [ {file: @file2, path: "/folder/file-name-2"}, {file: @file1, path: "/folder/subfolder/file-name-1"} ]
  871. oldDocs = [ {doc: @doc2, path: "/folder/doc-name-2"}, { doc: @doc1, path: "/folder/subfolder/doc-name-1"} ]
  872. @DocumentUpdaterHandler.updateProjectStructure
  873. .calledWith(project_id, projectHistoryId, userId, {oldFiles, oldDocs})
  874. .should.equal true
  875. describe "_cleanUpDoc", ->
  876. beforeEach ->
  877. @doc =
  878. _id: ObjectId()
  879. name: "test.tex"
  880. @path = "/path/to/doc"
  881. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  882. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference = sinon.stub().yields()
  883. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  884. @DocstoreManager.deleteDoc = sinon.stub().yields()
  885. @callback = sinon.stub()
  886. describe "when the doc is the root doc", ->
  887. beforeEach ->
  888. @project.rootDoc_id = @doc._id
  889. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  890. it "should unset the root doc", ->
  891. @ProjectEntityUpdateHandler.unsetRootDoc
  892. .calledWith(project_id)
  893. .should.equal true
  894. it "should delete the doc in the doc updater", ->
  895. @DocumentUpdaterHandler.deleteDoc
  896. .calledWith(project_id, @doc._id.toString())
  897. it "should insert the doc into the deletedDocs array", ->
  898. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference
  899. .calledWith(@project._id, @doc)
  900. .should.equal true
  901. it "should delete the doc in the doc store", ->
  902. @DocstoreManager.deleteDoc
  903. .calledWith(project_id, @doc._id.toString())
  904. .should.equal true
  905. it "should call the callback", ->
  906. @callback.called.should.equal true
  907. describe "when the doc is not the root doc", ->
  908. beforeEach ->
  909. @project.rootDoc_id = ObjectId()
  910. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  911. it "should not unset the root doc", ->
  912. @ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal false
  913. it "should call the callback", ->
  914. @callback.called.should.equal true