ProjectEntityUpdateHandlerTests.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  1. /* eslint-disable
  2. camelcase,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const chai = require('chai')
  16. const { assert } = require('chai')
  17. const should = chai.should()
  18. const { expect } = chai
  19. const modulePath =
  20. '../../../../app/src/Features/Project/ProjectEntityUpdateHandler'
  21. const sinon = require('sinon')
  22. const Errors = require('../../../../app/src/Features/Errors/Errors')
  23. const SandboxedModule = require('sandboxed-module')
  24. const { ObjectId } = require('mongoose').Types
  25. describe('ProjectEntityUpdateHandler', function() {
  26. const project_id = '4eecb1c1bffa66588e0000a1'
  27. const projectHistoryId = '123456'
  28. const doc_id = '4eecb1c1bffa66588e0000a2'
  29. const file_id = '4eecaffcbffa66588e000009'
  30. const folder_id = '4eecaffcbffa66588e000008'
  31. const rootFolderId = '4eecaffcbffa66588e000007'
  32. const new_file_id = '4eecaffcbffa66588e000099'
  33. const userId = 1234
  34. beforeEach(function() {
  35. let Doc, File
  36. this.project = {
  37. _id: project_id,
  38. name: 'project name',
  39. overleaf: {
  40. history: {
  41. id: projectHistoryId
  42. }
  43. }
  44. }
  45. this.fileUrl = 'filestore.example.com/file'
  46. this.FileStoreHandler = {}
  47. this.DocModel = Doc = class Doc {
  48. constructor(options) {
  49. ;({ name: this.name, lines: this.lines } = options)
  50. this._id = doc_id
  51. this.rev = 0
  52. }
  53. }
  54. this.FileModel = File = class File {
  55. constructor(options) {
  56. ;({ name: this.name } = options)
  57. // use a new id for replacement files
  58. if (this.name === 'dummy-upload-filename') {
  59. this._id = new_file_id
  60. } else {
  61. this._id = file_id
  62. }
  63. this.rev = 0
  64. if (options.linkedFileData != null) {
  65. this.linkedFileData = options.linkedFileData
  66. }
  67. if (options.hash != null) {
  68. this.hash = options.hash
  69. }
  70. }
  71. }
  72. this.docName = 'doc-name'
  73. this.docLines = ['1234', 'abc']
  74. this.fileName = 'something.jpg'
  75. this.fileSystemPath = 'somehintg'
  76. this.linkedFileData = { provider: 'url' }
  77. this.source = 'editor'
  78. this.callback = sinon.stub()
  79. return (this.ProjectEntityUpdateHandler = SandboxedModule.require(
  80. modulePath,
  81. {
  82. globals: {
  83. console: console
  84. },
  85. requires: {
  86. 'logger-sharelatex': (this.logger = {
  87. log: sinon.stub(),
  88. warn: sinon.stub(),
  89. error: sinon.stub(),
  90. err() {}
  91. }),
  92. '../../models/Doc': {
  93. Doc: this.DocModel
  94. },
  95. '../Docstore/DocstoreManager': (this.DocstoreManager = {}),
  96. '../Errors/Errors': Errors,
  97. '../../Features/DocumentUpdater/DocumentUpdaterHandler': (this.DocumentUpdaterHandler = {
  98. updateProjectStructure: sinon.stub().yields()
  99. }),
  100. '../../models/File': {
  101. File: this.FileModel
  102. },
  103. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  104. '../../infrastructure/LockManager': (this.LockManager = {
  105. runWithLock: sinon.spy((namespace, id, runner, callback) =>
  106. runner(callback)
  107. )
  108. }),
  109. '../../models/Project': {
  110. Project: (this.ProjectModel = {})
  111. },
  112. './ProjectGetter': (this.ProjectGetter = {}),
  113. './ProjectLocator': (this.ProjectLocator = {}),
  114. './ProjectUpdateHandler': (this.ProjectUpdater = {}),
  115. './ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  116. './ProjectEntityMongoUpdateHandler': (this.ProjectEntityMongoUpdateHandler = {}),
  117. '../ThirdPartyDataStore/TpdsUpdateSender': (this.TpdsUpdateSender = {
  118. addFile: sinon.stub().yields()
  119. })
  120. }
  121. }
  122. ))
  123. })
  124. describe('copyFileFromExistingProjectWithProject', function() {
  125. beforeEach(function() {
  126. this.oldProject_id = '123kljadas'
  127. this.oldFileRef = { name: this.fileName, _id: 'oldFileRef' }
  128. this.ProjectEntityMongoUpdateHandler._confirmFolder = sinon
  129. .stub()
  130. .returns(folder_id)
  131. this.ProjectEntityMongoUpdateHandler._putElement = sinon
  132. .stub()
  133. .yields(null, { path: { fileSystem: this.fileSystemPath } })
  134. this.FileStoreHandler.copyFile = sinon.stub().yields(null, this.fileUrl)
  135. return this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject(
  136. this.project._id,
  137. this.project,
  138. folder_id,
  139. this.oldProject_id,
  140. this.oldFileRef,
  141. userId,
  142. this.callback
  143. )
  144. })
  145. it('should copy the file in FileStoreHandler', function() {
  146. return this.FileStoreHandler.copyFile
  147. .calledWith(
  148. this.oldProject_id,
  149. this.oldFileRef._id,
  150. project_id,
  151. file_id
  152. )
  153. .should.equal(true)
  154. })
  155. it('should put file into folder by calling put element', function() {
  156. return this.ProjectEntityMongoUpdateHandler._putElement
  157. .calledWithMatch(
  158. this.project,
  159. folder_id,
  160. { _id: file_id, name: this.fileName },
  161. 'file'
  162. )
  163. .should.equal(true)
  164. })
  165. it('should return doc and parent folder', function() {
  166. return this.callback
  167. .calledWithMatch(null, { _id: file_id, name: this.fileName }, folder_id)
  168. .should.equal(true)
  169. })
  170. it('should call third party data store if versioning is enabled', function() {
  171. return this.TpdsUpdateSender.addFile
  172. .calledWith({
  173. project_id,
  174. file_id,
  175. path: this.fileSystemPath,
  176. rev: 0,
  177. project_name: this.project.name
  178. })
  179. .should.equal(true)
  180. })
  181. it('should should send the change in project structure to the doc updater', function() {
  182. const changesMatcher = sinon.match(changes => {
  183. const { newFiles } = changes
  184. if (newFiles.length !== 1) {
  185. return false
  186. }
  187. const newFile = newFiles[0]
  188. return (
  189. newFile.file._id === file_id &&
  190. newFile.path === this.fileSystemPath &&
  191. newFile.url === this.fileUrl
  192. )
  193. })
  194. return this.DocumentUpdaterHandler.updateProjectStructure
  195. .calledWithMatch(project_id, projectHistoryId, userId, changesMatcher)
  196. .should.equal(true)
  197. })
  198. })
  199. describe('copyFileFromExistingProjectWithProject, with linkedFileData and hash', function() {
  200. beforeEach(function() {
  201. this.oldProject_id = '123kljadas'
  202. this.oldFileRef = {
  203. _id: 'oldFileRef',
  204. name: this.fileName,
  205. linkedFileData: this.linkedFileData,
  206. hash: '123456'
  207. }
  208. this.ProjectEntityMongoUpdateHandler._confirmFolder = sinon
  209. .stub()
  210. .returns(folder_id)
  211. this.ProjectEntityMongoUpdateHandler._putElement = sinon
  212. .stub()
  213. .yields(null, { path: { fileSystem: this.fileSystemPath } })
  214. this.FileStoreHandler.copyFile = sinon.stub().yields(null, this.fileUrl)
  215. return this.ProjectEntityUpdateHandler.copyFileFromExistingProjectWithProject(
  216. this.project._id,
  217. this.project,
  218. folder_id,
  219. this.oldProject_id,
  220. this.oldFileRef,
  221. userId,
  222. this.callback
  223. )
  224. })
  225. it('should copy the file in FileStoreHandler', function() {
  226. return this.FileStoreHandler.copyFile
  227. .calledWith(
  228. this.oldProject_id,
  229. this.oldFileRef._id,
  230. project_id,
  231. file_id
  232. )
  233. .should.equal(true)
  234. })
  235. it('should put file into folder by calling put element, with the linkedFileData and hash', function() {
  236. return this.ProjectEntityMongoUpdateHandler._putElement
  237. .calledWithMatch(
  238. this.project,
  239. folder_id,
  240. {
  241. _id: file_id,
  242. name: this.fileName,
  243. linkedFileData: this.linkedFileData,
  244. hash: '123456'
  245. },
  246. 'file'
  247. )
  248. .should.equal(true)
  249. })
  250. })
  251. describe('updateDocLines', function() {
  252. beforeEach(function() {
  253. this.path = '/somewhere/something.tex'
  254. this.doc = {
  255. _id: doc_id
  256. }
  257. this.version = 42
  258. this.ranges = { mock: 'ranges' }
  259. this.lastUpdatedAt = new Date().getTime()
  260. this.lastUpdatedBy = 'fake-last-updater-id'
  261. this.ProjectGetter.getProjectWithoutDocLines = sinon
  262. .stub()
  263. .yields(null, this.project)
  264. this.ProjectLocator.findElement = sinon
  265. .stub()
  266. .yields(null, this.doc, { fileSystem: this.path })
  267. this.TpdsUpdateSender.addDoc = sinon.stub().yields()
  268. this.ProjectUpdater.markAsUpdated = sinon.stub()
  269. return (this.callback = sinon.stub())
  270. })
  271. describe('when the doc has been modified', function() {
  272. beforeEach(function() {
  273. this.DocstoreManager.updateDoc = sinon
  274. .stub()
  275. .yields(null, true, (this.rev = 5))
  276. return this.ProjectEntityUpdateHandler.updateDocLines(
  277. project_id,
  278. doc_id,
  279. this.docLines,
  280. this.version,
  281. this.ranges,
  282. this.lastUpdatedAt,
  283. this.lastUpdatedBy,
  284. this.callback
  285. )
  286. })
  287. it('should get the project without doc lines', function() {
  288. return this.ProjectGetter.getProjectWithoutDocLines
  289. .calledWith(project_id)
  290. .should.equal(true)
  291. })
  292. it('should find the doc', function() {
  293. return this.ProjectLocator.findElement
  294. .calledWith({
  295. project: this.project,
  296. type: 'docs',
  297. element_id: doc_id
  298. })
  299. .should.equal(true)
  300. })
  301. it('should update the doc in the docstore', function() {
  302. return this.DocstoreManager.updateDoc
  303. .calledWith(
  304. project_id,
  305. doc_id,
  306. this.docLines,
  307. this.version,
  308. this.ranges
  309. )
  310. .should.equal(true)
  311. })
  312. it('should mark the project as updated', function() {
  313. return sinon.assert.calledWith(
  314. this.ProjectUpdater.markAsUpdated,
  315. project_id,
  316. this.lastUpdatedAt,
  317. this.lastUpdatedBy
  318. )
  319. })
  320. it('should send the doc the to the TPDS', function() {
  321. return this.TpdsUpdateSender.addDoc
  322. .calledWith({
  323. project_id,
  324. project_name: this.project.name,
  325. doc_id,
  326. rev: this.rev,
  327. path: this.path
  328. })
  329. .should.equal(true)
  330. })
  331. it('should call the callback', function() {
  332. return this.callback.called.should.equal(true)
  333. })
  334. })
  335. describe('when the doc has not been modified', function() {
  336. beforeEach(function() {
  337. this.DocstoreManager.updateDoc = sinon
  338. .stub()
  339. .yields(null, false, (this.rev = 5))
  340. return this.ProjectEntityUpdateHandler.updateDocLines(
  341. project_id,
  342. doc_id,
  343. this.docLines,
  344. this.version,
  345. this.ranges,
  346. this.lastUpdatedAt,
  347. this.lastUpdatedBy,
  348. this.callback
  349. )
  350. })
  351. it('should not mark the project as updated', function() {
  352. return this.ProjectUpdater.markAsUpdated.called.should.equal(false)
  353. })
  354. it('should not send the doc the to the TPDS', function() {
  355. return this.TpdsUpdateSender.addDoc.called.should.equal(false)
  356. })
  357. it('should call the callback', function() {
  358. return this.callback.called.should.equal(true)
  359. })
  360. })
  361. describe('when the doc has been deleted', function() {
  362. beforeEach(function() {
  363. this.project.deletedDocs = [{ _id: doc_id }]
  364. this.ProjectGetter.getProjectWithoutDocLines = sinon
  365. .stub()
  366. .yields(null, this.project)
  367. this.ProjectLocator.findElement = sinon
  368. .stub()
  369. .yields(new Errors.NotFoundError())
  370. this.DocstoreManager.updateDoc = sinon.stub().yields()
  371. return this.ProjectEntityUpdateHandler.updateDocLines(
  372. project_id,
  373. doc_id,
  374. this.docLines,
  375. this.version,
  376. this.ranges,
  377. this.lastUpdatedAt,
  378. this.lastUpdatedBy,
  379. this.callback
  380. )
  381. })
  382. it('should update the doc in the docstore', function() {
  383. return this.DocstoreManager.updateDoc
  384. .calledWith(
  385. project_id,
  386. doc_id,
  387. this.docLines,
  388. this.version,
  389. this.ranges
  390. )
  391. .should.equal(true)
  392. })
  393. it('should not mark the project as updated', function() {
  394. return this.ProjectUpdater.markAsUpdated.called.should.equal(false)
  395. })
  396. it('should not send the doc the to the TPDS', function() {
  397. return this.TpdsUpdateSender.addDoc.called.should.equal(false)
  398. })
  399. it('should call the callback', function() {
  400. return this.callback.called.should.equal(true)
  401. })
  402. })
  403. describe('when the doc is not related to the project', function() {
  404. beforeEach(function() {
  405. this.ProjectLocator.findElement = sinon.stub().yields()
  406. return this.ProjectEntityUpdateHandler.updateDocLines(
  407. project_id,
  408. doc_id,
  409. this.docLines,
  410. this.version,
  411. this.ranges,
  412. this.lastUpdatedAt,
  413. this.lastUpdatedBy,
  414. this.callback
  415. )
  416. })
  417. it('should log out the error', function() {
  418. return this.logger.warn
  419. .calledWith(
  420. {
  421. projectId: project_id,
  422. docId: doc_id,
  423. lines: this.docLines
  424. },
  425. 'doc not found while updating doc lines'
  426. )
  427. .should.equal(true)
  428. })
  429. it('should return a not found error', function() {
  430. return this.callback
  431. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  432. .should.equal(true)
  433. })
  434. })
  435. describe('when the project is not found', function() {
  436. beforeEach(function() {
  437. this.ProjectGetter.getProjectWithoutDocLines = sinon.stub().yields()
  438. return this.ProjectEntityUpdateHandler.updateDocLines(
  439. project_id,
  440. doc_id,
  441. this.docLines,
  442. this.version,
  443. this.ranges,
  444. this.lastUpdatedAt,
  445. this.lastUpdatedBy,
  446. this.callback
  447. )
  448. })
  449. it('should return a not found error', function() {
  450. return this.callback
  451. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  452. .should.equal(true)
  453. })
  454. })
  455. })
  456. describe('setRootDoc', function() {
  457. beforeEach(function() {
  458. this.rootDoc_id = 'root-doc-id-123123'
  459. this.callback = sinon.stub()
  460. })
  461. it('should call Project.update when the doc exists and has a valid extension', function() {
  462. this.ProjectModel.update = sinon.stub()
  463. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId = sinon
  464. .stub()
  465. .yields(null, `/main.tex`)
  466. this.ProjectEntityUpdateHandler.setRootDoc(
  467. project_id,
  468. this.rootDoc_id,
  469. () => {}
  470. )
  471. return this.ProjectModel.update
  472. .calledWith({ _id: project_id }, { rootDoc_id: this.rootDoc_id })
  473. .should.equal(true)
  474. })
  475. it("should not call Project.update when the doc doesn't exist", function() {
  476. this.ProjectModel.update = sinon.stub()
  477. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId = sinon
  478. .stub()
  479. .yields(Errors.NotFoundError)
  480. this.ProjectEntityUpdateHandler.setRootDoc(
  481. project_id,
  482. this.rootDoc_id,
  483. () => {}
  484. )
  485. return this.ProjectModel.update
  486. .calledWith({ _id: project_id }, { rootDoc_id: this.rootDoc_id })
  487. .should.equal(false)
  488. })
  489. it('should call the callback with an UnsupportedFileTypeError when the doc has an unaccepted file extension', function() {
  490. this.ProjectModel.update = sinon.stub()
  491. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId = sinon
  492. .stub()
  493. .yields(null, `/foo/bar.baz`)
  494. this.ProjectEntityUpdateHandler.setRootDoc(
  495. project_id,
  496. this.rootDoc_id,
  497. this.callback
  498. )
  499. return expect(this.callback.firstCall.args[0]).to.be.an.instanceof(
  500. Errors.UnsupportedFileTypeError
  501. )
  502. })
  503. })
  504. describe('unsetRootDoc', function() {
  505. it('should call Project.update', function() {
  506. this.ProjectModel.update = sinon.stub()
  507. this.ProjectEntityUpdateHandler.unsetRootDoc(project_id)
  508. return this.ProjectModel.update
  509. .calledWith({ _id: project_id }, { $unset: { rootDoc_id: true } })
  510. .should.equal(true)
  511. })
  512. })
  513. describe('addDoc', function() {
  514. describe('adding a doc', function() {
  515. beforeEach(function() {
  516. this.path = '/path/to/doc'
  517. this.newDoc = new this.DocModel({
  518. name: this.docName,
  519. lines: undefined,
  520. _id: doc_id,
  521. rev: 0
  522. })
  523. this.DocstoreManager.updateDoc = sinon
  524. .stub()
  525. .yields(null, false, (this.rev = 5))
  526. this.TpdsUpdateSender.addDoc = sinon.stub().yields()
  527. this.ProjectEntityMongoUpdateHandler.addDoc = sinon
  528. .stub()
  529. .yields(null, { path: { fileSystem: this.path } }, this.project)
  530. return this.ProjectEntityUpdateHandler.addDoc(
  531. project_id,
  532. doc_id,
  533. this.docName,
  534. this.docLines,
  535. userId,
  536. this.callback
  537. )
  538. })
  539. it('creates the doc without history', function() {
  540. return this.DocstoreManager.updateDoc
  541. .calledWith(project_id, doc_id, this.docLines, 0, {})
  542. .should.equal(true)
  543. })
  544. it('sends the change in project structure to the doc updater', function() {
  545. const newDocs = [
  546. {
  547. doc: this.newDoc,
  548. path: this.path,
  549. docLines: this.docLines.join('\n')
  550. }
  551. ]
  552. this.DocumentUpdaterHandler.updateProjectStructure
  553. .calledWith(project_id, projectHistoryId, userId, {
  554. newDocs,
  555. newProject: this.project
  556. })
  557. .should.equal(true)
  558. })
  559. })
  560. describe('adding a doc with an invalid name', function() {
  561. beforeEach(function() {
  562. this.path = '/path/to/doc'
  563. this.newDoc = { _id: doc_id }
  564. return this.ProjectEntityUpdateHandler.addDoc(
  565. project_id,
  566. folder_id,
  567. `*${this.docName}`,
  568. this.docLines,
  569. userId,
  570. this.callback
  571. )
  572. })
  573. it('returns an error', function() {
  574. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  575. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  576. })
  577. })
  578. })
  579. describe('addFile', function() {
  580. describe('adding a file', function() {
  581. beforeEach(function() {
  582. this.path = '/path/to/file'
  583. this.newFile = {
  584. _id: file_id,
  585. rev: 0,
  586. name: this.fileName,
  587. linkedFileData: this.linkedFileData
  588. }
  589. this.FileStoreHandler.uploadFileFromDisk = sinon
  590. .stub()
  591. .yields(null, this.fileUrl, this.newFile)
  592. this.TpdsUpdateSender.addFile = sinon.stub().yields()
  593. this.ProjectEntityMongoUpdateHandler.addFile = sinon
  594. .stub()
  595. .yields(null, { path: { fileSystem: this.path } }, this.project)
  596. return this.ProjectEntityUpdateHandler.addFile(
  597. project_id,
  598. folder_id,
  599. this.fileName,
  600. this.fileSystemPath,
  601. this.linkedFileData,
  602. userId,
  603. this.callback
  604. )
  605. })
  606. it('updates the file in the filestore', function() {
  607. return this.FileStoreHandler.uploadFileFromDisk
  608. .calledWith(
  609. project_id,
  610. { name: this.fileName, linkedFileData: this.linkedFileData },
  611. this.fileSystemPath
  612. )
  613. .should.equal(true)
  614. })
  615. it('updates the file in mongo', function() {
  616. const fileMatcher = sinon.match(file => {
  617. return file.name === this.fileName
  618. })
  619. return this.ProjectEntityMongoUpdateHandler.addFile
  620. .calledWithMatch(project_id, folder_id, fileMatcher)
  621. .should.equal(true)
  622. })
  623. it('notifies the tpds', function() {
  624. return this.TpdsUpdateSender.addFile
  625. .calledWith({
  626. project_id,
  627. project_name: this.project.name,
  628. file_id,
  629. rev: 0,
  630. path: this.path
  631. })
  632. .should.equal(true)
  633. })
  634. it('sends the change in project structure to the doc updater', function() {
  635. const newFiles = [
  636. {
  637. file: this.newFile,
  638. path: this.path,
  639. url: this.fileUrl
  640. }
  641. ]
  642. return this.DocumentUpdaterHandler.updateProjectStructure
  643. .calledWith(project_id, projectHistoryId, userId, {
  644. newFiles,
  645. newProject: this.project
  646. })
  647. .should.equal(true)
  648. })
  649. })
  650. describe('adding a file with an invalid name', function() {
  651. beforeEach(function() {
  652. this.path = '/path/to/file'
  653. this.newFile = {
  654. _id: file_id,
  655. rev: 0,
  656. name: this.fileName,
  657. linkedFileData: this.linkedFileData
  658. }
  659. this.TpdsUpdateSender.addFile = sinon.stub().yields()
  660. this.ProjectEntityMongoUpdateHandler.addFile = sinon
  661. .stub()
  662. .yields(null, { path: { fileSystem: this.path } }, this.project)
  663. return this.ProjectEntityUpdateHandler.addFile(
  664. project_id,
  665. folder_id,
  666. `*${this.fileName}`,
  667. this.fileSystemPath,
  668. this.linkedFileData,
  669. userId,
  670. this.callback
  671. )
  672. })
  673. it('returns an error', function() {
  674. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  675. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  676. })
  677. })
  678. })
  679. describe('replaceFile', function() {
  680. beforeEach(function() {
  681. // replacement file now creates a new file object
  682. this.newFileUrl = 'new-file-url'
  683. this.FileStoreHandler.uploadFileFromDisk = sinon
  684. .stub()
  685. .yields(null, this.newFileUrl, this.newFile)
  686. this.newFile = {
  687. _id: new_file_id,
  688. name: 'dummy-upload-filename',
  689. rev: 0,
  690. linkedFileData: this.linkedFileData
  691. }
  692. this.oldFile = { _id: file_id, rev: 3 }
  693. this.path = '/path/to/file'
  694. this.newProject = 'new project'
  695. this.FileStoreHandler.uploadFileFromDisk = sinon
  696. .stub()
  697. .yields(null, this.newFileUrl, this.newFile)
  698. this.ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon
  699. .stub()
  700. .yields()
  701. this.ProjectEntityMongoUpdateHandler.replaceFileWithNew = sinon
  702. .stub()
  703. .yields(
  704. null,
  705. this.oldFile,
  706. this.project,
  707. { fileSystem: this.path },
  708. this.newProject
  709. )
  710. return this.ProjectEntityUpdateHandler.replaceFile(
  711. project_id,
  712. file_id,
  713. this.fileSystemPath,
  714. this.linkedFileData,
  715. userId,
  716. this.callback
  717. )
  718. })
  719. it('uploads a new version of the file', function() {
  720. return this.FileStoreHandler.uploadFileFromDisk
  721. .calledWith(
  722. project_id,
  723. {
  724. name: 'dummy-upload-filename',
  725. linkedFileData: this.linkedFileData
  726. },
  727. this.fileSystemPath
  728. )
  729. .should.equal(true)
  730. })
  731. it('replaces the file in mongo', function() {
  732. return this.ProjectEntityMongoUpdateHandler.replaceFileWithNew
  733. .calledWith(project_id, file_id, this.newFile)
  734. .should.equal(true)
  735. })
  736. it('notifies the tpds', function() {
  737. return this.TpdsUpdateSender.addFile
  738. .calledWith({
  739. project_id,
  740. project_name: this.project.name,
  741. file_id: new_file_id,
  742. rev: this.oldFile.rev + 1,
  743. path: this.path
  744. })
  745. .should.equal(true)
  746. })
  747. it('updates the project structure in the doc updater', function() {
  748. const oldFiles = [
  749. {
  750. file: this.oldFile,
  751. path: this.path
  752. }
  753. ]
  754. const newFiles = [
  755. {
  756. file: this.newFile,
  757. path: this.path,
  758. url: this.newFileUrl
  759. }
  760. ]
  761. return this.DocumentUpdaterHandler.updateProjectStructure
  762. .calledWith(project_id, projectHistoryId, userId, {
  763. oldFiles,
  764. newFiles,
  765. newProject: this.newProject
  766. })
  767. .should.equal(true)
  768. })
  769. })
  770. describe('upsertDoc', function() {
  771. describe('upserting into an invalid folder', function() {
  772. beforeEach(function() {
  773. this.ProjectLocator.findElement = sinon.stub().yields()
  774. return this.ProjectEntityUpdateHandler.upsertDoc(
  775. project_id,
  776. folder_id,
  777. this.docName,
  778. this.docLines,
  779. this.source,
  780. userId,
  781. this.callback
  782. )
  783. })
  784. it('returns an error', function() {
  785. const errorMatcher = sinon.match.instanceOf(Error)
  786. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  787. })
  788. })
  789. describe('updating an existing doc', function() {
  790. beforeEach(function() {
  791. this.existingDoc = { _id: doc_id, name: this.docName }
  792. this.folder = { _id: folder_id, docs: [this.existingDoc] }
  793. this.ProjectLocator.findElement = sinon.stub().yields(null, this.folder)
  794. this.DocumentUpdaterHandler.setDocument = sinon.stub().yields()
  795. this.DocumentUpdaterHandler.flushDocToMongo = sinon.stub().yields()
  796. return this.ProjectEntityUpdateHandler.upsertDoc(
  797. project_id,
  798. folder_id,
  799. this.docName,
  800. this.docLines,
  801. this.source,
  802. userId,
  803. this.callback
  804. )
  805. })
  806. it('tries to find the folder', function() {
  807. return this.ProjectLocator.findElement
  808. .calledWith({ project_id, element_id: folder_id, type: 'folder' })
  809. .should.equal(true)
  810. })
  811. it('updates the doc contents', function() {
  812. return this.DocumentUpdaterHandler.setDocument
  813. .calledWith(
  814. project_id,
  815. this.existingDoc._id,
  816. userId,
  817. this.docLines,
  818. this.source
  819. )
  820. .should.equal(true)
  821. })
  822. it('flushes the doc contents', function() {
  823. return this.DocumentUpdaterHandler.flushDocToMongo
  824. .calledWith(project_id, this.existingDoc._id)
  825. .should.equal(true)
  826. })
  827. it('returns the doc', function() {
  828. return this.callback.calledWith(null, this.existingDoc, false)
  829. })
  830. })
  831. describe('creating a new doc', function() {
  832. beforeEach(function() {
  833. this.folder = { _id: folder_id, docs: [] }
  834. this.newDoc = { _id: doc_id }
  835. this.ProjectLocator.findElement = sinon.stub().yields(null, this.folder)
  836. this.ProjectEntityUpdateHandler.addDocWithRanges = {
  837. withoutLock: sinon.stub().yields(null, this.newDoc)
  838. }
  839. return this.ProjectEntityUpdateHandler.upsertDoc(
  840. project_id,
  841. folder_id,
  842. this.docName,
  843. this.docLines,
  844. this.source,
  845. userId,
  846. this.callback
  847. )
  848. })
  849. it('tries to find the folder', function() {
  850. return this.ProjectLocator.findElement
  851. .calledWith({ project_id, element_id: folder_id, type: 'folder' })
  852. .should.equal(true)
  853. })
  854. it('adds the doc', function() {
  855. return this.ProjectEntityUpdateHandler.addDocWithRanges.withoutLock
  856. .calledWith(
  857. project_id,
  858. folder_id,
  859. this.docName,
  860. this.docLines,
  861. {},
  862. userId
  863. )
  864. .should.equal(true)
  865. })
  866. it('returns the doc', function() {
  867. return this.callback.calledWith(null, this.newDoc, true)
  868. })
  869. })
  870. describe('upserting a new doc with an invalid name', function() {
  871. beforeEach(function() {
  872. this.folder = { _id: folder_id, docs: [] }
  873. this.newDoc = { _id: doc_id }
  874. this.ProjectLocator.findElement = sinon.stub().yields(null, this.folder)
  875. this.ProjectEntityUpdateHandler.addDocWithRanges = {
  876. withoutLock: sinon.stub().yields(null, this.newDoc)
  877. }
  878. return this.ProjectEntityUpdateHandler.upsertDoc(
  879. project_id,
  880. folder_id,
  881. `*${this.docName}`,
  882. this.docLines,
  883. this.source,
  884. userId,
  885. this.callback
  886. )
  887. })
  888. it('returns an error', function() {
  889. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  890. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  891. })
  892. })
  893. })
  894. describe('upsertFile', function() {
  895. beforeEach(function() {
  896. return (this.FileStoreHandler.uploadFileFromDisk = sinon
  897. .stub()
  898. .yields(null, this.fileUrl, this.newFile))
  899. })
  900. describe('upserting into an invalid folder', function() {
  901. beforeEach(function() {
  902. this.ProjectLocator.findElement = sinon.stub().yields()
  903. return this.ProjectEntityUpdateHandler.upsertFile(
  904. project_id,
  905. folder_id,
  906. this.fileName,
  907. this.fileSystemPath,
  908. this.linkedFileData,
  909. userId,
  910. this.callback
  911. )
  912. })
  913. it('returns an error', function() {
  914. const errorMatcher = sinon.match.instanceOf(Error)
  915. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  916. })
  917. })
  918. describe('updating an existing file', function() {
  919. beforeEach(function() {
  920. this.existingFile = { _id: file_id, name: this.fileName }
  921. this.folder = { _id: folder_id, fileRefs: [this.existingFile] }
  922. this.ProjectLocator.findElement = sinon.stub().yields(null, this.folder)
  923. this.ProjectEntityUpdateHandler.replaceFile = {
  924. mainTask: sinon.stub().yields(null, this.newFile)
  925. }
  926. return this.ProjectEntityUpdateHandler.upsertFile(
  927. project_id,
  928. folder_id,
  929. this.fileName,
  930. this.fileSystemPath,
  931. this.linkedFileData,
  932. userId,
  933. this.callback
  934. )
  935. })
  936. it('replaces the file', function() {
  937. return this.ProjectEntityUpdateHandler.replaceFile.mainTask
  938. .calledWith(
  939. project_id,
  940. file_id,
  941. this.fileSystemPath,
  942. this.linkedFileData,
  943. userId
  944. )
  945. .should.equal(true)
  946. })
  947. it('returns the file', function() {
  948. return this.callback.calledWith(null, this.existingFile, false)
  949. })
  950. })
  951. describe('creating a new file', function() {
  952. beforeEach(function() {
  953. this.folder = { _id: folder_id, fileRefs: [] }
  954. this.newFile = { _id: file_id }
  955. this.ProjectLocator.findElement = sinon.stub().yields(null, this.folder)
  956. this.ProjectEntityUpdateHandler.addFile = {
  957. mainTask: sinon.stub().yields(null, this.newFile)
  958. }
  959. return this.ProjectEntityUpdateHandler.upsertFile(
  960. project_id,
  961. folder_id,
  962. this.fileName,
  963. this.fileSystemPath,
  964. this.linkedFileData,
  965. userId,
  966. this.callback
  967. )
  968. })
  969. it('tries to find the folder', function() {
  970. return this.ProjectLocator.findElement
  971. .calledWith({ project_id, element_id: folder_id, type: 'folder' })
  972. .should.equal(true)
  973. })
  974. it('adds the file', function() {
  975. return this.ProjectEntityUpdateHandler.addFile.mainTask
  976. .calledWith(
  977. project_id,
  978. folder_id,
  979. this.fileName,
  980. this.fileSystemPath,
  981. this.linkedFileData,
  982. userId
  983. )
  984. .should.equal(true)
  985. })
  986. it('returns the file', function() {
  987. return this.callback.calledWith(null, this.newFile, true)
  988. })
  989. })
  990. describe('upserting a new file with an invalid name', function() {
  991. beforeEach(function() {
  992. this.folder = { _id: folder_id, fileRefs: [] }
  993. this.newFile = { _id: file_id }
  994. this.ProjectLocator.findElement = sinon.stub().yields(null, this.folder)
  995. this.ProjectEntityUpdateHandler.addFile = {
  996. mainTask: sinon.stub().yields(null, this.newFile)
  997. }
  998. return this.ProjectEntityUpdateHandler.upsertFile(
  999. project_id,
  1000. folder_id,
  1001. `*${this.fileName}`,
  1002. this.fileSystemPath,
  1003. this.linkedFileData,
  1004. userId,
  1005. this.callback
  1006. )
  1007. })
  1008. it('returns an error', function() {
  1009. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1010. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1011. })
  1012. })
  1013. })
  1014. describe('upsertDocWithPath', function() {
  1015. describe('upserting a doc', function() {
  1016. beforeEach(function() {
  1017. this.path = '/folder/doc.tex'
  1018. this.newFolders = ['mock-a', 'mock-b']
  1019. this.folder = { _id: folder_id }
  1020. this.doc = { _id: doc_id }
  1021. this.isNewDoc = true
  1022. this.ProjectEntityUpdateHandler.mkdirp = {
  1023. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder)
  1024. }
  1025. this.ProjectEntityUpdateHandler.upsertDoc = {
  1026. withoutLock: sinon.stub().yields(null, this.doc, this.isNewDoc)
  1027. }
  1028. return this.ProjectEntityUpdateHandler.upsertDocWithPath(
  1029. project_id,
  1030. this.path,
  1031. this.docLines,
  1032. this.source,
  1033. userId,
  1034. this.callback
  1035. )
  1036. })
  1037. it('creates any necessary folders', function() {
  1038. return this.ProjectEntityUpdateHandler.mkdirp.withoutLock
  1039. .calledWith(project_id, '/folder')
  1040. .should.equal(true)
  1041. })
  1042. it('upserts the doc', function() {
  1043. return this.ProjectEntityUpdateHandler.upsertDoc.withoutLock
  1044. .calledWith(
  1045. project_id,
  1046. this.folder._id,
  1047. 'doc.tex',
  1048. this.docLines,
  1049. this.source,
  1050. userId
  1051. )
  1052. .should.equal(true)
  1053. })
  1054. it('calls the callback', function() {
  1055. return this.callback
  1056. .calledWith(
  1057. null,
  1058. this.doc,
  1059. this.isNewDoc,
  1060. this.newFolders,
  1061. this.folder
  1062. )
  1063. .should.equal(true)
  1064. })
  1065. })
  1066. describe('upserting a doc with an invalid path', function() {
  1067. beforeEach(function() {
  1068. this.path = '/*folder/doc.tex'
  1069. this.newFolders = ['mock-a', 'mock-b']
  1070. this.folder = { _id: folder_id }
  1071. this.doc = { _id: doc_id }
  1072. this.isNewDoc = true
  1073. this.ProjectEntityUpdateHandler.mkdirp = {
  1074. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder)
  1075. }
  1076. this.ProjectEntityUpdateHandler.upsertDoc = {
  1077. withoutLock: sinon.stub().yields(null, this.doc, this.isNewDoc)
  1078. }
  1079. return this.ProjectEntityUpdateHandler.upsertDocWithPath(
  1080. project_id,
  1081. this.path,
  1082. this.docLines,
  1083. this.source,
  1084. userId,
  1085. this.callback
  1086. )
  1087. })
  1088. it('returns an error', function() {
  1089. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1090. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1091. })
  1092. })
  1093. describe('upserting a doc with an invalid name', function() {
  1094. beforeEach(function() {
  1095. this.path = '/folder/*doc.tex'
  1096. this.newFolders = ['mock-a', 'mock-b']
  1097. this.folder = { _id: folder_id }
  1098. this.doc = { _id: doc_id }
  1099. this.isNewDoc = true
  1100. this.ProjectEntityUpdateHandler.mkdirp = {
  1101. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder)
  1102. }
  1103. this.ProjectEntityUpdateHandler.upsertDoc = {
  1104. withoutLock: sinon.stub().yields(null, this.doc, this.isNewDoc)
  1105. }
  1106. return this.ProjectEntityUpdateHandler.upsertDocWithPath(
  1107. project_id,
  1108. this.path,
  1109. this.docLines,
  1110. this.source,
  1111. userId,
  1112. this.callback
  1113. )
  1114. })
  1115. it('returns an error', function() {
  1116. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1117. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1118. })
  1119. })
  1120. })
  1121. describe('upsertFileWithPath', function() {
  1122. describe('upserting a file', function() {
  1123. beforeEach(function() {
  1124. this.path = '/folder/file.png'
  1125. this.newFolders = ['mock-a', 'mock-b']
  1126. this.folder = { _id: folder_id }
  1127. this.file = { _id: file_id }
  1128. this.isNewFile = true
  1129. this.FileStoreHandler.uploadFileFromDisk = sinon
  1130. .stub()
  1131. .yields(null, this.fileUrl, this.newFile)
  1132. this.ProjectEntityUpdateHandler.mkdirp = {
  1133. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder)
  1134. }
  1135. this.ProjectEntityUpdateHandler.upsertFile = {
  1136. mainTask: sinon.stub().yields(null, this.file, this.isNewFile)
  1137. }
  1138. return this.ProjectEntityUpdateHandler.upsertFileWithPath(
  1139. project_id,
  1140. this.path,
  1141. this.fileSystemPath,
  1142. this.linkedFileData,
  1143. userId,
  1144. this.callback
  1145. )
  1146. })
  1147. it('creates any necessary folders', function() {
  1148. return this.ProjectEntityUpdateHandler.mkdirp.withoutLock
  1149. .calledWith(project_id, '/folder')
  1150. .should.equal(true)
  1151. })
  1152. it('upserts the file', function() {
  1153. return this.ProjectEntityUpdateHandler.upsertFile.mainTask
  1154. .calledWith(
  1155. project_id,
  1156. this.folder._id,
  1157. 'file.png',
  1158. this.fileSystemPath,
  1159. this.linkedFileData,
  1160. userId
  1161. )
  1162. .should.equal(true)
  1163. })
  1164. it('calls the callback', function() {
  1165. return this.callback
  1166. .calledWith(
  1167. null,
  1168. this.file,
  1169. this.isNewFile,
  1170. undefined,
  1171. this.newFolders,
  1172. this.folder
  1173. )
  1174. .should.equal(true)
  1175. })
  1176. })
  1177. describe('upserting a file with an invalid path', function() {
  1178. beforeEach(function() {
  1179. this.path = '/*folder/file.png'
  1180. this.newFolders = ['mock-a', 'mock-b']
  1181. this.folder = { _id: folder_id }
  1182. this.file = { _id: file_id }
  1183. this.isNewFile = true
  1184. this.ProjectEntityUpdateHandler.mkdirp = {
  1185. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder)
  1186. }
  1187. this.ProjectEntityUpdateHandler.upsertFile = {
  1188. mainTask: sinon.stub().yields(null, this.file, this.isNewFile)
  1189. }
  1190. return this.ProjectEntityUpdateHandler.upsertFileWithPath(
  1191. project_id,
  1192. this.path,
  1193. this.fileSystemPath,
  1194. this.linkedFileData,
  1195. userId,
  1196. this.callback
  1197. )
  1198. })
  1199. it('returns an error', function() {
  1200. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1201. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1202. })
  1203. })
  1204. describe('upserting a file with an invalid name', function() {
  1205. beforeEach(function() {
  1206. this.path = '/folder/*file.png'
  1207. this.newFolders = ['mock-a', 'mock-b']
  1208. this.folder = { _id: folder_id }
  1209. this.file = { _id: file_id }
  1210. this.isNewFile = true
  1211. this.ProjectEntityUpdateHandler.mkdirp = {
  1212. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder)
  1213. }
  1214. this.ProjectEntityUpdateHandler.upsertFile = {
  1215. mainTask: sinon.stub().yields(null, this.file, this.isNewFile)
  1216. }
  1217. return this.ProjectEntityUpdateHandler.upsertFileWithPath(
  1218. project_id,
  1219. this.path,
  1220. this.fileSystemPath,
  1221. this.linkedFileData,
  1222. userId,
  1223. this.callback
  1224. )
  1225. })
  1226. it('returns an error', function() {
  1227. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1228. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1229. })
  1230. })
  1231. })
  1232. describe('deleteEntity', function() {
  1233. beforeEach(function() {
  1234. this.path = '/path/to/doc.tex'
  1235. this.doc = { _id: doc_id }
  1236. this.projectBeforeDeletion = { _id: project_id, name: 'project' }
  1237. this.newProject = 'new-project'
  1238. this.ProjectEntityMongoUpdateHandler.deleteEntity = sinon
  1239. .stub()
  1240. .yields(
  1241. null,
  1242. this.doc,
  1243. { fileSystem: this.path },
  1244. this.projectBeforeDeletion,
  1245. this.newProject
  1246. )
  1247. this.ProjectEntityUpdateHandler._cleanUpEntity = sinon.stub().yields()
  1248. this.TpdsUpdateSender.deleteEntity = sinon.stub().yields()
  1249. return this.ProjectEntityUpdateHandler.deleteEntity(
  1250. project_id,
  1251. doc_id,
  1252. 'doc',
  1253. userId,
  1254. this.callback
  1255. )
  1256. })
  1257. it('deletes the entity in mongo', function() {
  1258. return this.ProjectEntityMongoUpdateHandler.deleteEntity
  1259. .calledWith(project_id, doc_id, 'doc')
  1260. .should.equal(true)
  1261. })
  1262. it('cleans up the doc in the docstore', function() {
  1263. return this.ProjectEntityUpdateHandler._cleanUpEntity
  1264. .calledWith(
  1265. this.projectBeforeDeletion,
  1266. this.newProject,
  1267. this.doc,
  1268. 'doc',
  1269. this.path,
  1270. userId
  1271. )
  1272. .should.equal(true)
  1273. })
  1274. it('it notifies the tpds', function() {
  1275. return this.TpdsUpdateSender.deleteEntity
  1276. .calledWith({
  1277. project_id,
  1278. path: this.path,
  1279. project_name: this.projectBeforeDeletion.name
  1280. })
  1281. .should.equal(true)
  1282. })
  1283. it('retuns the entity_id', function() {
  1284. return this.callback.calledWith(null, doc_id).should.equal(true)
  1285. })
  1286. })
  1287. describe('deleteEntityWithPath', function() {
  1288. describe('when the entity exists', function() {
  1289. beforeEach(function() {
  1290. this.doc = { _id: doc_id }
  1291. this.ProjectLocator.findElementByPath = sinon
  1292. .stub()
  1293. .yields(null, this.doc, 'doc')
  1294. this.ProjectEntityUpdateHandler.deleteEntity = {
  1295. withoutLock: sinon.stub().yields()
  1296. }
  1297. this.path = '/path/to/doc.tex'
  1298. return this.ProjectEntityUpdateHandler.deleteEntityWithPath(
  1299. project_id,
  1300. this.path,
  1301. userId,
  1302. this.callback
  1303. )
  1304. })
  1305. it('finds the entity', function() {
  1306. return this.ProjectLocator.findElementByPath
  1307. .calledWith({ project_id, path: this.path })
  1308. .should.equal(true)
  1309. })
  1310. it('deletes the entity', function() {
  1311. return this.ProjectEntityUpdateHandler.deleteEntity.withoutLock
  1312. .calledWith(project_id, this.doc._id, 'doc', userId, this.callback)
  1313. .should.equal(true)
  1314. })
  1315. })
  1316. describe('when the entity does not exist', function() {
  1317. beforeEach(function() {
  1318. this.ProjectLocator.findElementByPath = sinon.stub().yields()
  1319. this.path = '/doc.tex'
  1320. return this.ProjectEntityUpdateHandler.deleteEntityWithPath(
  1321. project_id,
  1322. this.path,
  1323. userId,
  1324. this.callback
  1325. )
  1326. })
  1327. it('returns an error', function() {
  1328. return this.callback
  1329. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  1330. .should.equal(true)
  1331. })
  1332. })
  1333. })
  1334. describe('mkdirp', function() {
  1335. beforeEach(function() {
  1336. this.docPath = '/folder/doc.tex'
  1337. this.ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
  1338. return this.ProjectEntityUpdateHandler.mkdirp(
  1339. project_id,
  1340. this.docPath,
  1341. this.callback
  1342. )
  1343. })
  1344. it('calls ProjectEntityMongoUpdateHandler', function() {
  1345. return this.ProjectEntityMongoUpdateHandler.mkdirp
  1346. .calledWith(project_id, this.docPath)
  1347. .should.equal(true)
  1348. })
  1349. })
  1350. describe('mkdirpWithExactCase', function() {
  1351. beforeEach(function() {
  1352. this.docPath = '/folder/doc.tex'
  1353. this.ProjectEntityMongoUpdateHandler.mkdirp = sinon.stub().yields()
  1354. return this.ProjectEntityUpdateHandler.mkdirpWithExactCase(
  1355. project_id,
  1356. this.docPath,
  1357. this.callback
  1358. )
  1359. })
  1360. it('calls ProjectEntityMongoUpdateHandler', function() {
  1361. return this.ProjectEntityMongoUpdateHandler.mkdirp
  1362. .calledWith(project_id, this.docPath, { exactCaseMatch: true })
  1363. .should.equal(true)
  1364. })
  1365. })
  1366. describe('addFolder', function() {
  1367. describe('adding a folder', function() {
  1368. beforeEach(function() {
  1369. this.parentFolder_id = '123asdf'
  1370. this.folderName = 'new-folder'
  1371. this.ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  1372. return this.ProjectEntityUpdateHandler.addFolder(
  1373. project_id,
  1374. this.parentFolder_id,
  1375. this.folderName,
  1376. this.callback
  1377. )
  1378. })
  1379. it('calls ProjectEntityMongoUpdateHandler', function() {
  1380. return this.ProjectEntityMongoUpdateHandler.addFolder
  1381. .calledWith(project_id, this.parentFolder_id, this.folderName)
  1382. .should.equal(true)
  1383. })
  1384. })
  1385. describe('adding a folder with an invalid name', function() {
  1386. beforeEach(function() {
  1387. this.parentFolder_id = '123asdf'
  1388. this.folderName = '*new-folder'
  1389. this.ProjectEntityMongoUpdateHandler.addFolder = sinon.stub().yields()
  1390. return this.ProjectEntityUpdateHandler.addFolder(
  1391. project_id,
  1392. this.parentFolder_id,
  1393. this.folderName,
  1394. this.callback
  1395. )
  1396. })
  1397. it('returns an error', function() {
  1398. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1399. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1400. })
  1401. })
  1402. })
  1403. describe('moveEntity', function() {
  1404. beforeEach(function() {
  1405. this.project_name = 'project name'
  1406. this.startPath = '/a.tex'
  1407. this.endPath = '/folder/b.tex'
  1408. this.rev = 2
  1409. this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
  1410. this.ProjectEntityMongoUpdateHandler.moveEntity = sinon
  1411. .stub()
  1412. .yields(
  1413. null,
  1414. this.project,
  1415. this.startPath,
  1416. this.endPath,
  1417. this.rev,
  1418. this.changes
  1419. )
  1420. this.TpdsUpdateSender.moveEntity = sinon.stub()
  1421. this.DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  1422. return this.ProjectEntityUpdateHandler.moveEntity(
  1423. project_id,
  1424. doc_id,
  1425. folder_id,
  1426. 'doc',
  1427. userId,
  1428. this.callback
  1429. )
  1430. })
  1431. it('moves the entity in mongo', function() {
  1432. return this.ProjectEntityMongoUpdateHandler.moveEntity
  1433. .calledWith(project_id, doc_id, folder_id, 'doc')
  1434. .should.equal(true)
  1435. })
  1436. it('notifies tpds', function() {
  1437. return this.TpdsUpdateSender.moveEntity
  1438. .calledWith({
  1439. project_id,
  1440. project_name: this.project_name,
  1441. startPath: this.startPath,
  1442. endPath: this.endPath,
  1443. rev: this.rev
  1444. })
  1445. .should.equal(true)
  1446. })
  1447. it('sends the changes in project structure to the doc updater', function() {
  1448. return this.DocumentUpdaterHandler.updateProjectStructure
  1449. .calledWith(
  1450. project_id,
  1451. projectHistoryId,
  1452. userId,
  1453. this.changes,
  1454. this.callback
  1455. )
  1456. .should.equal(true)
  1457. })
  1458. })
  1459. describe('renameEntity', function() {
  1460. describe('renaming an entity', function() {
  1461. beforeEach(function() {
  1462. this.project_name = 'project name'
  1463. this.startPath = '/folder/a.tex'
  1464. this.endPath = '/folder/b.tex'
  1465. this.rev = 2
  1466. this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
  1467. this.newDocName = 'b.tex'
  1468. this.ProjectEntityMongoUpdateHandler.renameEntity = sinon
  1469. .stub()
  1470. .yields(
  1471. null,
  1472. this.project,
  1473. this.startPath,
  1474. this.endPath,
  1475. this.rev,
  1476. this.changes
  1477. )
  1478. this.TpdsUpdateSender.moveEntity = sinon.stub()
  1479. this.DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  1480. return this.ProjectEntityUpdateHandler.renameEntity(
  1481. project_id,
  1482. doc_id,
  1483. 'doc',
  1484. this.newDocName,
  1485. userId,
  1486. this.callback
  1487. )
  1488. })
  1489. it('moves the entity in mongo', function() {
  1490. return this.ProjectEntityMongoUpdateHandler.renameEntity
  1491. .calledWith(project_id, doc_id, 'doc', this.newDocName)
  1492. .should.equal(true)
  1493. })
  1494. it('notifies tpds', function() {
  1495. return this.TpdsUpdateSender.moveEntity
  1496. .calledWith({
  1497. project_id,
  1498. project_name: this.project_name,
  1499. startPath: this.startPath,
  1500. endPath: this.endPath,
  1501. rev: this.rev
  1502. })
  1503. .should.equal(true)
  1504. })
  1505. it('sends the changes in project structure to the doc updater', function() {
  1506. return this.DocumentUpdaterHandler.updateProjectStructure
  1507. .calledWith(
  1508. project_id,
  1509. projectHistoryId,
  1510. userId,
  1511. this.changes,
  1512. this.callback
  1513. )
  1514. .should.equal(true)
  1515. })
  1516. })
  1517. describe('renaming an entity to an invalid name', function() {
  1518. beforeEach(function() {
  1519. this.project_name = 'project name'
  1520. this.startPath = '/folder/a.tex'
  1521. this.endPath = '/folder/b.tex'
  1522. this.rev = 2
  1523. this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
  1524. this.newDocName = '*b.tex'
  1525. this.ProjectEntityMongoUpdateHandler.renameEntity = sinon
  1526. .stub()
  1527. .yields(
  1528. null,
  1529. this.project,
  1530. this.startPath,
  1531. this.endPath,
  1532. this.rev,
  1533. this.changes
  1534. )
  1535. this.TpdsUpdateSender.moveEntity = sinon.stub()
  1536. this.DocumentUpdaterHandler.updateProjectStructure = sinon.stub()
  1537. return this.ProjectEntityUpdateHandler.renameEntity(
  1538. project_id,
  1539. doc_id,
  1540. 'doc',
  1541. this.newDocName,
  1542. userId,
  1543. this.callback
  1544. )
  1545. })
  1546. it('returns an error', function() {
  1547. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1548. return this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1549. })
  1550. })
  1551. })
  1552. describe('resyncProjectHistory', function() {
  1553. describe('a deleted project', function() {
  1554. beforeEach(function() {
  1555. this.ProjectGetter.getProject = sinon.stub().yields()
  1556. return this.ProjectEntityUpdateHandler.resyncProjectHistory(
  1557. project_id,
  1558. this.callback
  1559. )
  1560. })
  1561. it('should return an error', function() {
  1562. expect(this.callback).to.have.been.calledWith(
  1563. sinon.match
  1564. .instanceOf(Errors.ProjectHistoryDisabledError)
  1565. .and(
  1566. sinon.match.has(
  1567. 'message',
  1568. `project history not enabled for ${project_id}`
  1569. )
  1570. )
  1571. )
  1572. })
  1573. })
  1574. describe('a project without project-history enabled', function() {
  1575. beforeEach(function() {
  1576. this.project.overleaf = {}
  1577. this.ProjectGetter.getProject = sinon.stub().yields(null, this.project)
  1578. return this.ProjectEntityUpdateHandler.resyncProjectHistory(
  1579. project_id,
  1580. this.callback
  1581. )
  1582. })
  1583. it('should return an error', function() {
  1584. expect(this.callback).to.have.been.calledWith(
  1585. sinon.match
  1586. .instanceOf(Errors.ProjectHistoryDisabledError)
  1587. .and(
  1588. sinon.match.has(
  1589. 'message',
  1590. `project history not enabled for ${project_id}`
  1591. )
  1592. )
  1593. )
  1594. })
  1595. })
  1596. describe('a project with project-history enabled', function() {
  1597. beforeEach(function() {
  1598. this.ProjectGetter.getProject = sinon.stub().yields(null, this.project)
  1599. const docs = [
  1600. {
  1601. doc: {
  1602. _id: doc_id
  1603. },
  1604. path: 'main.tex'
  1605. }
  1606. ]
  1607. const files = [
  1608. {
  1609. file: {
  1610. _id: file_id
  1611. },
  1612. path: 'universe.png'
  1613. }
  1614. ]
  1615. this.ProjectEntityHandler.getAllEntitiesFromProject = sinon
  1616. .stub()
  1617. .yields(null, docs, files)
  1618. this.FileStoreHandler._buildUrl = (project_id, file_id) =>
  1619. `www.filestore.test/${project_id}/${file_id}`
  1620. this.DocumentUpdaterHandler.resyncProjectHistory = sinon.stub().yields()
  1621. return this.ProjectEntityUpdateHandler.resyncProjectHistory(
  1622. project_id,
  1623. this.callback
  1624. )
  1625. })
  1626. it('gets the project', function() {
  1627. return this.ProjectGetter.getProject
  1628. .calledWith(project_id)
  1629. .should.equal(true)
  1630. })
  1631. it('gets the entities for the project', function() {
  1632. return this.ProjectEntityHandler.getAllEntitiesFromProject
  1633. .calledWith(this.project)
  1634. .should.equal(true)
  1635. })
  1636. it('tells the doc updater to sync the project', function() {
  1637. const docs = [
  1638. {
  1639. doc: doc_id,
  1640. path: 'main.tex'
  1641. }
  1642. ]
  1643. const files = [
  1644. {
  1645. file: file_id,
  1646. path: 'universe.png',
  1647. url: `www.filestore.test/${project_id}/${file_id}`
  1648. }
  1649. ]
  1650. return this.DocumentUpdaterHandler.resyncProjectHistory
  1651. .calledWith(project_id, projectHistoryId, docs, files)
  1652. .should.equal(true)
  1653. })
  1654. it('calls the callback', function() {
  1655. return this.callback.called.should.equal(true)
  1656. })
  1657. })
  1658. })
  1659. describe('_cleanUpEntity', function() {
  1660. beforeEach(function() {
  1661. this.entity_id = '4eecaffcbffa66588e000009'
  1662. this.FileStoreHandler.deleteFile = sinon.stub().yields()
  1663. this.DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  1664. this.ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  1665. return (this.ProjectEntityMongoUpdateHandler._insertDeletedFileReference = sinon
  1666. .stub()
  1667. .yields())
  1668. })
  1669. describe('a file', function() {
  1670. beforeEach(function(done) {
  1671. this.path = '/file/system/path.png'
  1672. this.entity = { _id: this.entity_id }
  1673. this.newProject = 'new-project'
  1674. return this.ProjectEntityUpdateHandler._cleanUpEntity(
  1675. this.project,
  1676. this.newProject,
  1677. this.entity,
  1678. 'file',
  1679. this.path,
  1680. userId,
  1681. done
  1682. )
  1683. })
  1684. it('should insert the file into the deletedFiles array', function() {
  1685. return this.ProjectEntityMongoUpdateHandler._insertDeletedFileReference
  1686. .calledWith(this.project._id, this.entity)
  1687. .should.equal(true)
  1688. })
  1689. it('should not delete the file from FileStoreHandler', function() {
  1690. return this.FileStoreHandler.deleteFile
  1691. .calledWith(project_id, this.entity_id)
  1692. .should.equal(false)
  1693. })
  1694. it('should not attempt to delete from the document updater', function() {
  1695. return this.DocumentUpdaterHandler.deleteDoc.called.should.equal(false)
  1696. })
  1697. it('should should send the update to the doc updater', function() {
  1698. const oldFiles = [{ file: this.entity, path: this.path }]
  1699. return this.DocumentUpdaterHandler.updateProjectStructure
  1700. .calledWith(project_id, projectHistoryId, userId, {
  1701. oldFiles,
  1702. newProject: this.newProject
  1703. })
  1704. .should.equal(true)
  1705. })
  1706. })
  1707. describe('a doc', function() {
  1708. beforeEach(function(done) {
  1709. this.path = '/file/system/path.tex'
  1710. this.ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  1711. this.entity = { _id: this.entity_id }
  1712. this.newProject = 'new-project'
  1713. return this.ProjectEntityUpdateHandler._cleanUpEntity(
  1714. this.project,
  1715. this.newProject,
  1716. this.entity,
  1717. 'doc',
  1718. this.path,
  1719. userId,
  1720. done
  1721. )
  1722. })
  1723. it('should clean up the doc', function() {
  1724. return this.ProjectEntityUpdateHandler._cleanUpDoc
  1725. .calledWith(this.project, this.entity, this.path, userId)
  1726. .should.equal(true)
  1727. })
  1728. it('should should send the update to the doc updater', function() {
  1729. const oldDocs = [{ doc: this.entity, path: this.path }]
  1730. return this.DocumentUpdaterHandler.updateProjectStructure
  1731. .calledWith(project_id, projectHistoryId, userId, {
  1732. oldDocs,
  1733. newProject: this.newProject
  1734. })
  1735. .should.equal(true)
  1736. })
  1737. })
  1738. describe('a folder', function() {
  1739. beforeEach(function(done) {
  1740. this.folder = {
  1741. folders: [
  1742. {
  1743. name: 'subfolder',
  1744. fileRefs: [
  1745. (this.file1 = { _id: 'file-id-1', name: 'file-name-1' })
  1746. ],
  1747. docs: [(this.doc1 = { _id: 'doc-id-1', name: 'doc-name-1' })],
  1748. folders: []
  1749. }
  1750. ],
  1751. fileRefs: [(this.file2 = { _id: 'file-id-2', name: 'file-name-2' })],
  1752. docs: [(this.doc2 = { _id: 'doc-id-2', name: 'doc-name-2' })]
  1753. }
  1754. this.ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  1755. this.ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
  1756. const path = '/folder'
  1757. this.newProject = 'new-project'
  1758. return this.ProjectEntityUpdateHandler._cleanUpEntity(
  1759. this.project,
  1760. this.newProject,
  1761. this.folder,
  1762. 'folder',
  1763. path,
  1764. userId,
  1765. done
  1766. )
  1767. })
  1768. it('should clean up all sub files', function() {
  1769. this.ProjectEntityUpdateHandler._cleanUpFile
  1770. .calledWith(
  1771. this.project,
  1772. this.file1,
  1773. '/folder/subfolder/file-name-1',
  1774. userId
  1775. )
  1776. .should.equal(true)
  1777. return this.ProjectEntityUpdateHandler._cleanUpFile
  1778. .calledWith(this.project, this.file2, '/folder/file-name-2', userId)
  1779. .should.equal(true)
  1780. })
  1781. it('should clean up all sub docs', function() {
  1782. this.ProjectEntityUpdateHandler._cleanUpDoc
  1783. .calledWith(
  1784. this.project,
  1785. this.doc1,
  1786. '/folder/subfolder/doc-name-1',
  1787. userId
  1788. )
  1789. .should.equal(true)
  1790. return this.ProjectEntityUpdateHandler._cleanUpDoc
  1791. .calledWith(this.project, this.doc2, '/folder/doc-name-2', userId)
  1792. .should.equal(true)
  1793. })
  1794. it('should should send one update to the doc updater for all docs and files', function() {
  1795. const oldFiles = [
  1796. { file: this.file2, path: '/folder/file-name-2' },
  1797. { file: this.file1, path: '/folder/subfolder/file-name-1' }
  1798. ]
  1799. const oldDocs = [
  1800. { doc: this.doc2, path: '/folder/doc-name-2' },
  1801. { doc: this.doc1, path: '/folder/subfolder/doc-name-1' }
  1802. ]
  1803. return this.DocumentUpdaterHandler.updateProjectStructure
  1804. .calledWith(project_id, projectHistoryId, userId, {
  1805. oldFiles,
  1806. oldDocs,
  1807. newProject: this.newProject
  1808. })
  1809. .should.equal(true)
  1810. })
  1811. })
  1812. })
  1813. describe('_cleanUpDoc', function() {
  1814. beforeEach(function() {
  1815. this.doc = {
  1816. _id: ObjectId(),
  1817. name: 'test.tex'
  1818. }
  1819. this.path = '/path/to/doc'
  1820. this.ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  1821. this.ProjectEntityMongoUpdateHandler._insertDeletedDocReference = sinon
  1822. .stub()
  1823. .yields()
  1824. this.DocumentUpdaterHandler.deleteDoc = sinon.stub().yields()
  1825. this.DocstoreManager.deleteDoc = sinon.stub().yields()
  1826. return (this.callback = sinon.stub())
  1827. })
  1828. describe('when the doc is the root doc', function() {
  1829. beforeEach(function() {
  1830. this.project.rootDoc_id = this.doc._id
  1831. return this.ProjectEntityUpdateHandler._cleanUpDoc(
  1832. this.project,
  1833. this.doc,
  1834. this.path,
  1835. userId,
  1836. this.callback
  1837. )
  1838. })
  1839. it('should unset the root doc', function() {
  1840. return this.ProjectEntityUpdateHandler.unsetRootDoc
  1841. .calledWith(project_id)
  1842. .should.equal(true)
  1843. })
  1844. it('should delete the doc in the doc updater', function() {
  1845. return this.DocumentUpdaterHandler.deleteDoc.calledWith(
  1846. project_id,
  1847. this.doc._id.toString()
  1848. )
  1849. })
  1850. it('should insert the doc into the deletedDocs array', function() {
  1851. return this.ProjectEntityMongoUpdateHandler._insertDeletedDocReference
  1852. .calledWith(this.project._id, this.doc)
  1853. .should.equal(true)
  1854. })
  1855. it('should delete the doc in the doc store', function() {
  1856. return this.DocstoreManager.deleteDoc
  1857. .calledWith(project_id, this.doc._id.toString())
  1858. .should.equal(true)
  1859. })
  1860. it('should call the callback', function() {
  1861. return this.callback.called.should.equal(true)
  1862. })
  1863. })
  1864. describe('when the doc is not the root doc', function() {
  1865. beforeEach(function() {
  1866. this.project.rootDoc_id = ObjectId()
  1867. return this.ProjectEntityUpdateHandler._cleanUpDoc(
  1868. this.project,
  1869. this.doc,
  1870. this.path,
  1871. userId,
  1872. this.callback
  1873. )
  1874. })
  1875. it('should not unset the root doc', function() {
  1876. return this.ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal(
  1877. false
  1878. )
  1879. })
  1880. it('should call the callback', function() {
  1881. return this.callback.called.should.equal(true)
  1882. })
  1883. })
  1884. })
  1885. })