ProjectEntityUpdateHandlerTests.js 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const Errors = require('../../../../app/src/Features/Errors/Errors')
  4. const SandboxedModule = require('sandboxed-module')
  5. const { ObjectId } = require('mongodb')
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/Project/ProjectEntityUpdateHandler'
  8. describe('ProjectEntityUpdateHandler', function () {
  9. const projectId = '4eecb1c1bffa66588e0000a1'
  10. const projectHistoryId = '123456'
  11. const docId = '4eecb1c1bffa66588e0000a2'
  12. const fileId = '4eecaffcbffa66588e000009'
  13. const folderId = '4eecaffcbffa66588e000008'
  14. const newFileId = '4eecaffcbffa66588e000099'
  15. const userId = 1234
  16. beforeEach(function () {
  17. this.project = {
  18. _id: projectId,
  19. name: 'project name',
  20. overleaf: {
  21. history: {
  22. id: projectHistoryId,
  23. },
  24. },
  25. }
  26. this.fileUrl = 'filestore.example.com/file'
  27. this.user = { _id: new ObjectId() }
  28. this.DocModel = class Doc {
  29. constructor(options) {
  30. this.name = options.name
  31. this.lines = options.lines
  32. this._id = docId
  33. this.rev = options.rev ?? 0
  34. }
  35. }
  36. this.FileModel = class File {
  37. constructor(options) {
  38. this.name = options.name
  39. // use a new id for replacement files
  40. if (this.name === 'dummy-upload-filename') {
  41. this._id = newFileId
  42. } else {
  43. this._id = fileId
  44. }
  45. this.rev = 0
  46. if (options.linkedFileData != null) {
  47. this.linkedFileData = options.linkedFileData
  48. }
  49. if (options.hash != null) {
  50. this.hash = options.hash
  51. }
  52. }
  53. }
  54. this.docName = 'doc-name'
  55. this.docLines = ['1234', 'abc']
  56. this.doc = { _id: new ObjectId(), name: this.docName }
  57. this.fileName = 'something.jpg'
  58. this.fileSystemPath = 'somehintg'
  59. this.file = { _id: new ObjectId(), name: this.fileName, rev: 2 }
  60. this.linkedFileData = { provider: 'url' }
  61. this.source = 'editor'
  62. this.callback = sinon.stub()
  63. this.DocstoreManager = {
  64. getDoc: sinon.stub(),
  65. isDocDeleted: sinon.stub(),
  66. updateDoc: sinon.stub(),
  67. deleteDoc: sinon.stub(),
  68. }
  69. this.DocumentUpdaterHandler = {
  70. flushDocToMongo: sinon.stub().yields(),
  71. updateProjectStructure: sinon.stub().yields(),
  72. setDocument: sinon.stub(),
  73. resyncProjectHistory: sinon.stub().yields(),
  74. deleteDoc: sinon.stub().yields(),
  75. }
  76. this.fs = {
  77. unlink: sinon.stub().yields(),
  78. }
  79. this.LockManager = {
  80. runWithLock: sinon.spy((namespace, id, runner, callback) =>
  81. runner(callback)
  82. ),
  83. withTimeout: sinon.stub().returns(this.LockManager),
  84. }
  85. this.ProjectModel = {
  86. updateOne: sinon.stub(),
  87. }
  88. this.ProjectGetter = {
  89. getProject: sinon.stub(),
  90. getProjectWithoutDocLines: sinon.stub(),
  91. }
  92. this.ProjectLocator = {
  93. findElement: sinon.stub(),
  94. findElementByPath: sinon.stub(),
  95. }
  96. this.ProjectUpdater = {
  97. markAsUpdated: sinon.stub(),
  98. }
  99. this.ProjectEntityHandler = {
  100. getDoc: sinon.stub(),
  101. getDocPathByProjectIdAndDocId: sinon.stub(),
  102. getAllEntitiesFromProject: sinon.stub(),
  103. }
  104. this.ProjectEntityMongoUpdateHandler = {
  105. addDoc: sinon.stub(),
  106. addFile: sinon.stub(),
  107. addFolder: sinon.stub(),
  108. _confirmFolder: sinon.stub(),
  109. _putElement: sinon.stub(),
  110. _insertDeletedFileReference: sinon.stub(),
  111. replaceFileWithNew: sinon.stub(),
  112. mkdirp: sinon.stub(),
  113. moveEntity: sinon.stub(),
  114. renameEntity: sinon.stub().yields(),
  115. deleteEntity: sinon.stub(),
  116. replaceDocWithFile: sinon.stub(),
  117. replaceFileWithDoc: sinon.stub(),
  118. }
  119. this.TpdsUpdateSender = {
  120. addFile: sinon.stub().yields(),
  121. addDoc: sinon.stub(),
  122. deleteEntity: sinon.stub().yields(),
  123. moveEntity: sinon.stub(),
  124. promises: {
  125. moveEntity: sinon.stub().resolves(),
  126. },
  127. }
  128. this.FileStoreHandler = {
  129. copyFile: sinon.stub(),
  130. uploadFileFromDisk: sinon.stub(),
  131. deleteFile: sinon.stub(),
  132. _buildUrl: sinon
  133. .stub()
  134. .callsFake(
  135. (projectId, fileId) => `www.filestore.test/${projectId}/${fileId}`
  136. ),
  137. }
  138. this.FileWriter = {
  139. writeLinesToDisk: sinon.stub(),
  140. }
  141. this.EditorRealTimeController = {
  142. emitToRoom: sinon.stub(),
  143. }
  144. this.ProjectEntityUpdateHandler = SandboxedModule.require(MODULE_PATH, {
  145. requires: {
  146. '@overleaf/settings': { validRootDocExtensions: ['tex'] },
  147. fs: this.fs,
  148. '../../models/Doc': { Doc: this.DocModel },
  149. '../Docstore/DocstoreManager': this.DocstoreManager,
  150. '../../Features/DocumentUpdater/DocumentUpdaterHandler':
  151. this.DocumentUpdaterHandler,
  152. '../../models/File': { File: this.FileModel },
  153. '../FileStore/FileStoreHandler': this.FileStoreHandler,
  154. '../../infrastructure/LockManager': this.LockManager,
  155. '../../models/Project': { Project: this.ProjectModel },
  156. './ProjectGetter': this.ProjectGetter,
  157. './ProjectLocator': this.ProjectLocator,
  158. './ProjectUpdateHandler': this.ProjectUpdater,
  159. './ProjectEntityHandler': this.ProjectEntityHandler,
  160. './ProjectEntityMongoUpdateHandler':
  161. this.ProjectEntityMongoUpdateHandler,
  162. '../ThirdPartyDataStore/TpdsUpdateSender': this.TpdsUpdateSender,
  163. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  164. '../../infrastructure/FileWriter': this.FileWriter,
  165. },
  166. })
  167. })
  168. describe('updateDocLines', function () {
  169. beforeEach(function () {
  170. this.path = '/somewhere/something.tex'
  171. this.doc = {
  172. _id: docId,
  173. }
  174. this.version = 42
  175. this.ranges = { mock: 'ranges' }
  176. this.lastUpdatedAt = new Date().getTime()
  177. this.lastUpdatedBy = 'fake-last-updater-id'
  178. this.parentFolder = { _id: ObjectId() }
  179. this.DocstoreManager.isDocDeleted.yields(null, false)
  180. this.ProjectGetter.getProject.yields(null, this.project)
  181. this.ProjectLocator.findElement.yields(
  182. null,
  183. this.doc,
  184. {
  185. fileSystem: this.path,
  186. },
  187. this.parentFolder
  188. )
  189. this.TpdsUpdateSender.addDoc.yields()
  190. })
  191. describe('when the doc has been modified', function () {
  192. beforeEach(function () {
  193. this.DocstoreManager.updateDoc.yields(null, true, (this.rev = 5))
  194. this.ProjectEntityUpdateHandler.updateDocLines(
  195. projectId,
  196. docId,
  197. this.docLines,
  198. this.version,
  199. this.ranges,
  200. this.lastUpdatedAt,
  201. this.lastUpdatedBy,
  202. this.callback
  203. )
  204. })
  205. it('should get the project with very few fields', function () {
  206. this.ProjectGetter.getProject
  207. .calledWith(projectId, {
  208. name: true,
  209. rootFolder: true,
  210. })
  211. .should.equal(true)
  212. })
  213. it('should find the doc', function () {
  214. this.ProjectLocator.findElement
  215. .calledWith({
  216. project: this.project,
  217. type: 'docs',
  218. element_id: docId,
  219. })
  220. .should.equal(true)
  221. })
  222. it('should update the doc in the docstore', function () {
  223. this.DocstoreManager.updateDoc
  224. .calledWith(
  225. projectId,
  226. docId,
  227. this.docLines,
  228. this.version,
  229. this.ranges
  230. )
  231. .should.equal(true)
  232. })
  233. it('should mark the project as updated', function () {
  234. sinon.assert.calledWith(
  235. this.ProjectUpdater.markAsUpdated,
  236. projectId,
  237. this.lastUpdatedAt,
  238. this.lastUpdatedBy
  239. )
  240. })
  241. it('should send the doc the to the TPDS', function () {
  242. this.TpdsUpdateSender.addDoc.should.have.been.calledWith({
  243. projectId,
  244. projectName: this.project.name,
  245. docId,
  246. rev: this.rev,
  247. path: this.path,
  248. folderId: this.parentFolder._id,
  249. })
  250. })
  251. it('should call the callback', function () {
  252. this.callback.called.should.equal(true)
  253. })
  254. })
  255. describe('when the doc has not been modified', function () {
  256. beforeEach(function () {
  257. this.DocstoreManager.updateDoc.yields(null, false, (this.rev = 5))
  258. this.ProjectEntityUpdateHandler.updateDocLines(
  259. projectId,
  260. docId,
  261. this.docLines,
  262. this.version,
  263. this.ranges,
  264. this.lastUpdatedAt,
  265. this.lastUpdatedBy,
  266. this.callback
  267. )
  268. })
  269. it('should not mark the project as updated', function () {
  270. this.ProjectUpdater.markAsUpdated.called.should.equal(false)
  271. })
  272. it('should not send the doc the to the TPDS', function () {
  273. this.TpdsUpdateSender.addDoc.called.should.equal(false)
  274. })
  275. it('should call the callback', function () {
  276. this.callback.called.should.equal(true)
  277. })
  278. })
  279. describe('when the doc has been deleted', function () {
  280. beforeEach(function () {
  281. this.ProjectGetter.getProject.yields(null, this.project)
  282. this.ProjectLocator.findElement.yields(new Errors.NotFoundError())
  283. this.DocstoreManager.isDocDeleted.yields(null, true)
  284. this.DocstoreManager.updateDoc.yields()
  285. this.ProjectEntityUpdateHandler.updateDocLines(
  286. projectId,
  287. docId,
  288. this.docLines,
  289. this.version,
  290. this.ranges,
  291. this.lastUpdatedAt,
  292. this.lastUpdatedBy,
  293. this.callback
  294. )
  295. })
  296. it('should update the doc in the docstore', function () {
  297. this.DocstoreManager.updateDoc
  298. .calledWith(
  299. projectId,
  300. docId,
  301. this.docLines,
  302. this.version,
  303. this.ranges
  304. )
  305. .should.equal(true)
  306. })
  307. it('should not mark the project as updated', function () {
  308. this.ProjectUpdater.markAsUpdated.called.should.equal(false)
  309. })
  310. it('should not send the doc the to the TPDS', function () {
  311. this.TpdsUpdateSender.addDoc.called.should.equal(false)
  312. })
  313. it('should call the callback', function () {
  314. this.callback.called.should.equal(true)
  315. })
  316. })
  317. describe('when projects and docs collection are de-synced', function () {
  318. beforeEach(function () {
  319. this.ProjectGetter.getProject.yields(null, this.project)
  320. // The doc is not in the file-tree, but also not marked as deleted.
  321. // This should not happen, but web should handle it.
  322. this.ProjectLocator.findElement.yields(new Errors.NotFoundError())
  323. this.DocstoreManager.isDocDeleted.yields(null, false)
  324. this.DocstoreManager.updateDoc.yields()
  325. this.ProjectEntityUpdateHandler.updateDocLines(
  326. projectId,
  327. docId,
  328. this.docLines,
  329. this.version,
  330. this.ranges,
  331. this.lastUpdatedAt,
  332. this.lastUpdatedBy,
  333. this.callback
  334. )
  335. })
  336. it('should update the doc in the docstore', function () {
  337. this.DocstoreManager.updateDoc
  338. .calledWith(
  339. projectId,
  340. docId,
  341. this.docLines,
  342. this.version,
  343. this.ranges
  344. )
  345. .should.equal(true)
  346. })
  347. it('should not mark the project as updated', function () {
  348. this.ProjectUpdater.markAsUpdated.called.should.equal(false)
  349. })
  350. it('should not send the doc the to the TPDS', function () {
  351. this.TpdsUpdateSender.addDoc.called.should.equal(false)
  352. })
  353. it('should call the callback', function () {
  354. this.callback.called.should.equal(true)
  355. })
  356. })
  357. describe('when the doc is not related to the project', function () {
  358. beforeEach(function () {
  359. this.ProjectGetter.getProject.yields(null, this.project)
  360. this.ProjectLocator.findElement.yields(new Errors.NotFoundError())
  361. this.DocstoreManager.isDocDeleted.yields(new Errors.NotFoundError())
  362. this.ProjectEntityUpdateHandler.updateDocLines(
  363. projectId,
  364. docId,
  365. this.docLines,
  366. this.version,
  367. this.ranges,
  368. this.lastUpdatedAt,
  369. this.lastUpdatedBy,
  370. this.callback
  371. )
  372. })
  373. it('should return a not found error', function () {
  374. this.callback
  375. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  376. .should.equal(true)
  377. })
  378. it('should not update the doc', function () {
  379. this.DocstoreManager.updateDoc.called.should.equal(false)
  380. })
  381. it('should not send the doc the to the TPDS', function () {
  382. this.TpdsUpdateSender.addDoc.called.should.equal(false)
  383. })
  384. })
  385. describe('when the project is not found', function () {
  386. beforeEach(function () {
  387. this.ProjectGetter.getProject.yields(new Errors.NotFoundError())
  388. this.ProjectEntityUpdateHandler.updateDocLines(
  389. projectId,
  390. docId,
  391. this.docLines,
  392. this.version,
  393. this.ranges,
  394. this.lastUpdatedAt,
  395. this.lastUpdatedBy,
  396. this.callback
  397. )
  398. })
  399. it('should return a not found error', function () {
  400. this.callback
  401. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  402. .should.equal(true)
  403. })
  404. it('should not update the doc', function () {
  405. this.DocstoreManager.updateDoc.called.should.equal(false)
  406. })
  407. it('should not send the doc the to the TPDS', function () {
  408. this.TpdsUpdateSender.addDoc.called.should.equal(false)
  409. })
  410. })
  411. })
  412. describe('setRootDoc', function () {
  413. beforeEach(function () {
  414. this.rootDocId = 'root-doc-id-123123'
  415. })
  416. it('should call Project.updateOne when the doc exists and has a valid extension', function () {
  417. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId.yields(
  418. null,
  419. `/main.tex`
  420. )
  421. this.ProjectEntityUpdateHandler.setRootDoc(
  422. projectId,
  423. this.rootDocId,
  424. () => {}
  425. )
  426. this.ProjectModel.updateOne
  427. .calledWith({ _id: projectId }, { rootDoc_id: this.rootDocId })
  428. .should.equal(true)
  429. })
  430. it("should not call Project.updateOne when the doc doesn't exist", function () {
  431. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId.yields(
  432. Errors.NotFoundError
  433. )
  434. this.ProjectEntityUpdateHandler.setRootDoc(
  435. projectId,
  436. this.rootDocId,
  437. () => {}
  438. )
  439. this.ProjectModel.updateOne
  440. .calledWith({ _id: projectId }, { rootDoc_id: this.rootDocId })
  441. .should.equal(false)
  442. })
  443. it('should call the callback with an UnsupportedFileTypeError when the doc has an unaccepted file extension', function () {
  444. this.ProjectEntityHandler.getDocPathByProjectIdAndDocId.yields(
  445. null,
  446. `/foo/bar.baz`
  447. )
  448. this.ProjectEntityUpdateHandler.setRootDoc(
  449. projectId,
  450. this.rootDocId,
  451. this.callback
  452. )
  453. expect(this.callback.firstCall.args[0]).to.be.an.instanceof(
  454. Errors.UnsupportedFileTypeError
  455. )
  456. })
  457. })
  458. describe('unsetRootDoc', function () {
  459. it('should call Project.updateOne', function () {
  460. this.ProjectEntityUpdateHandler.unsetRootDoc(projectId)
  461. this.ProjectModel.updateOne
  462. .calledWith({ _id: projectId }, { $unset: { rootDoc_id: true } })
  463. .should.equal(true)
  464. })
  465. })
  466. describe('addDoc', function () {
  467. describe('adding a doc', function () {
  468. beforeEach(function () {
  469. this.path = '/path/to/doc'
  470. this.rev = 5
  471. this.newDoc = new this.DocModel({
  472. name: this.docName,
  473. lines: undefined,
  474. _id: docId,
  475. rev: this.rev,
  476. })
  477. this.DocstoreManager.updateDoc.yields(null, false, this.rev)
  478. this.TpdsUpdateSender.addDoc.yields()
  479. this.ProjectEntityMongoUpdateHandler.addDoc.yields(
  480. null,
  481. { path: { fileSystem: this.path } },
  482. this.project
  483. )
  484. this.ProjectEntityUpdateHandler.addDoc(
  485. projectId,
  486. docId,
  487. this.docName,
  488. this.docLines,
  489. userId,
  490. this.source,
  491. this.callback
  492. )
  493. })
  494. it('creates the doc without history', function () {
  495. this.DocstoreManager.updateDoc
  496. .calledWith(projectId, docId, this.docLines, 0, {})
  497. .should.equal(true)
  498. })
  499. it('sends the change in project structure to the doc updater', function () {
  500. const newDocs = [
  501. {
  502. doc: this.newDoc,
  503. path: this.path,
  504. docLines: this.docLines.join('\n'),
  505. },
  506. ]
  507. this.DocumentUpdaterHandler.updateProjectStructure.should.have.been.calledWith(
  508. projectId,
  509. projectHistoryId,
  510. userId,
  511. {
  512. newDocs,
  513. newProject: this.project,
  514. },
  515. this.source
  516. )
  517. })
  518. })
  519. describe('adding a doc with an invalid name', function () {
  520. beforeEach(function () {
  521. this.path = '/path/to/doc'
  522. this.newDoc = { _id: docId }
  523. this.ProjectEntityUpdateHandler.addDoc(
  524. projectId,
  525. folderId,
  526. `*${this.docName}`,
  527. this.docLines,
  528. userId,
  529. this.source,
  530. this.callback
  531. )
  532. })
  533. it('returns an error', function () {
  534. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  535. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  536. })
  537. })
  538. })
  539. describe('addFile', function () {
  540. describe('adding a file', function () {
  541. beforeEach(function () {
  542. this.path = '/path/to/file'
  543. this.newFile = {
  544. _id: fileId,
  545. rev: 0,
  546. name: this.fileName,
  547. linkedFileData: this.linkedFileData,
  548. }
  549. this.FileStoreHandler.uploadFileFromDisk.yields(
  550. null,
  551. this.fileUrl,
  552. this.newFile
  553. )
  554. this.TpdsUpdateSender.addFile.yields()
  555. this.ProjectEntityMongoUpdateHandler.addFile.yields(
  556. null,
  557. { path: { fileSystem: this.path } },
  558. this.project
  559. )
  560. this.ProjectEntityUpdateHandler.addFile(
  561. projectId,
  562. folderId,
  563. this.fileName,
  564. this.fileSystemPath,
  565. this.linkedFileData,
  566. userId,
  567. this.source,
  568. this.callback
  569. )
  570. })
  571. it('updates the file in the filestore', function () {
  572. this.FileStoreHandler.uploadFileFromDisk
  573. .calledWith(
  574. projectId,
  575. { name: this.fileName, linkedFileData: this.linkedFileData },
  576. this.fileSystemPath
  577. )
  578. .should.equal(true)
  579. })
  580. it('updates the file in mongo', function () {
  581. const fileMatcher = sinon.match(file => {
  582. return file.name === this.fileName
  583. })
  584. this.ProjectEntityMongoUpdateHandler.addFile
  585. .calledWithMatch(projectId, folderId, fileMatcher)
  586. .should.equal(true)
  587. })
  588. it('notifies the tpds', function () {
  589. this.TpdsUpdateSender.addFile
  590. .calledWith({
  591. projectId,
  592. projectName: this.project.name,
  593. fileId,
  594. rev: 0,
  595. path: this.path,
  596. folderId,
  597. })
  598. .should.equal(true)
  599. })
  600. it('should mark the project as updated', function () {
  601. const args = this.ProjectUpdater.markAsUpdated.args[0]
  602. args[0].should.equal(projectId)
  603. args[1].should.exist
  604. args[2].should.equal(userId)
  605. })
  606. it('sends the change in project structure to the doc updater', function () {
  607. const newFiles = [
  608. {
  609. file: this.newFile,
  610. path: this.path,
  611. url: this.fileUrl,
  612. },
  613. ]
  614. this.DocumentUpdaterHandler.updateProjectStructure
  615. .calledWith(
  616. projectId,
  617. projectHistoryId,
  618. userId,
  619. {
  620. newFiles,
  621. newProject: this.project,
  622. },
  623. this.source
  624. )
  625. .should.equal(true)
  626. })
  627. })
  628. describe('adding a file with an invalid name', function () {
  629. beforeEach(function () {
  630. this.path = '/path/to/file'
  631. this.newFile = {
  632. _id: fileId,
  633. rev: 0,
  634. name: this.fileName,
  635. linkedFileData: this.linkedFileData,
  636. }
  637. this.TpdsUpdateSender.addFile.yields()
  638. this.ProjectEntityMongoUpdateHandler.addFile.yields(
  639. null,
  640. { path: { fileSystem: this.path } },
  641. this.project
  642. )
  643. this.ProjectEntityUpdateHandler.addFile(
  644. projectId,
  645. folderId,
  646. `*${this.fileName}`,
  647. this.fileSystemPath,
  648. this.linkedFileData,
  649. userId,
  650. this.source,
  651. this.callback
  652. )
  653. })
  654. it('returns an error', function () {
  655. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  656. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  657. })
  658. })
  659. })
  660. describe('upsertDoc', function () {
  661. describe('upserting into an invalid folder', function () {
  662. beforeEach(function () {
  663. this.ProjectLocator.findElement.yields()
  664. this.ProjectEntityUpdateHandler.upsertDoc(
  665. projectId,
  666. folderId,
  667. this.docName,
  668. this.docLines,
  669. this.source,
  670. userId,
  671. this.callback
  672. )
  673. })
  674. it('returns an error', function () {
  675. const errorMatcher = sinon.match.instanceOf(Error)
  676. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  677. })
  678. })
  679. describe('updating an existing doc', function () {
  680. beforeEach(function () {
  681. this.existingDoc = { _id: docId, name: this.docName }
  682. this.existingFile = { _id: fileId, name: this.fileName }
  683. this.folder = {
  684. _id: folderId,
  685. docs: [this.existingDoc],
  686. fileRefs: [this.existingFile],
  687. }
  688. this.ProjectLocator.findElement.yields(null, this.folder)
  689. this.DocumentUpdaterHandler.setDocument.yields()
  690. this.ProjectEntityUpdateHandler.upsertDoc(
  691. projectId,
  692. folderId,
  693. this.docName,
  694. this.docLines,
  695. this.source,
  696. userId,
  697. this.callback
  698. )
  699. })
  700. it('tries to find the folder', function () {
  701. this.ProjectLocator.findElement
  702. .calledWith({
  703. project_id: projectId,
  704. element_id: folderId,
  705. type: 'folder',
  706. })
  707. .should.equal(true)
  708. })
  709. it('updates the doc contents', function () {
  710. this.DocumentUpdaterHandler.setDocument
  711. .calledWith(
  712. projectId,
  713. this.existingDoc._id,
  714. userId,
  715. this.docLines,
  716. this.source
  717. )
  718. .should.equal(true)
  719. })
  720. it('returns the doc', function () {
  721. this.callback.calledWith(null, this.existingDoc, false)
  722. })
  723. })
  724. describe('creating a new doc', function () {
  725. beforeEach(function () {
  726. this.folder = { _id: folderId, docs: [], fileRefs: [] }
  727. this.newDoc = { _id: docId }
  728. this.ProjectLocator.findElement.yields(null, this.folder)
  729. this.ProjectEntityUpdateHandler.addDocWithRanges = {
  730. withoutLock: sinon.stub().yields(null, this.newDoc),
  731. }
  732. this.ProjectEntityUpdateHandler.upsertDoc(
  733. projectId,
  734. folderId,
  735. this.docName,
  736. this.docLines,
  737. this.source,
  738. userId,
  739. this.callback
  740. )
  741. })
  742. it('tries to find the folder', function () {
  743. this.ProjectLocator.findElement
  744. .calledWith({
  745. project_id: projectId,
  746. element_id: folderId,
  747. type: 'folder',
  748. })
  749. .should.equal(true)
  750. })
  751. it('adds the doc', function () {
  752. this.ProjectEntityUpdateHandler.addDocWithRanges.withoutLock
  753. .calledWith(
  754. projectId,
  755. folderId,
  756. this.docName,
  757. this.docLines,
  758. {},
  759. userId,
  760. this.source
  761. )
  762. .should.equal(true)
  763. })
  764. it('returns the doc', function () {
  765. this.callback.calledWith(null, this.newDoc, true)
  766. })
  767. })
  768. describe('upserting a new doc with an invalid name', function () {
  769. beforeEach(function () {
  770. this.folder = { _id: folderId, docs: [], fileRefs: [] }
  771. this.newDoc = { _id: docId }
  772. this.ProjectLocator.findElement.yields(null, this.folder)
  773. this.ProjectEntityUpdateHandler.addDocWithRanges = {
  774. withoutLock: sinon.stub().yields(null, this.newDoc),
  775. }
  776. this.ProjectEntityUpdateHandler.upsertDoc(
  777. projectId,
  778. folderId,
  779. `*${this.docName}`,
  780. this.docLines,
  781. this.source,
  782. userId,
  783. this.callback
  784. )
  785. })
  786. it('returns an error', function () {
  787. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  788. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  789. })
  790. })
  791. describe('upserting a doc on top of a file', function () {
  792. beforeEach(function () {
  793. this.newProject = {
  794. name: 'new project',
  795. overleaf: { history: { id: projectHistoryId } },
  796. }
  797. this.existingFile = { _id: fileId, name: 'foo.tex', rev: 12 }
  798. this.folder = { _id: folderId, docs: [], fileRefs: [this.existingFile] }
  799. this.newDoc = { _id: docId }
  800. this.docLines = ['line one', 'line two']
  801. this.folderPath = '/path/to/folder'
  802. this.filePath = '/path/to/folder/foo.tex'
  803. this.ProjectLocator.findElement
  804. .withArgs({
  805. project_id: projectId,
  806. element_id: this.folder._id,
  807. type: 'folder',
  808. })
  809. .yields(null, this.folder, {
  810. fileSystem: this.folderPath,
  811. })
  812. this.DocstoreManager.updateDoc.yields()
  813. this.ProjectEntityMongoUpdateHandler.replaceFileWithDoc.yields(
  814. null,
  815. this.newProject
  816. )
  817. this.TpdsUpdateSender.addDoc.yields()
  818. this.ProjectEntityUpdateHandler.upsertDoc(
  819. projectId,
  820. folderId,
  821. 'foo.tex',
  822. this.docLines,
  823. this.source,
  824. userId,
  825. this.callback
  826. )
  827. })
  828. it('notifies docstore of the new doc', function () {
  829. expect(this.DocstoreManager.updateDoc).to.have.been.calledWith(
  830. projectId,
  831. this.newDoc._id,
  832. this.docLines
  833. )
  834. })
  835. it('adds the new doc and removes the file in one go', function () {
  836. expect(
  837. this.ProjectEntityMongoUpdateHandler.replaceFileWithDoc
  838. ).to.have.been.calledWithMatch(
  839. projectId,
  840. this.existingFile._id,
  841. this.newDoc
  842. )
  843. })
  844. it('sends the doc to TPDS', function () {
  845. expect(this.TpdsUpdateSender.addDoc).to.have.been.calledWith({
  846. projectId,
  847. docId: this.newDoc._id,
  848. path: this.filePath,
  849. projectName: this.newProject.name,
  850. rev: this.existingFile.rev + 1,
  851. folderId,
  852. })
  853. })
  854. it('sends the updates to the doc updater', function () {
  855. const oldFiles = [
  856. {
  857. file: this.existingFile,
  858. path: this.filePath,
  859. },
  860. ]
  861. const newDocs = [
  862. {
  863. doc: sinon.match(this.newDoc),
  864. path: this.filePath,
  865. docLines: this.docLines.join('\n'),
  866. },
  867. ]
  868. expect(
  869. this.DocumentUpdaterHandler.updateProjectStructure
  870. ).to.have.been.calledWith(
  871. projectId,
  872. projectHistoryId,
  873. userId,
  874. {
  875. oldFiles,
  876. newDocs,
  877. newProject: this.newProject,
  878. },
  879. this.source
  880. )
  881. })
  882. it('should notify everyone of the file deletion', function () {
  883. expect(
  884. this.EditorRealTimeController.emitToRoom
  885. ).to.have.been.calledWith(
  886. projectId,
  887. 'removeEntity',
  888. this.existingFile._id,
  889. 'convertFileToDoc'
  890. )
  891. })
  892. })
  893. })
  894. describe('upsertFile', function () {
  895. beforeEach(function () {
  896. this.FileStoreHandler.uploadFileFromDisk.yields(
  897. null,
  898. this.fileUrl,
  899. this.file
  900. )
  901. })
  902. describe('upserting into an invalid folder', function () {
  903. beforeEach(function () {
  904. this.ProjectLocator.findElement.yields()
  905. this.ProjectEntityUpdateHandler.upsertFile(
  906. projectId,
  907. folderId,
  908. this.fileName,
  909. this.fileSystemPath,
  910. this.linkedFileData,
  911. userId,
  912. this.source,
  913. this.callback
  914. )
  915. })
  916. it('returns an error', function () {
  917. const errorMatcher = sinon.match.instanceOf(Error)
  918. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  919. })
  920. })
  921. describe('updating an existing file', function () {
  922. beforeEach(function () {
  923. this.existingFile = { _id: fileId, name: this.fileName, rev: 1 }
  924. this.newFile = { _id: ObjectId(), name: this.fileName, rev: 3 }
  925. this.folder = { _id: folderId, fileRefs: [this.existingFile], docs: [] }
  926. this.ProjectLocator.findElement.yields(null, this.folder)
  927. this.newProject = 'new-project-stub'
  928. this.ProjectEntityMongoUpdateHandler.replaceFileWithNew.yields(
  929. null,
  930. this.existingFile,
  931. this.project,
  932. { fileSystem: this.fileSystemPath },
  933. this.newProject,
  934. this.newFile
  935. )
  936. this.ProjectEntityUpdateHandler.upsertFile(
  937. projectId,
  938. folderId,
  939. this.fileName,
  940. this.fileSystemPath,
  941. this.linkedFileData,
  942. userId,
  943. this.source,
  944. this.callback
  945. )
  946. })
  947. it('uploads a new version of the file', function () {
  948. this.FileStoreHandler.uploadFileFromDisk.should.have.been.calledWith(
  949. projectId,
  950. {
  951. name: this.fileName,
  952. linkedFileData: this.linkedFileData,
  953. },
  954. this.fileSystemPath
  955. )
  956. })
  957. it('replaces the file in mongo', function () {
  958. this.ProjectEntityMongoUpdateHandler.replaceFileWithNew.should.have.been.calledWith(
  959. projectId,
  960. this.existingFile._id,
  961. this.file
  962. )
  963. })
  964. it('notifies the tpds', function () {
  965. this.TpdsUpdateSender.addFile.should.have.been.calledWith({
  966. projectId,
  967. projectName: this.project.name,
  968. fileId: this.newFile._id,
  969. rev: this.newFile.rev,
  970. path: this.fileSystemPath,
  971. folderId,
  972. })
  973. })
  974. it('should mark the project as updated', function () {
  975. const args = this.ProjectUpdater.markAsUpdated.args[0]
  976. args[0].should.equal(projectId)
  977. args[1].should.exist
  978. args[2].should.equal(userId)
  979. })
  980. it('updates the project structure in the doc updater', function () {
  981. const oldFiles = [
  982. {
  983. file: this.existingFile,
  984. path: this.fileSystemPath,
  985. },
  986. ]
  987. const newFiles = [
  988. {
  989. file: this.newFile,
  990. path: this.fileSystemPath,
  991. url: this.fileUrl,
  992. },
  993. ]
  994. this.DocumentUpdaterHandler.updateProjectStructure.should.have.been.calledWith(
  995. projectId,
  996. projectHistoryId,
  997. userId,
  998. {
  999. oldFiles,
  1000. newFiles,
  1001. newProject: this.newProject,
  1002. },
  1003. this.source
  1004. )
  1005. })
  1006. it('returns the file', function () {
  1007. this.callback.calledWith(null, this.existingFile, false)
  1008. })
  1009. })
  1010. describe('creating a new file', function () {
  1011. beforeEach(function () {
  1012. this.folder = { _id: folderId, fileRefs: [], docs: [] }
  1013. this.newFile = { _id: fileId }
  1014. this.ProjectLocator.findElement.yields(null, this.folder)
  1015. this.FileStoreHandler.uploadFileFromDisk.yields(
  1016. null,
  1017. this.fileUrl,
  1018. this.newFile
  1019. )
  1020. this.ProjectEntityUpdateHandler.addFile = {
  1021. mainTask: sinon.stub().yields(null, this.newFile),
  1022. }
  1023. this.ProjectEntityUpdateHandler.upsertFile(
  1024. projectId,
  1025. folderId,
  1026. this.fileName,
  1027. this.fileSystemPath,
  1028. this.linkedFileData,
  1029. userId,
  1030. this.source,
  1031. this.callback
  1032. )
  1033. })
  1034. it('tries to find the folder', function () {
  1035. this.ProjectLocator.findElement
  1036. .calledWith({
  1037. project_id: projectId,
  1038. element_id: folderId,
  1039. type: 'folder',
  1040. })
  1041. .should.equal(true)
  1042. })
  1043. it('adds the file', function () {
  1044. expect(
  1045. this.ProjectEntityUpdateHandler.addFile.mainTask
  1046. ).to.have.been.calledWith(
  1047. projectId,
  1048. folderId,
  1049. this.fileName,
  1050. this.fileSystemPath,
  1051. this.linkedFileData,
  1052. userId,
  1053. this.newFile,
  1054. this.fileUrl,
  1055. this.source
  1056. )
  1057. })
  1058. it('returns the file', function () {
  1059. this.callback.calledWith(null, this.newFile, true)
  1060. })
  1061. })
  1062. describe('upserting a new file with an invalid name', function () {
  1063. beforeEach(function () {
  1064. this.folder = { _id: folderId, fileRefs: [] }
  1065. this.newFile = { _id: fileId }
  1066. this.ProjectLocator.findElement.yields(null, this.folder)
  1067. this.ProjectEntityUpdateHandler.addFile = {
  1068. mainTask: sinon.stub().yields(null, this.newFile),
  1069. }
  1070. this.ProjectEntityUpdateHandler.upsertFile(
  1071. projectId,
  1072. folderId,
  1073. `*${this.fileName}`,
  1074. this.fileSystemPath,
  1075. this.linkedFileData,
  1076. userId,
  1077. this.source,
  1078. this.callback
  1079. )
  1080. })
  1081. it('returns an error', function () {
  1082. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1083. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1084. })
  1085. })
  1086. describe('upserting file on top of a doc', function () {
  1087. beforeEach(function (done) {
  1088. this.path = '/path/to/doc'
  1089. this.existingDoc = { _id: new ObjectId(), name: this.fileName }
  1090. this.folder = {
  1091. _id: folderId,
  1092. fileRefs: [],
  1093. docs: [this.existingDoc],
  1094. }
  1095. this.ProjectLocator.findElement
  1096. .withArgs({
  1097. project_id: this.project._id.toString(),
  1098. element_id: folderId,
  1099. type: 'folder',
  1100. })
  1101. .yields(null, this.folder)
  1102. this.ProjectLocator.findElement
  1103. .withArgs({
  1104. project_id: this.project._id.toString(),
  1105. element_id: this.existingDoc._id,
  1106. type: 'doc',
  1107. })
  1108. .yields(
  1109. null,
  1110. this.existingDoc,
  1111. { fileSystem: this.path },
  1112. this.folder
  1113. )
  1114. this.newFileUrl = 'new-file-url'
  1115. this.newFile = {
  1116. _id: newFileId,
  1117. name: 'dummy-upload-filename',
  1118. rev: 0,
  1119. linkedFileData: this.linkedFileData,
  1120. }
  1121. this.newProject = {
  1122. name: 'new project',
  1123. overleaf: { history: { id: projectHistoryId } },
  1124. }
  1125. this.FileStoreHandler.uploadFileFromDisk.yields(
  1126. null,
  1127. this.newFileUrl,
  1128. this.newFile
  1129. )
  1130. this.ProjectEntityMongoUpdateHandler.replaceDocWithFile.yields(
  1131. null,
  1132. this.newProject
  1133. )
  1134. this.ProjectEntityUpdateHandler.upsertFile(
  1135. projectId,
  1136. folderId,
  1137. this.fileName,
  1138. this.fileSystemPath,
  1139. this.linkedFileData,
  1140. userId,
  1141. this.source,
  1142. done
  1143. )
  1144. })
  1145. it('replaces the existing doc with a file', function () {
  1146. expect(
  1147. this.ProjectEntityMongoUpdateHandler.replaceDocWithFile
  1148. ).to.have.been.calledWith(projectId, this.existingDoc._id, this.newFile)
  1149. })
  1150. it('updates the doc structure', function () {
  1151. const oldDocs = [
  1152. {
  1153. doc: this.existingDoc,
  1154. path: this.path,
  1155. },
  1156. ]
  1157. const newFiles = [
  1158. {
  1159. file: this.newFile,
  1160. path: this.path,
  1161. url: this.newFileUrl,
  1162. },
  1163. ]
  1164. const updates = {
  1165. oldDocs,
  1166. newFiles,
  1167. newProject: this.newProject,
  1168. }
  1169. expect(
  1170. this.DocumentUpdaterHandler.updateProjectStructure
  1171. ).to.have.been.calledWith(
  1172. projectId,
  1173. projectHistoryId,
  1174. userId,
  1175. updates,
  1176. this.source
  1177. )
  1178. })
  1179. it('tells everyone in the room the doc is removed', function () {
  1180. expect(
  1181. this.EditorRealTimeController.emitToRoom
  1182. ).to.have.been.calledWith(
  1183. projectId,
  1184. 'removeEntity',
  1185. this.existingDoc._id,
  1186. 'convertDocToFile'
  1187. )
  1188. })
  1189. })
  1190. })
  1191. describe('upsertDocWithPath', function () {
  1192. describe('upserting a doc', function () {
  1193. beforeEach(function () {
  1194. this.path = '/folder/doc.tex'
  1195. this.newFolders = ['mock-a', 'mock-b']
  1196. this.folder = { _id: folderId }
  1197. this.doc = { _id: docId }
  1198. this.isNewDoc = true
  1199. this.ProjectEntityUpdateHandler.mkdirp = {
  1200. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder),
  1201. }
  1202. this.ProjectEntityUpdateHandler.upsertDoc = {
  1203. withoutLock: sinon.stub().yields(null, this.doc, this.isNewDoc),
  1204. }
  1205. this.ProjectEntityUpdateHandler.upsertDocWithPath(
  1206. projectId,
  1207. this.path,
  1208. this.docLines,
  1209. this.source,
  1210. userId,
  1211. this.callback
  1212. )
  1213. })
  1214. it('creates any necessary folders', function () {
  1215. this.ProjectEntityUpdateHandler.mkdirp.withoutLock
  1216. .calledWith(projectId, '/folder')
  1217. .should.equal(true)
  1218. })
  1219. it('upserts the doc', function () {
  1220. this.ProjectEntityUpdateHandler.upsertDoc.withoutLock
  1221. .calledWith(
  1222. projectId,
  1223. this.folder._id,
  1224. 'doc.tex',
  1225. this.docLines,
  1226. this.source,
  1227. userId
  1228. )
  1229. .should.equal(true)
  1230. })
  1231. it('calls the callback', function () {
  1232. this.callback
  1233. .calledWith(
  1234. null,
  1235. this.doc,
  1236. this.isNewDoc,
  1237. this.newFolders,
  1238. this.folder
  1239. )
  1240. .should.equal(true)
  1241. })
  1242. })
  1243. describe('upserting a doc with an invalid path', function () {
  1244. beforeEach(function () {
  1245. this.path = '/*folder/doc.tex'
  1246. this.newFolders = ['mock-a', 'mock-b']
  1247. this.folder = { _id: folderId }
  1248. this.doc = { _id: docId }
  1249. this.isNewDoc = true
  1250. this.ProjectEntityUpdateHandler.mkdirp = {
  1251. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder),
  1252. }
  1253. this.ProjectEntityUpdateHandler.upsertDoc = {
  1254. withoutLock: sinon.stub().yields(null, this.doc, this.isNewDoc),
  1255. }
  1256. this.ProjectEntityUpdateHandler.upsertDocWithPath(
  1257. projectId,
  1258. this.path,
  1259. this.docLines,
  1260. this.source,
  1261. userId,
  1262. this.callback
  1263. )
  1264. })
  1265. it('returns an error', function () {
  1266. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1267. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1268. })
  1269. })
  1270. describe('upserting a doc with an invalid name', function () {
  1271. beforeEach(function () {
  1272. this.path = '/folder/*doc.tex'
  1273. this.newFolders = ['mock-a', 'mock-b']
  1274. this.folder = { _id: folderId }
  1275. this.doc = { _id: docId }
  1276. this.isNewDoc = true
  1277. this.ProjectEntityUpdateHandler.mkdirp = {
  1278. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder),
  1279. }
  1280. this.ProjectEntityUpdateHandler.upsertDoc = {
  1281. withoutLock: sinon.stub().yields(null, this.doc, this.isNewDoc),
  1282. }
  1283. this.ProjectEntityUpdateHandler.upsertDocWithPath(
  1284. projectId,
  1285. this.path,
  1286. this.docLines,
  1287. this.source,
  1288. userId,
  1289. this.callback
  1290. )
  1291. })
  1292. it('returns an error', function () {
  1293. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1294. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1295. })
  1296. })
  1297. })
  1298. describe('upsertFileWithPath', function () {
  1299. describe('upserting a file', function () {
  1300. beforeEach(function () {
  1301. this.path = '/folder/file.png'
  1302. this.newFolders = ['mock-a', 'mock-b']
  1303. this.folder = { _id: folderId }
  1304. this.file = { _id: fileId }
  1305. this.isNewFile = true
  1306. this.FileStoreHandler.uploadFileFromDisk.yields(
  1307. null,
  1308. this.fileUrl,
  1309. this.newFile
  1310. )
  1311. this.ProjectEntityUpdateHandler.mkdirp = {
  1312. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder),
  1313. }
  1314. this.ProjectEntityUpdateHandler.upsertFile = {
  1315. mainTask: sinon.stub().yields(null, this.file, this.isNewFile),
  1316. }
  1317. this.ProjectEntityUpdateHandler.upsertFileWithPath(
  1318. projectId,
  1319. this.path,
  1320. this.fileSystemPath,
  1321. this.linkedFileData,
  1322. userId,
  1323. this.source,
  1324. this.callback
  1325. )
  1326. })
  1327. it('creates any necessary folders', function () {
  1328. this.ProjectEntityUpdateHandler.mkdirp.withoutLock
  1329. .calledWith(projectId, '/folder')
  1330. .should.equal(true)
  1331. })
  1332. it('upserts the file', function () {
  1333. this.ProjectEntityUpdateHandler.upsertFile.mainTask
  1334. .calledWith(
  1335. projectId,
  1336. this.folder._id,
  1337. 'file.png',
  1338. this.fileSystemPath,
  1339. this.linkedFileData,
  1340. userId,
  1341. this.newFile,
  1342. this.fileUrl,
  1343. this.source
  1344. )
  1345. .should.equal(true)
  1346. })
  1347. it('calls the callback', function () {
  1348. this.callback
  1349. .calledWith(
  1350. null,
  1351. this.file,
  1352. this.isNewFile,
  1353. undefined,
  1354. this.newFolders,
  1355. this.folder
  1356. )
  1357. .should.equal(true)
  1358. })
  1359. })
  1360. describe('upserting a file with an invalid path', function () {
  1361. beforeEach(function () {
  1362. this.path = '/*folder/file.png'
  1363. this.newFolders = ['mock-a', 'mock-b']
  1364. this.folder = { _id: folderId }
  1365. this.file = { _id: fileId }
  1366. this.isNewFile = true
  1367. this.ProjectEntityUpdateHandler.mkdirp = {
  1368. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder),
  1369. }
  1370. this.ProjectEntityUpdateHandler.upsertFile = {
  1371. mainTask: sinon.stub().yields(null, this.file, this.isNewFile),
  1372. }
  1373. this.ProjectEntityUpdateHandler.upsertFileWithPath(
  1374. projectId,
  1375. this.path,
  1376. this.fileSystemPath,
  1377. this.linkedFileData,
  1378. userId,
  1379. this.source,
  1380. this.callback
  1381. )
  1382. })
  1383. it('returns an error', function () {
  1384. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1385. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1386. })
  1387. })
  1388. describe('upserting a file with an invalid name', function () {
  1389. beforeEach(function () {
  1390. this.path = '/folder/*file.png'
  1391. this.newFolders = ['mock-a', 'mock-b']
  1392. this.folder = { _id: folderId }
  1393. this.file = { _id: fileId }
  1394. this.isNewFile = true
  1395. this.ProjectEntityUpdateHandler.mkdirp = {
  1396. withoutLock: sinon.stub().yields(null, this.newFolders, this.folder),
  1397. }
  1398. this.ProjectEntityUpdateHandler.upsertFile = {
  1399. mainTask: sinon.stub().yields(null, this.file, this.isNewFile),
  1400. }
  1401. this.ProjectEntityUpdateHandler.upsertFileWithPath(
  1402. projectId,
  1403. this.path,
  1404. this.fileSystemPath,
  1405. this.linkedFileData,
  1406. userId,
  1407. this.source,
  1408. this.callback
  1409. )
  1410. })
  1411. it('returns an error', function () {
  1412. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1413. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1414. })
  1415. })
  1416. })
  1417. describe('deleteEntity', function () {
  1418. beforeEach(function () {
  1419. this.path = '/path/to/doc.tex'
  1420. this.doc = { _id: docId }
  1421. this.projectBeforeDeletion = { _id: projectId, name: 'project' }
  1422. this.newProject = 'new-project'
  1423. this.ProjectEntityMongoUpdateHandler.deleteEntity.yields(
  1424. null,
  1425. this.doc,
  1426. { fileSystem: this.path },
  1427. this.projectBeforeDeletion,
  1428. this.newProject
  1429. )
  1430. this.ProjectEntityUpdateHandler._cleanUpEntity = sinon
  1431. .stub()
  1432. .yields(null, [{ type: 'doc', entity: this.doc, path: this.path }])
  1433. this.ProjectEntityUpdateHandler.deleteEntity(
  1434. projectId,
  1435. docId,
  1436. 'doc',
  1437. userId,
  1438. this.source,
  1439. this.callback
  1440. )
  1441. })
  1442. it('deletes the entity in mongo', function () {
  1443. this.ProjectEntityMongoUpdateHandler.deleteEntity
  1444. .calledWith(projectId, docId, 'doc')
  1445. .should.equal(true)
  1446. })
  1447. it('cleans up the doc in the docstore', function () {
  1448. this.ProjectEntityUpdateHandler._cleanUpEntity
  1449. .calledWith(
  1450. this.projectBeforeDeletion,
  1451. this.newProject,
  1452. this.doc,
  1453. 'doc',
  1454. this.path,
  1455. userId,
  1456. this.source
  1457. )
  1458. .should.equal(true)
  1459. })
  1460. it('it notifies the tpds', function () {
  1461. this.TpdsUpdateSender.deleteEntity.should.have.been.calledWith({
  1462. projectId,
  1463. path: this.path,
  1464. projectName: this.projectBeforeDeletion.name,
  1465. entityId: docId,
  1466. entityType: 'doc',
  1467. subtreeEntityIds: [this.doc._id],
  1468. })
  1469. })
  1470. it('retuns the entity_id', function () {
  1471. this.callback.calledWith(null, docId).should.equal(true)
  1472. })
  1473. })
  1474. describe('deleteEntityWithPath', function () {
  1475. describe('when the entity exists', function () {
  1476. beforeEach(function () {
  1477. this.doc = { _id: docId }
  1478. this.ProjectLocator.findElementByPath.yields(null, this.doc, 'doc')
  1479. this.ProjectEntityUpdateHandler.deleteEntity = {
  1480. withoutLock: sinon.stub().yields(),
  1481. }
  1482. this.path = '/path/to/doc.tex'
  1483. this.ProjectEntityUpdateHandler.deleteEntityWithPath(
  1484. projectId,
  1485. this.path,
  1486. userId,
  1487. this.source,
  1488. this.callback
  1489. )
  1490. })
  1491. it('finds the entity', function () {
  1492. this.ProjectLocator.findElementByPath
  1493. .calledWith({ project_id: projectId, path: this.path })
  1494. .should.equal(true)
  1495. })
  1496. it('deletes the entity', function () {
  1497. this.ProjectEntityUpdateHandler.deleteEntity.withoutLock
  1498. .calledWith(
  1499. projectId,
  1500. this.doc._id,
  1501. 'doc',
  1502. userId,
  1503. this.source,
  1504. this.callback
  1505. )
  1506. .should.equal(true)
  1507. })
  1508. })
  1509. describe('when the entity does not exist', function () {
  1510. beforeEach(function () {
  1511. this.ProjectLocator.findElementByPath.yields()
  1512. this.path = '/doc.tex'
  1513. this.ProjectEntityUpdateHandler.deleteEntityWithPath(
  1514. projectId,
  1515. this.path,
  1516. userId,
  1517. this.source,
  1518. this.callback
  1519. )
  1520. })
  1521. it('returns an error', function () {
  1522. this.callback
  1523. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  1524. .should.equal(true)
  1525. })
  1526. })
  1527. })
  1528. describe('mkdirp', function () {
  1529. beforeEach(function () {
  1530. this.docPath = '/folder/doc.tex'
  1531. this.ProjectEntityMongoUpdateHandler.mkdirp.yields()
  1532. this.ProjectEntityUpdateHandler.mkdirp(
  1533. projectId,
  1534. this.docPath,
  1535. this.callback
  1536. )
  1537. })
  1538. it('calls ProjectEntityMongoUpdateHandler', function () {
  1539. this.ProjectEntityMongoUpdateHandler.mkdirp
  1540. .calledWith(projectId, this.docPath)
  1541. .should.equal(true)
  1542. })
  1543. })
  1544. describe('mkdirpWithExactCase', function () {
  1545. beforeEach(function () {
  1546. this.docPath = '/folder/doc.tex'
  1547. this.ProjectEntityMongoUpdateHandler.mkdirp.yields()
  1548. this.ProjectEntityUpdateHandler.mkdirpWithExactCase(
  1549. projectId,
  1550. this.docPath,
  1551. this.callback
  1552. )
  1553. })
  1554. it('calls ProjectEntityMongoUpdateHandler', function () {
  1555. this.ProjectEntityMongoUpdateHandler.mkdirp
  1556. .calledWith(projectId, this.docPath, { exactCaseMatch: true })
  1557. .should.equal(true)
  1558. })
  1559. })
  1560. describe('addFolder', function () {
  1561. describe('adding a folder', function () {
  1562. beforeEach(function () {
  1563. this.parentFolderId = '123asdf'
  1564. this.folderName = 'new-folder'
  1565. this.ProjectEntityMongoUpdateHandler.addFolder.yields()
  1566. this.ProjectEntityUpdateHandler.addFolder(
  1567. projectId,
  1568. this.parentFolderId,
  1569. this.folderName,
  1570. this.callback
  1571. )
  1572. })
  1573. it('calls ProjectEntityMongoUpdateHandler', function () {
  1574. this.ProjectEntityMongoUpdateHandler.addFolder
  1575. .calledWith(projectId, this.parentFolderId, this.folderName)
  1576. .should.equal(true)
  1577. })
  1578. })
  1579. describe('adding a folder with an invalid name', function () {
  1580. beforeEach(function () {
  1581. this.parentFolderId = '123asdf'
  1582. this.folderName = '*new-folder'
  1583. this.ProjectEntityMongoUpdateHandler.addFolder.yields()
  1584. this.ProjectEntityUpdateHandler.addFolder(
  1585. projectId,
  1586. this.parentFolderId,
  1587. this.folderName,
  1588. this.callback
  1589. )
  1590. })
  1591. it('returns an error', function () {
  1592. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1593. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1594. })
  1595. })
  1596. })
  1597. describe('moveEntity', function () {
  1598. beforeEach(function () {
  1599. this.project_name = 'project name'
  1600. this.startPath = '/a.tex'
  1601. this.endPath = '/folder/b.tex'
  1602. this.rev = 2
  1603. this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
  1604. this.ProjectEntityMongoUpdateHandler.moveEntity.yields(
  1605. null,
  1606. this.project,
  1607. this.startPath,
  1608. this.endPath,
  1609. this.rev,
  1610. this.changes
  1611. )
  1612. this.ProjectEntityUpdateHandler.moveEntity(
  1613. projectId,
  1614. docId,
  1615. folderId,
  1616. 'doc',
  1617. userId,
  1618. this.source,
  1619. this.callback
  1620. )
  1621. })
  1622. it('moves the entity in mongo', function () {
  1623. this.ProjectEntityMongoUpdateHandler.moveEntity
  1624. .calledWith(projectId, docId, folderId, 'doc')
  1625. .should.equal(true)
  1626. })
  1627. it('notifies tpds', function () {
  1628. this.TpdsUpdateSender.promises.moveEntity
  1629. .calledWith({
  1630. projectId,
  1631. projectName: this.project_name,
  1632. startPath: this.startPath,
  1633. endPath: this.endPath,
  1634. rev: this.rev,
  1635. entityId: docId,
  1636. entityType: 'doc',
  1637. folderId,
  1638. })
  1639. .should.equal(true)
  1640. })
  1641. it('sends the changes in project structure to the doc updater', function () {
  1642. this.DocumentUpdaterHandler.updateProjectStructure
  1643. .calledWith(
  1644. projectId,
  1645. projectHistoryId,
  1646. userId,
  1647. this.changes,
  1648. this.source,
  1649. this.callback
  1650. )
  1651. .should.equal(true)
  1652. })
  1653. })
  1654. describe('renameEntity', function () {
  1655. describe('renaming an entity', function () {
  1656. beforeEach(function () {
  1657. this.project_name = 'project name'
  1658. this.startPath = '/folder/a.tex'
  1659. this.endPath = '/folder/b.tex'
  1660. this.rev = 2
  1661. this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
  1662. this.newDocName = 'b.tex'
  1663. this.ProjectEntityMongoUpdateHandler.renameEntity.yields(
  1664. null,
  1665. this.project,
  1666. this.startPath,
  1667. this.endPath,
  1668. this.rev,
  1669. this.changes
  1670. )
  1671. this.ProjectEntityUpdateHandler.renameEntity(
  1672. projectId,
  1673. docId,
  1674. 'doc',
  1675. this.newDocName,
  1676. userId,
  1677. this.source,
  1678. this.callback
  1679. )
  1680. })
  1681. it('moves the entity in mongo', function () {
  1682. this.ProjectEntityMongoUpdateHandler.renameEntity
  1683. .calledWith(projectId, docId, 'doc', this.newDocName)
  1684. .should.equal(true)
  1685. })
  1686. it('notifies tpds', function () {
  1687. this.TpdsUpdateSender.promises.moveEntity
  1688. .calledWith({
  1689. projectId,
  1690. projectName: this.project_name,
  1691. startPath: this.startPath,
  1692. endPath: this.endPath,
  1693. rev: this.rev,
  1694. entityId: docId,
  1695. entityType: 'doc',
  1696. folderId: null,
  1697. })
  1698. .should.equal(true)
  1699. })
  1700. it('sends the changes in project structure to the doc updater', function () {
  1701. this.DocumentUpdaterHandler.updateProjectStructure
  1702. .calledWith(
  1703. projectId,
  1704. projectHistoryId,
  1705. userId,
  1706. this.changes,
  1707. this.source,
  1708. this.callback
  1709. )
  1710. .should.equal(true)
  1711. })
  1712. })
  1713. describe('renaming an entity to an invalid name', function () {
  1714. beforeEach(function () {
  1715. this.project_name = 'project name'
  1716. this.startPath = '/folder/a.tex'
  1717. this.endPath = '/folder/b.tex'
  1718. this.rev = 2
  1719. this.changes = { newDocs: ['old-doc'], newFiles: ['old-file'] }
  1720. this.newDocName = '*b.tex'
  1721. this.ProjectEntityMongoUpdateHandler.renameEntity.yields(
  1722. null,
  1723. this.project,
  1724. this.startPath,
  1725. this.endPath,
  1726. this.rev,
  1727. this.changes
  1728. )
  1729. this.ProjectEntityUpdateHandler.renameEntity(
  1730. projectId,
  1731. docId,
  1732. 'doc',
  1733. this.newDocName,
  1734. userId,
  1735. this.source,
  1736. this.callback
  1737. )
  1738. })
  1739. it('returns an error', function () {
  1740. const errorMatcher = sinon.match.instanceOf(Errors.InvalidNameError)
  1741. this.callback.calledWithMatch(errorMatcher).should.equal(true)
  1742. })
  1743. })
  1744. })
  1745. describe('resyncProjectHistory', function () {
  1746. describe('a deleted project', function () {
  1747. beforeEach(function () {
  1748. this.ProjectGetter.getProject.yields()
  1749. this.ProjectEntityUpdateHandler.resyncProjectHistory(
  1750. projectId,
  1751. this.callback
  1752. )
  1753. })
  1754. it('should return an error', function () {
  1755. expect(this.callback).to.have.been.calledWith(
  1756. sinon.match
  1757. .instanceOf(Errors.ProjectHistoryDisabledError)
  1758. .and(
  1759. sinon.match.has(
  1760. 'message',
  1761. `project history not enabled for ${projectId}`
  1762. )
  1763. )
  1764. )
  1765. })
  1766. })
  1767. describe('a project without project-history enabled', function () {
  1768. beforeEach(function () {
  1769. this.project.overleaf = {}
  1770. this.ProjectGetter.getProject.yields(null, this.project)
  1771. this.ProjectEntityUpdateHandler.resyncProjectHistory(
  1772. projectId,
  1773. this.callback
  1774. )
  1775. })
  1776. it('should return an error', function () {
  1777. expect(this.callback).to.have.been.calledWith(
  1778. sinon.match
  1779. .instanceOf(Errors.ProjectHistoryDisabledError)
  1780. .and(
  1781. sinon.match.has(
  1782. 'message',
  1783. `project history not enabled for ${projectId}`
  1784. )
  1785. )
  1786. )
  1787. })
  1788. })
  1789. describe('a project with project-history enabled', function () {
  1790. beforeEach(function () {
  1791. this.ProjectGetter.getProject.yields(null, this.project)
  1792. const docs = [
  1793. { doc: { _id: docId, name: 'main.tex' }, path: 'main.tex' },
  1794. ]
  1795. const files = [
  1796. {
  1797. file: { _id: fileId, name: 'universe.png', hash: '123456' },
  1798. path: 'universe.png',
  1799. },
  1800. ]
  1801. const folders = []
  1802. this.ProjectEntityHandler.getAllEntitiesFromProject.returns({
  1803. docs,
  1804. files,
  1805. folders,
  1806. })
  1807. this.ProjectEntityUpdateHandler.resyncProjectHistory(
  1808. projectId,
  1809. this.callback
  1810. )
  1811. })
  1812. it('gets the project', function () {
  1813. this.ProjectGetter.getProject.calledWith(projectId).should.equal(true)
  1814. })
  1815. it('gets the entities for the project', function () {
  1816. this.ProjectEntityHandler.getAllEntitiesFromProject
  1817. .calledWith(this.project)
  1818. .should.equal(true)
  1819. })
  1820. it('uses an extended timeout', function () {
  1821. this.LockManager.withTimeout.calledWith(6 * 60).should.equal(true)
  1822. })
  1823. it('tells the doc updater to sync the project', function () {
  1824. const docs = [
  1825. {
  1826. doc: docId,
  1827. path: 'main.tex',
  1828. },
  1829. ]
  1830. const files = [
  1831. {
  1832. file: fileId,
  1833. path: 'universe.png',
  1834. url: `www.filestore.test/${projectId}/${fileId}`,
  1835. _hash: '123456',
  1836. },
  1837. ]
  1838. this.DocumentUpdaterHandler.resyncProjectHistory
  1839. .calledWith(projectId, projectHistoryId, docs, files)
  1840. .should.equal(true)
  1841. })
  1842. it('calls the callback', function () {
  1843. this.callback.called.should.equal(true)
  1844. })
  1845. })
  1846. describe('a project with duplicate filenames', function () {
  1847. beforeEach(function (done) {
  1848. this.ProjectGetter.getProject.yields(null, this.project)
  1849. this.docs = [
  1850. { doc: { _id: 'doc1', name: 'main.tex' }, path: 'main.tex' },
  1851. {
  1852. doc: { _id: 'doc2', name: 'duplicate.tex' },
  1853. path: 'a/b/c/duplicate.tex',
  1854. },
  1855. {
  1856. doc: { _id: 'doc3', name: 'duplicate.tex' },
  1857. path: 'a/b/c/duplicate.tex',
  1858. },
  1859. {
  1860. doc: { _id: 'doc4', name: 'another dupe (22)' },
  1861. path: 'another dupe (22)',
  1862. },
  1863. {
  1864. doc: { _id: 'doc5', name: 'duplicate.tex' },
  1865. path: 'a/b/c/duplicate.tex',
  1866. },
  1867. ]
  1868. this.files = [
  1869. {
  1870. file: { _id: 'file1', name: 'image.jpg', hash: 'hash1' },
  1871. path: 'image.jpg',
  1872. },
  1873. {
  1874. file: { _id: 'file2', name: 'duplicate.jpg', hash: 'hash2' },
  1875. path: 'duplicate.jpg',
  1876. },
  1877. {
  1878. file: { _id: 'file3', name: 'duplicate.jpg', hash: 'hash3' },
  1879. path: 'duplicate.jpg',
  1880. },
  1881. {
  1882. file: { _id: 'file4', name: 'another dupe (22)', hash: 'hash4' },
  1883. path: 'another dupe (22)',
  1884. },
  1885. ]
  1886. this.ProjectEntityHandler.getAllEntitiesFromProject.returns({
  1887. docs: this.docs,
  1888. files: this.files,
  1889. folders: [],
  1890. })
  1891. this.ProjectEntityUpdateHandler.resyncProjectHistory(projectId, done)
  1892. })
  1893. it('renames the duplicate files', function () {
  1894. const renameEntity = this.ProjectEntityMongoUpdateHandler.renameEntity
  1895. expect(renameEntity).to.have.callCount(4)
  1896. expect(renameEntity).to.have.been.calledWith(
  1897. projectId,
  1898. 'doc3',
  1899. 'doc',
  1900. 'duplicate.tex (1)'
  1901. )
  1902. expect(renameEntity).to.have.been.calledWith(
  1903. projectId,
  1904. 'doc5',
  1905. 'doc',
  1906. 'duplicate.tex (2)'
  1907. )
  1908. expect(renameEntity).to.have.been.calledWith(
  1909. projectId,
  1910. 'file3',
  1911. 'file',
  1912. 'duplicate.jpg (1)'
  1913. )
  1914. expect(renameEntity).to.have.been.calledWith(
  1915. projectId,
  1916. 'file4',
  1917. 'file',
  1918. 'another dupe (23)'
  1919. )
  1920. })
  1921. it('tells the doc updater to resync the project', function () {
  1922. const docs = [
  1923. { doc: 'doc1', path: 'main.tex' },
  1924. { doc: 'doc2', path: 'a/b/c/duplicate.tex' },
  1925. { doc: 'doc3', path: 'a/b/c/duplicate.tex (1)' },
  1926. { doc: 'doc4', path: 'another dupe (22)' },
  1927. { doc: 'doc5', path: 'a/b/c/duplicate.tex (2)' },
  1928. ]
  1929. const urlPrefix = `www.filestore.test/${projectId}`
  1930. const files = [
  1931. {
  1932. file: 'file1',
  1933. path: 'image.jpg',
  1934. url: `${urlPrefix}/file1`,
  1935. _hash: 'hash1',
  1936. },
  1937. {
  1938. file: 'file2',
  1939. path: 'duplicate.jpg',
  1940. url: `${urlPrefix}/file2`,
  1941. _hash: 'hash2',
  1942. },
  1943. {
  1944. file: 'file3',
  1945. path: 'duplicate.jpg (1)',
  1946. url: `${urlPrefix}/file3`,
  1947. _hash: 'hash3',
  1948. },
  1949. {
  1950. file: 'file4',
  1951. path: 'another dupe (23)',
  1952. url: `${urlPrefix}/file4`,
  1953. _hash: 'hash4',
  1954. },
  1955. ]
  1956. expect(
  1957. this.DocumentUpdaterHandler.resyncProjectHistory
  1958. ).to.have.been.calledWith(projectId, projectHistoryId, docs, files)
  1959. })
  1960. })
  1961. describe('a project with bad filenames', function () {
  1962. beforeEach(function (done) {
  1963. this.ProjectGetter.getProject.yields(null, this.project)
  1964. this.docs = [
  1965. {
  1966. doc: { _id: 'doc1', name: '/d/e/f/test.tex' },
  1967. path: 'a/b/c/d/e/f/test.tex',
  1968. },
  1969. {
  1970. doc: { _id: 'doc2', name: '' },
  1971. path: 'a',
  1972. },
  1973. ]
  1974. this.files = [
  1975. {
  1976. file: { _id: 'file1', name: 'A*.png', hash: 'hash1' },
  1977. path: 'A*.png',
  1978. },
  1979. {
  1980. file: { _id: 'file2', name: 'A_.png', hash: 'hash2' },
  1981. path: 'A_.png',
  1982. },
  1983. ]
  1984. this.ProjectEntityHandler.getAllEntitiesFromProject.returns({
  1985. docs: this.docs,
  1986. files: this.files,
  1987. folders: [],
  1988. })
  1989. this.ProjectEntityUpdateHandler.resyncProjectHistory(projectId, done)
  1990. })
  1991. it('renames the files', function () {
  1992. const renameEntity = this.ProjectEntityMongoUpdateHandler.renameEntity
  1993. expect(renameEntity).to.have.callCount(4)
  1994. expect(renameEntity).to.have.been.calledWith(
  1995. projectId,
  1996. 'doc1',
  1997. 'doc',
  1998. '_d_e_f_test.tex'
  1999. )
  2000. expect(renameEntity).to.have.been.calledWith(
  2001. projectId,
  2002. 'doc2',
  2003. 'doc',
  2004. 'untitled'
  2005. )
  2006. expect(renameEntity).to.have.been.calledWith(
  2007. projectId,
  2008. 'file1',
  2009. 'file',
  2010. 'A_.png'
  2011. )
  2012. expect(renameEntity).to.have.been.calledWith(
  2013. projectId,
  2014. 'file2',
  2015. 'file',
  2016. 'A_.png (1)'
  2017. )
  2018. })
  2019. it('tells the doc updater to resync the project', function () {
  2020. const docs = [
  2021. { doc: 'doc1', path: 'a/b/c/_d_e_f_test.tex' },
  2022. { doc: 'doc2', path: 'a/untitled' },
  2023. ]
  2024. const urlPrefix = `www.filestore.test/${projectId}`
  2025. const files = [
  2026. {
  2027. file: 'file1',
  2028. path: 'A_.png',
  2029. url: `${urlPrefix}/file1`,
  2030. _hash: 'hash1',
  2031. },
  2032. {
  2033. file: 'file2',
  2034. path: 'A_.png (1)',
  2035. url: `${urlPrefix}/file2`,
  2036. _hash: 'hash2',
  2037. },
  2038. ]
  2039. expect(
  2040. this.DocumentUpdaterHandler.resyncProjectHistory
  2041. ).to.have.been.calledWith(projectId, projectHistoryId, docs, files)
  2042. })
  2043. })
  2044. describe('a project with a bad folder name', function () {
  2045. beforeEach(function (done) {
  2046. this.ProjectGetter.getProject.yields(null, this.project)
  2047. const folders = [
  2048. {
  2049. folder: { _id: 'folder1', name: 'good' },
  2050. path: 'good',
  2051. },
  2052. {
  2053. folder: { _id: 'folder2', name: 'bad*' },
  2054. path: 'bad*',
  2055. },
  2056. ]
  2057. const docs = [
  2058. {
  2059. doc: { _id: 'doc1', name: 'doc1.tex' },
  2060. path: 'good/doc1.tex',
  2061. },
  2062. {
  2063. doc: { _id: 'doc2', name: 'duplicate.tex' },
  2064. path: 'bad*/doc2.tex',
  2065. },
  2066. ]
  2067. const files = []
  2068. this.ProjectEntityHandler.getAllEntitiesFromProject.returns({
  2069. docs,
  2070. files,
  2071. folders,
  2072. })
  2073. this.ProjectEntityUpdateHandler.resyncProjectHistory(projectId, done)
  2074. })
  2075. it('renames the folder', function () {
  2076. const renameEntity = this.ProjectEntityMongoUpdateHandler.renameEntity
  2077. expect(renameEntity).to.have.callCount(1)
  2078. expect(renameEntity).to.have.been.calledWith(
  2079. projectId,
  2080. 'folder2',
  2081. 'folder',
  2082. 'bad_'
  2083. )
  2084. })
  2085. it('tells the doc updater to resync the project', function () {
  2086. const docs = [
  2087. { doc: 'doc1', path: 'good/doc1.tex' },
  2088. { doc: 'doc2', path: 'bad_/doc2.tex' },
  2089. ]
  2090. const files = []
  2091. expect(
  2092. this.DocumentUpdaterHandler.resyncProjectHistory
  2093. ).to.have.been.calledWith(projectId, projectHistoryId, docs, files)
  2094. })
  2095. })
  2096. describe('a project with duplicate names between a folder and a doc', function () {
  2097. beforeEach(function (done) {
  2098. this.ProjectGetter.getProject.yields(null, this.project)
  2099. const folders = [
  2100. {
  2101. folder: { _id: 'folder1', name: 'chapters' },
  2102. path: 'chapters',
  2103. },
  2104. ]
  2105. const docs = [
  2106. {
  2107. doc: { _id: 'doc1', name: 'chapters' },
  2108. path: 'chapters',
  2109. },
  2110. {
  2111. doc: { _id: 'doc2', name: 'chapter1.tex' },
  2112. path: 'chapters/chapter1.tex',
  2113. },
  2114. ]
  2115. const files = []
  2116. this.ProjectEntityHandler.getAllEntitiesFromProject.returns({
  2117. docs,
  2118. files,
  2119. folders,
  2120. })
  2121. this.ProjectEntityUpdateHandler.resyncProjectHistory(projectId, done)
  2122. })
  2123. it('renames the doc', function () {
  2124. const renameEntity = this.ProjectEntityMongoUpdateHandler.renameEntity
  2125. expect(renameEntity).to.have.callCount(1)
  2126. expect(renameEntity).to.have.been.calledWith(
  2127. projectId,
  2128. 'doc1',
  2129. 'doc',
  2130. 'chapters (1)'
  2131. )
  2132. })
  2133. it('tells the doc updater to resync the project', function () {
  2134. const docs = [
  2135. { doc: 'doc1', path: 'chapters (1)' },
  2136. { doc: 'doc2', path: 'chapters/chapter1.tex' },
  2137. ]
  2138. const files = []
  2139. expect(
  2140. this.DocumentUpdaterHandler.resyncProjectHistory
  2141. ).to.have.been.calledWith(projectId, projectHistoryId, docs, files)
  2142. })
  2143. })
  2144. })
  2145. describe('_cleanUpEntity', function () {
  2146. beforeEach(function () {
  2147. this.entityId = '4eecaffcbffa66588e000009'
  2148. this.FileStoreHandler.deleteFile.yields()
  2149. this.ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  2150. this.ProjectEntityMongoUpdateHandler._insertDeletedFileReference.yields()
  2151. })
  2152. describe('a file', function () {
  2153. beforeEach(function (done) {
  2154. this.path = '/file/system/path.png'
  2155. this.entity = { _id: this.entityId }
  2156. this.newProject = 'new-project'
  2157. this.ProjectEntityUpdateHandler._cleanUpEntity(
  2158. this.project,
  2159. this.newProject,
  2160. this.entity,
  2161. 'file',
  2162. this.path,
  2163. userId,
  2164. this.source,
  2165. (err, subtreeListing) => {
  2166. if (err) {
  2167. return done(err)
  2168. }
  2169. this.subtreeListing = subtreeListing
  2170. done()
  2171. }
  2172. )
  2173. })
  2174. it('should insert the file into the deletedFiles collection', function () {
  2175. this.ProjectEntityMongoUpdateHandler._insertDeletedFileReference
  2176. .calledWith(this.project._id, this.entity)
  2177. .should.equal(true)
  2178. })
  2179. it('should not delete the file from FileStoreHandler', function () {
  2180. this.FileStoreHandler.deleteFile
  2181. .calledWith(projectId, this.entityId)
  2182. .should.equal(false)
  2183. })
  2184. it('should not attempt to delete from the document updater', function () {
  2185. this.DocumentUpdaterHandler.deleteDoc.called.should.equal(false)
  2186. })
  2187. it('should send the update to the doc updater', function () {
  2188. const oldFiles = [{ file: this.entity, path: this.path }]
  2189. this.DocumentUpdaterHandler.updateProjectStructure.should.have.been.calledWith(
  2190. projectId,
  2191. projectHistoryId,
  2192. userId,
  2193. {
  2194. oldFiles,
  2195. oldDocs: [],
  2196. newProject: this.newProject,
  2197. },
  2198. this.source
  2199. )
  2200. })
  2201. it('should return a subtree listing containing only the file', function () {
  2202. expect(this.subtreeListing).to.deep.equal([
  2203. { type: 'file', entity: this.entity, path: this.path },
  2204. ])
  2205. })
  2206. })
  2207. describe('a doc', function () {
  2208. beforeEach(function (done) {
  2209. this.path = '/file/system/path.tex'
  2210. this.ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  2211. this.entity = { _id: this.entityId }
  2212. this.newProject = 'new-project'
  2213. this.ProjectEntityUpdateHandler._cleanUpEntity(
  2214. this.project,
  2215. this.newProject,
  2216. this.entity,
  2217. 'doc',
  2218. this.path,
  2219. userId,
  2220. this.source,
  2221. (err, subtreeListing) => {
  2222. if (err) {
  2223. return done(err)
  2224. }
  2225. this.subtreeListing = subtreeListing
  2226. done()
  2227. }
  2228. )
  2229. })
  2230. it('should clean up the doc', function () {
  2231. this.ProjectEntityUpdateHandler._cleanUpDoc
  2232. .calledWith(this.project, this.entity, this.path, userId)
  2233. .should.equal(true)
  2234. })
  2235. it('should send the update to the doc updater', function () {
  2236. const oldDocs = [{ doc: this.entity, path: this.path }]
  2237. this.DocumentUpdaterHandler.updateProjectStructure.should.have.been.calledWith(
  2238. projectId,
  2239. projectHistoryId,
  2240. userId,
  2241. {
  2242. oldDocs,
  2243. oldFiles: [],
  2244. newProject: this.newProject,
  2245. },
  2246. this.source
  2247. )
  2248. })
  2249. it('should return a subtree listing containing only the doc', function () {
  2250. expect(this.subtreeListing).to.deep.equal([
  2251. { type: 'doc', entity: this.entity, path: this.path },
  2252. ])
  2253. })
  2254. })
  2255. describe('a folder', function () {
  2256. beforeEach(function (done) {
  2257. this.folder = {
  2258. folders: [
  2259. {
  2260. name: 'subfolder',
  2261. fileRefs: [
  2262. (this.file1 = { _id: 'file-id-1', name: 'file-name-1' }),
  2263. ],
  2264. docs: [(this.doc1 = { _id: 'doc-id-1', name: 'doc-name-1' })],
  2265. folders: [],
  2266. },
  2267. ],
  2268. fileRefs: [(this.file2 = { _id: 'file-id-2', name: 'file-name-2' })],
  2269. docs: [(this.doc2 = { _id: 'doc-id-2', name: 'doc-name-2' })],
  2270. }
  2271. this.ProjectEntityUpdateHandler._cleanUpDoc = sinon.stub().yields()
  2272. this.ProjectEntityUpdateHandler._cleanUpFile = sinon.stub().yields()
  2273. const path = '/folder'
  2274. this.newProject = 'new-project'
  2275. this.ProjectEntityUpdateHandler._cleanUpEntity(
  2276. this.project,
  2277. this.newProject,
  2278. this.folder,
  2279. 'folder',
  2280. path,
  2281. userId,
  2282. this.source,
  2283. (err, subtreeListing) => {
  2284. if (err) {
  2285. return done(err)
  2286. }
  2287. this.subtreeListing = subtreeListing
  2288. done()
  2289. }
  2290. )
  2291. })
  2292. it('should clean up all sub files', function () {
  2293. this.ProjectEntityUpdateHandler._cleanUpFile
  2294. .calledWith(
  2295. this.project,
  2296. this.file1,
  2297. '/folder/subfolder/file-name-1',
  2298. userId
  2299. )
  2300. .should.equal(true)
  2301. this.ProjectEntityUpdateHandler._cleanUpFile
  2302. .calledWith(this.project, this.file2, '/folder/file-name-2', userId)
  2303. .should.equal(true)
  2304. })
  2305. it('should clean up all sub docs', function () {
  2306. this.ProjectEntityUpdateHandler._cleanUpDoc
  2307. .calledWith(
  2308. this.project,
  2309. this.doc1,
  2310. '/folder/subfolder/doc-name-1',
  2311. userId
  2312. )
  2313. .should.equal(true)
  2314. this.ProjectEntityUpdateHandler._cleanUpDoc
  2315. .calledWith(this.project, this.doc2, '/folder/doc-name-2', userId)
  2316. .should.equal(true)
  2317. })
  2318. it('should should send one update to the doc updater for all docs and files', function () {
  2319. const oldFiles = [
  2320. { file: this.file2, path: '/folder/file-name-2' },
  2321. { file: this.file1, path: '/folder/subfolder/file-name-1' },
  2322. ]
  2323. const oldDocs = [
  2324. { doc: this.doc2, path: '/folder/doc-name-2' },
  2325. { doc: this.doc1, path: '/folder/subfolder/doc-name-1' },
  2326. ]
  2327. this.DocumentUpdaterHandler.updateProjectStructure
  2328. .calledWith(
  2329. projectId,
  2330. projectHistoryId,
  2331. userId,
  2332. {
  2333. oldFiles,
  2334. oldDocs,
  2335. newProject: this.newProject,
  2336. },
  2337. this.source
  2338. )
  2339. .should.equal(true)
  2340. })
  2341. it('should return a subtree listing containing all sub-entities', function () {
  2342. expect(this.subtreeListing).to.have.deep.members([
  2343. { type: 'folder', entity: this.folder, path: '/folder' },
  2344. {
  2345. type: 'folder',
  2346. entity: this.folder.folders[0],
  2347. path: '/folder/subfolder',
  2348. },
  2349. {
  2350. type: 'file',
  2351. entity: this.file1,
  2352. path: '/folder/subfolder/file-name-1',
  2353. },
  2354. {
  2355. type: 'doc',
  2356. entity: this.doc1,
  2357. path: '/folder/subfolder/doc-name-1',
  2358. },
  2359. { type: 'file', entity: this.file2, path: '/folder/file-name-2' },
  2360. { type: 'doc', entity: this.doc2, path: '/folder/doc-name-2' },
  2361. ])
  2362. })
  2363. })
  2364. })
  2365. describe('_cleanUpDoc', function () {
  2366. beforeEach(function () {
  2367. this.doc = {
  2368. _id: ObjectId(),
  2369. name: 'test.tex',
  2370. }
  2371. this.path = '/path/to/doc'
  2372. this.ProjectEntityUpdateHandler.unsetRootDoc = sinon.stub().yields()
  2373. this.DocstoreManager.deleteDoc.yields()
  2374. })
  2375. describe('when the doc is the root doc', function () {
  2376. beforeEach(function () {
  2377. this.project.rootDoc_id = this.doc._id
  2378. this.ProjectEntityUpdateHandler._cleanUpDoc(
  2379. this.project,
  2380. this.doc,
  2381. this.path,
  2382. userId,
  2383. this.callback
  2384. )
  2385. })
  2386. it('should unset the root doc', function () {
  2387. this.ProjectEntityUpdateHandler.unsetRootDoc
  2388. .calledWith(projectId)
  2389. .should.equal(true)
  2390. })
  2391. it('should delete the doc in the doc updater', function () {
  2392. this.DocumentUpdaterHandler.deleteDoc
  2393. .calledWith(projectId, this.doc._id.toString())
  2394. .should.equal(true)
  2395. })
  2396. it('should delete the doc in the doc store', function () {
  2397. this.DocstoreManager.deleteDoc
  2398. .calledWith(projectId, this.doc._id.toString(), 'test.tex')
  2399. .should.equal(true)
  2400. })
  2401. it('should call the callback', function () {
  2402. this.callback.called.should.equal(true)
  2403. })
  2404. })
  2405. describe('when the doc is not the root doc', function () {
  2406. beforeEach(function () {
  2407. this.project.rootDoc_id = ObjectId()
  2408. this.ProjectEntityUpdateHandler._cleanUpDoc(
  2409. this.project,
  2410. this.doc,
  2411. this.path,
  2412. userId,
  2413. this.callback
  2414. )
  2415. })
  2416. it('should not unset the root doc', function () {
  2417. this.ProjectEntityUpdateHandler.unsetRootDoc.called.should.equal(false)
  2418. })
  2419. it('should call the callback', function () {
  2420. this.callback.called.should.equal(true)
  2421. })
  2422. })
  2423. })
  2424. describe('convertDocToFile', function () {
  2425. beforeEach(function () {
  2426. this.docPath = '/folder/doc.tex'
  2427. this.docLines = ['line one', 'line two']
  2428. this.tmpFilePath = '/tmp/file'
  2429. this.fileStoreUrl = 'http://filestore/file'
  2430. this.folder = { _id: new ObjectId() }
  2431. this.rev = 3
  2432. this.ProjectLocator.findElement
  2433. .withArgs({
  2434. project_id: this.project._id,
  2435. element_id: this.doc._id,
  2436. type: 'doc',
  2437. })
  2438. .yields(null, this.doc, { fileSystem: this.path }, this.folder)
  2439. this.ProjectLocator.findElement
  2440. .withArgs({
  2441. project_id: this.project._id.toString(),
  2442. element_id: this.file._id,
  2443. type: 'file',
  2444. })
  2445. .yields(null, this.file, this.docPath, this.folder)
  2446. this.DocstoreManager.getDoc
  2447. .withArgs(this.project._id, this.doc._id)
  2448. .yields(null, this.docLines, this.rev)
  2449. this.FileWriter.writeLinesToDisk.yields(null, this.tmpFilePath)
  2450. this.FileStoreHandler.uploadFileFromDisk.yields(
  2451. null,
  2452. this.fileStoreUrl,
  2453. this.file
  2454. )
  2455. this.ProjectEntityMongoUpdateHandler.replaceDocWithFile.yields(
  2456. null,
  2457. this.project
  2458. )
  2459. })
  2460. describe('successfully', function () {
  2461. beforeEach(function (done) {
  2462. this.ProjectEntityUpdateHandler.convertDocToFile(
  2463. this.project._id,
  2464. this.doc._id,
  2465. this.user._id,
  2466. this.source,
  2467. done
  2468. )
  2469. })
  2470. it('deletes the document in doc updater', function () {
  2471. expect(this.DocumentUpdaterHandler.deleteDoc).to.have.been.calledWith(
  2472. this.project._id,
  2473. this.doc._id
  2474. )
  2475. })
  2476. it('uploads the file to filestore', function () {
  2477. expect(
  2478. this.FileStoreHandler.uploadFileFromDisk
  2479. ).to.have.been.calledWith(
  2480. this.project._id,
  2481. { name: this.doc.name, rev: this.rev + 1 },
  2482. this.tmpFilePath
  2483. )
  2484. })
  2485. it('cleans up the temporary file', function () {
  2486. expect(this.fs.unlink).to.have.been.calledWith(this.tmpFilePath)
  2487. })
  2488. it('replaces the doc with the file', function () {
  2489. expect(
  2490. this.ProjectEntityMongoUpdateHandler.replaceDocWithFile
  2491. ).to.have.been.calledWith(this.project._id, this.doc._id, this.file)
  2492. })
  2493. it('notifies document updater of changes', function () {
  2494. expect(
  2495. this.DocumentUpdaterHandler.updateProjectStructure
  2496. ).to.have.been.calledWith(
  2497. this.project._id,
  2498. this.project.overleaf.history.id,
  2499. this.user._id,
  2500. {
  2501. oldDocs: [{ doc: this.doc, path: this.path }],
  2502. newFiles: [
  2503. { file: this.file, path: this.path, url: this.fileStoreUrl },
  2504. ],
  2505. newProject: this.project,
  2506. },
  2507. this.source
  2508. )
  2509. })
  2510. it('should notify real-time of the doc deletion', function () {
  2511. expect(
  2512. this.EditorRealTimeController.emitToRoom
  2513. ).to.have.been.calledWith(
  2514. this.project._id,
  2515. 'removeEntity',
  2516. this.doc._id,
  2517. 'convertDocToFile'
  2518. )
  2519. })
  2520. it('should notify real-time of the file creation', function () {
  2521. expect(
  2522. this.EditorRealTimeController.emitToRoom
  2523. ).to.have.been.calledWith(
  2524. this.project._id,
  2525. 'reciveNewFile',
  2526. this.folder._id,
  2527. this.file,
  2528. 'convertDocToFile',
  2529. null
  2530. )
  2531. })
  2532. })
  2533. describe('when the doc has ranges', function () {
  2534. it('should throw a DocHasRangesError', function (done) {
  2535. this.ranges = { comments: [{ id: 123 }] }
  2536. this.DocstoreManager.getDoc
  2537. .withArgs(this.project._id, this.doc._id)
  2538. .yields(null, this.docLines, 'rev', 'version', this.ranges)
  2539. this.ProjectEntityUpdateHandler.convertDocToFile(
  2540. this.project._id,
  2541. this.doc._id,
  2542. this.user._id,
  2543. this.source,
  2544. err => {
  2545. expect(err).to.be.instanceof(Errors.DocHasRangesError)
  2546. done()
  2547. }
  2548. )
  2549. })
  2550. })
  2551. })
  2552. })