ReferencesControllerTests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. const SandboxedModule = require('sandboxed-module')
  14. const sinon = require('sinon')
  15. const { assert } = require('chai')
  16. const modulePath =
  17. '../../../../app/src/Features/References/ReferencesController'
  18. const MockRequest = require('../helpers/MockRequest')
  19. const MockResponse = require('../helpers/MockResponse')
  20. describe('ReferencesController', function () {
  21. beforeEach(function () {
  22. this.projectId = '2222'
  23. this.controller = SandboxedModule.require(modulePath, {
  24. requires: {
  25. '@overleaf/settings': (this.settings = {
  26. apis: { web: { url: 'http://some.url' } },
  27. }),
  28. './ReferencesHandler': (this.ReferencesHandler = {
  29. index: sinon.stub(),
  30. indexAll: sinon.stub(),
  31. }),
  32. '../Editor/EditorRealTimeController': (this.EditorRealTimeController = {
  33. emitToRoom: sinon.stub(),
  34. }),
  35. },
  36. })
  37. this.req = new MockRequest()
  38. this.req.params.Project_id = this.projectId
  39. this.req.body = {
  40. docIds: (this.docIds = ['aaa', 'bbb']),
  41. shouldBroadcast: false,
  42. }
  43. this.res = new MockResponse()
  44. this.res.json = sinon.stub()
  45. this.res.sendStatus = sinon.stub()
  46. this.next = sinon.stub()
  47. return (this.fakeResponseData = {
  48. projectId: this.projectId,
  49. keys: ['one', 'two', 'three'],
  50. })
  51. })
  52. describe('indexAll', function () {
  53. beforeEach(function () {
  54. this.req.body = { shouldBroadcast: false }
  55. this.ReferencesHandler.indexAll.callsArgWith(
  56. 1,
  57. null,
  58. this.fakeResponseData
  59. )
  60. return (this.call = callback => {
  61. this.controller.indexAll(this.req, this.res, this.next)
  62. return callback()
  63. })
  64. })
  65. it('should not produce an error', function (done) {
  66. return this.call(() => {
  67. this.res.sendStatus.callCount.should.equal(0)
  68. this.res.sendStatus.calledWith(500).should.equal(false)
  69. this.res.sendStatus.calledWith(400).should.equal(false)
  70. return done()
  71. })
  72. })
  73. it('should return data', function (done) {
  74. return this.call(() => {
  75. this.res.json.callCount.should.equal(1)
  76. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  77. return done()
  78. })
  79. })
  80. it('should call ReferencesHandler.indexAll', function (done) {
  81. return this.call(() => {
  82. this.ReferencesHandler.indexAll.callCount.should.equal(1)
  83. this.ReferencesHandler.indexAll
  84. .calledWith(this.projectId)
  85. .should.equal(true)
  86. return done()
  87. })
  88. })
  89. describe('when shouldBroadcast is true', function () {
  90. beforeEach(function () {
  91. this.ReferencesHandler.index.callsArgWith(
  92. 2,
  93. null,
  94. this.fakeResponseData
  95. )
  96. return (this.req.body.shouldBroadcast = true)
  97. })
  98. it('should call EditorRealTimeController.emitToRoom', function (done) {
  99. return this.call(() => {
  100. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  101. return done()
  102. })
  103. })
  104. it('should not produce an error', function (done) {
  105. return this.call(() => {
  106. this.res.sendStatus.callCount.should.equal(0)
  107. this.res.sendStatus.calledWith(500).should.equal(false)
  108. this.res.sendStatus.calledWith(400).should.equal(false)
  109. return done()
  110. })
  111. })
  112. it('should still return data', function (done) {
  113. return this.call(() => {
  114. this.res.json.callCount.should.equal(1)
  115. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  116. return done()
  117. })
  118. })
  119. })
  120. describe('when shouldBroadcast is false', function () {
  121. beforeEach(function () {
  122. this.ReferencesHandler.index.callsArgWith(
  123. 2,
  124. null,
  125. this.fakeResponseData
  126. )
  127. return (this.req.body.shouldBroadcast = false)
  128. })
  129. it('should not call EditorRealTimeController.emitToRoom', function (done) {
  130. return this.call(() => {
  131. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  132. return done()
  133. })
  134. })
  135. it('should not produce an error', function (done) {
  136. return this.call(() => {
  137. this.res.sendStatus.callCount.should.equal(0)
  138. this.res.sendStatus.calledWith(500).should.equal(false)
  139. this.res.sendStatus.calledWith(400).should.equal(false)
  140. return done()
  141. })
  142. })
  143. it('should still return data', function (done) {
  144. return this.call(() => {
  145. this.res.json.callCount.should.equal(1)
  146. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  147. return done()
  148. })
  149. })
  150. })
  151. })
  152. describe('there is no data', function () {
  153. beforeEach(function () {
  154. this.ReferencesHandler.indexAll.callsArgWith(1)
  155. return (this.call = callback => {
  156. this.controller.indexAll(this.req, this.res, this.next)
  157. return callback()
  158. })
  159. })
  160. it('should not call EditorRealTimeController.emitToRoom', function (done) {
  161. return this.call(() => {
  162. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  163. return done()
  164. })
  165. })
  166. it('should not produce an error', function (done) {
  167. return this.call(() => {
  168. this.res.sendStatus.callCount.should.equal(0)
  169. this.res.sendStatus.calledWith(500).should.equal(false)
  170. this.res.sendStatus.calledWith(400).should.equal(false)
  171. return done()
  172. })
  173. })
  174. it('should send a response with an empty keys list', function (done) {
  175. return this.call(() => {
  176. this.res.json.called.should.equal(true)
  177. this.res.json
  178. .calledWith({ projectId: this.projectId, keys: [] })
  179. .should.equal(true)
  180. return done()
  181. })
  182. })
  183. })
  184. describe('index', function () {
  185. beforeEach(function () {
  186. return (this.call = callback => {
  187. this.controller.index(this.req, this.res, this.next)
  188. return callback()
  189. })
  190. })
  191. describe('with docIds as an array and shouldBroadcast as false', function () {
  192. beforeEach(function () {
  193. return this.ReferencesHandler.index.callsArgWith(
  194. 2,
  195. null,
  196. this.fakeResponseData
  197. )
  198. })
  199. it('should call ReferencesHandler.index', function (done) {
  200. return this.call(() => {
  201. this.ReferencesHandler.index.callCount.should.equal(1)
  202. this.ReferencesHandler.index
  203. .calledWith(this.projectId, this.docIds)
  204. .should.equal(true)
  205. return done()
  206. })
  207. })
  208. it('should return data', function (done) {
  209. return this.call(() => {
  210. this.res.json.callCount.should.equal(1)
  211. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  212. return done()
  213. })
  214. })
  215. it('should not produce an error', function (done) {
  216. return this.call(() => {
  217. this.res.sendStatus.callCount.should.equal(0)
  218. this.res.sendStatus.calledWith(500).should.equal(false)
  219. this.res.sendStatus.calledWith(400).should.equal(false)
  220. return done()
  221. })
  222. })
  223. it('should not call EditorRealTimController.emitToRoom', function (done) {
  224. return this.call(() => {
  225. this.EditorRealTimeController.emitToRoom.callCount.should.equal(0)
  226. return done()
  227. })
  228. })
  229. describe('when ReferencesHandler.index produces an error', function () {
  230. beforeEach(function () {
  231. return this.ReferencesHandler.index.callsArgWith(
  232. 2,
  233. new Error('woops'),
  234. null
  235. )
  236. })
  237. it('should produce an error response', function (done) {
  238. return this.call(() => {
  239. this.next.callCount.should.equal(1)
  240. this.next
  241. .calledWith(sinon.match.instanceOf(Error))
  242. .should.equal(true)
  243. return done()
  244. })
  245. })
  246. })
  247. })
  248. describe('when shouldBroadcast is true', function () {
  249. beforeEach(function () {
  250. this.ReferencesHandler.index.callsArgWith(
  251. 2,
  252. null,
  253. this.fakeResponseData
  254. )
  255. return (this.req.body.shouldBroadcast = true)
  256. })
  257. it('should call EditorRealTimeController.emitToRoom', function (done) {
  258. return this.call(() => {
  259. this.EditorRealTimeController.emitToRoom.callCount.should.equal(1)
  260. return done()
  261. })
  262. })
  263. it('should not produce an error', function (done) {
  264. return this.call(() => {
  265. this.res.sendStatus.callCount.should.equal(0)
  266. this.res.sendStatus.calledWith(500).should.equal(false)
  267. this.res.sendStatus.calledWith(400).should.equal(false)
  268. return done()
  269. })
  270. })
  271. it('should still return data', function (done) {
  272. return this.call(() => {
  273. this.res.json.callCount.should.equal(1)
  274. this.res.json.calledWith(this.fakeResponseData).should.equal(true)
  275. return done()
  276. })
  277. })
  278. })
  279. describe('with missing docIds', function () {
  280. beforeEach(function () {
  281. return delete this.req.body.docIds
  282. })
  283. it('should produce an error response', function (done) {
  284. return this.call(() => {
  285. this.res.sendStatus.callCount.should.equal(1)
  286. this.res.sendStatus.calledWith(400).should.equal(true)
  287. return done()
  288. })
  289. })
  290. it('should not call ReferencesHandler.index', function (done) {
  291. return this.call(() => {
  292. this.ReferencesHandler.index.callCount.should.equal(0)
  293. return done()
  294. })
  295. })
  296. })
  297. describe('with invalid docIds', function () {
  298. beforeEach(function () {
  299. return (this.req.body.docIds = 42)
  300. })
  301. it('should produce an error response', function (done) {
  302. return this.call(() => {
  303. this.res.sendStatus.callCount.should.equal(1)
  304. this.res.sendStatus.calledWith(400).should.equal(true)
  305. return done()
  306. })
  307. })
  308. it('should not call ReferencesHandler.index', function (done) {
  309. return this.call(() => {
  310. this.ReferencesHandler.index.callCount.should.equal(0)
  311. return done()
  312. })
  313. })
  314. })
  315. })
  316. })