ProjectEntityUpdateHandlerTests.coffee 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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 = 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
  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: @newFile.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 'addFolder', ->
  646. describe 'adding a folder', ->
  647. beforeEach ->
  648. @parentFolder_id = '123asdf'
  649. @folderName = 'new-folder'
  650. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  651. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  652. it 'calls ProjectEntityMongoUpdateHandler', ->
  653. @ProjectEntityMongoUpdateHandler.addFolder
  654. .calledWith(project_id, @parentFolder_id, @folderName)
  655. .should.equal true
  656. describe 'adding a folder with an invalid name', ->
  657. beforeEach ->
  658. @parentFolder_id = '123asdf'
  659. @folderName = '*new-folder'
  660. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  661. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  662. it 'returns an error', ->
  663. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  664. @callback.calledWithMatch(errorMatcher)
  665. .should.equal true
  666. describe 'moveEntity', ->
  667. beforeEach ->
  668. @project_name = 'project name'
  669. @startPath = '/a.tex'
  670. @endPath = '/folder/b.tex'
  671. @rev = 2
  672. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  673. @ProjectEntityMongoUpdateHandler.moveEntity = sinon.stub().yields(
  674. null, @project, @startPath, @endPath, @rev, @changes
  675. )
  676. @TpdsUpdateSender.moveEntity = sinon.stub()
  677. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  678. @ProjectEntityUpdateHandler.moveEntity project_id, doc_id, folder_id, 'doc', userId, @callback
  679. it 'moves the entity in mongo', ->
  680. @ProjectEntityMongoUpdateHandler.moveEntity
  681. .calledWith(project_id, doc_id, folder_id, 'doc')
  682. .should.equal true
  683. it 'notifies tpds', ->
  684. @TpdsUpdateSender.moveEntity
  685. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  686. .should.equal true
  687. it 'sends the changes in project structure to the doc updater', ->
  688. @DocumentUpdaterHandler.updateProjectStructure
  689. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  690. .should.equal true
  691. describe "renameEntity", ->
  692. describe 'renaming an entity', ->
  693. beforeEach ->
  694. @project_name = 'project name'
  695. @startPath = '/folder/a.tex'
  696. @endPath = '/folder/b.tex'
  697. @rev = 2
  698. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  699. @newDocName = 'b.tex'
  700. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  701. null, @project, @startPath, @endPath, @rev, @changes
  702. )
  703. @TpdsUpdateSender.moveEntity = sinon.stub()
  704. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  705. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  706. it 'moves the entity in mongo', ->
  707. @ProjectEntityMongoUpdateHandler.renameEntity
  708. .calledWith(project_id, doc_id, 'doc', @newDocName)
  709. .should.equal true
  710. it 'notifies tpds', ->
  711. @TpdsUpdateSender.moveEntity
  712. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  713. .should.equal true
  714. it 'sends the changes in project structure to the doc updater', ->
  715. @DocumentUpdaterHandler.updateProjectStructure
  716. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  717. .should.equal true
  718. describe 'renaming an entity to an invalid name', ->
  719. beforeEach ->
  720. @project_name = 'project name'
  721. @startPath = '/folder/a.tex'
  722. @endPath = '/folder/b.tex'
  723. @rev = 2
  724. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  725. @newDocName = '*b.tex'
  726. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  727. null, @project, @startPath, @endPath, @rev, @changes
  728. )
  729. @TpdsUpdateSender.moveEntity = sinon.stub()
  730. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  731. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  732. it 'returns an error', ->
  733. errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  734. @callback.calledWithMatch(errorMatcher)
  735. .should.equal true
  736. describe "resyncProjectHistory", ->
  737. describe "a deleted project", ->
  738. beforeEach ->
  739. @ProjectGetter.getProject = sinon.stub().yields()
  740. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  741. it "should return an error", ->
  742. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  743. @callback.calledWith(error).should.equal true
  744. describe "a project without project-history enabled", ->
  745. beforeEach ->
  746. @project.overleaf = {}
  747. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  748. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  749. it "should return an error", ->
  750. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  751. @callback.calledWith(error).should.equal true
  752. describe "a project with project-history enabled", ->
  753. beforeEach ->
  754. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  755. docs = [
  756. doc: _id: doc_id
  757. path: 'main.tex'
  758. ]
  759. files = [
  760. file: _id: file_id
  761. path: 'universe.png'
  762. ]
  763. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().yields(null, docs, files)
  764. @FileStoreHandler._buildUrl = (project_id, file_id) ->
  765. "www.filestore.test/#{project_id}/#{file_id}"
  766. @DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
  767. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  768. it 'gets the project', ->
  769. @ProjectGetter.getProject
  770. .calledWith(project_id)
  771. .should.equal true
  772. it 'gets the entities for the project', ->
  773. @ProjectEntityHandler.getAllEntitiesFromProject
  774. .calledWith(@project)
  775. .should.equal true
  776. it 'tells the doc updater to sync the project', ->
  777. docs = [
  778. doc: doc_id
  779. path: 'main.tex'
  780. ]
  781. files = [
  782. file: file_id
  783. path: 'universe.png'
  784. url: "www.filestore.test/#{project_id}/#{file_id}"
  785. ]
  786. @DocumentUpdaterHandler.resyncProjectHistory
  787. .calledWith(project_id, projectHistoryId, docs, files)
  788. .should.equal true
  789. it 'calls the callback', ->
  790. @callback.called.should.equal true
  791. describe "_cleanUpEntity", ->
  792. beforeEach ->
  793. @entity_id = "4eecaffcbffa66588e000009"
  794. @FileStoreHandler.deleteFile = sinon.stub().yields()
  795. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  796. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  797. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  798. describe "a file", ->
  799. beforeEach (done) ->
  800. @path = "/file/system/path.png"
  801. @entity = _id: @entity_id
  802. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'file', @path, userId, done
  803. it "should insert the file into the deletedFiles array", ->
  804. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference
  805. .calledWith(@project._id, @entity)
  806. .should.equal true
  807. it "should not delete the file from FileStoreHandler", ->
  808. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal false
  809. it "should not attempt to delete from the document updater", ->
  810. @DocumentUpdaterHandler.deleteDoc.called.should.equal false
  811. it "should should send the update to the doc updater", ->
  812. oldFiles = [ file: @entity, path: @path ]
  813. @DocumentUpdaterHandler.updateProjectStructure
  814. .calledWith(project_id, projectHistoryId, userId, {oldFiles})
  815. .should.equal true
  816. describe "a doc", ->
  817. beforeEach (done) ->
  818. @path = "/file/system/path.tex"
  819. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  820. @entity = {_id: @entity_id}
  821. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'doc', @path, userId, done
  822. it "should clean up the doc", ->
  823. @ProjectEntityUpdateHandler._cleanUpDoc
  824. .calledWith(@project, @entity, @path, userId)
  825. .should.equal true
  826. it "should should send the update to the doc updater", ->
  827. oldDocs = [ doc: @entity, path: @path ]
  828. @DocumentUpdaterHandler.updateProjectStructure
  829. .calledWith(project_id, projectHistoryId, userId, {oldDocs})
  830. .should.equal true
  831. describe "a folder", ->
  832. beforeEach (done) ->
  833. @folder =
  834. folders: [
  835. name: "subfolder"
  836. fileRefs: [ @file1 = { _id: "file-id-1", name: "file-name-1"} ]
  837. docs: [ @doc1 = { _id: "doc-id-1", name: "doc-name-1" } ]
  838. folders: []
  839. ]
  840. fileRefs: [ @file2 = { _id: "file-id-2", name: "file-name-2" } ]
  841. docs: [ @doc2 = { _id: "doc-id-2", name: "doc-name-2" } ]
  842. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  843. @ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
  844. path = "/folder"
  845. @ProjectEntityUpdateHandler._cleanUpEntity @project, @folder, "folder", path, userId, done
  846. it "should clean up all sub files", ->
  847. @ProjectEntityUpdateHandler._cleanUpFile
  848. .calledWith(@project, @file1, "/folder/subfolder/file-name-1", userId)
  849. .should.equal true
  850. @ProjectEntityUpdateHandler._cleanUpFile
  851. .calledWith(@project, @file2, "/folder/file-name-2", userId)
  852. .should.equal true
  853. it "should clean up all sub docs", ->
  854. @ProjectEntityUpdateHandler._cleanUpDoc
  855. .calledWith(@project, @doc1, "/folder/subfolder/doc-name-1", userId)
  856. .should.equal true
  857. @ProjectEntityUpdateHandler._cleanUpDoc
  858. .calledWith(@project, @doc2, "/folder/doc-name-2", userId)
  859. .should.equal true
  860. it "should should send one update to the doc updater for all docs and files", ->
  861. oldFiles = [ {file: @file2, path: "/folder/file-name-2"}, {file: @file1, path: "/folder/subfolder/file-name-1"} ]
  862. oldDocs = [ {doc: @doc2, path: "/folder/doc-name-2"}, { doc: @doc1, path: "/folder/subfolder/doc-name-1"} ]
  863. @DocumentUpdaterHandler.updateProjectStructure
  864. .calledWith(project_id, projectHistoryId, userId, {oldFiles, oldDocs})
  865. .should.equal true
  866. describe "_cleanUpDoc", ->
  867. beforeEach ->
  868. @doc =
  869. _id: ObjectId()
  870. name: "test.tex"
  871. @path = "/path/to/doc"
  872. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  873. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference = sinon.stub().yields()
  874. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  875. @DocstoreManager.deleteDoc = sinon.stub().yields()
  876. @callback = sinon.stub()
  877. describe "when the doc is the root doc", ->
  878. beforeEach ->
  879. @project.rootDoc_id = @doc._id
  880. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  881. it "should unset the root doc", ->
  882. @ProjectEntityUpdateHandler.unsetRootDoc
  883. .calledWith(project_id)
  884. .should.equal true
  885. it "should delete the doc in the doc updater", ->
  886. @DocumentUpdaterHandler.deleteDoc
  887. .calledWith(project_id, @doc._id.toString())
  888. it "should insert the doc into the deletedDocs array", ->
  889. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference
  890. .calledWith(@project._id, @doc)
  891. .should.equal true
  892. it "should delete the doc in the doc store", ->
  893. @DocstoreManager.deleteDoc
  894. .calledWith(project_id, @doc._id.toString())
  895. .should.equal true
  896. it "should call the callback", ->
  897. @callback.called.should.equal true
  898. describe "when the doc is not the root doc", ->
  899. beforeEach ->
  900. @project.rootDoc_id = ObjectId()
  901. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  902. it "should not unset the root doc", ->
  903. @ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal false
  904. it "should call the callback", ->
  905. @callback.called.should.equal true