RestoreManagerTests.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. SandboxedModule = require('sandboxed-module')
  2. assert = require('assert')
  3. require('chai').should()
  4. expect = require('chai').expect
  5. sinon = require('sinon')
  6. modulePath = require('path').join __dirname, '../../../../app/js/Features/History/RestoreManager'
  7. Errors = require '../../../../app/js/Features/Errors/Errors'
  8. tk = require("timekeeper")
  9. moment = require('moment')
  10. describe 'RestoreManager', ->
  11. beforeEach ->
  12. tk.freeze Date.now() # freeze the time for these tests
  13. @RestoreManager = SandboxedModule.require modulePath, requires:
  14. '../../infrastructure/FileWriter': @FileWriter = {}
  15. '../Uploads/FileSystemImportManager': @FileSystemImportManager = {}
  16. '../Project/ProjectLocator': @ProjectLocator = {}
  17. '../Errors/Errors': Errors
  18. '../Project/ProjectEntityHandler': @ProjectEntityHandler = {}
  19. '../Editor/EditorController': @EditorController = {}
  20. 'logger-sharelatex': @logger = {log: sinon.stub(), err: sinon.stub()}
  21. @user_id = 'mock-user-id'
  22. @project_id = 'mock-project-id'
  23. @version = 42
  24. @callback = sinon.stub()
  25. afterEach ->
  26. tk.reset()
  27. describe 'restoreFileFromV2', ->
  28. beforeEach ->
  29. @RestoreManager._writeFileVersionToDisk = sinon.stub().yields(null, @fsPath = "/tmp/path/on/disk")
  30. @RestoreManager._findOrCreateFolder = sinon.stub().yields(null, @folder_id = 'mock-folder-id')
  31. @FileSystemImportManager.addEntity = sinon.stub().yields(null, @entity = 'mock-entity')
  32. describe "with a file not in a folder", ->
  33. beforeEach ->
  34. @pathname = 'foo.tex'
  35. @RestoreManager.restoreFileFromV2 @user_id, @project_id, @version, @pathname, @callback
  36. it 'should write the file version to disk', ->
  37. @RestoreManager._writeFileVersionToDisk
  38. .calledWith(@project_id, @version, @pathname)
  39. .should.equal true
  40. it 'should find the root folder', ->
  41. @RestoreManager._findOrCreateFolder
  42. .calledWith(@project_id, "")
  43. .should.equal true
  44. it 'should add the entity', ->
  45. @FileSystemImportManager.addEntity
  46. .calledWith(@user_id, @project_id, @folder_id, 'foo.tex', @fsPath, false)
  47. .should.equal true
  48. it 'should call the callback with the entity', ->
  49. @callback.calledWith(null, @entity).should.equal true
  50. describe "with a file in a folder", ->
  51. beforeEach ->
  52. @pathname = 'foo/bar.tex'
  53. @RestoreManager.restoreFileFromV2 @user_id, @project_id, @version, @pathname, @callback
  54. it 'should find the folder', ->
  55. @RestoreManager._findOrCreateFolder
  56. .calledWith(@project_id, "foo")
  57. .should.equal true
  58. it 'should add the entity by its basename', ->
  59. @FileSystemImportManager.addEntity
  60. .calledWith(@user_id, @project_id, @folder_id, 'bar.tex', @fsPath, false)
  61. .should.equal true
  62. describe '_findOrCreateFolder', ->
  63. beforeEach ->
  64. @EditorController.mkdirp = sinon.stub().yields(null, [], {_id: @folder_id = 'mock-folder-id'})
  65. @RestoreManager._findOrCreateFolder @project_id, 'folder/name', @callback
  66. it 'should look up or create the folder', ->
  67. @EditorController.mkdirp
  68. .calledWith(@project_id, 'folder/name')
  69. .should.equal true
  70. it 'should return the folder_id', ->
  71. @callback.calledWith(null, @folder_id).should.equal true
  72. describe '_addEntityWithUniqueName', ->
  73. beforeEach ->
  74. @addEntityWithName = sinon.stub()
  75. @name = 'foo.tex'
  76. describe 'with a valid name', ->
  77. beforeEach ->
  78. @addEntityWithName.yields(null, @entity = 'mock-entity')
  79. @RestoreManager._addEntityWithUniqueName @addEntityWithName, @name, @callback
  80. it 'should add the entity', ->
  81. @addEntityWithName.calledWith(@name).should.equal true
  82. it 'should return the entity', ->
  83. @callback.calledWith(null, @entity).should.equal true
  84. describe "with an invalid name", ->
  85. beforeEach ->
  86. @addEntityWithName.onFirstCall().yields(new Errors.InvalidNameError())
  87. @addEntityWithName.onSecondCall().yields(null, @entity = 'mock-entity')
  88. @RestoreManager._addEntityWithUniqueName @addEntityWithName, @name, @callback
  89. it 'should try to add the entity with its original name', ->
  90. @addEntityWithName.calledWith('foo.tex').should.equal true
  91. it 'should try to add the entity with a unique name', ->
  92. date = moment(new Date()).format('Do MMM YY H:mm:ss')
  93. @addEntityWithName.calledWith("foo (Restored on #{date}).tex").should.equal true
  94. it 'should return the entity', ->
  95. @callback.calledWith(null, @entity).should.equal true