SendingAMessageTests.js 6.1 KB

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