2020-08-19 08:00:01 -04:00
|
|
|
const { ObjectId } = require('../../../app/js/mongodb')
|
2018-12-20 14:14:11 -05:00
|
|
|
const { expect } = require('chai')
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2018-12-20 14:14:11 -05:00
|
|
|
const ChatClient = require('./helpers/ChatClient')
|
|
|
|
const ChatApp = require('./helpers/ChatApp')
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('Getting messages', async function () {
|
|
|
|
before(async function () {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.user_id1 = ObjectId().toString()
|
|
|
|
this.user_id2 = ObjectId().toString()
|
|
|
|
this.content1 = 'foo bar'
|
|
|
|
this.content2 = 'hello world'
|
2022-01-10 04:29:32 -05:00
|
|
|
await ChatApp.ensureRunning()
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('globally', async function () {
|
|
|
|
before(async function () {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response } = await ChatClient.sendGlobalMessage(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id,
|
|
|
|
this.user_id1,
|
|
|
|
this.content1
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(201)
|
|
|
|
const { response: response2 } = await ChatClient.sendGlobalMessage(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id,
|
|
|
|
this.user_id2,
|
|
|
|
this.content2
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response2.statusCode).to.equal(201)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
2016-12-14 12:38:14 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should contain the messages and populated users when getting the messages', async function () {
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response, body: messages } = await ChatClient.getGlobalMessages(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(200)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(messages.length).to.equal(2)
|
|
|
|
messages.reverse()
|
|
|
|
expect(messages[0].content).to.equal(this.content1)
|
|
|
|
expect(messages[0].user_id).to.equal(this.user_id1)
|
|
|
|
expect(messages[1].content).to.equal(this.content2)
|
|
|
|
expect(messages[1].user_id).to.equal(this.user_id2)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('from all the threads', async function () {
|
|
|
|
before(async function () {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.thread_id1 = ObjectId().toString()
|
|
|
|
this.thread_id2 = ObjectId().toString()
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response } = await ChatClient.sendMessage(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id1,
|
|
|
|
this.user_id1,
|
|
|
|
'one'
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response.statusCode).to.equal(201)
|
|
|
|
const { response: response2 } = await ChatClient.sendMessage(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id2,
|
|
|
|
this.user_id2,
|
|
|
|
'two'
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response2.statusCode).to.equal(201)
|
|
|
|
const { response: response3 } = await ChatClient.sendMessage(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id1,
|
|
|
|
this.user_id1,
|
|
|
|
'three'
|
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response3.statusCode).to.equal(201)
|
|
|
|
const { response: response4 } = await ChatClient.sendMessage(
|
2022-01-10 04:29:32 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id2,
|
|
|
|
this.user_id2,
|
|
|
|
'four'
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:35:18 -05:00
|
|
|
expect(response4.statusCode).to.equal(201)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should contain a dictionary of threads with messages with populated users', async function () {
|
2022-01-10 04:35:18 -05:00
|
|
|
const { response, body: threads } = await ChatClient.getThreads(
|
|
|
|
this.project_id
|
|
|
|
)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(Object.keys(threads).length).to.equal(2)
|
|
|
|
const thread1 = threads[this.thread_id1]
|
|
|
|
expect(thread1.messages.length).to.equal(2)
|
|
|
|
const thread2 = threads[this.thread_id2]
|
|
|
|
expect(thread2.messages.length).to.equal(2)
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread1.messages[0].content).to.equal('one')
|
|
|
|
expect(thread1.messages[0].user_id).to.equal(this.user_id1)
|
|
|
|
expect(thread1.messages[1].content).to.equal('three')
|
|
|
|
expect(thread1.messages[1].user_id).to.equal(this.user_id1)
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(thread2.messages[0].content).to.equal('two')
|
|
|
|
expect(thread2.messages[0].user_id).to.equal(this.user_id2)
|
|
|
|
expect(thread2.messages[1].content).to.equal('four')
|
|
|
|
expect(thread2.messages[1].user_id).to.equal(this.user_id2)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|