DestroyingAProjectTests.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. const db = ChatApp.db
  6. async function getMessage(messageId) {
  7. return await db.messages.findOne({
  8. _id: new ObjectId(messageId),
  9. })
  10. }
  11. describe('Destroying a project', async function () {
  12. const projectId = new ObjectId().toString()
  13. const userId = new ObjectId().toString()
  14. before(async function () {
  15. await ChatApp.ensureRunning()
  16. })
  17. describe('with a project that has threads and messages', async function () {
  18. const threadId = new ObjectId().toString()
  19. before(async function () {
  20. const { response } = await ChatClient.sendMessage(
  21. projectId,
  22. threadId,
  23. userId,
  24. 'destroyed thread message'
  25. )
  26. expect(response.statusCode).to.equal(201)
  27. this.threadMessageId = response.body.id
  28. const { response: response2 } = await ChatClient.sendGlobalMessage(
  29. projectId,
  30. userId,
  31. 'destroyed global message'
  32. )
  33. expect(response2.statusCode).to.equal(201)
  34. this.globalThreadMessageId = response2.body.id
  35. const threadRooms = await db.rooms
  36. .find({ project_id: new ObjectId(projectId) })
  37. .toArray()
  38. expect(threadRooms.length).to.equal(2)
  39. const threadMessage = await getMessage(this.threadMessageId)
  40. expect(threadMessage).to.exist
  41. const globalThreadMessage = await getMessage(this.globalThreadMessageId)
  42. expect(globalThreadMessage).to.exist
  43. const { response: responseDestroy } =
  44. await ChatClient.destroyProject(projectId)
  45. expect(responseDestroy.statusCode).to.equal(204)
  46. })
  47. it('should remove the messages and threads from the database', async function () {
  48. const threadRooms = await db.rooms
  49. .find({ project_id: new ObjectId(projectId) })
  50. .toArray()
  51. expect(threadRooms.length).to.equal(0)
  52. const threadMessage = await getMessage(this.threadMessageId)
  53. expect(threadMessage).to.be.null
  54. const globalThreadMessage = await getMessage(this.globalThreadMessageId)
  55. expect(globalThreadMessage).to.be.null
  56. })
  57. })
  58. })