ReferencesControllerTests.js 11 KB

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