ApplyingUpdatesToProjectStructureTests.coffee 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. Settings = require('settings-sharelatex')
  5. rclient_history = require("redis-sharelatex").createClient(Settings.redis.history)
  6. ProjectHistoryKeys = Settings.redis.project_history.key_schema
  7. MockWebApi = require "./helpers/MockWebApi"
  8. DocUpdaterClient = require "./helpers/DocUpdaterClient"
  9. describe "Applying updates to a project's structure", ->
  10. before ->
  11. @user_id = 'user-id-123'
  12. describe "renaming a file", ->
  13. before (done) ->
  14. @project_id = DocUpdaterClient.randomId()
  15. @fileUpdate =
  16. id: DocUpdaterClient.randomId()
  17. pathname: '/file-path'
  18. newPathname: '/new-file-path'
  19. @fileUpdates = [ @fileUpdate ]
  20. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, [], @fileUpdates, (error) ->
  21. throw error if error?
  22. setTimeout done, 200
  23. it "should push the applied file renames to the project history changes api", (done) ->
  24. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  25. throw error if error?
  26. update = JSON.parse(updates[0])
  27. update.file.should.equal @fileUpdate.id
  28. update.pathname.should.equal '/file-path'
  29. update.new_pathname.should.equal '/new-file-path'
  30. update.meta.user_id.should.equal @user_id
  31. update.meta.ts.should.be.a('string')
  32. done()
  33. describe "renaming a document", ->
  34. before ->
  35. @docUpdate =
  36. id: DocUpdaterClient.randomId()
  37. pathname: '/doc-path'
  38. newPathname: '/new-doc-path'
  39. @docUpdates = [ @docUpdate ]
  40. describe "when the document is not loaded", ->
  41. before (done) ->
  42. @project_id = DocUpdaterClient.randomId()
  43. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  44. throw error if error?
  45. setTimeout done, 200
  46. it "should push the applied doc renames to the project history changes api", (done) ->
  47. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  48. throw error if error?
  49. update = JSON.parse(updates[0])
  50. update.doc.should.equal @docUpdate.id
  51. update.pathname.should.equal '/doc-path'
  52. update.new_pathname.should.equal '/new-doc-path'
  53. update.meta.user_id.should.equal @user_id
  54. update.meta.ts.should.be.a('string')
  55. done()
  56. describe "when the document is loaded", ->
  57. before (done) ->
  58. @project_id = DocUpdaterClient.randomId()
  59. MockWebApi.insertDoc @project_id, @docUpdate.id, {}
  60. DocUpdaterClient.preloadDoc @project_id, @docUpdate.id, (error) =>
  61. throw error if error?
  62. sinon.spy MockWebApi, "getDocument"
  63. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  64. throw error if error?
  65. setTimeout done, 200
  66. after ->
  67. MockWebApi.getDocument.restore()
  68. it "should update the doc", (done) ->
  69. DocUpdaterClient.getDoc @project_id, @docUpdate.id, (error, res, doc) =>
  70. doc.pathname.should.equal @docUpdate.newPathname
  71. done()
  72. it "should push the applied doc renames to the project history changes api", (done) ->
  73. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  74. throw error if error?
  75. update = JSON.parse(updates[0])
  76. update.doc.should.equal @docUpdate.id
  77. update.pathname.should.equal '/doc-path'
  78. update.new_pathname.should.equal '/new-doc-path'
  79. update.meta.user_id.should.equal @user_id
  80. update.meta.ts.should.be.a('string')
  81. done()