ChatApiHandlerTests.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/${this.project_id}/messages`,
  68. method: 'POST',
  69. json: {
  70. content: this.content,
  71. user_id: this.user_id
  72. }
  73. })
  74. .should.equal(true)
  75. })
  76. it('should return the message from the post', function() {
  77. return this.callback.calledWith(null, this.message).should.equal(true)
  78. })
  79. })
  80. describe('with a non-success status code', function() {
  81. beforeEach(function() {
  82. this.request.callsArgWith(1, null, { statusCode: 500 })
  83. return this.ChatApiHandler.sendGlobalMessage(
  84. this.project_id,
  85. this.user_id,
  86. this.content,
  87. this.callback
  88. )
  89. })
  90. it('should return an error', function() {
  91. expect(this.callback).to.have.been.calledWith(
  92. sinon.match.instanceOf(Error).and(sinon.match.has('statusCode', 500))
  93. )
  94. })
  95. })
  96. })
  97. describe('getGlobalMessages', function() {
  98. beforeEach(function() {
  99. this.messages = [{ mock: 'message' }]
  100. this.limit = 30
  101. return (this.before = '1234')
  102. })
  103. describe('successfully', function() {
  104. beforeEach(function() {
  105. this.request.callsArgWith(1, null, { statusCode: 200 }, this.messages)
  106. return this.ChatApiHandler.getGlobalMessages(
  107. this.project_id,
  108. this.limit,
  109. this.before,
  110. this.callback
  111. )
  112. })
  113. it('should make get request for room to chat api', function() {
  114. return this.request
  115. .calledWith({
  116. method: 'GET',
  117. url: `${this.settings.apis.chat.internal_url}/project/${this.project_id}/messages`,
  118. qs: {
  119. limit: this.limit,
  120. before: this.before
  121. },
  122. json: true
  123. })
  124. .should.equal(true)
  125. })
  126. it('should return the messages from the request', function() {
  127. return this.callback.calledWith(null, this.messages).should.equal(true)
  128. })
  129. })
  130. describe('with failure error code', function() {
  131. beforeEach(function() {
  132. this.request.callsArgWith(1, null, { statusCode: 500 }, null)
  133. return this.ChatApiHandler.getGlobalMessages(
  134. this.project_id,
  135. this.limit,
  136. this.before,
  137. this.callback
  138. )
  139. })
  140. it('should return an error', function() {
  141. expect(this.callback).to.have.been.calledWith(
  142. sinon.match.instanceOf(Error).and(sinon.match.has('statusCode', 500))
  143. )
  144. })
  145. })
  146. })
  147. })