SendingAMessageTests.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. node/no-deprecated-api,
  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 { ObjectId } = require('../../../app/js/mongodb')
  15. const { expect } = require('chai')
  16. const ChatClient = require('./helpers/ChatClient')
  17. const ChatApp = require('./helpers/ChatApp')
  18. describe('Sending a message', function () {
  19. before(function (done) {
  20. return ChatApp.ensureRunning(done)
  21. })
  22. describe('globally', function () {
  23. before(function (done) {
  24. this.project_id = ObjectId().toString()
  25. this.user_id = ObjectId().toString()
  26. this.content = 'global message'
  27. return ChatClient.sendGlobalMessage(
  28. this.project_id,
  29. this.user_id,
  30. this.content,
  31. (error, response, body) => {
  32. expect(error).to.be.null
  33. expect(response.statusCode).to.equal(201)
  34. expect(body.content).to.equal(this.content)
  35. expect(body.user_id).to.equal(this.user_id)
  36. expect(body.room_id).to.equal(this.project_id)
  37. return done()
  38. }
  39. )
  40. })
  41. return it('should then list the message in the project messages', function (done) {
  42. return ChatClient.getGlobalMessages(
  43. this.project_id,
  44. (error, response, messages) => {
  45. expect(error).to.be.null
  46. expect(response.statusCode).to.equal(200)
  47. expect(messages.length).to.equal(1)
  48. expect(messages[0].content).to.equal(this.content)
  49. return done()
  50. }
  51. )
  52. })
  53. })
  54. describe('to a thread', function () {
  55. before(function (done) {
  56. this.project_id = ObjectId().toString()
  57. this.user_id = ObjectId().toString()
  58. this.thread_id = ObjectId().toString()
  59. this.content = 'thread message'
  60. return ChatClient.sendMessage(
  61. this.project_id,
  62. this.thread_id,
  63. this.user_id,
  64. this.content,
  65. (error, response, body) => {
  66. expect(error).to.be.null
  67. expect(response.statusCode).to.equal(201)
  68. expect(body.content).to.equal(this.content)
  69. expect(body.user_id).to.equal(this.user_id)
  70. expect(body.room_id).to.equal(this.project_id)
  71. return done()
  72. }
  73. )
  74. })
  75. it('should then list the message in the threads', function (done) {
  76. return ChatClient.getThreads(
  77. this.project_id,
  78. (error, response, threads) => {
  79. expect(error).to.be.null
  80. expect(response.statusCode).to.equal(200)
  81. expect(threads[this.thread_id].messages.length).to.equal(1)
  82. expect(threads[this.thread_id].messages[0].content).to.equal(
  83. this.content
  84. )
  85. return done()
  86. }
  87. )
  88. })
  89. return it('should not appear in the global messages', function (done) {
  90. return ChatClient.getGlobalMessages(
  91. this.project_id,
  92. (error, response, messages) => {
  93. expect(error).to.be.null
  94. expect(response.statusCode).to.equal(200)
  95. expect(messages.length).to.equal(0)
  96. return done()
  97. }
  98. )
  99. })
  100. })
  101. return describe('failure cases', function () {
  102. before(function () {
  103. this.project_id = ObjectId().toString()
  104. this.user_id = ObjectId().toString()
  105. return (this.thread_id = ObjectId().toString())
  106. })
  107. describe('with a malformed user_id', function () {
  108. return it('should return a graceful error', function (done) {
  109. return ChatClient.sendMessage(
  110. this.project_id,
  111. this.thread_id,
  112. 'malformed-user',
  113. 'content',
  114. (error, response, body) => {
  115. expect(response.statusCode).to.equal(400)
  116. expect(body).to.equal('Invalid user_id')
  117. return done()
  118. }
  119. )
  120. })
  121. })
  122. describe('with a malformed project_id', function () {
  123. return it('should return a graceful error', function (done) {
  124. return ChatClient.sendMessage(
  125. 'malformed-project',
  126. this.thread_id,
  127. this.user_id,
  128. 'content',
  129. (error, response, body) => {
  130. expect(response.statusCode).to.equal(400)
  131. expect(body).to.equal('Invalid project_id')
  132. return done()
  133. }
  134. )
  135. })
  136. })
  137. describe('with a malformed thread_id', function () {
  138. return it('should return a graceful error', function (done) {
  139. return ChatClient.sendMessage(
  140. this.project_id,
  141. 'malformed-thread-id',
  142. this.user_id,
  143. 'content',
  144. (error, response, body) => {
  145. expect(response.statusCode).to.equal(400)
  146. expect(body).to.equal('Invalid thread_id')
  147. return done()
  148. }
  149. )
  150. })
  151. })
  152. describe('with no content', function () {
  153. return it('should return a graceful error', function (done) {
  154. return ChatClient.sendMessage(
  155. this.project_id,
  156. this.thread_id,
  157. this.user_id,
  158. null,
  159. (error, response, body) => {
  160. expect(response.statusCode).to.equal(400)
  161. expect(body).to.equal('No content provided')
  162. return done()
  163. }
  164. )
  165. })
  166. })
  167. return describe('with very long content', function () {
  168. return it('should return a graceful error', function (done) {
  169. const content = '-'.repeat(10 * 1024 + 1)
  170. return ChatClient.sendMessage(
  171. this.project_id,
  172. this.thread_id,
  173. this.user_id,
  174. content,
  175. (error, response, body) => {
  176. expect(response.statusCode).to.equal(400)
  177. expect(body).to.equal('Content too long (> 10240 bytes)')
  178. return done()
  179. }
  180. )
  181. })
  182. })
  183. })
  184. })