ChatApiHandlerTests.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 assert = require('assert')
  15. const path = require('path')
  16. const sinon = require('sinon')
  17. const modulePath = path.join(
  18. __dirname,
  19. '../../../../app/src/Features/Chat/ChatApiHandler'
  20. )
  21. const { expect } = require('chai')
  22. describe('ChatApiHandler', function () {
  23. beforeEach(function () {
  24. this.settings = {
  25. apis: {
  26. chat: {
  27. internal_url: 'chat.overleaf.env',
  28. },
  29. },
  30. }
  31. this.request = sinon.stub()
  32. this.ChatApiHandler = SandboxedModule.require(modulePath, {
  33. requires: {
  34. '@overleaf/settings': this.settings,
  35. request: this.request,
  36. },
  37. })
  38. this.project_id = '3213213kl12j'
  39. this.user_id = '2k3jlkjs9'
  40. this.content = 'my message here'
  41. return (this.callback = sinon.stub())
  42. })
  43. describe('sendGlobalMessage', function () {
  44. describe('successfully', function () {
  45. beforeEach(function () {
  46. this.message = { mock: 'message' }
  47. this.request.callsArgWith(1, null, { statusCode: 200 }, this.message)
  48. return this.ChatApiHandler.sendGlobalMessage(
  49. this.project_id,
  50. this.user_id,
  51. this.content,
  52. this.callback
  53. )
  54. })
  55. it('should post the data to the chat api', function () {
  56. return this.request
  57. .calledWith({
  58. url: `${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages`,
  59. method: 'POST',
  60. json: {
  61. content: this.content,
  62. user_id: this.user_id,
  63. },
  64. })
  65. .should.equal(true)
  66. })
  67. it('should return the message from the post', function () {
  68. return this.callback.calledWith(null, this.message).should.equal(true)
  69. })
  70. })
  71. describe('with a non-success status code', function () {
  72. beforeEach(function () {
  73. this.request.callsArgWith(1, null, { statusCode: 500 })
  74. return this.ChatApiHandler.sendGlobalMessage(
  75. this.project_id,
  76. this.user_id,
  77. this.content,
  78. this.callback
  79. )
  80. })
  81. it('should return an error', function () {
  82. expect(this.callback).to.have.been.calledWith(
  83. sinon.match.instanceOf(Error).and(sinon.match.has('statusCode', 500))
  84. )
  85. })
  86. })
  87. })
  88. describe('getGlobalMessages', function () {
  89. beforeEach(function () {
  90. this.messages = [{ mock: 'message' }]
  91. this.limit = 30
  92. return (this.before = '1234')
  93. })
  94. describe('successfully', function () {
  95. beforeEach(function () {
  96. this.request.callsArgWith(1, null, { statusCode: 200 }, this.messages)
  97. return this.ChatApiHandler.getGlobalMessages(
  98. this.project_id,
  99. this.limit,
  100. this.before,
  101. this.callback
  102. )
  103. })
  104. it('should make get request for room to chat api', function () {
  105. return this.request
  106. .calledWith({
  107. method: 'GET',
  108. url: `${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages`,
  109. qs: {
  110. limit: this.limit,
  111. before: this.before,
  112. },
  113. json: true,
  114. })
  115. .should.equal(true)
  116. })
  117. it('should return the messages from the request', function () {
  118. return this.callback.calledWith(null, this.messages).should.equal(true)
  119. })
  120. })
  121. describe('with failure error code', function () {
  122. beforeEach(function () {
  123. this.request.callsArgWith(1, null, { statusCode: 500 }, null)
  124. return this.ChatApiHandler.getGlobalMessages(
  125. this.project_id,
  126. this.limit,
  127. this.before,
  128. this.callback
  129. )
  130. })
  131. it('should return an error', function () {
  132. expect(this.callback).to.have.been.calledWith(
  133. sinon.match.instanceOf(Error).and(sinon.match.has('statusCode', 500))
  134. )
  135. })
  136. })
  137. })
  138. })