SendingAMessageTests.js 4.7 KB

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