ProjectEntityUpdateHandlerTests.coffee 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 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. beforeEach ->
  247. @path = "/path/to/doc"
  248. @newDoc = _id: doc_id
  249. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory =
  250. withoutLock: sinon.stub().yields(null, @newDoc, folder_id, @path, @project)
  251. @ProjectEntityUpdateHandler.addDoc project_id, folder_id, @docName, @docLines, userId, @callback
  252. it "creates the doc without history", () ->
  253. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory.withoutLock
  254. .calledWith(project_id, folder_id, @docName, @docLines, userId)
  255. .should.equal true
  256. it "sends the change in project structure to the doc updater", () ->
  257. newDocs = [
  258. doc: @newDoc
  259. path: @path
  260. docLines: @docLines.join('\n')
  261. ]
  262. @DocumentUpdaterHandler.updateProjectStructure
  263. .calledWith(project_id, projectHistoryId, userId, {newDocs})
  264. .should.equal true
  265. describe 'addFile', ->
  266. beforeEach ->
  267. @path = "/path/to/file"
  268. @newFile = {_id: file_id, rev: 0, name: @fileName, linkedFileData: @linkedFileData}
  269. @TpdsUpdateSender.addFile = sinon.stub().yields()
  270. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  271. @ProjectEntityUpdateHandler.addFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  272. it "updates the file in the filestore", () ->
  273. @FileStoreHandler.uploadFileFromDisk
  274. .calledWith(project_id, file_id, @fileSystemPath)
  275. .should.equal true
  276. it "updates the file in mongo", () ->
  277. fileMatcher = sinon.match (file) =>
  278. file.name == @fileName
  279. @ProjectEntityMongoUpdateHandler.addFile
  280. .calledWithMatch(project_id, folder_id, fileMatcher)
  281. .should.equal true
  282. it "notifies the tpds", () ->
  283. @TpdsUpdateSender.addFile
  284. .calledWith({
  285. project_id: project_id
  286. project_name: @project.name
  287. file_id: file_id
  288. rev: 0
  289. path: @path
  290. })
  291. .should.equal true
  292. it "sends the change in project structure to the doc updater", () ->
  293. newFiles = [
  294. file: @newFile
  295. path: @path
  296. url: @fileUrl
  297. ]
  298. @DocumentUpdaterHandler.updateProjectStructure
  299. .calledWith(project_id, projectHistoryId, userId, {newFiles})
  300. .should.equal true
  301. describe 'replaceFile', ->
  302. beforeEach ->
  303. # replacement file now creates a new file object
  304. @newFileUrl = "new-file-url"
  305. @FileStoreHandler.uploadFileFromDisk = sinon.stub().yields(null, @newFileUrl)
  306. @newFile = _id: new_file_id, name: "dummy-upload-filename", rev: 0, linkedFileData: @linkedFileData
  307. @oldFile = _id: file_id
  308. @path = "/path/to/file"
  309. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  310. @ProjectEntityMongoUpdateHandler.replaceFileWithNew = sinon.stub().yields(null, @oldFile, @project, fileSystem: @path)
  311. @ProjectEntityUpdateHandler.replaceFile project_id, file_id, @fileSystemPath, @linkedFileData, userId, @callback
  312. it 'uploads a new version of the file', ->
  313. @FileStoreHandler.uploadFileFromDisk
  314. .calledWith(project_id, new_file_id, @fileSystemPath)
  315. .should.equal true
  316. it 'replaces the file in mongo', ->
  317. @ProjectEntityMongoUpdateHandler.replaceFileWithNew
  318. .calledWith(project_id, file_id, @newFile)
  319. .should.equal true
  320. it 'notifies the tpds', ->
  321. @TpdsUpdateSender.addFile
  322. .calledWith({
  323. project_id: project_id
  324. project_name: @project.name
  325. file_id: new_file_id
  326. rev: @newFile.rev + 1
  327. path: @path
  328. })
  329. .should.equal true
  330. it 'updates the project structure in the doc updater', ->
  331. oldFiles = [
  332. file: @oldFile
  333. path: @path
  334. ]
  335. newFiles = [
  336. file: @newFile
  337. path: @path
  338. url: @newFileUrl
  339. ]
  340. @DocumentUpdaterHandler.updateProjectStructure
  341. .calledWith(project_id, projectHistoryId, userId, {oldFiles, newFiles})
  342. .should.equal true
  343. describe 'addDocWithoutUpdatingHistory', ->
  344. beforeEach ->
  345. @path = "/path/to/doc"
  346. @project = _id: project_id, name: 'some project'
  347. @TpdsUpdateSender.addDoc = sinon.stub().yields()
  348. @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
  349. @ProjectEntityMongoUpdateHandler.addDoc = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  350. @ProjectEntityUpdateHandler.addDocWithoutUpdatingHistory project_id, folder_id, @docName, @docLines, userId, @callback
  351. it "updates the doc in the docstore", () ->
  352. @DocstoreManager.updateDoc
  353. .calledWith(project_id, doc_id, @docLines, 0, {})
  354. .should.equal true
  355. it "updates the doc in mongo", () ->
  356. docMatcher = sinon.match (doc) =>
  357. doc.name == @docName
  358. @ProjectEntityMongoUpdateHandler.addDoc
  359. .calledWithMatch(project_id, folder_id, docMatcher)
  360. .should.equal true
  361. it "notifies the tpds", () ->
  362. @TpdsUpdateSender.addDoc
  363. .calledWith({
  364. project_id: project_id
  365. project_name: @project.name
  366. doc_id: doc_id
  367. rev: 0
  368. path: @path
  369. })
  370. .should.equal true
  371. it "should not should send the change in project structure to the doc updater", () ->
  372. @DocumentUpdaterHandler.updateProjectStructure
  373. .called
  374. .should.equal false
  375. describe 'addFileWithoutUpdatingHistory', ->
  376. beforeEach ->
  377. @path = "/path/to/file"
  378. @project = _id: project_id, name: 'some project'
  379. @TpdsUpdateSender.addFile = sinon.stub().yields()
  380. @ProjectEntityMongoUpdateHandler.addFile = sinon.stub().yields(null, {path: fileSystem: @path}, @project)
  381. @ProjectEntityUpdateHandler.addFileWithoutUpdatingHistory project_id, folder_id, @fileName, @fileSystemPath, userId, @callback
  382. it "updates the file in the filestore", () ->
  383. @FileStoreHandler.uploadFileFromDisk
  384. .calledWith(project_id, file_id, @fileSystemPath)
  385. .should.equal true
  386. it "updates the file in mongo", () ->
  387. fileMatcher = sinon.match (file) =>
  388. file.name == @fileName
  389. @ProjectEntityMongoUpdateHandler.addFile
  390. .calledWithMatch(project_id, folder_id, fileMatcher)
  391. .should.equal true
  392. it "notifies the tpds", () ->
  393. @TpdsUpdateSender.addFile
  394. .calledWith({
  395. project_id: project_id
  396. project_name: @project.name
  397. file_id: file_id
  398. rev: 0
  399. path: @path
  400. })
  401. .should.equal true
  402. it "should not should send the change in project structure to the doc updater", () ->
  403. @DocumentUpdaterHandler.updateProjectStructure
  404. .called
  405. .should.equal false
  406. describe 'upsertDoc', ->
  407. describe 'upserting into an invalid folder', ->
  408. beforeEach ->
  409. @ProjectLocator.findElement = sinon.stub().yields()
  410. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  411. it 'returns an error', ->
  412. errorMatcher = sinon.match.instanceOf(Error)
  413. @callback.calledWithMatch(errorMatcher)
  414. .should.equal true
  415. describe 'updating an existing doc', ->
  416. beforeEach ->
  417. @existingDoc = _id: doc_id, name: @docName
  418. @folder = _id: folder_id, docs: [@existingDoc]
  419. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  420. @DocumentUpdaterHandler.setDocument = sinon.stub().yields()
  421. @DocumentUpdaterHandler.flushDocToMongo = sinon.stub().yields()
  422. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  423. it 'tries to find the folder', ->
  424. @ProjectLocator.findElement
  425. .calledWith({project_id, element_id: folder_id, type: "folder"})
  426. .should.equal true
  427. it 'updates the doc contents', ->
  428. @DocumentUpdaterHandler.setDocument
  429. .calledWith(project_id, @existingDoc._id, userId, @docLines, @source)
  430. .should.equal true
  431. it 'flushes the doc contents', ->
  432. @DocumentUpdaterHandler.flushDocToMongo
  433. .calledWith(project_id, @existingDoc._id )
  434. .should.equal true
  435. it 'returns the doc', ->
  436. @callback.calledWith(null, @existingDoc, false)
  437. describe 'creating a new doc', ->
  438. beforeEach ->
  439. @folder = _id: folder_id, docs: []
  440. @newDoc = _id: doc_id
  441. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  442. @ProjectEntityUpdateHandler.addDoc = withoutLock: sinon.stub().yields(null, @newDoc)
  443. @ProjectEntityUpdateHandler.upsertDoc project_id, folder_id, @docName, @docLines, @source, userId, @callback
  444. it 'tries to find the folder', ->
  445. @ProjectLocator.findElement
  446. .calledWith({project_id, element_id: folder_id, type: "folder"})
  447. .should.equal true
  448. it 'adds the doc', ->
  449. @ProjectEntityUpdateHandler.addDoc.withoutLock
  450. .calledWith(project_id, folder_id, @docName, @docLines, userId)
  451. .should.equal true
  452. it 'returns the doc', ->
  453. @callback.calledWith(null, @newDoc, true)
  454. describe 'upsertFile', ->
  455. describe 'upserting into an invalid folder', ->
  456. beforeEach ->
  457. @ProjectLocator.findElement = sinon.stub().yields()
  458. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  459. it 'returns an error', ->
  460. errorMatcher = sinon.match.instanceOf(Error)
  461. @callback.calledWithMatch(errorMatcher)
  462. .should.equal true
  463. describe 'updating an existing file', ->
  464. beforeEach ->
  465. @existingFile = _id: file_id, name: @fileName
  466. @folder = _id: folder_id, fileRefs: [@existingFile]
  467. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  468. @ProjectEntityUpdateHandler.replaceFile = mainTask: sinon.stub().yields(null, @newFile)
  469. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  470. it 'replaces the file', ->
  471. @ProjectEntityUpdateHandler.replaceFile.mainTask
  472. .calledWith(project_id, file_id, @fileSystemPath, @linkedFileData, userId)
  473. .should.equal true
  474. it 'returns the file', ->
  475. @callback.calledWith(null, @existingFile, false)
  476. describe 'creating a new file', ->
  477. beforeEach ->
  478. @folder = _id: folder_id, fileRefs: []
  479. @newFile = _id: file_id
  480. @ProjectLocator.findElement = sinon.stub().yields(null, @folder)
  481. @ProjectEntityUpdateHandler.addFile = mainTask: sinon.stub().yields(null, @newFile)
  482. @ProjectEntityUpdateHandler.upsertFile project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId, @callback
  483. it 'tries to find the folder', ->
  484. @ProjectLocator.findElement
  485. .calledWith({project_id, element_id: folder_id, type: "folder"})
  486. .should.equal true
  487. it 'adds the file', ->
  488. @ProjectEntityUpdateHandler.addFile.mainTask
  489. .calledWith(project_id, folder_id, @fileName, @fileSystemPath, @linkedFileData, userId)
  490. .should.equal true
  491. it 'returns the file', ->
  492. @callback.calledWith(null, @newFile, true)
  493. describe 'upsertDocWithPath', ->
  494. beforeEach ->
  495. @path = "/folder/doc.tex"
  496. @newFolders = [ 'mock-a', 'mock-b' ]
  497. @folder = _id: folder_id
  498. @doc = _id: doc_id
  499. @isNewDoc = true
  500. @ProjectEntityUpdateHandler.mkdirp =
  501. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  502. @ProjectEntityUpdateHandler.upsertDoc =
  503. withoutLock: sinon.stub().yields(null, @doc, @isNewDoc)
  504. @ProjectEntityUpdateHandler.upsertDocWithPath project_id, @path, @docLines, @source, userId, @callback
  505. it 'creates any necessary folders', ->
  506. @ProjectEntityUpdateHandler.mkdirp.withoutLock
  507. .calledWith(project_id, '/folder')
  508. .should.equal true
  509. it 'upserts the doc', ->
  510. @ProjectEntityUpdateHandler.upsertDoc.withoutLock
  511. .calledWith(project_id, @folder._id, 'doc.tex', @docLines, @source, userId)
  512. .should.equal true
  513. it 'calls the callback', ->
  514. @callback
  515. .calledWith(null, @doc, @isNewDoc, @newFolders, @folder)
  516. .should.equal true
  517. describe 'upsertFileWithPath', ->
  518. beforeEach ->
  519. @path = "/folder/file.png"
  520. @newFolders = [ 'mock-a', 'mock-b' ]
  521. @folder = _id: folder_id
  522. @file = _id: file_id
  523. @isNewFile = true
  524. @ProjectEntityUpdateHandler.mkdirp =
  525. withoutLock: sinon.stub().yields(null, @newFolders, @folder)
  526. @ProjectEntityUpdateHandler.upsertFile =
  527. mainTask: sinon.stub().yields(null, @file, @isNewFile)
  528. @ProjectEntityUpdateHandler.upsertFileWithPath project_id, @path, @fileSystemPath, @linkedFileData, userId, @callback
  529. it 'creates any necessary folders', ->
  530. @ProjectEntityUpdateHandler.mkdirp.withoutLock
  531. .calledWith(project_id, '/folder')
  532. .should.equal true
  533. it 'upserts the file', ->
  534. @ProjectEntityUpdateHandler.upsertFile.mainTask
  535. .calledWith(project_id, @folder._id, 'file.png', @fileSystemPath, @linkedFileData, userId)
  536. .should.equal true
  537. it 'calls the callback', ->
  538. @callback
  539. .calledWith(null, @file, @isNewFile, undefined, @newFolders, @folder)
  540. .should.equal true
  541. describe 'deleteEntity', ->
  542. beforeEach ->
  543. @path = '/path/to/doc.tex'
  544. @doc = _id: doc_id
  545. @projectBeforeDeletion = _id: project_id, name: 'project'
  546. @ProjectEntityMongoUpdateHandler.deleteEntity = sinon.stub().yields(null, @doc, {fileSystem: @path}, @projectBeforeDeletion)
  547. @ProjectEntityUpdateHandler._cleanUpEntity = sinon.stub().yields()
  548. @TpdsUpdateSender.deleteEntity = sinon.stub().yields()
  549. @ProjectEntityUpdateHandler.deleteEntity project_id, doc_id, 'doc', userId, @callback
  550. it 'deletes the entity in mongo', ->
  551. @ProjectEntityMongoUpdateHandler.deleteEntity
  552. .calledWith(project_id, doc_id, 'doc')
  553. .should.equal true
  554. it 'cleans up the doc in the docstore', ->
  555. @ProjectEntityUpdateHandler._cleanUpEntity
  556. .calledWith(@projectBeforeDeletion, @doc, 'doc', @path, userId)
  557. .should.equal true
  558. it 'it notifies the tpds', ->
  559. @TpdsUpdateSender.deleteEntity
  560. .calledWith({ project_id, @path, project_name: @projectBeforeDeletion.name })
  561. .should.equal true
  562. it 'retuns the entity_id', ->
  563. @callback.calledWith(null, doc_id).should.equal true
  564. describe 'deleteEntityWithPath', ->
  565. describe 'when the entity exists', ->
  566. beforeEach ->
  567. @doc = _id: doc_id
  568. @ProjectLocator.findElementByPath = sinon.stub().yields(null, @doc, 'doc')
  569. @ProjectEntityUpdateHandler.deleteEntity =
  570. withoutLock: sinon.stub().yields()
  571. @path = '/path/to/doc.tex'
  572. @ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
  573. it 'finds the entity', ->
  574. @ProjectLocator.findElementByPath
  575. .calledWith({project_id, @path})
  576. .should.equal true
  577. it 'deletes the entity', ->
  578. @ProjectEntityUpdateHandler.deleteEntity.withoutLock
  579. .calledWith(project_id, @doc._id, 'doc', userId, @callback)
  580. .should.equal true
  581. describe 'when the entity does not exist', ->
  582. beforeEach ->
  583. @ProjectLocator.findElementByPath = sinon.stub().yields()
  584. @path = '/doc.tex'
  585. @ProjectEntityUpdateHandler.deleteEntityWithPath project_id, @path, userId, @callback
  586. it 'returns an error', ->
  587. @callback.calledWith(new Errors.NotFoundError()).should.equal true
  588. describe 'mkdirp', ->
  589. beforeEach ->
  590. @docPath = '/folder/doc.tex'
  591. @ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
  592. @ProjectEntityUpdateHandler.mkdirp project_id, @docPath, @callback
  593. it 'calls ProjectEntityMongoUpdateHandler', ->
  594. @ProjectEntityMongoUpdateHandler.mkdirp
  595. .calledWith(project_id, @docPath)
  596. .should.equal true
  597. describe 'addFolder', ->
  598. beforeEach ->
  599. @parentFolder_id = '123asdf'
  600. @folderName = 'new-folder'
  601. @ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  602. @ProjectEntityUpdateHandler.addFolder project_id, @parentFolder_id, @folderName, @callback
  603. it 'calls ProjectEntityMongoUpdateHandler', ->
  604. @ProjectEntityMongoUpdateHandler.addFolder
  605. .calledWith(project_id, @parentFolder_id, @folderName)
  606. .should.equal true
  607. describe 'moveEntity', ->
  608. beforeEach ->
  609. @project_name = 'project name'
  610. @startPath = '/a.tex'
  611. @endPath = '/folder/b.tex'
  612. @rev = 2
  613. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  614. @ProjectEntityMongoUpdateHandler.moveEntity = sinon.stub().yields(
  615. null, @project, @startPath, @endPath, @rev, @changes
  616. )
  617. @TpdsUpdateSender.moveEntity = sinon.stub()
  618. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  619. @ProjectEntityUpdateHandler.moveEntity project_id, doc_id, folder_id, 'doc', userId, @callback
  620. it 'moves the entity in mongo', ->
  621. @ProjectEntityMongoUpdateHandler.moveEntity
  622. .calledWith(project_id, doc_id, folder_id, 'doc')
  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 "renameEntity", ->
  633. beforeEach ->
  634. @project_name = 'project name'
  635. @startPath = '/folder/a.tex'
  636. @endPath = '/folder/b.tex'
  637. @rev = 2
  638. @changes = newDocs: ['old-doc'], newFiles: ['old-file']
  639. @newDocName = 'b.tex'
  640. @ProjectEntityMongoUpdateHandler.renameEntity = sinon.stub().yields(
  641. null, @project, @startPath, @endPath, @rev, @changes
  642. )
  643. @TpdsUpdateSender.moveEntity = sinon.stub()
  644. @DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  645. @ProjectEntityUpdateHandler.renameEntity project_id, doc_id, 'doc', @newDocName, userId, @callback
  646. it 'moves the entity in mongo', ->
  647. @ProjectEntityMongoUpdateHandler.renameEntity
  648. .calledWith(project_id, doc_id, 'doc', @newDocName)
  649. .should.equal true
  650. it 'notifies tpds', ->
  651. @TpdsUpdateSender.moveEntity
  652. .calledWith({project_id, @project_name, @startPath, @endPath, @rev})
  653. .should.equal true
  654. it 'sends the changes in project structure to the doc updater', ->
  655. @DocumentUpdaterHandler.updateProjectStructure
  656. .calledWith(project_id, projectHistoryId, userId, @changes, @callback)
  657. .should.equal true
  658. describe "resyncProjectHistory", ->
  659. describe "a deleted project", ->
  660. beforeEach ->
  661. @ProjectGetter.getProject = sinon.stub().yields()
  662. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  663. it "should return an error", ->
  664. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  665. @callback.calledWith(error).should.equal true
  666. describe "a project without project-history enabled", ->
  667. beforeEach ->
  668. @project.overleaf = {}
  669. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  670. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  671. it "should return an error", ->
  672. error = new Errors.ProjectHistoryDisabledError("project history not enabled for #{project_id}")
  673. @callback.calledWith(error).should.equal true
  674. describe "a project with project-history enabled", ->
  675. beforeEach ->
  676. @ProjectGetter.getProject = sinon.stub().yields(null, @project)
  677. docs = [
  678. doc: _id: doc_id
  679. path: 'main.tex'
  680. ]
  681. files = [
  682. file: _id: file_id
  683. path: 'universe.png'
  684. ]
  685. @ProjectEntityHandler.getAllEntitiesFromProject = sinon.stub().yields(null, docs, files)
  686. @FileStoreHandler._buildUrl = (project_id, file_id) ->
  687. "www.filestore.test/#{project_id}/#{file_id}"
  688. @DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
  689. @ProjectEntityUpdateHandler.resyncProjectHistory project_id, @callback
  690. it 'gets the project', ->
  691. @ProjectGetter.getProject
  692. .calledWith(project_id)
  693. .should.equal true
  694. it 'gets the entities for the project', ->
  695. @ProjectEntityHandler.getAllEntitiesFromProject
  696. .calledWith(@project)
  697. .should.equal true
  698. it 'tells the doc updater to sync the project', ->
  699. docs = [
  700. doc: doc_id
  701. path: 'main.tex'
  702. ]
  703. files = [
  704. file: file_id
  705. path: 'universe.png'
  706. url: "www.filestore.test/#{project_id}/#{file_id}"
  707. ]
  708. @DocumentUpdaterHandler.resyncProjectHistory
  709. .calledWith(project_id, projectHistoryId, docs, files)
  710. .should.equal true
  711. it 'calls the callback', ->
  712. @callback.called.should.equal true
  713. describe "_cleanUpEntity", ->
  714. beforeEach ->
  715. @entity_id = "4eecaffcbffa66588e000009"
  716. @FileStoreHandler.deleteFile = sinon.stub().yields()
  717. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  718. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  719. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon.stub().yields()
  720. describe "a file", ->
  721. beforeEach (done) ->
  722. @path = "/file/system/path.png"
  723. @entity = _id: @entity_id
  724. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'file', @path, userId, done
  725. it "should insert the file into the deletedFiles array", ->
  726. @ProjectEntityMongoUpdateHandler._insertDeletedFileReference
  727. .calledWith(@project._id, @entity)
  728. .should.equal true
  729. it "should not delete the file from FileStoreHandler", ->
  730. @FileStoreHandler.deleteFile.calledWith(project_id, @entity_id).should.equal false
  731. it "should not attempt to delete from the document updater", ->
  732. @DocumentUpdaterHandler.deleteDoc.called.should.equal false
  733. it "should should send the update to the doc updater", ->
  734. oldFiles = [ file: @entity, path: @path ]
  735. @DocumentUpdaterHandler.updateProjectStructure
  736. .calledWith(project_id, projectHistoryId, userId, {oldFiles})
  737. .should.equal true
  738. describe "a doc", ->
  739. beforeEach (done) ->
  740. @path = "/file/system/path.tex"
  741. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  742. @entity = {_id: @entity_id}
  743. @ProjectEntityUpdateHandler._cleanUpEntity @project, @entity, 'doc', @path, userId, done
  744. it "should clean up the doc", ->
  745. @ProjectEntityUpdateHandler._cleanUpDoc
  746. .calledWith(@project, @entity, @path, userId)
  747. .should.equal true
  748. it "should should send the update to the doc updater", ->
  749. oldDocs = [ doc: @entity, path: @path ]
  750. @DocumentUpdaterHandler.updateProjectStructure
  751. .calledWith(project_id, projectHistoryId, userId, {oldDocs})
  752. .should.equal true
  753. describe "a folder", ->
  754. beforeEach (done) ->
  755. @folder =
  756. folders: [
  757. name: "subfolder"
  758. fileRefs: [ @file1 = { _id: "file-id-1", name: "file-name-1"} ]
  759. docs: [ @doc1 = { _id: "doc-id-1", name: "doc-name-1" } ]
  760. folders: []
  761. ]
  762. fileRefs: [ @file2 = { _id: "file-id-2", name: "file-name-2" } ]
  763. docs: [ @doc2 = { _id: "doc-id-2", name: "doc-name-2" } ]
  764. @ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  765. @ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
  766. path = "/folder"
  767. @ProjectEntityUpdateHandler._cleanUpEntity @project, @folder, "folder", path, userId, done
  768. it "should clean up all sub files", ->
  769. @ProjectEntityUpdateHandler._cleanUpFile
  770. .calledWith(@project, @file1, "/folder/subfolder/file-name-1", userId)
  771. .should.equal true
  772. @ProjectEntityUpdateHandler._cleanUpFile
  773. .calledWith(@project, @file2, "/folder/file-name-2", userId)
  774. .should.equal true
  775. it "should clean up all sub docs", ->
  776. @ProjectEntityUpdateHandler._cleanUpDoc
  777. .calledWith(@project, @doc1, "/folder/subfolder/doc-name-1", userId)
  778. .should.equal true
  779. @ProjectEntityUpdateHandler._cleanUpDoc
  780. .calledWith(@project, @doc2, "/folder/doc-name-2", userId)
  781. .should.equal true
  782. it "should should send one update to the doc updater for all docs and files", ->
  783. oldFiles = [ {file: @file2, path: "/folder/file-name-2"}, {file: @file1, path: "/folder/subfolder/file-name-1"} ]
  784. oldDocs = [ {doc: @doc2, path: "/folder/doc-name-2"}, { doc: @doc1, path: "/folder/subfolder/doc-name-1"} ]
  785. @DocumentUpdaterHandler.updateProjectStructure
  786. .calledWith(project_id, projectHistoryId, userId, {oldFiles, oldDocs})
  787. .should.equal true
  788. describe "_cleanUpDoc", ->
  789. beforeEach ->
  790. @doc =
  791. _id: ObjectId()
  792. name: "test.tex"
  793. @path = "/path/to/doc"
  794. @ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  795. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference = sinon.stub().yields()
  796. @DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  797. @DocstoreManager.deleteDoc = sinon.stub().yields()
  798. @callback = sinon.stub()
  799. describe "when the doc is the root doc", ->
  800. beforeEach ->
  801. @project.rootDoc_id = @doc._id
  802. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  803. it "should unset the root doc", ->
  804. @ProjectEntityUpdateHandler.unsetRootDoc
  805. .calledWith(project_id)
  806. .should.equal true
  807. it "should delete the doc in the doc updater", ->
  808. @DocumentUpdaterHandler.deleteDoc
  809. .calledWith(project_id, @doc._id.toString())
  810. it "should insert the doc into the deletedDocs array", ->
  811. @ProjectEntityMongoUpdateHandler._insertDeletedDocReference
  812. .calledWith(@project._id, @doc)
  813. .should.equal true
  814. it "should delete the doc in the doc store", ->
  815. @DocstoreManager.deleteDoc
  816. .calledWith(project_id, @doc._id.toString())
  817. .should.equal true
  818. it "should call the callback", ->
  819. @callback.called.should.equal true
  820. describe "when the doc is not the root doc", ->
  821. beforeEach ->
  822. @project.rootDoc_id = ObjectId()
  823. @ProjectEntityUpdateHandler._cleanUpDoc @project, @doc, @path, userId, @callback
  824. it "should not unset the root doc", ->
  825. @ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal false
  826. it "should call the callback", ->
  827. @callback.called.should.equal true