GettingUpdatesTests.coffee 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. sinon = require "sinon"
  2. chai = require("chai")
  3. chai.should()
  4. expect = chai.expect
  5. mongojs = require "../../../app/js/mongojs"
  6. db = mongojs.db
  7. ObjectId = mongojs.ObjectId
  8. Settings = require "settings-sharelatex"
  9. TrackChangesClient = require "./helpers/TrackChangesClient"
  10. MockWebApi = require "./helpers/MockWebApi"
  11. describe "Getting updates", ->
  12. before (done) ->
  13. @now = Date.now()
  14. @to = @now
  15. @user_id = ObjectId().toString()
  16. @doc_id = ObjectId().toString()
  17. @project_id = ObjectId().toString()
  18. @minutes = 60 * 1000
  19. MockWebApi.users[@user_id] = @user =
  20. email: "user@sharelatex.com"
  21. first_name: "Leo"
  22. last_name: "Lion"
  23. id: @user_id
  24. sinon.spy MockWebApi, "getUser"
  25. @updates = [{
  26. op: [{ i: "one ", p: 0 }]
  27. meta: { ts: @to - 4 * @minutes, user_id: @user_id }
  28. v: 3
  29. }, {
  30. op: [{ i: "two ", p: 4 }]
  31. meta: { ts: @to - 2 * @minutes, user_id: @user_id }
  32. v: 4
  33. }, {
  34. op: [{ i: "three ", p: 8 }]
  35. meta: { ts: @to, user_id: @user_id }
  36. v: @toVersion = 5
  37. }, {
  38. op: [{ i: "four", p: 14 }]
  39. meta: { ts: @to + 2 * @minutes, user_id: @user_id }
  40. v: 6
  41. }]
  42. TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
  43. throw error if error?
  44. TrackChangesClient.getUpdates @project_id, @doc_id, { to: @toVersion, limit: 2 }, (error, body) =>
  45. throw error if error?
  46. @updates = body.updates
  47. done()
  48. after: () ->
  49. MockWebApi.getUser.restore()
  50. it "should fetch the user details from the web api", ->
  51. MockWebApi.getUser
  52. .calledWith(@user_id)
  53. .should.equal true
  54. it "should return the updates", ->
  55. expect(@updates).to.deep.equal [{
  56. meta:
  57. start_ts: @to
  58. end_ts: @to
  59. user: @user
  60. v: 5
  61. }, {
  62. meta:
  63. start_ts: @to - 2 * @minutes
  64. end_ts: @to - 2 * @minutes
  65. user: @user
  66. v: 4
  67. }]