ProjectEntityUpdateHandlerTests.coffee 40 KB

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