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('Sending a message', async function () {
|
|
|
|
before(async function () {
|
|
|
|
await ChatApp.ensureRunning()
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
2016-12-16 11:52:50 -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()
|
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
this.content = 'global message'
|
2022-01-10 04:29:32 -05:00
|
|
|
const { response, body } = await ChatClient.sendGlobalMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id,
|
|
|
|
this.user_id,
|
2022-01-10 04:29:32 -05:00
|
|
|
this.content
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(201)
|
|
|
|
expect(body.content).to.equal(this.content)
|
|
|
|
expect(body.user_id).to.equal(this.user_id)
|
|
|
|
expect(body.room_id).to.equal(this.project_id)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should then list the message in the project messages', async function () {
|
|
|
|
const { response, body: messages } = await ChatClient.getGlobalMessages(
|
|
|
|
this.project_id
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(messages.length).to.equal(1)
|
|
|
|
expect(messages[0].content).to.equal(this.content)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('to a thread', async function () {
|
|
|
|
before(async function () {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
this.thread_id = ObjectId().toString()
|
|
|
|
this.content = 'thread message'
|
2022-01-10 04:29:32 -05:00
|
|
|
const { response, body } = await ChatClient.sendMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
2022-01-10 04:29:32 -05:00
|
|
|
this.content
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(201)
|
|
|
|
expect(body.content).to.equal(this.content)
|
|
|
|
expect(body.user_id).to.equal(this.user_id)
|
|
|
|
expect(body.room_id).to.equal(this.project_id)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should then list the message in the threads', async function () {
|
|
|
|
const { response, body: threads } = await ChatClient.getThreads(
|
|
|
|
this.project_id
|
|
|
|
)
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(threads[this.thread_id].messages.length).to.equal(1)
|
|
|
|
expect(threads[this.thread_id].messages[0].content).to.equal(this.content)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
it('should not appear in the global messages', async function () {
|
|
|
|
const { response, body: messages } = await ChatClient.getGlobalMessages(
|
|
|
|
this.project_id
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(messages.length).to.equal(0)
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('failure cases', async function () {
|
|
|
|
before(async function () {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.user_id = ObjectId().toString()
|
2022-01-07 05:41:23 -05:00
|
|
|
this.thread_id = ObjectId().toString()
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('with a malformed user_id', async function () {
|
|
|
|
it('should return a graceful error', async function () {
|
|
|
|
const { response, body } = await ChatClient.sendMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
'malformed-user',
|
2022-01-10 04:29:32 -05:00
|
|
|
'content'
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Invalid userId')
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('with a malformed project_id', async function () {
|
|
|
|
it('should return a graceful error', async function () {
|
|
|
|
const { response, body } = await ChatClient.sendMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
'malformed-project',
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
2022-01-10 04:29:32 -05:00
|
|
|
'content'
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Invalid projectId')
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('with a malformed thread_id', async function () {
|
|
|
|
it('should return a graceful error', async function () {
|
|
|
|
const { response, body } = await ChatClient.sendMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id,
|
|
|
|
'malformed-thread-id',
|
|
|
|
this.user_id,
|
2022-01-10 04:29:32 -05:00
|
|
|
'content'
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Invalid threadId')
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('with no content', async function () {
|
|
|
|
it('should return a graceful error', async function () {
|
|
|
|
const { response, body } = await ChatClient.sendMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
2022-01-10 04:29:32 -05:00
|
|
|
null
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('No content provided')
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2022-01-10 04:29:32 -05:00
|
|
|
describe('with very long content', async function () {
|
|
|
|
it('should return a graceful error', async function () {
|
2020-08-19 07:59:33 -04:00
|
|
|
const content = '-'.repeat(10 * 1024 + 1)
|
2022-01-10 04:29:32 -05:00
|
|
|
const { response, body } = await ChatClient.sendMessage(
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
2022-01-10 04:29:32 -05:00
|
|
|
content
|
2018-12-20 14:14:11 -05:00
|
|
|
)
|
2022-01-10 04:29:32 -05:00
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Content too long (> 10240 bytes)')
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|