SendingAMessageTests.js 5.8 KB

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