SendingAMessageTests.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { ObjectId } from '../../../app/js/mongodb.js'
  2. import { expect } from 'chai'
  3. import * as ChatClient from './helpers/ChatClient.js'
  4. import * as ChatApp from './helpers/ChatApp.js'
  5. describe('Sending a message', async function () {
  6. before(async function () {
  7. await ChatApp.ensureRunning()
  8. })
  9. describe('globally', async function () {
  10. const projectId = ObjectId().toString()
  11. const userId = ObjectId().toString()
  12. const content = 'global message'
  13. before(async function () {
  14. const { response, body } = await ChatClient.sendGlobalMessage(
  15. projectId,
  16. userId,
  17. content
  18. )
  19. expect(response.statusCode).to.equal(201)
  20. expect(body.content).to.equal(content)
  21. expect(body.user_id).to.equal(userId)
  22. expect(body.room_id).to.equal(projectId)
  23. })
  24. it('should then list the message in the project messages', async function () {
  25. const { response, body: messages } = await ChatClient.getGlobalMessages(
  26. projectId
  27. )
  28. expect(response.statusCode).to.equal(200)
  29. expect(messages.length).to.equal(1)
  30. expect(messages[0].content).to.equal(content)
  31. })
  32. })
  33. describe('to a thread', async function () {
  34. const projectId = ObjectId().toString()
  35. const userId = ObjectId().toString()
  36. const threadId = ObjectId().toString()
  37. const content = 'thread message'
  38. before(async function () {
  39. const { response, body } = await ChatClient.sendMessage(
  40. projectId,
  41. threadId,
  42. userId,
  43. content
  44. )
  45. expect(response.statusCode).to.equal(201)
  46. expect(body.content).to.equal(content)
  47. expect(body.user_id).to.equal(userId)
  48. expect(body.room_id).to.equal(projectId)
  49. })
  50. it('should then list the message in the threads', async function () {
  51. const { response, body: threads } = await ChatClient.getThreads(projectId)
  52. expect(response.statusCode).to.equal(200)
  53. expect(threads[threadId].messages.length).to.equal(1)
  54. expect(threads[threadId].messages[0].content).to.equal(content)
  55. })
  56. it('should not appear in the global messages', async function () {
  57. const { response, body: messages } = await ChatClient.getGlobalMessages(
  58. projectId
  59. )
  60. expect(response.statusCode).to.equal(200)
  61. expect(messages.length).to.equal(0)
  62. })
  63. })
  64. describe('failure cases', async function () {
  65. const projectId = ObjectId().toString()
  66. const userId = ObjectId().toString()
  67. const threadId = ObjectId().toString()
  68. describe('with a malformed userId', async function () {
  69. it('should return a graceful error', async function () {
  70. const { response, body } = await ChatClient.sendMessage(
  71. projectId,
  72. threadId,
  73. 'malformed-user',
  74. 'content'
  75. )
  76. expect(response.statusCode).to.equal(400)
  77. expect(body).to.equal('Invalid userId')
  78. })
  79. })
  80. describe('with a malformed projectId', async function () {
  81. it('should return a graceful error', async function () {
  82. const { response, body } = await ChatClient.sendMessage(
  83. 'malformed-project',
  84. threadId,
  85. userId,
  86. 'content'
  87. )
  88. expect(response.statusCode).to.equal(400)
  89. expect(body).to.equal('Invalid projectId')
  90. })
  91. })
  92. describe('with a malformed threadId', async function () {
  93. it('should return a graceful error', async function () {
  94. const { response, body } = await ChatClient.sendMessage(
  95. projectId,
  96. 'malformed-thread-id',
  97. userId,
  98. 'content'
  99. )
  100. expect(response.statusCode).to.equal(400)
  101. expect(body).to.equal('Invalid threadId')
  102. })
  103. })
  104. describe('with no content', async function () {
  105. it('should return a graceful error', async function () {
  106. const { response, body } = await ChatClient.sendMessage(
  107. projectId,
  108. threadId,
  109. userId,
  110. null
  111. )
  112. expect(response.statusCode).to.equal(400)
  113. // Exegesis is responding with validation errors. I can´t find a way to choose the validation error yet.
  114. // expect(body).to.equal('No content provided')
  115. expect(body.message).to.equal('Validation errors')
  116. })
  117. })
  118. describe('with very long content', async function () {
  119. it('should return a graceful error', async function () {
  120. const content = '-'.repeat(10 * 1024 + 1)
  121. const { response, body } = await ChatClient.sendMessage(
  122. projectId,
  123. threadId,
  124. userId,
  125. content
  126. )
  127. expect(response.statusCode).to.equal(400)
  128. expect(body).to.equal('Content too long (> 10240 bytes)')
  129. })
  130. })
  131. })
  132. })