asyncify tests

GitOrigin-RevId: 62f875bb121a599fab830a3244959596796cd6e1
This commit is contained in:
Tim Alby 2022-01-10 10:29:32 +01:00 committed by Copybot
parent 67ce7c3c75
commit 795ed56034
8 changed files with 385 additions and 502 deletions

View file

@ -4,57 +4,47 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp') const ChatApp = require('./helpers/ChatApp')
describe('Deleting a message', function () { describe('Deleting a message', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
ChatApp.ensureRunning(done) await ChatApp.ensureRunning()
}) })
describe('in a thread', function () { describe('in a thread', async function () {
before(function (done) { before(async function () {
ChatClient.sendMessage( const { response, body: message } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
'first message', 'first message'
(error, response, message) => {
this.message = message
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
'deleted message',
(error, response, message1) => {
this.message = message1
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.deleteMessage(
this.project_id,
this.thread_id,
this.message.id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
)
}
) )
this.message = message
expect(response.statusCode).to.equal(201)
const { response: response2, body: message2 } =
await ChatClient.sendMessage(
this.project_id,
this.thread_id,
this.user_id,
'deleted message'
)
this.message = message2
expect(response2.statusCode).to.equal(201)
const { response: response3 } = await ChatClient.deleteMessage(
this.project_id,
this.thread_id,
this.message.id
)
expect(response3.statusCode).to.equal(204)
}) })
it('should then remove the message from the threads', function (done) { it('should then remove the message from the threads', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { response, body: threads } = await ChatClient.getThreads(
expect(error).to.be.null this.project_id
expect(response.statusCode).to.equal(200) )
expect(threads[this.thread_id].messages.length).to.equal(1) expect(response.statusCode).to.equal(200)
done() expect(threads[this.thread_id].messages.length).to.equal(1)
})
}) })
}) })
}) })

View file

@ -4,45 +4,37 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp') const ChatApp = require('./helpers/ChatApp')
describe('Deleting a thread', function () { describe('Deleting a thread', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
ChatApp.ensureRunning(done) await ChatApp.ensureRunning()
}) })
describe('with a thread that is deleted', function () { describe('with a thread that is deleted', async function () {
before(function (done) { before(async function () {
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
this.content = 'deleted thread message' this.content = 'deleted thread message'
ChatClient.sendMessage( const { response } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
this.content, this.content
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.deleteThread(
this.project_id,
this.thread_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
) )
expect(response.statusCode).to.equal(201)
const { response: response2 } = await ChatClient.deleteThread(
this.project_id,
this.thread_id
)
expect(response2.statusCode).to.equal(204)
}) })
it('should then not list the thread for the project', function (done) { it('should then not list the thread for the project', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { response, body: threads } = await ChatClient.getThreads(
expect(error).to.be.null this.project_id
expect(response.statusCode).to.equal(200) )
expect(Object.keys(threads).length).to.equal(0) expect(response.statusCode).to.equal(200)
done() expect(Object.keys(threads).length).to.equal(0)
})
}) })
}) })
}) })

View file

@ -4,55 +4,48 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp') const ChatApp = require('./helpers/ChatApp')
describe('Editing a message', function () { describe('Editing a message', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
ChatApp.ensureRunning(done) await ChatApp.ensureRunning()
}) })
describe('in a thread', function () { describe('in a thread', async function () {
before(function (done) { before(async function () {
this.content = 'thread message' this.content = 'thread message'
this.new_content = 'updated thread message' this.new_content = 'updated thread message'
ChatClient.sendMessage( const { response, body: message } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
this.content, this.content
(error, response, message) => {
this.message = message
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
expect(this.message.id).to.exist
expect(this.message.content).to.equal(this.content)
ChatClient.editMessage(
this.project_id,
this.thread_id,
this.message.id,
this.new_content,
(error, response, newMessage) => {
this.new_message = newMessage
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
) )
}) this.message = message
expect(response.statusCode).to.equal(201)
it('should then list the updated message in the threads', function (done) { expect(this.message.id).to.exist
ChatClient.getThreads(this.project_id, (error, response, threads) => { expect(this.message.content).to.equal(this.content)
expect(error).to.be.null const { response: response2, body: newMessage } =
expect(response.statusCode).to.equal(200) await ChatClient.editMessage(
expect(threads[this.thread_id].messages.length).to.equal(1) this.project_id,
expect(threads[this.thread_id].messages[0].content).to.equal( this.thread_id,
this.message.id,
this.new_content this.new_content
) )
done() this.new_message = newMessage
}) expect(response2.statusCode).to.equal(204)
})
it('should then list the updated 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.new_content
)
}) })
}) })
}) })

View file

@ -1,124 +1,94 @@
const { ObjectId } = require('../../../app/js/mongodb') const { ObjectId } = require('../../../app/js/mongodb')
const { expect } = require('chai') const { expect } = require('chai')
const async = require('async')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp') const ChatApp = require('./helpers/ChatApp')
describe('Getting messages', function () { describe('Getting messages', async function () {
before(function (done) { before(async function () {
this.user_id1 = ObjectId().toString() this.user_id1 = ObjectId().toString()
this.user_id2 = ObjectId().toString() this.user_id2 = ObjectId().toString()
this.content1 = 'foo bar' this.content1 = 'foo bar'
this.content2 = 'hello world' this.content2 = 'hello world'
ChatApp.ensureRunning(done) await ChatApp.ensureRunning()
}) })
describe('globally', function () { describe('globally', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
async.series( await ChatClient.sendGlobalMessage(
[ this.project_id,
cb => this.user_id1,
ChatClient.sendGlobalMessage( this.content1
this.project_id, )
this.user_id1, await ChatClient.sendGlobalMessage(
this.content1, this.project_id,
cb this.user_id2,
), this.content2
cb =>
ChatClient.sendGlobalMessage(
this.project_id,
this.user_id2,
this.content2,
cb
),
],
done
) )
}) })
it('should contain the messages and populated users when getting the messages', function (done) { it('should contain the messages and populated users when getting the messages', async function () {
ChatClient.getGlobalMessages( const { body: messages } = await ChatClient.getGlobalMessages(
this.project_id, this.project_id
(error, response, messages) => {
if (error) return done(error)
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)
done()
}
) )
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)
}) })
}) })
describe('from all the threads', function () { describe('from all the threads', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.thread_id1 = ObjectId().toString() this.thread_id1 = ObjectId().toString()
this.thread_id2 = ObjectId().toString() this.thread_id2 = ObjectId().toString()
async.series( await ChatClient.sendMessage(
[ this.project_id,
cb => this.thread_id1,
ChatClient.sendMessage( this.user_id1,
this.project_id, 'one'
this.thread_id1, )
this.user_id1, await ChatClient.sendMessage(
'one', this.project_id,
cb this.thread_id2,
), this.user_id2,
cb => 'two'
ChatClient.sendMessage( )
this.project_id, await ChatClient.sendMessage(
this.thread_id2, this.project_id,
this.user_id2, this.thread_id1,
'two', this.user_id1,
cb 'three'
), )
cb => await ChatClient.sendMessage(
ChatClient.sendMessage( this.project_id,
this.project_id, this.thread_id2,
this.thread_id1, this.user_id2,
this.user_id1, 'four'
'three',
cb
),
cb =>
ChatClient.sendMessage(
this.project_id,
this.thread_id2,
this.user_id2,
'four',
cb
),
],
done
) )
}) })
it('should contain a dictionary of threads with messages with populated users', function (done) { it('should contain a dictionary of threads with messages with populated users', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { body: threads } = await ChatClient.getThreads(this.project_id)
if (error) return done(error) expect(Object.keys(threads).length).to.equal(2)
expect(Object.keys(threads).length).to.equal(2) const thread1 = threads[this.thread_id1]
const thread1 = threads[this.thread_id1] expect(thread1.messages.length).to.equal(2)
expect(thread1.messages.length).to.equal(2) const thread2 = threads[this.thread_id2]
const thread2 = threads[this.thread_id2] expect(thread2.messages.length).to.equal(2)
expect(thread2.messages.length).to.equal(2)
expect(thread1.messages[0].content).to.equal('one') expect(thread1.messages[0].content).to.equal('one')
expect(thread1.messages[0].user_id).to.equal(this.user_id1) expect(thread1.messages[0].user_id).to.equal(this.user_id1)
expect(thread1.messages[1].content).to.equal('three') expect(thread1.messages[1].content).to.equal('three')
expect(thread1.messages[1].user_id).to.equal(this.user_id1) expect(thread1.messages[1].user_id).to.equal(this.user_id1)
expect(thread2.messages[0].content).to.equal('two') expect(thread2.messages[0].content).to.equal('two')
expect(thread2.messages[0].user_id).to.equal(this.user_id2) expect(thread2.messages[0].user_id).to.equal(this.user_id2)
expect(thread2.messages[1].content).to.equal('four') expect(thread2.messages[1].content).to.equal('four')
expect(thread2.messages[1].user_id).to.equal(this.user_id2) expect(thread2.messages[1].user_id).to.equal(this.user_id2)
done()
})
}) })
}) })
}) })

View file

@ -4,122 +4,96 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp') const ChatApp = require('./helpers/ChatApp')
describe('Resolving a thread', function () { describe('Resolving a thread', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
ChatApp.ensureRunning(done) await ChatApp.ensureRunning()
}) })
describe('with a resolved thread', function () { describe('with a resolved thread', async function () {
before(function (done) { before(async function () {
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
this.content = 'resolved message' this.content = 'resolved message'
ChatClient.sendMessage( const { response } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
this.content, this.content
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
) )
expect(response.statusCode).to.equal(201)
const { response: response2 } = await ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id
)
expect(response2.statusCode).to.equal(204)
}) })
it('should then list the thread as resolved', function (done) { it('should then list the thread as resolved', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { response, body: threads } = await ChatClient.getThreads(
expect(error).to.be.null this.project_id
expect(response.statusCode).to.equal(200) )
expect(threads[this.thread_id].resolved).to.equal(true) expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].resolved_by_user_id).to.equal( expect(threads[this.thread_id].resolved).to.equal(true)
this.user_id expect(threads[this.thread_id].resolved_by_user_id).to.equal(this.user_id)
) const resolvedAt = new Date(threads[this.thread_id].resolved_at)
const resolvedAt = new Date(threads[this.thread_id].resolved_at) expect(new Date() - resolvedAt).to.be.below(1000)
expect(new Date() - resolvedAt).to.be.below(1000)
done()
})
}) })
}) })
describe('when a thread is not resolved', function () { describe('when a thread is not resolved', async function () {
before(function (done) { before(async function () {
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
this.content = 'open message' this.content = 'open message'
ChatClient.sendMessage( const { response } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
this.content, this.content
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
done()
}
) )
expect(response.statusCode).to.equal(201)
}) })
it('should not list the thread as resolved', function (done) { it('should not list the thread as resolved', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { response, body: threads } = await ChatClient.getThreads(
expect(error).to.be.null this.project_id
expect(response.statusCode).to.equal(200) )
expect(threads[this.thread_id].resolved).to.be.undefined expect(response.statusCode).to.equal(200)
done() expect(threads[this.thread_id].resolved).to.be.undefined
})
}) })
}) })
describe('when a thread is resolved then reopened', function () { describe('when a thread is resolved then reopened', async function () {
before(function (done) { before(async function () {
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
this.content = 'resolved message' this.content = 'resolved message'
ChatClient.sendMessage( const { response } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
this.content, this.content
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(201)
ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
ChatClient.reopenThread(
this.project_id,
this.thread_id,
(error, response, body) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(204)
done()
}
)
}
)
}
) )
expect(response.statusCode).to.equal(201)
const { response: response2 } = await ChatClient.resolveThread(
this.project_id,
this.thread_id,
this.user_id
)
expect(response2.statusCode).to.equal(204)
const { response: response3 } = await ChatClient.reopenThread(
this.project_id,
this.thread_id
)
expect(response3.statusCode).to.equal(204)
}) })
it('should not list the thread as resolved', function (done) { it('should not list the thread as resolved', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { response, body: threads } = await ChatClient.getThreads(
expect(error).to.be.null this.project_id
expect(response.statusCode).to.equal(200) )
expect(threads[this.thread_id].resolved).to.be.undefined expect(response.statusCode).to.equal(200)
done() expect(threads[this.thread_id].resolved).to.be.undefined
})
}) })
}) })
}) })

View file

@ -4,182 +4,143 @@ const { expect } = require('chai')
const ChatClient = require('./helpers/ChatClient') const ChatClient = require('./helpers/ChatClient')
const ChatApp = require('./helpers/ChatApp') const ChatApp = require('./helpers/ChatApp')
describe('Sending a message', function () { describe('Sending a message', async function () {
before(function (done) { before(async function () {
ChatApp.ensureRunning(done) await ChatApp.ensureRunning()
}) })
describe('globally', function () { describe('globally', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
this.content = 'global message' this.content = 'global message'
ChatClient.sendGlobalMessage( const { response, body } = await ChatClient.sendGlobalMessage(
this.project_id, this.project_id,
this.user_id, this.user_id,
this.content, 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)
done()
}
) )
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)
}) })
it('should then list the message in the project messages', function (done) { it('should then list the message in the project messages', async function () {
ChatClient.getGlobalMessages( const { response, body: messages } = await ChatClient.getGlobalMessages(
this.project_id, 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)
done()
}
) )
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(1)
expect(messages[0].content).to.equal(this.content)
}) })
}) })
describe('to a thread', function () { describe('to a thread', async function () {
before(function (done) { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
this.content = 'thread message' this.content = 'thread message'
ChatClient.sendMessage( const { response, body } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
this.content, 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)
done()
}
) )
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)
}) })
it('should then list the message in the threads', function (done) { it('should then list the message in the threads', async function () {
ChatClient.getThreads(this.project_id, (error, response, threads) => { const { response, body: threads } = await ChatClient.getThreads(
expect(error).to.be.null this.project_id
expect(response.statusCode).to.equal(200) )
expect(threads[this.thread_id].messages.length).to.equal(1) expect(response.statusCode).to.equal(200)
expect(threads[this.thread_id].messages[0].content).to.equal( expect(threads[this.thread_id].messages.length).to.equal(1)
this.content expect(threads[this.thread_id].messages[0].content).to.equal(this.content)
)
done()
})
}) })
it('should not appear in the global messages', function (done) { it('should not appear in the global messages', async function () {
ChatClient.getGlobalMessages( const { response, body: messages } = await ChatClient.getGlobalMessages(
this.project_id, this.project_id
(error, response, messages) => {
expect(error).to.be.null
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(0)
done()
}
) )
expect(response.statusCode).to.equal(200)
expect(messages.length).to.equal(0)
}) })
}) })
describe('failure cases', function () { describe('failure cases', async function () {
before(function () { before(async function () {
this.project_id = ObjectId().toString() this.project_id = ObjectId().toString()
this.user_id = ObjectId().toString() this.user_id = ObjectId().toString()
this.thread_id = ObjectId().toString() this.thread_id = ObjectId().toString()
}) })
describe('with a malformed user_id', function () { describe('with a malformed user_id', async function () {
it('should return a graceful error', function (done) { it('should return a graceful error', async function () {
ChatClient.sendMessage( const { response, body } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
'malformed-user', 'malformed-user',
'content', 'content'
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid userId')
done()
}
) )
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid userId')
}) })
}) })
describe('with a malformed project_id', function () { describe('with a malformed project_id', async function () {
it('should return a graceful error', function (done) { it('should return a graceful error', async function () {
ChatClient.sendMessage( const { response, body } = await ChatClient.sendMessage(
'malformed-project', 'malformed-project',
this.thread_id, this.thread_id,
this.user_id, this.user_id,
'content', 'content'
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid projectId')
done()
}
) )
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid projectId')
}) })
}) })
describe('with a malformed thread_id', function () { describe('with a malformed thread_id', async function () {
it('should return a graceful error', function (done) { it('should return a graceful error', async function () {
ChatClient.sendMessage( const { response, body } = await ChatClient.sendMessage(
this.project_id, this.project_id,
'malformed-thread-id', 'malformed-thread-id',
this.user_id, this.user_id,
'content', 'content'
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid threadId')
done()
}
) )
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Invalid threadId')
}) })
}) })
describe('with no content', function () { describe('with no content', async function () {
it('should return a graceful error', function (done) { it('should return a graceful error', async function () {
ChatClient.sendMessage( const { response, body } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
null, null
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('No content provided')
done()
}
) )
expect(response.statusCode).to.equal(400)
expect(body).to.equal('No content provided')
}) })
}) })
describe('with very long content', function () { describe('with very long content', async function () {
it('should return a graceful error', function (done) { it('should return a graceful error', async function () {
const content = '-'.repeat(10 * 1024 + 1) const content = '-'.repeat(10 * 1024 + 1)
ChatClient.sendMessage( const { response, body } = await ChatClient.sendMessage(
this.project_id, this.project_id,
this.thread_id, this.thread_id,
this.user_id, this.user_id,
content, content
(error, response, body) => {
if (error) return done(error)
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Content too long (> 10240 bytes)')
done()
}
) )
expect(response.statusCode).to.equal(400)
expect(body).to.equal('Content too long (> 10240 bytes)')
}) })
}) })
}) })

View file

@ -1,28 +1,27 @@
const { waitForDb } = require('../../../../app/js/mongodb') const { waitForDb } = require('../../../../app/js/mongodb')
const app = require('../../../../app') const app = require('../../../../app')
module.exports = { let serverPromise = null
running: false, function startServer(resolve, reject) {
initing: false, waitForDb()
callbacks: [], .then(() => {
ensureRunning(callback) {
if (this.running) {
return callback()
} else if (this.initing) {
return this.callbacks.push(callback)
}
this.initing = true
this.callbacks.push(callback)
waitForDb().then(() => {
app.listen(3010, 'localhost', error => { app.listen(3010, 'localhost', error => {
if (error) { if (error) {
throw error return reject(error)
}
this.running = true
for (callback of this.callbacks) {
callback()
} }
resolve()
}) })
}) })
}, .catch(reject)
}
async function ensureRunning() {
if (!serverPromise) {
serverPromise = new Promise(startServer)
}
return serverPromise
}
module.exports = {
ensureRunning,
} }

View file

@ -2,101 +2,105 @@ const request = require('request').defaults({
baseUrl: 'http://localhost:3010', baseUrl: 'http://localhost:3010',
}) })
module.exports = { async function asyncRequest(options) {
sendGlobalMessage(projectId, userId, content, callback) { return new Promise((resolve, reject) => {
request.post( request(options, (err, response, body) => {
{ if (err) {
url: `/project/${projectId}/messages`, reject(err)
json: { } else {
user_id: userId, resolve({ response, body })
content, }
}, })
}, })
callback }
)
}, async function sendGlobalMessage(projectId, userId, content) {
return asyncRequest({
getGlobalMessages(projectId, callback) { method: 'post',
request.get( url: `/project/${projectId}/messages`,
{ json: {
url: `/project/${projectId}/messages`, user_id: userId,
json: true, content,
}, },
callback })
) }
},
async function getGlobalMessages(projectId) {
sendMessage(projectId, threadId, userId, content, callback) { return asyncRequest({
request.post( method: 'get',
{ url: `/project/${projectId}/messages`,
url: `/project/${projectId}/thread/${threadId}/messages`, json: true,
json: { })
user_id: userId, }
content,
}, async function sendMessage(projectId, threadId, userId, content) {
}, return asyncRequest({
callback method: 'post',
) url: `/project/${projectId}/thread/${threadId}/messages`,
}, json: {
user_id: userId,
getThreads(projectId, callback) { content,
request.get( },
{ })
url: `/project/${projectId}/threads`, }
json: true,
}, async function getThreads(projectId) {
callback return asyncRequest({
) method: 'get',
}, url: `/project/${projectId}/threads`,
json: true,
resolveThread(projectId, threadId, userId, callback) { })
request.post( }
{
url: `/project/${projectId}/thread/${threadId}/resolve`, async function resolveThread(projectId, threadId, userId) {
json: { return asyncRequest({
user_id: userId, method: 'post',
}, url: `/project/${projectId}/thread/${threadId}/resolve`,
}, json: {
callback user_id: userId,
) },
}, })
}
reopenThread(projectId, threadId, callback) {
request.post( async function reopenThread(projectId, threadId) {
{ return asyncRequest({
url: `/project/${projectId}/thread/${threadId}/reopen`, method: 'post',
}, url: `/project/${projectId}/thread/${threadId}/reopen`,
callback })
) }
},
async function deleteThread(projectId, threadId) {
deleteThread(projectId, threadId, callback) { return asyncRequest({
request.del( method: 'delete',
{ url: `/project/${projectId}/thread/${threadId}`,
url: `/project/${projectId}/thread/${threadId}`, })
}, }
callback
) async function editMessage(projectId, threadId, messageId, content) {
}, return asyncRequest({
method: 'post',
editMessage(projectId, threadId, messageId, content, callback) { url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
request.post( json: {
{ content,
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`, },
json: { })
content, }
},
}, async function deleteMessage(projectId, threadId, messageId) {
callback return asyncRequest({
) method: 'delete',
}, url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
})
deleteMessage(projectId, threadId, messageId, callback) { }
request.del(
{ module.exports = {
url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`, sendGlobalMessage,
}, getGlobalMessages,
callback sendMessage,
) getThreads,
}, resolveThread,
reopenThread,
deleteThread,
editMessage,
deleteMessage,
} }