ChatControllerTests.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const SandboxedModule = require('sandboxed-module')
  15. const assert = require('assert')
  16. const path = require('path')
  17. const sinon = require('sinon')
  18. const modulePath = path.join(
  19. __dirname,
  20. '../../../../app/src/Features/Chat/ChatController'
  21. )
  22. describe('ChatController', function () {
  23. beforeEach(function () {
  24. this.user_id = 'mock-user-id'
  25. this.settings = {}
  26. this.ChatApiHandler = {}
  27. this.ChatManager = {}
  28. this.EditorRealTimeController = { emitToRoom: sinon.stub() }
  29. this.SessionManager = {
  30. getLoggedInUserId: sinon.stub().returns(this.user_id),
  31. }
  32. this.ChatController = SandboxedModule.require(modulePath, {
  33. requires: {
  34. '@overleaf/settings': this.settings,
  35. './ChatApiHandler': this.ChatApiHandler,
  36. './ChatManager': this.ChatManager,
  37. '../Editor/EditorRealTimeController': this.EditorRealTimeController,
  38. '../Authentication/SessionManager': this.SessionManager,
  39. '../User/UserInfoManager': (this.UserInfoManager = {}),
  40. '../User/UserInfoController': (this.UserInfoController = {}),
  41. },
  42. })
  43. this.req = {
  44. params: {
  45. project_id: this.project_id,
  46. },
  47. }
  48. this.res = {
  49. json: sinon.stub(),
  50. send: sinon.stub(),
  51. sendStatus: sinon.stub(),
  52. }
  53. })
  54. describe('sendMessage', function () {
  55. beforeEach(function () {
  56. this.req.body = { content: (this.content = 'message-content') }
  57. this.UserInfoManager.getPersonalInfo = sinon
  58. .stub()
  59. .yields(null, (this.user = { unformatted: 'user' }))
  60. this.UserInfoController.formatPersonalInfo = sinon
  61. .stub()
  62. .returns((this.formatted_user = { formatted: 'user' }))
  63. this.ChatApiHandler.sendGlobalMessage = sinon
  64. .stub()
  65. .yields(
  66. null,
  67. (this.message = { mock: 'message', user_id: this.user_id })
  68. )
  69. return this.ChatController.sendMessage(this.req, this.res)
  70. })
  71. it('should look up the user', function () {
  72. return this.UserInfoManager.getPersonalInfo
  73. .calledWith(this.user_id)
  74. .should.equal(true)
  75. })
  76. it('should format and inject the user into the message', function () {
  77. this.UserInfoController.formatPersonalInfo
  78. .calledWith(this.user)
  79. .should.equal(true)
  80. return this.message.user.should.deep.equal(this.formatted_user)
  81. })
  82. it('should tell the chat handler about the message', function () {
  83. return this.ChatApiHandler.sendGlobalMessage
  84. .calledWith(this.project_id, this.user_id, this.content)
  85. .should.equal(true)
  86. })
  87. it('should tell the editor real time controller about the update with the data from the chat handler', function () {
  88. return this.EditorRealTimeController.emitToRoom
  89. .calledWith(this.project_id, 'new-chat-message', this.message)
  90. .should.equal(true)
  91. })
  92. it('should return a 204 status code', function () {
  93. return this.res.sendStatus.calledWith(204).should.equal(true)
  94. })
  95. })
  96. describe('getMessages', function () {
  97. beforeEach(function () {
  98. this.req.query = {
  99. limit: (this.limit = '30'),
  100. before: (this.before = '12345'),
  101. }
  102. this.ChatManager.injectUserInfoIntoThreads = sinon.stub().yields()
  103. this.ChatApiHandler.getGlobalMessages = sinon
  104. .stub()
  105. .yields(null, (this.messages = ['mock', 'messages']))
  106. return this.ChatController.getMessages(this.req, this.res)
  107. })
  108. it('should ask the chat handler about the request', function () {
  109. return this.ChatApiHandler.getGlobalMessages
  110. .calledWith(this.project_id, this.limit, this.before)
  111. .should.equal(true)
  112. })
  113. it('should return the messages', function () {
  114. return this.res.json.calledWith(this.messages).should.equal(true)
  115. })
  116. })
  117. })