ApplyingUpdatesToProjectStructureTests.coffee 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. DocUpdaterApp = require "./helpers/DocUpdaterApp"
  10. describe "Applying updates to a project's structure", ->
  11. before ->
  12. @user_id = 'user-id-123'
  13. describe "renaming a file", ->
  14. before (done) ->
  15. @project_id = DocUpdaterClient.randomId()
  16. @fileUpdate =
  17. id: DocUpdaterClient.randomId()
  18. pathname: '/file-path'
  19. newPathname: '/new-file-path'
  20. @fileUpdates = [ @fileUpdate ]
  21. DocUpdaterApp.ensureRunning (error) =>
  22. throw error if error?
  23. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, [], @fileUpdates, (error) ->
  24. throw error if error?
  25. setTimeout done, 200
  26. it "should push the applied file renames to the project history api", (done) ->
  27. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  28. throw error if error?
  29. update = JSON.parse(updates[0])
  30. update.file.should.equal @fileUpdate.id
  31. update.pathname.should.equal '/file-path'
  32. update.new_pathname.should.equal '/new-file-path'
  33. update.meta.user_id.should.equal @user_id
  34. update.meta.ts.should.be.a('string')
  35. done()
  36. describe "renaming a document", ->
  37. before ->
  38. @docUpdate =
  39. id: DocUpdaterClient.randomId()
  40. pathname: '/doc-path'
  41. newPathname: '/new-doc-path'
  42. @docUpdates = [ @docUpdate ]
  43. describe "when the document is not loaded", ->
  44. before (done) ->
  45. @project_id = DocUpdaterClient.randomId()
  46. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  47. throw error if error?
  48. setTimeout done, 200
  49. it "should push the applied doc renames to the project history api", (done) ->
  50. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  51. throw error if error?
  52. update = JSON.parse(updates[0])
  53. update.doc.should.equal @docUpdate.id
  54. update.pathname.should.equal '/doc-path'
  55. update.new_pathname.should.equal '/new-doc-path'
  56. update.meta.user_id.should.equal @user_id
  57. update.meta.ts.should.be.a('string')
  58. done()
  59. describe "when the document is loaded", ->
  60. before (done) ->
  61. @project_id = DocUpdaterClient.randomId()
  62. MockWebApi.insertDoc @project_id, @docUpdate.id, {}
  63. DocUpdaterClient.preloadDoc @project_id, @docUpdate.id, (error) =>
  64. throw error if error?
  65. sinon.spy MockWebApi, "getDocument"
  66. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  67. throw error if error?
  68. setTimeout done, 200
  69. after ->
  70. MockWebApi.getDocument.restore()
  71. it "should update the doc", (done) ->
  72. DocUpdaterClient.getDoc @project_id, @docUpdate.id, (error, res, doc) =>
  73. doc.pathname.should.equal @docUpdate.newPathname
  74. done()
  75. it "should push the applied doc renames to the project history api", (done) ->
  76. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  77. throw error if error?
  78. update = JSON.parse(updates[0])
  79. update.doc.should.equal @docUpdate.id
  80. update.pathname.should.equal '/doc-path'
  81. update.new_pathname.should.equal '/new-doc-path'
  82. update.meta.user_id.should.equal @user_id
  83. update.meta.ts.should.be.a('string')
  84. done()
  85. describe "adding a file", ->
  86. before (done) ->
  87. @project_id = DocUpdaterClient.randomId()
  88. @fileUpdate =
  89. id: DocUpdaterClient.randomId()
  90. pathname: '/file-path'
  91. url: 'filestore.example.com'
  92. @fileUpdates = [ @fileUpdate ]
  93. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, [], @fileUpdates, (error) ->
  94. throw error if error?
  95. setTimeout done, 200
  96. it "should push the file addition to the project history api", (done) ->
  97. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  98. throw error if error?
  99. update = JSON.parse(updates[0])
  100. update.file.should.equal @fileUpdate.id
  101. update.pathname.should.equal '/file-path'
  102. update.url.should.equal 'filestore.example.com'
  103. update.meta.user_id.should.equal @user_id
  104. update.meta.ts.should.be.a('string')
  105. done()
  106. describe "adding a doc", ->
  107. before (done) ->
  108. @project_id = DocUpdaterClient.randomId()
  109. @docUpdate =
  110. id: DocUpdaterClient.randomId()
  111. pathname: '/file-path'
  112. docLines: 'a\nb'
  113. @docUpdates = [ @docUpdate ]
  114. DocUpdaterClient.sendProjectUpdate @project_id, @user_id, @docUpdates, [], (error) ->
  115. throw error if error?
  116. setTimeout done, 200
  117. it "should push the doc addition to the project history api", (done) ->
  118. rclient_history.lrange ProjectHistoryKeys.projectHistoryOps({@project_id}), 0, -1, (error, updates) =>
  119. throw error if error?
  120. update = JSON.parse(updates[0])
  121. update.doc.should.equal @docUpdate.id
  122. update.pathname.should.equal '/file-path'
  123. update.docLines.should.equal 'a\nb'
  124. update.meta.user_id.should.equal @user_id
  125. update.meta.ts.should.be.a('string')
  126. done()