RestoreManagerTests.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. require('chai').should()
  16. const { expect } = require('chai')
  17. const sinon = require('sinon')
  18. const modulePath = require('path').join(
  19. __dirname,
  20. '../../../../app/src/Features/History/RestoreManager'
  21. )
  22. const Errors = require('../../../../app/src/Features/Errors/Errors')
  23. const tk = require('timekeeper')
  24. const moment = require('moment')
  25. describe('RestoreManager', function() {
  26. beforeEach(function() {
  27. tk.freeze(Date.now()) // freeze the time for these tests
  28. this.RestoreManager = SandboxedModule.require(modulePath, {
  29. globals: {
  30. console: console
  31. },
  32. requires: {
  33. '../../infrastructure/FileWriter': (this.FileWriter = {}),
  34. '../Uploads/FileSystemImportManager': (this.FileSystemImportManager = {}),
  35. '../Project/ProjectLocator': (this.ProjectLocator = {}),
  36. '../Errors/Errors': Errors,
  37. '../Project/ProjectEntityHandler': (this.ProjectEntityHandler = {}),
  38. '../Editor/EditorController': (this.EditorController = {}),
  39. 'logger-sharelatex': (this.logger = {
  40. log: sinon.stub(),
  41. err: sinon.stub()
  42. })
  43. }
  44. })
  45. this.user_id = 'mock-user-id'
  46. this.project_id = 'mock-project-id'
  47. this.version = 42
  48. return (this.callback = sinon.stub())
  49. })
  50. afterEach(function() {
  51. return tk.reset()
  52. })
  53. describe('restoreFileFromV2', function() {
  54. beforeEach(function() {
  55. this.RestoreManager._writeFileVersionToDisk = sinon
  56. .stub()
  57. .yields(null, (this.fsPath = '/tmp/path/on/disk'))
  58. this.RestoreManager._findOrCreateFolder = sinon
  59. .stub()
  60. .yields(null, (this.folder_id = 'mock-folder-id'))
  61. return (this.FileSystemImportManager.addEntity = sinon
  62. .stub()
  63. .yields(null, (this.entity = 'mock-entity')))
  64. })
  65. describe('with a file not in a folder', function() {
  66. beforeEach(function() {
  67. this.pathname = 'foo.tex'
  68. return this.RestoreManager.restoreFileFromV2(
  69. this.user_id,
  70. this.project_id,
  71. this.version,
  72. this.pathname,
  73. this.callback
  74. )
  75. })
  76. it('should write the file version to disk', function() {
  77. return this.RestoreManager._writeFileVersionToDisk
  78. .calledWith(this.project_id, this.version, this.pathname)
  79. .should.equal(true)
  80. })
  81. it('should find the root folder', function() {
  82. return this.RestoreManager._findOrCreateFolder
  83. .calledWith(this.project_id, '')
  84. .should.equal(true)
  85. })
  86. it('should add the entity', function() {
  87. return this.FileSystemImportManager.addEntity
  88. .calledWith(
  89. this.user_id,
  90. this.project_id,
  91. this.folder_id,
  92. 'foo.tex',
  93. this.fsPath,
  94. false
  95. )
  96. .should.equal(true)
  97. })
  98. it('should call the callback with the entity', function() {
  99. return this.callback.calledWith(null, this.entity).should.equal(true)
  100. })
  101. })
  102. describe('with a file in a folder', function() {
  103. beforeEach(function() {
  104. this.pathname = 'foo/bar.tex'
  105. return this.RestoreManager.restoreFileFromV2(
  106. this.user_id,
  107. this.project_id,
  108. this.version,
  109. this.pathname,
  110. this.callback
  111. )
  112. })
  113. it('should find the folder', function() {
  114. return this.RestoreManager._findOrCreateFolder
  115. .calledWith(this.project_id, 'foo')
  116. .should.equal(true)
  117. })
  118. it('should add the entity by its basename', function() {
  119. return this.FileSystemImportManager.addEntity
  120. .calledWith(
  121. this.user_id,
  122. this.project_id,
  123. this.folder_id,
  124. 'bar.tex',
  125. this.fsPath,
  126. false
  127. )
  128. .should.equal(true)
  129. })
  130. })
  131. })
  132. describe('_findOrCreateFolder', function() {
  133. beforeEach(function() {
  134. this.EditorController.mkdirp = sinon
  135. .stub()
  136. .yields(null, [], { _id: (this.folder_id = 'mock-folder-id') })
  137. return this.RestoreManager._findOrCreateFolder(
  138. this.project_id,
  139. 'folder/name',
  140. this.callback
  141. )
  142. })
  143. it('should look up or create the folder', function() {
  144. return this.EditorController.mkdirp
  145. .calledWith(this.project_id, 'folder/name')
  146. .should.equal(true)
  147. })
  148. it('should return the folder_id', function() {
  149. return this.callback.calledWith(null, this.folder_id).should.equal(true)
  150. })
  151. })
  152. describe('_addEntityWithUniqueName', function() {
  153. beforeEach(function() {
  154. this.addEntityWithName = sinon.stub()
  155. return (this.name = 'foo.tex')
  156. })
  157. describe('with a valid name', function() {
  158. beforeEach(function() {
  159. this.addEntityWithName.yields(null, (this.entity = 'mock-entity'))
  160. return this.RestoreManager._addEntityWithUniqueName(
  161. this.addEntityWithName,
  162. this.name,
  163. this.callback
  164. )
  165. })
  166. it('should add the entity', function() {
  167. return this.addEntityWithName.calledWith(this.name).should.equal(true)
  168. })
  169. it('should return the entity', function() {
  170. return this.callback.calledWith(null, this.entity).should.equal(true)
  171. })
  172. })
  173. describe('with an invalid name', function() {
  174. beforeEach(function() {
  175. this.addEntityWithName
  176. .onFirstCall()
  177. .yields(new Errors.InvalidNameError())
  178. this.addEntityWithName
  179. .onSecondCall()
  180. .yields(null, (this.entity = 'mock-entity'))
  181. return this.RestoreManager._addEntityWithUniqueName(
  182. this.addEntityWithName,
  183. this.name,
  184. this.callback
  185. )
  186. })
  187. it('should try to add the entity with its original name', function() {
  188. return this.addEntityWithName.calledWith('foo.tex').should.equal(true)
  189. })
  190. it('should try to add the entity with a unique name', function() {
  191. const date = moment(new Date()).format('Do MMM YY H:mm:ss')
  192. return this.addEntityWithName
  193. .calledWith(`foo (Restored on ${date}).tex`)
  194. .should.equal(true)
  195. })
  196. it('should return the entity', function() {
  197. return this.callback.calledWith(null, this.entity).should.equal(true)
  198. })
  199. })
  200. })
  201. })