ProjectEntityUpdateHandlerTests.coffee 40 KB

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