ProjectEntityUpdateHandlerTests.coffee 33 KB

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