RestoreManagerTests.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const modulePath = require('path').join(
  4. __dirname,
  5. '../../../../app/src/Features/History/RestoreManager'
  6. )
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. const tk = require('timekeeper')
  9. const moment = require('moment')
  10. const { expect } = require('chai')
  11. describe('RestoreManager', function () {
  12. beforeEach(function () {
  13. tk.freeze(Date.now()) // freeze the time for these tests
  14. this.RestoreManager = SandboxedModule.require(modulePath, {
  15. requires: {
  16. '@overleaf/settings': {},
  17. '../../infrastructure/FileWriter': (this.FileWriter = { promises: {} }),
  18. '../Uploads/FileSystemImportManager': (this.FileSystemImportManager = {
  19. promises: {},
  20. }),
  21. '../Editor/EditorController': (this.EditorController = {
  22. promises: {},
  23. }),
  24. '../Project/ProjectLocator': (this.ProjectLocator = { promises: {} }),
  25. '../DocumentUpdater/DocumentUpdaterHandler':
  26. (this.DocumentUpdaterHandler = {
  27. promises: { flushProjectToMongo: sinon.stub().resolves() },
  28. }),
  29. '../Docstore/DocstoreManager': (this.DocstoreManager = {
  30. promises: {},
  31. }),
  32. '../Chat/ChatApiHandler': (this.ChatApiHandler = { promises: {} }),
  33. '../Chat/ChatManager': (this.ChatManager = { promises: {} }),
  34. '../Editor/EditorRealTimeController': (this.EditorRealTimeController =
  35. {}),
  36. '../Project/ProjectGetter': (this.ProjectGetter = { promises: {} }),
  37. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {
  38. promises: {},
  39. }),
  40. },
  41. })
  42. this.user_id = 'mock-user-id'
  43. this.project_id = 'mock-project-id'
  44. this.version = 42
  45. })
  46. afterEach(function () {
  47. tk.reset()
  48. })
  49. describe('restoreFileFromV2', function () {
  50. beforeEach(function () {
  51. this.RestoreManager.promises._writeFileVersionToDisk = sinon
  52. .stub()
  53. .resolves((this.fsPath = '/tmp/path/on/disk'))
  54. this.RestoreManager.promises._findOrCreateFolder = sinon
  55. .stub()
  56. .resolves((this.folder_id = 'mock-folder-id'))
  57. this.FileSystemImportManager.promises.addEntity = sinon
  58. .stub()
  59. .resolves((this.entity = 'mock-entity'))
  60. })
  61. describe('with a file not in a folder', function () {
  62. beforeEach(async function () {
  63. this.pathname = 'foo.tex'
  64. this.result = await this.RestoreManager.promises.restoreFileFromV2(
  65. this.user_id,
  66. this.project_id,
  67. this.version,
  68. this.pathname
  69. )
  70. })
  71. it('should write the file version to disk', function () {
  72. this.RestoreManager.promises._writeFileVersionToDisk
  73. .calledWith(this.project_id, this.version, this.pathname)
  74. .should.equal(true)
  75. })
  76. it('should find the root folder', function () {
  77. this.RestoreManager.promises._findOrCreateFolder
  78. .calledWith(this.project_id, '', this.user_id)
  79. .should.equal(true)
  80. })
  81. it('should add the entity', function () {
  82. this.FileSystemImportManager.promises.addEntity
  83. .calledWith(
  84. this.user_id,
  85. this.project_id,
  86. this.folder_id,
  87. 'foo.tex',
  88. this.fsPath,
  89. false
  90. )
  91. .should.equal(true)
  92. })
  93. it('should return the entity', function () {
  94. expect(this.result).to.equal(this.entity)
  95. })
  96. })
  97. describe('with a file in a folder', function () {
  98. beforeEach(async function () {
  99. this.pathname = 'foo/bar.tex'
  100. await this.RestoreManager.promises.restoreFileFromV2(
  101. this.user_id,
  102. this.project_id,
  103. this.version,
  104. this.pathname
  105. )
  106. })
  107. it('should find the folder', function () {
  108. this.RestoreManager.promises._findOrCreateFolder
  109. .calledWith(this.project_id, 'foo', this.user_id)
  110. .should.equal(true)
  111. })
  112. it('should add the entity by its basename', function () {
  113. this.FileSystemImportManager.promises.addEntity
  114. .calledWith(
  115. this.user_id,
  116. this.project_id,
  117. this.folder_id,
  118. 'bar.tex',
  119. this.fsPath,
  120. false
  121. )
  122. .should.equal(true)
  123. })
  124. })
  125. })
  126. describe('_findOrCreateFolder', function () {
  127. beforeEach(async function () {
  128. this.EditorController.promises.mkdirp = sinon.stub().resolves({
  129. newFolders: [],
  130. lastFolder: { _id: (this.folder_id = 'mock-folder-id') },
  131. })
  132. this.result = await this.RestoreManager.promises._findOrCreateFolder(
  133. this.project_id,
  134. 'folder/name',
  135. this.user_id
  136. )
  137. })
  138. it('should look up or create the folder', function () {
  139. this.EditorController.promises.mkdirp
  140. .calledWith(this.project_id, 'folder/name', this.user_id)
  141. .should.equal(true)
  142. })
  143. it('should return the folder_id', function () {
  144. expect(this.result).to.equal(this.folder_id)
  145. })
  146. })
  147. describe('_addEntityWithUniqueName', function () {
  148. beforeEach(function () {
  149. this.addEntityWithName = sinon.stub()
  150. this.name = 'foo.tex'
  151. })
  152. describe('with a valid name', function () {
  153. beforeEach(async function () {
  154. this.addEntityWithName.resolves((this.entity = 'mock-entity'))
  155. this.result =
  156. await this.RestoreManager.promises._addEntityWithUniqueName(
  157. this.addEntityWithName,
  158. this.name
  159. )
  160. })
  161. it('should add the entity', function () {
  162. this.addEntityWithName.calledWith(this.name).should.equal(true)
  163. })
  164. it('should return the entity', function () {
  165. expect(this.result).to.equal(this.entity)
  166. })
  167. })
  168. describe('with a duplicate name', function () {
  169. beforeEach(async function () {
  170. this.addEntityWithName.rejects(new Errors.DuplicateNameError())
  171. this.addEntityWithName
  172. .onSecondCall()
  173. .resolves((this.entity = 'mock-entity'))
  174. this.result =
  175. await this.RestoreManager.promises._addEntityWithUniqueName(
  176. this.addEntityWithName,
  177. this.name
  178. )
  179. })
  180. it('should try to add the entity with its original name', function () {
  181. this.addEntityWithName.calledWith('foo.tex').should.equal(true)
  182. })
  183. it('should try to add the entity with a unique name', function () {
  184. const date = moment(new Date()).format('Do MMM YY H:mm:ss')
  185. this.addEntityWithName
  186. .calledWith(`foo (Restored on ${date}).tex`)
  187. .should.equal(true)
  188. })
  189. it('should return the entity', function () {
  190. expect(this.result).to.equal(this.entity)
  191. })
  192. })
  193. })
  194. describe('revertFile', function () {
  195. beforeEach(function () {
  196. this.ProjectGetter.promises.getProject = sinon.stub()
  197. this.ProjectGetter.promises.getProject
  198. .withArgs(this.project_id)
  199. .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
  200. this.RestoreManager.promises._writeFileVersionToDisk = sinon
  201. .stub()
  202. .resolves((this.fsPath = '/tmp/path/on/disk'))
  203. this.RestoreManager.promises._findOrCreateFolder = sinon
  204. .stub()
  205. .resolves((this.folder_id = 'mock-folder-id'))
  206. this.FileSystemImportManager.promises.addEntity = sinon
  207. .stub()
  208. .resolves((this.entity = 'mock-entity'))
  209. this.RestoreManager.promises._getRangesFromHistory = sinon
  210. .stub()
  211. .rejects()
  212. this.RestoreManager.promises._getMetadataFromHistory = sinon
  213. .stub()
  214. .resolves({ metadata: undefined })
  215. })
  216. describe('reverting a project without ranges support', function () {
  217. beforeEach(function () {
  218. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  219. overleaf: { history: { rangesSupportEnabled: false } },
  220. })
  221. })
  222. it('should throw an error', async function () {
  223. await expect(
  224. this.RestoreManager.promises.revertFile(
  225. this.user_id,
  226. this.project_id,
  227. this.version,
  228. this.pathname
  229. )
  230. ).to.eventually.be.rejectedWith('project does not have ranges support')
  231. })
  232. })
  233. describe('reverting a document with ranges', function () {
  234. beforeEach(function () {
  235. this.pathname = 'foo.tex'
  236. this.comments = [
  237. { op: { t: 'comment-in-other-doc', p: 0, c: 'foo' } },
  238. { op: { t: 'single-comment', p: 10, c: 'bar' } },
  239. { op: { t: 'deleted-comment', p: 20, c: 'baz' } },
  240. ]
  241. this.remappedComments = [
  242. { op: { t: 'duplicate-comment', p: 0, c: 'foo' } },
  243. { op: { t: 'single-comment', p: 10, c: 'bar' } },
  244. ]
  245. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  246. this.DocstoreManager.promises.getAllRanges = sinon.stub().resolves([
  247. {
  248. ranges: {
  249. comments: this.comments.slice(0, 1),
  250. },
  251. },
  252. ])
  253. this.ChatApiHandler.promises.duplicateCommentThreads = sinon
  254. .stub()
  255. .resolves({
  256. newThreads: {
  257. 'comment-in-other-doc': {
  258. duplicateId: 'duplicate-comment',
  259. },
  260. },
  261. })
  262. this.ChatApiHandler.promises.generateThreadData = sinon.stub().resolves(
  263. (this.threadData = {
  264. 'single-comment': {
  265. messages: [
  266. {
  267. content: 'message-content',
  268. timestamp: '2024-01-01T00:00:00.000Z',
  269. user_id: 'user-1',
  270. },
  271. ],
  272. },
  273. 'duplicate-comment': {
  274. messages: [
  275. {
  276. content: 'another message',
  277. timestamp: '2024-01-01T00:00:00.000Z',
  278. user_id: 'user-1',
  279. },
  280. ],
  281. },
  282. })
  283. )
  284. this.ChatManager.promises.injectUserInfoIntoThreads = sinon
  285. .stub()
  286. .resolves(this.threadData)
  287. this.EditorRealTimeController.emitToRoom = sinon.stub()
  288. this.tracked_changes = [
  289. {
  290. op: { pos: 4, i: 'bar' },
  291. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-1' },
  292. },
  293. {
  294. op: { pos: 8, d: 'qux' },
  295. metadata: { ts: '2024-01-01T00:00:00.000Z', user_id: 'user-2' },
  296. },
  297. ]
  298. this.FileSystemImportManager.promises.importFile = sinon
  299. .stub()
  300. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  301. this.RestoreManager.promises._getRangesFromHistory = sinon
  302. .stub()
  303. .resolves({
  304. changes: this.tracked_changes,
  305. comments: this.comments,
  306. })
  307. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  308. .stub()
  309. .resolves([
  310. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  311. ])
  312. this.EditorController.promises.addDocWithRanges = sinon
  313. .stub()
  314. .resolves((this.addedFile = { _id: 'mock-doc', type: 'doc' }))
  315. })
  316. describe("when reverting a file that doesn't current exist", function () {
  317. beforeEach(async function () {
  318. this.data = await this.RestoreManager.promises.revertFile(
  319. this.user_id,
  320. this.project_id,
  321. this.version,
  322. this.pathname
  323. )
  324. })
  325. it('should flush the document before fetching ranges', function () {
  326. expect(
  327. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  328. ).to.have.been.calledBefore(
  329. this.DocstoreManager.promises.getAllRanges
  330. )
  331. })
  332. it('should import the file', function () {
  333. expect(
  334. this.EditorController.promises.addDocWithRanges
  335. ).to.have.been.calledWith(
  336. this.project_id,
  337. this.folder_id,
  338. 'foo.tex',
  339. ['foo', 'bar', 'baz'],
  340. { changes: this.tracked_changes, comments: this.remappedComments }
  341. )
  342. })
  343. it('should return the created entity', function () {
  344. expect(this.data).to.deep.equal(this.addedFile)
  345. })
  346. it('should look up ranges', function () {
  347. expect(
  348. this.RestoreManager.promises._getRangesFromHistory
  349. ).to.have.been.calledWith(
  350. this.project_id,
  351. this.version,
  352. this.pathname
  353. )
  354. })
  355. })
  356. describe('with an existing file in the current project', function () {
  357. beforeEach(async function () {
  358. this.ProjectLocator.promises.findElementByPath = sinon
  359. .stub()
  360. .resolves({ type: 'file', element: { _id: 'mock-file-id' } })
  361. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  362. this.data = await this.RestoreManager.promises.revertFile(
  363. this.user_id,
  364. this.project_id,
  365. this.version,
  366. this.pathname
  367. )
  368. })
  369. it('should delete the existing file', async function () {
  370. expect(
  371. this.EditorController.promises.deleteEntity
  372. ).to.have.been.calledWith(
  373. this.project_id,
  374. 'mock-file-id',
  375. 'file',
  376. {
  377. kind: 'file-restore',
  378. path: this.pathname,
  379. version: this.version,
  380. timestamp: new Date(this.endTs).toISOString(),
  381. },
  382. this.user_id
  383. )
  384. })
  385. })
  386. describe('with an existing document in the current project', function () {
  387. beforeEach(async function () {
  388. this.ProjectLocator.promises.findElementByPath = sinon
  389. .stub()
  390. .resolves({ type: 'doc', element: { _id: 'mock-file-id' } })
  391. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  392. this.data = await this.RestoreManager.promises.revertFile(
  393. this.user_id,
  394. this.project_id,
  395. this.version,
  396. this.pathname
  397. )
  398. })
  399. it('should delete the existing document', async function () {
  400. expect(
  401. this.EditorController.promises.deleteEntity
  402. ).to.have.been.calledWith(
  403. this.project_id,
  404. 'mock-file-id',
  405. 'doc',
  406. {
  407. kind: 'file-restore',
  408. path: this.pathname,
  409. version: this.version,
  410. timestamp: new Date(this.endTs).toISOString(),
  411. },
  412. this.user_id
  413. )
  414. })
  415. it('should delete the document before flushing', function () {
  416. expect(
  417. this.EditorController.promises.deleteEntity
  418. ).to.have.been.calledBefore(
  419. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  420. )
  421. })
  422. it('should flush the document before fetching ranges', function () {
  423. expect(
  424. this.DocumentUpdaterHandler.promises.flushProjectToMongo
  425. ).to.have.been.calledBefore(
  426. this.DocstoreManager.promises.getAllRanges
  427. )
  428. })
  429. it('should import the file', function () {
  430. expect(
  431. this.EditorController.promises.addDocWithRanges
  432. ).to.have.been.calledWith(
  433. this.project_id,
  434. this.folder_id,
  435. 'foo.tex',
  436. ['foo', 'bar', 'baz'],
  437. { changes: this.tracked_changes, comments: this.remappedComments },
  438. {
  439. kind: 'file-restore',
  440. path: this.pathname,
  441. version: this.version,
  442. timestamp: new Date(this.endTs).toISOString(),
  443. }
  444. )
  445. })
  446. it('should return the created entity', function () {
  447. expect(this.data).to.deep.equal(this.addedFile)
  448. })
  449. it('should look up ranges', function () {
  450. expect(
  451. this.RestoreManager.promises._getRangesFromHistory
  452. ).to.have.been.calledWith(
  453. this.project_id,
  454. this.version,
  455. this.pathname
  456. )
  457. })
  458. })
  459. })
  460. describe('reverting a file or document with metadata', function () {
  461. beforeEach(function () {
  462. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  463. this.EditorController.promises.addDocWithRanges = sinon.stub()
  464. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  465. .stub()
  466. .resolves([
  467. { toV: this.version, meta: { end_ts: (this.endTs = new Date()) } },
  468. ])
  469. this.EditorController.promises.upsertFile = sinon
  470. .stub()
  471. .resolves({ _id: 'mock-file-id', type: 'file' })
  472. this.RestoreManager.promises._getRangesFromHistory = sinon
  473. .stub()
  474. .resolves({
  475. changes: [],
  476. comments: [],
  477. })
  478. this.EditorController.promises.addDocWithRanges = sinon
  479. .stub()
  480. .resolves((this.addedFile = { _id: 'mock-doc-id', type: 'doc' }))
  481. this.DocstoreManager.promises.getAllRanges = sinon.stub().resolves([])
  482. this.ChatApiHandler.promises.generateThreadData = sinon
  483. .stub()
  484. .resolves({})
  485. this.ChatManager.promises.injectUserInfoIntoThreads = sinon
  486. .stub()
  487. .resolves({})
  488. this.EditorRealTimeController.emitToRoom = sinon.stub()
  489. })
  490. describe('when reverting a linked file', function () {
  491. beforeEach(async function () {
  492. this.pathname = 'foo.png'
  493. this.FileSystemImportManager.promises.importFile = sinon
  494. .stub()
  495. .resolves({ type: 'file' })
  496. this.RestoreManager.promises._getMetadataFromHistory = sinon
  497. .stub()
  498. .resolves({ metadata: { provider: 'bar' } })
  499. this.result = await this.RestoreManager.promises.revertFile(
  500. this.user_id,
  501. this.project_id,
  502. this.version,
  503. this.pathname
  504. )
  505. })
  506. it('should revert it as a file', function () {
  507. expect(this.result).to.deep.equal({
  508. _id: 'mock-file-id',
  509. type: 'file',
  510. })
  511. })
  512. it('should upload to the project as a file', function () {
  513. expect(
  514. this.EditorController.promises.upsertFile
  515. ).to.have.been.calledWith(
  516. this.project_id,
  517. 'mock-folder-id',
  518. 'foo.png',
  519. this.fsPath,
  520. { provider: 'bar' },
  521. {
  522. kind: 'file-restore',
  523. path: this.pathname,
  524. version: this.version,
  525. timestamp: new Date(this.endTs).toISOString(),
  526. },
  527. this.user_id
  528. )
  529. })
  530. it('should not look up ranges', function () {
  531. expect(this.RestoreManager.promises._getRangesFromHistory).to.not.have
  532. .been.called
  533. })
  534. it('should not try to add a document', function () {
  535. expect(this.EditorController.promises.addDocWithRanges).to.not.have
  536. .been.called
  537. })
  538. })
  539. describe('when reverting a linked document with provider', function () {
  540. beforeEach(async function () {
  541. this.pathname = 'foo.tex'
  542. this.FileSystemImportManager.promises.importFile = sinon
  543. .stub()
  544. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  545. this.RestoreManager.promises._getMetadataFromHistory = sinon
  546. .stub()
  547. .resolves({ metadata: { provider: 'bar' } })
  548. this.result = await this.RestoreManager.promises.revertFile(
  549. this.user_id,
  550. this.project_id,
  551. this.version,
  552. this.pathname
  553. )
  554. })
  555. it('should revert it as a file', function () {
  556. expect(this.result).to.deep.equal({
  557. _id: 'mock-file-id',
  558. type: 'file',
  559. })
  560. })
  561. it('should upload to the project as a file', function () {
  562. expect(
  563. this.EditorController.promises.upsertFile
  564. ).to.have.been.calledWith(
  565. this.project_id,
  566. 'mock-folder-id',
  567. 'foo.tex',
  568. this.fsPath,
  569. { provider: 'bar' },
  570. {
  571. kind: 'file-restore',
  572. path: this.pathname,
  573. version: this.version,
  574. timestamp: new Date(this.endTs).toISOString(),
  575. },
  576. this.user_id
  577. )
  578. })
  579. it('should not look up ranges', function () {
  580. expect(this.RestoreManager.promises._getRangesFromHistory).to.not.have
  581. .been.called
  582. })
  583. it('should not try to add a document', function () {
  584. expect(this.EditorController.promises.addDocWithRanges).to.not.have
  585. .been.called
  586. })
  587. })
  588. describe('when reverting a linked document with { main: true }', function () {
  589. beforeEach(async function () {
  590. this.pathname = 'foo.tex'
  591. this.FileSystemImportManager.promises.importFile = sinon
  592. .stub()
  593. .resolves({ type: 'doc', lines: ['foo', 'bar', 'baz'] })
  594. this.RestoreManager.promises._getMetadataFromHistory = sinon
  595. .stub()
  596. .resolves({ metadata: { main: true } })
  597. this.result = await this.RestoreManager.promises.revertFile(
  598. this.user_id,
  599. this.project_id,
  600. this.version,
  601. this.pathname
  602. )
  603. })
  604. it('should revert it as a document', function () {
  605. expect(this.result).to.deep.equal({
  606. _id: 'mock-doc-id',
  607. type: 'doc',
  608. })
  609. })
  610. it('should not upload to the project as a file', function () {
  611. expect(this.EditorController.promises.upsertFile).to.not.have.been
  612. .called
  613. })
  614. it('should look up ranges', function () {
  615. expect(this.RestoreManager.promises._getRangesFromHistory).to.have
  616. .been.called
  617. })
  618. it('should add the document', function () {
  619. expect(
  620. this.EditorController.promises.addDocWithRanges
  621. ).to.have.been.calledWith(
  622. this.project_id,
  623. this.folder_id,
  624. 'foo.tex',
  625. ['foo', 'bar', 'baz'],
  626. { changes: [], comments: [] }
  627. )
  628. })
  629. })
  630. })
  631. describe('when reverting a binary file', function () {
  632. beforeEach(async function () {
  633. this.pathname = 'foo.png'
  634. this.FileSystemImportManager.promises.importFile = sinon
  635. .stub()
  636. .resolves({ type: 'file' })
  637. this.EditorController.promises.upsertFile = sinon
  638. .stub()
  639. .resolves({ _id: 'mock-file-id', type: 'file' })
  640. this.EditorController.promises.deleteEntity = sinon.stub().resolves()
  641. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  642. .stub()
  643. .resolves([{ toV: this.version, meta: { end_ts: Date.now() } }])
  644. })
  645. it('should return the created entity if file exists', async function () {
  646. this.ProjectLocator.promises.findElementByPath = sinon
  647. .stub()
  648. .resolves({ type: 'file', element: { _id: 'existing-file-id' } })
  649. const revertRes = await this.RestoreManager.promises.revertFile(
  650. this.user_id,
  651. this.project_id,
  652. this.version,
  653. this.pathname
  654. )
  655. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  656. })
  657. it('should return the created entity if file does not exists', async function () {
  658. this.ProjectLocator.promises.findElementByPath = sinon.stub().rejects()
  659. const revertRes = await this.RestoreManager.promises.revertFile(
  660. this.user_id,
  661. this.project_id,
  662. this.version,
  663. this.pathname
  664. )
  665. expect(revertRes).to.deep.equal({ _id: 'mock-file-id', type: 'file' })
  666. })
  667. })
  668. })
  669. describe('revertProject', function () {
  670. beforeEach(function () {
  671. this.ProjectGetter.promises.getProject = sinon.stub()
  672. this.ProjectGetter.promises.getProject
  673. .withArgs(this.project_id)
  674. .resolves({ overleaf: { history: { rangesSupportEnabled: true } } })
  675. this.RestoreManager.promises.revertFile = sinon.stub().resolves()
  676. this.RestoreManager.promises._getProjectPathsAtVersion = sinon
  677. .stub()
  678. .resolves([])
  679. this.ProjectEntityHandler.promises.getAllEntities = sinon
  680. .stub()
  681. .resolves({ docs: [], files: [] })
  682. this.EditorController.promises.deleteEntityWithPath = sinon
  683. .stub()
  684. .resolves()
  685. this.RestoreManager.promises._getUpdatesFromHistory = sinon
  686. .stub()
  687. .resolves([
  688. { toV: this.version, meta: { end_ts: (this.end_ts = Date.now()) } },
  689. ])
  690. })
  691. describe('reverting a project without ranges support', function () {
  692. beforeEach(function () {
  693. this.ProjectGetter.promises.getProject = sinon.stub().resolves({
  694. overleaf: { history: { rangesSupportEnabled: false } },
  695. })
  696. })
  697. it('should throw an error', async function () {
  698. await expect(
  699. this.RestoreManager.promises.revertProject(
  700. this.user_id,
  701. this.project_id,
  702. this.version
  703. )
  704. ).to.eventually.be.rejectedWith('project does not have ranges support')
  705. })
  706. })
  707. describe('for a project with overlap in current files and old files', function () {
  708. beforeEach(async function () {
  709. this.ProjectEntityHandler.promises.getAllEntities = sinon
  710. .stub()
  711. .resolves({
  712. docs: [{ path: '/main.tex' }, { path: '/new-file.tex' }],
  713. files: [{ path: '/figures/image.png' }],
  714. })
  715. this.RestoreManager.promises._getProjectPathsAtVersion = sinon
  716. .stub()
  717. .resolves(['main.tex', 'figures/image.png', 'since-deleted.tex'])
  718. await this.RestoreManager.promises.revertProject(
  719. this.user_id,
  720. this.project_id,
  721. this.version
  722. )
  723. this.origin = {
  724. kind: 'project-restore',
  725. version: this.version,
  726. timestamp: new Date(this.end_ts).toISOString(),
  727. }
  728. })
  729. it('should delete the old files', function () {
  730. expect(
  731. this.EditorController.promises.deleteEntityWithPath
  732. ).to.have.been.calledWith(
  733. this.project_id,
  734. 'new-file.tex',
  735. this.origin,
  736. this.user_id
  737. )
  738. })
  739. it('should not delete the current files', function () {
  740. expect(
  741. this.EditorController.promises.deleteEntityWithPath
  742. ).to.not.have.been.calledWith(
  743. this.project_id,
  744. 'main.tex',
  745. this.origin,
  746. this.user_id
  747. )
  748. expect(
  749. this.EditorController.promises.deleteEntityWithPath
  750. ).to.not.have.been.calledWith(
  751. this.project_id,
  752. 'figures/image.png',
  753. this.origin,
  754. this.user_id
  755. )
  756. })
  757. it('should revert the old files', function () {
  758. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  759. this.user_id,
  760. this.project_id,
  761. this.version,
  762. 'main.tex'
  763. )
  764. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  765. this.user_id,
  766. this.project_id,
  767. this.version,
  768. 'figures/image.png'
  769. )
  770. expect(this.RestoreManager.promises.revertFile).to.have.been.calledWith(
  771. this.user_id,
  772. this.project_id,
  773. this.version,
  774. 'since-deleted.tex'
  775. )
  776. })
  777. it('should not revert the current files', function () {
  778. expect(
  779. this.RestoreManager.promises.revertFile
  780. ).to.not.have.been.calledWith(
  781. this.user_id,
  782. this.project_id,
  783. this.version,
  784. 'new-file.tex'
  785. )
  786. })
  787. })
  788. })
  789. })