ProjectEntityUpdateHandlerTests.js 93 KB

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