ProjectHistoryHandlerTests.coffee 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. chai = require('chai')
  2. assert = require('chai').assert
  3. should = chai.should()
  4. expect = chai.expect
  5. sinon = require 'sinon'
  6. modulePath = "../../../../app/js/Features/Project/ProjectHistoryHandler"
  7. SandboxedModule = require('sandboxed-module')
  8. ObjectId = require("mongoose").Types.ObjectId
  9. describe 'ProjectHistoryHandler', ->
  10. project_id = '4eecb1c1bffa66588e0000a1'
  11. userId = 1234
  12. beforeEach ->
  13. @ProjectModel = class Project
  14. constructor:(options)->
  15. @._id = project_id
  16. @name = "project_name_here"
  17. @rev = 0
  18. rootFolder:[@rootFolder]
  19. @project = new @ProjectModel()
  20. @callback = sinon.stub()
  21. @ProjectHistoryHandler = SandboxedModule.require modulePath, requires:
  22. 'logger-sharelatex': @logger = {log:sinon.stub(), error: sinon.stub(), err:->}
  23. 'settings-sharelatex': @Settings = {}
  24. '../../models/Project': Project:@ProjectModel
  25. './ProjectDetailsHandler': @ProjectDetailsHandler = {}
  26. '../History/HistoryManager': @HistoryManager = {}
  27. './ProjectEntityUpdateHandler': @ProjectEntityUpdateHandler = {}
  28. describe "starting history for an existing project", ->
  29. beforeEach ->
  30. @newHistoryId = 123456789
  31. @HistoryManager.initializeProject = sinon.stub().callsArgWith(0, null, {overleaf_id: @newHistoryId})
  32. @HistoryManager.flushProject = sinon.stub().callsArg(1)
  33. @ProjectEntityUpdateHandler.resyncProjectHistory = sinon.stub().callsArg(1)
  34. describe "when the history does not already exist", ->
  35. beforeEach ->
  36. @ProjectDetailsHandler.getDetails = sinon.stub().withArgs(project_id).callsArgWith(1, null, @project)
  37. @ProjectModel.update = sinon.stub().callsArgWith(2,null,{n:1})
  38. @ProjectHistoryHandler.ensureHistoryExistsForProject project_id, @callback
  39. it "should get any existing history id for the project", ->
  40. @ProjectDetailsHandler.getDetails
  41. .calledWith(project_id)
  42. .should.equal true
  43. it "should initialize a new history in the v1 history service", ->
  44. @HistoryManager.initializeProject
  45. .called.should.equal.true
  46. it "should set the new history id on the project", ->
  47. @ProjectModel.update
  48. .calledWith({_id: project_id, "overleaf.history.id": {$exists:false}}, {"overleaf.history.id":@newHistoryId})
  49. .should.equal true
  50. it "should resync the project history", ->
  51. @ProjectEntityUpdateHandler.resyncProjectHistory
  52. .calledWith(project_id)
  53. .should.equal true
  54. it "should flush the project history", ->
  55. @HistoryManager.flushProject
  56. .calledWith(project_id)
  57. .should.equal true
  58. it "should call the callback without an error", ->
  59. @callback.called.should.equal true
  60. describe "when the history already exists", ->
  61. beforeEach ->
  62. @project.overleaf = {history: {id: 1234}}
  63. @ProjectDetailsHandler.getDetails = sinon.stub().withArgs(project_id).callsArgWith(1, null, @project)
  64. @ProjectModel.update = sinon.stub()
  65. @ProjectHistoryHandler.ensureHistoryExistsForProject project_id, @callback
  66. it "should get any existing history id for the project", ->
  67. @ProjectDetailsHandler.getDetails
  68. .calledWith(project_id)
  69. .should.equal true
  70. it "should not initialize a new history in the v1 history service", ->
  71. @HistoryManager.initializeProject
  72. .called.should.equal false
  73. it "should not set the new history id on the project", ->
  74. @ProjectModel.update
  75. .called
  76. .should.equal false
  77. it "should not resync the project history", ->
  78. @ProjectEntityUpdateHandler.resyncProjectHistory
  79. .called
  80. .should.equal false
  81. it "should not flush the project history", ->
  82. @HistoryManager.flushProject
  83. .called
  84. .should.equal false
  85. it "should call the callback", ->
  86. @callback.calledWith().should.equal true