ProjectEntityUpdateHandlerTests.coffee 34 KB

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