HistoryControllerTests.js 11 KB

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