HistoryManagerTests.coffee 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. chai = require('chai')
  2. chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. modulePath = "../../../../app/js/Features/History/HistoryManager"
  6. SandboxedModule = require('sandboxed-module')
  7. describe "HistoryManager", ->
  8. beforeEach ->
  9. @callback = sinon.stub()
  10. @user_id = "user-id-123"
  11. @AuthenticationController =
  12. getLoggedInUserId: sinon.stub().returns(@user_id)
  13. @HistoryManager = SandboxedModule.require modulePath, requires:
  14. "request" : @request = sinon.stub()
  15. "settings-sharelatex": @settings = {}
  16. "../User/UserGetter": @UserGetter = {}
  17. @settings.apis =
  18. trackchanges:
  19. enabled: false
  20. url: "http://trackchanges.example.com"
  21. project_history:
  22. url: "http://project_history.example.com"
  23. describe "initializeProject", ->
  24. describe "with project history enabled", ->
  25. beforeEach ->
  26. @settings.apis.project_history.initializeHistoryForNewProjects = true
  27. describe "project history returns a successful response", ->
  28. beforeEach ->
  29. @overleaf_id = 1234
  30. @res = statusCode: 200
  31. @body = JSON.stringify(project: id: @overleaf_id)
  32. @request.post = sinon.stub().callsArgWith(1, null, @res, @body)
  33. @HistoryManager.initializeProject @callback
  34. it "should call the project history api", ->
  35. @request.post.calledWith(
  36. url: "#{@settings.apis.project_history.url}/project"
  37. ).should.equal true
  38. it "should return the callback with the overleaf id", ->
  39. @callback.calledWithExactly(null, { @overleaf_id }).should.equal true
  40. describe "project history returns a response without the project id", ->
  41. beforeEach ->
  42. @res = statusCode: 200
  43. @body = JSON.stringify(project: {})
  44. @request.post = sinon.stub().callsArgWith(1, null, @res, @body)
  45. @HistoryManager.initializeProject @callback
  46. it "should return the callback with an error", ->
  47. @callback
  48. .calledWith(sinon.match.has("message", "project-history did not provide an id"))
  49. .should.equal true
  50. describe "project history returns a unsuccessful response", ->
  51. beforeEach ->
  52. @res = statusCode: 404
  53. @request.post = sinon.stub().callsArgWith(1, null, @res)
  54. @HistoryManager.initializeProject @callback
  55. it "should return the callback with an error", ->
  56. @callback
  57. .calledWith(sinon.match.has("message", "project-history returned a non-success status code: 404"))
  58. .should.equal true
  59. describe "project history errors", ->
  60. beforeEach ->
  61. @error = sinon.stub()
  62. @request.post = sinon.stub().callsArgWith(1, @error)
  63. @HistoryManager.initializeProject @callback
  64. it "should return the callback with the error", ->
  65. @callback.calledWithExactly(@error).should.equal true
  66. describe "with project history disabled", ->
  67. beforeEach ->
  68. @settings.apis.project_history.initializeHistoryForNewProjects = false
  69. @HistoryManager.initializeProject @callback
  70. it "should return the callback", ->
  71. @callback.calledWithExactly().should.equal true
  72. describe "injectUserDetails", ->
  73. beforeEach ->
  74. @user1 = {
  75. _id: @user_id1 = "123456"
  76. first_name: "Jane",
  77. last_name: "Doe"
  78. email: "jane@example.com"
  79. }
  80. @user1_view = {
  81. id: @user_id1
  82. first_name: "Jane",
  83. last_name: "Doe"
  84. email: "jane@example.com"
  85. }
  86. @user2 = {
  87. _id: @user_id2 = "abcdef"
  88. first_name: "John",
  89. last_name: "Doe"
  90. email: "john@example.com"
  91. }
  92. @user2_view = {
  93. id: @user_id2
  94. first_name: "John",
  95. last_name: "Doe"
  96. email: "john@example.com"
  97. }
  98. @UserGetter.getUsers = sinon.stub().yields(null, [@user1, @user2])
  99. describe "with a diff", ->
  100. it "should turn user_ids into user objects", (done) ->
  101. @HistoryManager.injectUserDetails {
  102. diff: [{
  103. i: "foo"
  104. meta:
  105. users: [@user_id1]
  106. }, {
  107. i: "bar"
  108. meta:
  109. users: [@user_id2]
  110. }]
  111. }, (error, diff) =>
  112. expect(error).to.be.null
  113. expect(diff.diff[0].meta.users).to.deep.equal [@user1_view]
  114. expect(diff.diff[1].meta.users).to.deep.equal [@user2_view]
  115. done()
  116. it "should leave user objects", (done) ->
  117. @HistoryManager.injectUserDetails {
  118. diff: [{
  119. i: "foo"
  120. meta:
  121. users: [@user1_view]
  122. }, {
  123. i: "bar"
  124. meta:
  125. users: [@user_id2]
  126. }]
  127. }, (error, diff) =>
  128. expect(error).to.be.null
  129. expect(diff.diff[0].meta.users).to.deep.equal [@user1_view]
  130. expect(diff.diff[1].meta.users).to.deep.equal [@user2_view]
  131. done()
  132. describe "with a list of updates", ->
  133. it "should turn user_ids into user objects", (done) ->
  134. @HistoryManager.injectUserDetails {
  135. updates: [{
  136. fromV: 5
  137. toV: 8
  138. meta:
  139. users: [@user_id1]
  140. }, {
  141. fromV: 4
  142. toV: 5
  143. meta:
  144. users: [@user_id2]
  145. }]
  146. }, (error, updates) =>
  147. expect(error).to.be.null
  148. expect(updates.updates[0].meta.users).to.deep.equal [@user1_view]
  149. expect(updates.updates[1].meta.users).to.deep.equal [@user2_view]
  150. done()
  151. it "should leave user objects", (done) ->
  152. @HistoryManager.injectUserDetails {
  153. updates: [{
  154. fromV: 5
  155. toV: 8
  156. meta:
  157. users: [@user1_view]
  158. }, {
  159. fromV: 4
  160. toV: 5
  161. meta:
  162. users: [@user_id2]
  163. }]
  164. }, (error, updates) =>
  165. expect(error).to.be.null
  166. expect(updates.updates[0].meta.users).to.deep.equal [@user1_view]
  167. expect(updates.updates[1].meta.users).to.deep.equal [@user2_view]
  168. done()