HistoryManagerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const chai = require('chai')
  13. chai.should()
  14. const { expect } = chai
  15. const sinon = require('sinon')
  16. const modulePath = '../../../../app/src/Features/History/HistoryManager'
  17. const SandboxedModule = require('sandboxed-module')
  18. describe('HistoryManager', function() {
  19. beforeEach(function() {
  20. this.callback = sinon.stub()
  21. this.user_id = 'user-id-123'
  22. this.AuthenticationController = {
  23. getLoggedInUserId: sinon.stub().returns(this.user_id)
  24. }
  25. this.HistoryManager = SandboxedModule.require(modulePath, {
  26. globals: {
  27. console: console
  28. },
  29. requires: {
  30. request: (this.request = sinon.stub()),
  31. 'settings-sharelatex': (this.settings = {}),
  32. '../User/UserGetter': (this.UserGetter = {})
  33. }
  34. })
  35. return (this.settings.apis = {
  36. trackchanges: {
  37. enabled: false,
  38. url: 'http://trackchanges.example.com'
  39. },
  40. project_history: {
  41. url: 'http://project_history.example.com'
  42. }
  43. })
  44. })
  45. describe('initializeProject', function() {
  46. describe('with project history enabled', function() {
  47. beforeEach(function() {
  48. return (this.settings.apis.project_history.initializeHistoryForNewProjects = true)
  49. })
  50. describe('project history returns a successful response', function() {
  51. beforeEach(function() {
  52. this.overleaf_id = 1234
  53. this.res = { statusCode: 200 }
  54. this.body = JSON.stringify({ project: { id: this.overleaf_id } })
  55. this.request.post = sinon
  56. .stub()
  57. .callsArgWith(1, null, this.res, this.body)
  58. return this.HistoryManager.initializeProject(this.callback)
  59. })
  60. it('should call the project history api', function() {
  61. return this.request.post
  62. .calledWith({
  63. url: `${this.settings.apis.project_history.url}/project`
  64. })
  65. .should.equal(true)
  66. })
  67. it('should return the callback with the overleaf id', function() {
  68. return this.callback
  69. .calledWithExactly(null, { overleaf_id: this.overleaf_id })
  70. .should.equal(true)
  71. })
  72. })
  73. describe('project history returns a response without the project id', function() {
  74. beforeEach(function() {
  75. this.res = { statusCode: 200 }
  76. this.body = JSON.stringify({ project: {} })
  77. this.request.post = sinon
  78. .stub()
  79. .callsArgWith(1, null, this.res, this.body)
  80. return this.HistoryManager.initializeProject(this.callback)
  81. })
  82. it('should return the callback with an error', function() {
  83. return this.callback
  84. .calledWith(
  85. sinon.match.has(
  86. 'message',
  87. 'project-history did not provide an id'
  88. )
  89. )
  90. .should.equal(true)
  91. })
  92. })
  93. describe('project history returns a unsuccessful response', function() {
  94. beforeEach(function() {
  95. this.res = { statusCode: 404 }
  96. this.request.post = sinon.stub().callsArgWith(1, null, this.res)
  97. return this.HistoryManager.initializeProject(this.callback)
  98. })
  99. it('should return the callback with an error', function() {
  100. return this.callback
  101. .calledWith(
  102. sinon.match.has(
  103. 'message',
  104. 'project-history returned a non-success status code: 404'
  105. )
  106. )
  107. .should.equal(true)
  108. })
  109. })
  110. describe('project history errors', function() {
  111. beforeEach(function() {
  112. this.error = sinon.stub()
  113. this.request.post = sinon.stub().callsArgWith(1, this.error)
  114. return this.HistoryManager.initializeProject(this.callback)
  115. })
  116. it('should return the callback with the error', function() {
  117. return this.callback.calledWithExactly(this.error).should.equal(true)
  118. })
  119. })
  120. })
  121. describe('with project history disabled', function() {
  122. beforeEach(function() {
  123. this.settings.apis.project_history.initializeHistoryForNewProjects = false
  124. return this.HistoryManager.initializeProject(this.callback)
  125. })
  126. it('should return the callback', function() {
  127. return this.callback.calledWithExactly().should.equal(true)
  128. })
  129. })
  130. })
  131. describe('injectUserDetails', function() {
  132. beforeEach(function() {
  133. this.user1 = {
  134. _id: (this.user_id1 = '123456'),
  135. first_name: 'Jane',
  136. last_name: 'Doe',
  137. email: 'jane@example.com',
  138. overleaf: { id: 5011 }
  139. }
  140. this.user1_view = {
  141. id: this.user_id1,
  142. first_name: 'Jane',
  143. last_name: 'Doe',
  144. email: 'jane@example.com'
  145. }
  146. this.user2 = {
  147. _id: (this.user_id2 = 'abcdef'),
  148. first_name: 'John',
  149. last_name: 'Doe',
  150. email: 'john@example.com'
  151. }
  152. this.user2_view = {
  153. id: this.user_id2,
  154. first_name: 'John',
  155. last_name: 'Doe',
  156. email: 'john@example.com'
  157. }
  158. this.UserGetter.getUsersByV1Ids = sinon.stub().yields(null, [this.user1])
  159. return (this.UserGetter.getUsers = sinon
  160. .stub()
  161. .yields(null, [this.user1, this.user2]))
  162. })
  163. describe('with a diff', function() {
  164. it('should turn user_ids into user objects', function(done) {
  165. return this.HistoryManager.injectUserDetails(
  166. {
  167. diff: [
  168. {
  169. i: 'foo',
  170. meta: {
  171. users: [this.user_id1]
  172. }
  173. },
  174. {
  175. i: 'bar',
  176. meta: {
  177. users: [this.user_id2]
  178. }
  179. }
  180. ]
  181. },
  182. (error, diff) => {
  183. expect(error).to.be.null
  184. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  185. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  186. return done()
  187. }
  188. )
  189. })
  190. it('should handle v1 user ids', function(done) {
  191. return this.HistoryManager.injectUserDetails(
  192. {
  193. diff: [
  194. {
  195. i: 'foo',
  196. meta: {
  197. users: [5011]
  198. }
  199. },
  200. {
  201. i: 'bar',
  202. meta: {
  203. users: [this.user_id2]
  204. }
  205. }
  206. ]
  207. },
  208. (error, diff) => {
  209. expect(error).to.be.null
  210. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  211. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  212. return done()
  213. }
  214. )
  215. })
  216. it('should leave user objects', function(done) {
  217. return this.HistoryManager.injectUserDetails(
  218. {
  219. diff: [
  220. {
  221. i: 'foo',
  222. meta: {
  223. users: [this.user1_view]
  224. }
  225. },
  226. {
  227. i: 'bar',
  228. meta: {
  229. users: [this.user_id2]
  230. }
  231. }
  232. ]
  233. },
  234. (error, diff) => {
  235. expect(error).to.be.null
  236. expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
  237. expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
  238. return done()
  239. }
  240. )
  241. })
  242. })
  243. describe('with a list of updates', function() {
  244. it('should turn user_ids into user objects', function(done) {
  245. return this.HistoryManager.injectUserDetails(
  246. {
  247. updates: [
  248. {
  249. fromV: 5,
  250. toV: 8,
  251. meta: {
  252. users: [this.user_id1]
  253. }
  254. },
  255. {
  256. fromV: 4,
  257. toV: 5,
  258. meta: {
  259. users: [this.user_id2]
  260. }
  261. }
  262. ]
  263. },
  264. (error, updates) => {
  265. expect(error).to.be.null
  266. expect(updates.updates[0].meta.users).to.deep.equal([
  267. this.user1_view
  268. ])
  269. expect(updates.updates[1].meta.users).to.deep.equal([
  270. this.user2_view
  271. ])
  272. return done()
  273. }
  274. )
  275. })
  276. it('should leave user objects', function(done) {
  277. return this.HistoryManager.injectUserDetails(
  278. {
  279. updates: [
  280. {
  281. fromV: 5,
  282. toV: 8,
  283. meta: {
  284. users: [this.user1_view]
  285. }
  286. },
  287. {
  288. fromV: 4,
  289. toV: 5,
  290. meta: {
  291. users: [this.user_id2]
  292. }
  293. }
  294. ]
  295. },
  296. (error, updates) => {
  297. expect(error).to.be.null
  298. expect(updates.updates[0].meta.users).to.deep.equal([
  299. this.user1_view
  300. ])
  301. expect(updates.updates[1].meta.users).to.deep.equal([
  302. this.user2_view
  303. ])
  304. return done()
  305. }
  306. )
  307. })
  308. })
  309. })
  310. })