RestoreManagerTests.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const assert = require('assert')
  15. const { expect } = require('chai')
  16. const sinon = require('sinon')
  17. const modulePath = require('path').join(
  18. __dirname,
  19. '../../../../app/src/Features/History/RestoreManager'
  20. )
  21. const Errors = require('../../../../app/src/Features/Errors/Errors')
  22. const tk = require('timekeeper')
  23. const moment = require('moment')
  24. describe('RestoreManager', function () {
  25. beforeEach(function () {
  26. tk.freeze(Date.now()) // freeze the time for these tests
  27. this.RestoreManager = SandboxedModule.require(modulePath, {
  28. requires: {
  29. '@overleaf/settings': {},
  30. '../../infrastructure/FileWriter': (this.FileWriter = {}),
  31. '../Uploads/FileSystemImportManager': (this.FileSystemImportManager =
  32. {}),
  33. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  34. '../Editor/EditorController': (this.EditorController = {}),
  35. },
  36. })
  37. this.user_id = 'mock-user-id'
  38. this.project_id = 'mock-project-id'
  39. this.version = 42
  40. return (this.callback = sinon.stub())
  41. })
  42. afterEach(function () {
  43. return tk.reset()
  44. })
  45. describe('restoreFileFromV2', function () {
  46. beforeEach(function () {
  47. this.RestoreManager._writeFileVersionToDisk = sinon
  48. .stub()
  49. .yields(null, (this.fsPath = '/tmp/path/on/disk'))
  50. this.RestoreManager._findOrCreateFolder = sinon
  51. .stub()
  52. .yields(null, (this.folder_id = 'mock-folder-id'))
  53. return (this.FileSystemImportManager.addEntity = sinon
  54. .stub()
  55. .yields(null, (this.entity = 'mock-entity')))
  56. })
  57. describe('with a file not in a folder', function () {
  58. beforeEach(function () {
  59. this.pathname = 'foo.tex'
  60. return this.RestoreManager.restoreFileFromV2(
  61. this.user_id,
  62. this.project_id,
  63. this.version,
  64. this.pathname,
  65. this.callback
  66. )
  67. })
  68. it('should write the file version to disk', function () {
  69. return this.RestoreManager._writeFileVersionToDisk
  70. .calledWith(this.project_id, this.version, this.pathname)
  71. .should.equal(true)
  72. })
  73. it('should find the root folder', function () {
  74. return this.RestoreManager._findOrCreateFolder
  75. .calledWith(this.project_id, '')
  76. .should.equal(true)
  77. })
  78. it('should add the entity', function () {
  79. return this.FileSystemImportManager.addEntity
  80. .calledWith(
  81. this.user_id,
  82. this.project_id,
  83. this.folder_id,
  84. 'foo.tex',
  85. this.fsPath,
  86. false
  87. )
  88. .should.equal(true)
  89. })
  90. it('should call the callback with the entity', function () {
  91. return this.callback.calledWith(null, this.entity).should.equal(true)
  92. })
  93. })
  94. describe('with a file in a folder', function () {
  95. beforeEach(function () {
  96. this.pathname = 'foo/bar.tex'
  97. return this.RestoreManager.restoreFileFromV2(
  98. this.user_id,
  99. this.project_id,
  100. this.version,
  101. this.pathname,
  102. this.callback
  103. )
  104. })
  105. it('should find the folder', function () {
  106. return this.RestoreManager._findOrCreateFolder
  107. .calledWith(this.project_id, 'foo')
  108. .should.equal(true)
  109. })
  110. it('should add the entity by its basename', function () {
  111. return this.FileSystemImportManager.addEntity
  112. .calledWith(
  113. this.user_id,
  114. this.project_id,
  115. this.folder_id,
  116. 'bar.tex',
  117. this.fsPath,
  118. false
  119. )
  120. .should.equal(true)
  121. })
  122. })
  123. })
  124. describe('_findOrCreateFolder', function () {
  125. beforeEach(function () {
  126. this.EditorController.mkdirp = sinon
  127. .stub()
  128. .yields(null, [], { _id: (this.folder_id = 'mock-folder-id') })
  129. return this.RestoreManager._findOrCreateFolder(
  130. this.project_id,
  131. 'folder/name',
  132. this.callback
  133. )
  134. })
  135. it('should look up or create the folder', function () {
  136. return this.EditorController.mkdirp
  137. .calledWith(this.project_id, 'folder/name')
  138. .should.equal(true)
  139. })
  140. it('should return the folder_id', function () {
  141. return this.callback.calledWith(null, this.folder_id).should.equal(true)
  142. })
  143. })
  144. describe('_addEntityWithUniqueName', function () {
  145. beforeEach(function () {
  146. this.addEntityWithName = sinon.stub()
  147. return (this.name = 'foo.tex')
  148. })
  149. describe('with a valid name', function () {
  150. beforeEach(function () {
  151. this.addEntityWithName.yields(null, (this.entity = 'mock-entity'))
  152. return this.RestoreManager._addEntityWithUniqueName(
  153. this.addEntityWithName,
  154. this.name,
  155. this.callback
  156. )
  157. })
  158. it('should add the entity', function () {
  159. return this.addEntityWithName.calledWith(this.name).should.equal(true)
  160. })
  161. it('should return the entity', function () {
  162. return this.callback.calledWith(null, this.entity).should.equal(true)
  163. })
  164. })
  165. describe('with an invalid name', function () {
  166. beforeEach(function () {
  167. this.addEntityWithName
  168. .onFirstCall()
  169. .yields(new Errors.InvalidNameError())
  170. this.addEntityWithName
  171. .onSecondCall()
  172. .yields(null, (this.entity = 'mock-entity'))
  173. return this.RestoreManager._addEntityWithUniqueName(
  174. this.addEntityWithName,
  175. this.name,
  176. this.callback
  177. )
  178. })
  179. it('should try to add the entity with its original name', function () {
  180. return this.addEntityWithName.calledWith('foo.tex').should.equal(true)
  181. })
  182. it('should try to add the entity with a unique name', function () {
  183. const date = moment(new Date()).format('Do MMM YY H:mm:ss')
  184. return this.addEntityWithName
  185. .calledWith(`foo (Restored on ${date}).tex`)
  186. .should.equal(true)
  187. })
  188. it('should return the entity', function () {
  189. return this.callback.calledWith(null, this.entity).should.equal(true)
  190. })
  191. })
  192. })
  193. })