ProjectEntityUpdateHandlerTests.coffee 32 KB

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