ChatApiHandlerTests.js 4.5 KB

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