ProjectEntityUpdateHandlerTests.coffee 34 KB

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