ProjectEntityUpdateHandlerTests.js 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273
  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. describe('appendToDoc', function () {
  2913. describe('when document cannot be found', function () {
  2914. beforeEach(function (done) {
  2915. this.appendedLines = ['5678', 'def']
  2916. this.DocumentUpdaterHandler.promises.appendToDocument = sinon.stub()
  2917. this.ProjectLocator.promises.findElement = sinon.stub()
  2918. this.ProjectLocator.promises.findElement
  2919. .withArgs({ project_id: projectId, element_id: docId, type: 'doc' })
  2920. .rejects(new Errors.NotFoundError())
  2921. this.ProjectEntityUpdateHandler.appendToDoc(
  2922. projectId,
  2923. docId,
  2924. this.appendedLines,
  2925. this.source,
  2926. userId,
  2927. (...args) => {
  2928. this.callback(...args)
  2929. done()
  2930. }
  2931. )
  2932. })
  2933. it('should not talk to DocumentUpdaterHandler', function () {
  2934. this.DocumentUpdaterHandler.promises.appendToDocument.should.not.have
  2935. .been.called
  2936. })
  2937. it('should throw the error', function () {
  2938. this.callback.should.have.been.calledWith(
  2939. sinon.match.instanceOf(Errors.NotFoundError)
  2940. )
  2941. })
  2942. })
  2943. describe('when document is found', function () {
  2944. beforeEach(function (done) {
  2945. this.appendedLines = ['5678', 'def']
  2946. this.DocumentUpdaterHandler.promises.appendToDocument = sinon.stub()
  2947. this.DocumentUpdaterHandler.promises.appendToDocument
  2948. .withArgs(projectId, docId, userId, this.appendedLines, this.source)
  2949. .resolves({ rev: 1 })
  2950. this.ProjectLocator.promises.findElement = sinon.stub()
  2951. this.ProjectLocator.promises.findElement
  2952. .withArgs({ project_id: projectId, element_id: docId, type: 'doc' })
  2953. .resolves({ element: { _id: docId } })
  2954. this.ProjectEntityUpdateHandler.appendToDoc(
  2955. projectId,
  2956. docId,
  2957. this.appendedLines,
  2958. this.source,
  2959. userId,
  2960. (...args) => {
  2961. this.callback(...args)
  2962. done()
  2963. }
  2964. )
  2965. })
  2966. it('should forward call to DocumentUpdaterHandler.appendToDocument', function () {
  2967. this.DocumentUpdaterHandler.promises.appendToDocument.should.have.been.calledWith(
  2968. projectId,
  2969. docId,
  2970. userId,
  2971. this.appendedLines,
  2972. this.source
  2973. )
  2974. })
  2975. it('should return the response from DocumentUpdaterHandler', function () {
  2976. this.callback.should.have.been.calledWith(null, { rev: 1 })
  2977. })
  2978. })
  2979. describe('when DocumentUpdater throws an error', function () {
  2980. beforeEach(function (done) {
  2981. this.appendedLines = ['5678', 'def']
  2982. this.DocumentUpdaterHandler.promises.appendToDocument = sinon.stub()
  2983. this.DocumentUpdaterHandler.promises.appendToDocument.rejects(
  2984. new Error()
  2985. )
  2986. this.ProjectLocator.promises.findElement = sinon.stub()
  2987. this.ProjectLocator.promises.findElement
  2988. .withArgs({ project_id: projectId, element_id: docId, type: 'doc' })
  2989. .resolves({ element: { _id: docId } })
  2990. this.ProjectEntityUpdateHandler.appendToDoc(
  2991. projectId,
  2992. docId,
  2993. this.appendedLines,
  2994. this.source,
  2995. userId,
  2996. (...args) => {
  2997. this.callback(...args)
  2998. done()
  2999. }
  3000. )
  3001. })
  3002. it('should return the response from DocumentUpdaterHandler', function () {
  3003. this.callback.should.have.been.calledWith(sinon.match.instanceOf(Error))
  3004. })
  3005. })
  3006. })
  3007. })