ProjectEntityUpdateHandlerTests.coffee 34 KB

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