ProjectEntityUpdateHandlerTests.coffee 33 KB

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