ProjectEntityUpdateHandlerTests.js 92 KB

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