2018-12-20 14:14:08 -05:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
node/no-deprecated-api,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-12-20 14:14:05 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
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
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('Sending a message', function () {
|
|
|
|
before(function (done) {
|
2020-02-11 04:08:37 -05:00
|
|
|
return ChatApp.ensureRunning(done)
|
|
|
|
})
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('globally', function () {
|
|
|
|
before(function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
this.content = 'global message'
|
|
|
|
return ChatClient.sendGlobalMessage(
|
|
|
|
this.project_id,
|
|
|
|
this.user_id,
|
|
|
|
this.content,
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(error).to.be.null
|
|
|
|
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)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2016-12-16 11:52:50 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should then list the message in the project messages', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.getGlobalMessages(
|
|
|
|
this.project_id,
|
|
|
|
(error, response, messages) => {
|
|
|
|
expect(error).to.be.null
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(messages.length).to.equal(1)
|
|
|
|
expect(messages[0].content).to.equal(this.content)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('to a thread', function () {
|
|
|
|
before(function (done) {
|
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'
|
|
|
|
return ChatClient.sendMessage(
|
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
|
|
|
this.content,
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(error).to.be.null
|
|
|
|
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)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should then list the message in the threads', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.getThreads(
|
|
|
|
this.project_id,
|
|
|
|
(error, response, threads) => {
|
|
|
|
expect(error).to.be.null
|
|
|
|
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
|
|
|
|
)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should not appear in the global messages', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.getGlobalMessages(
|
|
|
|
this.project_id,
|
|
|
|
(error, response, messages) => {
|
|
|
|
expect(error).to.be.null
|
|
|
|
expect(response.statusCode).to.equal(200)
|
|
|
|
expect(messages.length).to.equal(0)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('failure cases', function () {
|
|
|
|
before(function () {
|
2018-12-20 14:14:11 -05:00
|
|
|
this.project_id = ObjectId().toString()
|
|
|
|
this.user_id = ObjectId().toString()
|
|
|
|
return (this.thread_id = ObjectId().toString())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a malformed user_id', function () {
|
|
|
|
return it('should return a graceful error', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.sendMessage(
|
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
'malformed-user',
|
|
|
|
'content',
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Invalid user_id')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a malformed project_id', function () {
|
|
|
|
return it('should return a graceful error', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.sendMessage(
|
|
|
|
'malformed-project',
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
|
|
|
'content',
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Invalid project_id')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a malformed thread_id', function () {
|
|
|
|
return it('should return a graceful error', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.sendMessage(
|
|
|
|
this.project_id,
|
|
|
|
'malformed-thread-id',
|
|
|
|
this.user_id,
|
|
|
|
'content',
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Invalid thread_id')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with no content', function () {
|
|
|
|
return it('should return a graceful error', function (done) {
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.sendMessage(
|
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
|
|
|
null,
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('No content provided')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('with very long content', function () {
|
|
|
|
return it('should return a graceful error', function (done) {
|
2020-08-19 07:59:33 -04:00
|
|
|
const content = '-'.repeat(10 * 1024 + 1)
|
2018-12-20 14:14:11 -05:00
|
|
|
return ChatClient.sendMessage(
|
|
|
|
this.project_id,
|
|
|
|
this.thread_id,
|
|
|
|
this.user_id,
|
|
|
|
content,
|
|
|
|
(error, response, body) => {
|
|
|
|
expect(response.statusCode).to.equal(400)
|
|
|
|
expect(body).to.equal('Content too long (> 10240 bytes)')
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
2020-02-11 04:08:37 -05:00
|
|
|
})
|
|
|
|
})
|
2018-12-20 14:14:11 -05:00
|
|
|
})
|
|
|
|
})
|