ProjectEntityUpdateHandlerTests.coffee 31 KB

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