HistoryControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 sinon = require('sinon')
  13. const Errors = require('../../../../app/src/Features/Errors/Errors')
  14. const modulePath = '../../../../app/src/Features/History/HistoryController'
  15. const SandboxedModule = require('sandboxed-module')
  16. describe('HistoryController', function () {
  17. beforeEach(function () {
  18. this.callback = sinon.stub()
  19. this.user_id = 'user-id-123'
  20. this.SessionManager = {
  21. getLoggedInUserId: sinon.stub().returns(this.user_id),
  22. }
  23. this.HistoryController = SandboxedModule.require(modulePath, {
  24. requires: {
  25. request: (this.request = sinon.stub()),
  26. '@overleaf/settings': (this.settings = {}),
  27. '../Authentication/SessionManager': this.SessionManager,
  28. './HistoryManager': (this.HistoryManager = {}),
  29. '../Project/ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
  30. '../Project/ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {}),
  31. '../User/UserGetter': (this.UserGetter = {}),
  32. './RestoreManager': (this.RestoreManager = {}),
  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('selectHistoryApi', function () {
  46. beforeEach(function () {
  47. this.req = { url: '/mock/url', method: 'POST', params: {} }
  48. this.res = 'mock-res'
  49. return (this.next = sinon.stub())
  50. })
  51. describe('for a project with project history', function () {
  52. beforeEach(function () {
  53. this.ProjectDetailsHandler.getDetails = sinon
  54. .stub()
  55. .callsArgWith(1, null, {
  56. overleaf: { history: { id: 42, display: true } },
  57. })
  58. return this.HistoryController.selectHistoryApi(
  59. this.req,
  60. this.res,
  61. this.next
  62. )
  63. })
  64. it('should set the flag for project history to true', function () {
  65. return this.req.useProjectHistory.should.equal(true)
  66. })
  67. })
  68. describe('for any other project ', function () {
  69. beforeEach(function () {
  70. this.ProjectDetailsHandler.getDetails = sinon
  71. .stub()
  72. .callsArgWith(1, null, {})
  73. return this.HistoryController.selectHistoryApi(
  74. this.req,
  75. this.res,
  76. this.next
  77. )
  78. })
  79. it('should not set the flag for project history to false', function () {
  80. return this.req.useProjectHistory.should.equal(false)
  81. })
  82. })
  83. })
  84. describe('proxyToHistoryApi', function () {
  85. beforeEach(function () {
  86. this.req = { url: '/mock/url', method: 'POST' }
  87. this.res = 'mock-res'
  88. this.next = sinon.stub()
  89. this.proxy = {
  90. events: {},
  91. pipe: sinon.stub(),
  92. on(event, handler) {
  93. return (this.events[event] = handler)
  94. },
  95. }
  96. return this.request.returns(this.proxy)
  97. })
  98. describe('for a project with the project history flag', function () {
  99. beforeEach(function () {
  100. this.req.useProjectHistory = true
  101. return this.HistoryController.proxyToHistoryApi(
  102. this.req,
  103. this.res,
  104. this.next
  105. )
  106. })
  107. it('should get the user id', function () {
  108. return this.SessionManager.getLoggedInUserId
  109. .calledWith(this.req.session)
  110. .should.equal(true)
  111. })
  112. it('should call the project history api', function () {
  113. return this.request
  114. .calledWith({
  115. url: `${this.settings.apis.project_history.url}${this.req.url}`,
  116. method: this.req.method,
  117. headers: {
  118. 'X-User-Id': this.user_id,
  119. },
  120. })
  121. .should.equal(true)
  122. })
  123. it('should pipe the response to the client', function () {
  124. return this.proxy.pipe.calledWith(this.res).should.equal(true)
  125. })
  126. })
  127. describe('for a project without the project history flag', function () {
  128. beforeEach(function () {
  129. this.req.useProjectHistory = false
  130. return this.HistoryController.proxyToHistoryApi(
  131. this.req,
  132. this.res,
  133. this.next
  134. )
  135. })
  136. it('should get the user id', function () {
  137. return this.SessionManager.getLoggedInUserId
  138. .calledWith(this.req.session)
  139. .should.equal(true)
  140. })
  141. it('should call the track changes api', function () {
  142. return this.request
  143. .calledWith({
  144. url: `${this.settings.apis.trackchanges.url}${this.req.url}`,
  145. method: this.req.method,
  146. headers: {
  147. 'X-User-Id': this.user_id,
  148. },
  149. })
  150. .should.equal(true)
  151. })
  152. it('should pipe the response to the client', function () {
  153. return this.proxy.pipe.calledWith(this.res).should.equal(true)
  154. })
  155. })
  156. describe('with an error', function () {
  157. beforeEach(function () {
  158. this.HistoryController.proxyToHistoryApi(this.req, this.res, this.next)
  159. return this.proxy.events.error.call(
  160. this.proxy,
  161. (this.error = new Error('oops'))
  162. )
  163. })
  164. it('should pass the error up the call chain', function () {
  165. return this.next.calledWith(this.error).should.equal(true)
  166. })
  167. })
  168. })
  169. describe('proxyToHistoryApiAndInjectUserDetails', function () {
  170. beforeEach(function () {
  171. this.req = { url: '/mock/url', method: 'POST' }
  172. this.res = { json: sinon.stub() }
  173. this.next = sinon.stub()
  174. this.request.yields(null, { statusCode: 200 }, (this.data = 'mock-data'))
  175. return (this.HistoryManager.injectUserDetails = sinon
  176. .stub()
  177. .yields(null, (this.data_with_users = 'mock-injected-data')))
  178. })
  179. describe('for a project with the project history flag', function () {
  180. beforeEach(function () {
  181. this.req.useProjectHistory = true
  182. return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  183. this.req,
  184. this.res,
  185. this.next
  186. )
  187. })
  188. it('should get the user id', function () {
  189. return this.SessionManager.getLoggedInUserId
  190. .calledWith(this.req.session)
  191. .should.equal(true)
  192. })
  193. it('should call the project history api', function () {
  194. return this.request
  195. .calledWith({
  196. url: `${this.settings.apis.project_history.url}${this.req.url}`,
  197. method: this.req.method,
  198. json: true,
  199. headers: {
  200. 'X-User-Id': this.user_id,
  201. },
  202. })
  203. .should.equal(true)
  204. })
  205. it('should inject the user data', function () {
  206. return this.HistoryManager.injectUserDetails
  207. .calledWith(this.data)
  208. .should.equal(true)
  209. })
  210. it('should return the data with users to the client', function () {
  211. return this.res.json.calledWith(this.data_with_users).should.equal(true)
  212. })
  213. })
  214. describe('for a project without the project history flag', function () {
  215. beforeEach(function () {
  216. this.req.useProjectHistory = false
  217. return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  218. this.req,
  219. this.res,
  220. this.next
  221. )
  222. })
  223. it('should get the user id', function () {
  224. return this.SessionManager.getLoggedInUserId
  225. .calledWith(this.req.session)
  226. .should.equal(true)
  227. })
  228. it('should call the track changes api', function () {
  229. return this.request
  230. .calledWith({
  231. url: `${this.settings.apis.trackchanges.url}${this.req.url}`,
  232. method: this.req.method,
  233. json: true,
  234. headers: {
  235. 'X-User-Id': this.user_id,
  236. },
  237. })
  238. .should.equal(true)
  239. })
  240. it('should inject the user data', function () {
  241. return this.HistoryManager.injectUserDetails
  242. .calledWith(this.data)
  243. .should.equal(true)
  244. })
  245. it('should return the data with users to the client', function () {
  246. return this.res.json.calledWith(this.data_with_users).should.equal(true)
  247. })
  248. })
  249. })
  250. describe('proxyToHistoryApiAndInjectUserDetails (with the history API failing)', function () {
  251. beforeEach(function () {
  252. this.req = { url: '/mock/url', method: 'POST', useProjectHistory: true }
  253. this.res = { json: sinon.stub() }
  254. this.next = sinon.stub()
  255. this.request.yields(null, { statusCode: 500 }, (this.data = 'mock-data'))
  256. this.HistoryManager.injectUserDetails = sinon
  257. .stub()
  258. .yields(null, (this.data_with_users = 'mock-injected-data'))
  259. return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
  260. this.req,
  261. this.res,
  262. this.next
  263. )
  264. })
  265. it('should not inject the user data', function () {
  266. return this.HistoryManager.injectUserDetails
  267. .calledWith(this.data)
  268. .should.equal(false)
  269. })
  270. it('should not return the data with users to the client', function () {
  271. return this.res.json.calledWith(this.data_with_users).should.equal(false)
  272. })
  273. })
  274. describe('resyncProjectHistory', function () {
  275. describe('for a project without project-history enabled', function () {
  276. beforeEach(function () {
  277. this.project_id = 'mock-project-id'
  278. this.req = { params: { Project_id: this.project_id } }
  279. this.res = { sendStatus: sinon.stub() }
  280. this.next = sinon.stub()
  281. this.error = new Errors.ProjectHistoryDisabledError()
  282. this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  283. .stub()
  284. .yields(this.error)
  285. return this.HistoryController.resyncProjectHistory(
  286. this.req,
  287. this.res,
  288. this.next
  289. )
  290. })
  291. it('response with a 404', function () {
  292. return this.res.sendStatus.calledWith(404).should.equal(true)
  293. })
  294. })
  295. describe('for a project with project-history enabled', function () {
  296. beforeEach(function () {
  297. this.project_id = 'mock-project-id'
  298. this.req = { params: { Project_id: this.project_id } }
  299. this.res = { sendStatus: sinon.stub() }
  300. this.next = sinon.stub()
  301. this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
  302. .stub()
  303. .yields()
  304. return this.HistoryController.resyncProjectHistory(
  305. this.req,
  306. this.res,
  307. this.next
  308. )
  309. })
  310. it('resyncs the project', function () {
  311. return this.ProjectEntityUpdateHandler.resyncProjectHistory
  312. .calledWith(this.project_id)
  313. .should.equal(true)
  314. })
  315. it('responds with a 204', function () {
  316. return this.res.sendStatus.calledWith(204).should.equal(true)
  317. })
  318. })
  319. })
  320. })