ChatApiHandlerTests.js 4.4 KB

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