ApplyingUpdatesToProjectStructureTests.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 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 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 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()
  82. describe "adding a file", ->
  83. before (done) ->
  84. @project_id = DocUpdaterClient.randomId()
  85. @fileUpdate =
  86. id: DocUpdaterClient.randomId()
  87. pathname: '/file-path'
  88. url: 'filestore.example.com'
  89. @fileUpdates = [ @fileUpdate ]
  90. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, [], @fileUpdates, (error) ->
  91. throw error if error?
  92. setTimeout done, 200
  93. it "should push the file addition to the project history api", (done) ->
  94. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  95. throw error if error?
  96. update = JSON.parse(updates[0])
  97. update.file.should.equal @fileUpdate.id
  98. update.pathname.should.equal '/file-path'
  99. update.url.should.equal 'filestore.example.com'
  100. update.meta.user_id.should.equal @user_id
  101. update.meta.ts.should.be.a('string')
  102. done()
  103. describe "adding a doc", ->
  104. before (done) ->
  105. @project_id = DocUpdaterClient.randomId()
  106. @docUpdate =
  107. id: DocUpdaterClient.randomId()
  108. pathname: '/file-path'
  109. docLines: 'a\nb'
  110. @docUpdates = [ @docUpdate ]
  111. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  112. throw error if error?
  113. setTimeout done, 200
  114. it "should push the doc addition to the project history api", (done) ->
  115. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  116. throw error if error?
  117. update = JSON.parse(updates[0])
  118. update.doc.should.equal @docUpdate.id
  119. update.pathname.should.equal '/file-path'
  120. update.docLines.should.equal 'a\nb'
  121. update.meta.user_id.should.equal @user_id
  122. update.meta.ts.should.be.a('string')
  123. done()