ProjectEntityUpdateHandlerTests.coffee 40 KB

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