HistoryManagerTests.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const SandboxedModule = require('sandboxed-module')
  4. const { ObjectId } = require('mongodb')
  5. const MODULE_PATH = '../../../../app/src/Features/History/HistoryManager'
  6. describe('HistoryManager', function() {
  7. beforeEach(function() {
  8. this.user_id = 'user-id-123'
  9. this.AuthenticationController = {
  10. getLoggedInUserId: sinon.stub().returns(this.user_id)
  11. }
  12. this.request = {
  13. post: sinon.stub(),
  14. delete: sinon.stub().resolves()
  15. }
  16. this.projectHistoryUrl = 'http://project_history.example.com'
  17. this.settings = {
  18. apis: {
  19. trackchanges: {
  20. enabled: false,
  21. url: 'http://trackchanges.example.com'
  22. },
  23. project_history: {
  24. url: this.projectHistoryUrl
  25. }
  26. }
  27. }
  28. this.UserGetter = {
  29. promises: {
  30. getUsersByV1Ids: sinon.stub(),
  31. getUsers: sinon.stub()
  32. }
  33. }
  34. this.HistoryManager = SandboxedModule.require(MODULE_PATH, {
  35. globals: {
  36. console: console
  37. },
  38. requires: {
  39. 'request-promise-native': this.request,
  40. 'settings-sharelatex': this.settings,
  41. '../User/UserGetter': this.UserGetter
  42. }
  43. })
  44. })
  45. describe('initializeProject', function() {
  46. describe('with project history enabled', function() {
  47. beforeEach(function() {
  48. this.settings.apis.project_history.initializeHistoryForNewProjects = true
  49. })
  50. describe('project history returns a successful response', function() {
  51. beforeEach(async function() {
  52. this.overleaf_id = 1234
  53. this.request.post.resolves(
  54. JSON.stringify({ project: { id: this.overleaf_id } })
  55. )
  56. this.result = await this.HistoryManager.promises.initializeProject()
  57. })
  58. it('should call the project history api', function() {
  59. this.request.post
  60. .calledWith({
  61. url: `${this.settings.apis.project_history.url}/project`
  62. })
  63. .should.equal(true)
  64. })
  65. it('should return the overleaf id', function() {
  66. expect(this.result).to.deep.equal({ overleaf_id: this.overleaf_id })
  67. })
  68. })
  69. describe('project history returns a response without the project id', function() {
  70. it('should throw an error', async function() {
  71. this.request.post.resolves(JSON.stringify({ project: {} }))
  72. await expect(this.HistoryManager.promises.initializeProject()).to.be
  73. .rejected
  74. })
  75. })
  76. describe('project history errors', function() {
  77. it('should propagate the error', async function() {
  78. this.request.post.rejects(new Error('problem connecting'))
  79. await expect(this.HistoryManager.promises.initializeProject()).to.be
  80. .rejected
  81. })
  82. })
  83. })
  84. describe('with project history disabled', function() {
  85. it('should return without errors', async function() {
  86. this.settings.apis.project_history.initializeHistoryForNewProjects = false
  87. await expect(this.HistoryManager.promises.initializeProject()).to.be
  88. .fulfilled
  89. })
  90. })
  91. })
  92. describe('injectUserDetails', function() {
  93. beforeEach(function() {
  94. this.user1 = {
  95. _id: (this.user_id1 = '123456'),
  96. first_name: 'Jane',
  97. last_name: 'Doe',
  98. email: 'jane@example.com',
  99. overleaf: { id: 5011 }
  100. }
  101. this.user1_view = {
  102. id: this.user_id1,
  103. first_name: 'Jane',
  104. last_name: 'Doe',
  105. email: 'jane@example.com'
  106. }
  107. this.user2 = {
  108. _id: (this.user_id2 = 'abcdef'),
  109. first_name: 'John',
  110. last_name: 'Doe',
  111. email: 'john@example.com'
  112. }
  113. this.user2_view = {
  114. id: this.user_id2,
  115. first_name: 'John',
  116. last_name: 'Doe',
  117. email: 'john@example.com'
  118. }
  119. this.UserGetter.promises.getUsersByV1Ids.resolves([this.user1])
  120. this.UserGetter.promises.getUsers.resolves([this.user1, this.user2])
  121. })
  122. describe('with a diff', function() {
  123. it('should turn user_ids into user objects', async function() {
  124. const diff = await this.HistoryManager.promises.injectUserDetails({
  125. diff: [
  126. {
  127. i: 'foo',
  128. meta: {
  129. users: [this.user_id1]
  130. }
  131. },
  132. {
  133. i: 'bar',
  134. meta: {
  135. users: [this.user_id2]
  136. }
  137. }
  138. ]
  139. })
  140. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  141. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  142. })
  143. it('should handle v1 user ids', async function() {
  144. const diff = await this.HistoryManager.promises.injectUserDetails({
  145. diff: [
  146. {
  147. i: 'foo',
  148. meta: {
  149. users: [5011]
  150. }
  151. },
  152. {
  153. i: 'bar',
  154. meta: {
  155. users: [this.user_id2]
  156. }
  157. }
  158. ]
  159. })
  160. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  161. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  162. })
  163. it('should leave user objects', async function() {
  164. const diff = await this.HistoryManager.promises.injectUserDetails({
  165. diff: [
  166. {
  167. i: 'foo',
  168. meta: {
  169. users: [this.user1_view]
  170. }
  171. },
  172. {
  173. i: 'bar',
  174. meta: {
  175. users: [this.user_id2]
  176. }
  177. }
  178. ]
  179. })
  180. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  181. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  182. })
  183. it('should handle a binary diff marker', async function() {
  184. const diff = await this.HistoryManager.promises.injectUserDetails({
  185. diff: { binary: true }
  186. })
  187. expect(diff.diff.binary).to.be.true
  188. })
  189. })
  190. describe('with a list of updates', function() {
  191. it('should turn user_ids into user objects', async function() {
  192. const updates = await this.HistoryManager.promises.injectUserDetails({
  193. updates: [
  194. {
  195. fromV: 5,
  196. toV: 8,
  197. meta: {
  198. users: [this.user_id1]
  199. }
  200. },
  201. {
  202. fromV: 4,
  203. toV: 5,
  204. meta: {
  205. users: [this.user_id2]
  206. }
  207. }
  208. ]
  209. })
  210. expect(updates.updates[0].meta.users).to.deep.equal([this.user1_view])
  211. expect(updates.updates[1].meta.users).to.deep.equal([this.user2_view])
  212. })
  213. it('should leave user objects', async function() {
  214. const updates = await this.HistoryManager.promises.injectUserDetails({
  215. updates: [
  216. {
  217. fromV: 5,
  218. toV: 8,
  219. meta: {
  220. users: [this.user1_view]
  221. }
  222. },
  223. {
  224. fromV: 4,
  225. toV: 5,
  226. meta: {
  227. users: [this.user_id2]
  228. }
  229. }
  230. ]
  231. })
  232. expect(updates.updates[0].meta.users).to.deep.equal([this.user1_view])
  233. expect(updates.updates[1].meta.users).to.deep.equal([this.user2_view])
  234. })
  235. })
  236. })
  237. describe('deleteProject', function() {
  238. it('should call the project-history service', async function() {
  239. const projectId = new ObjectId()
  240. await this.HistoryManager.promises.deleteProject(projectId)
  241. expect(this.request.delete).to.have.been.calledWith(
  242. `${this.projectHistoryUrl}/project/${projectId}`
  243. )
  244. })
  245. })
  246. })