Skip to content

Commit

Permalink
implement sendMessage logic and testing b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 26, 2024
1 parent 01b5042 commit f4ddbbe
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
4 changes: 4 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import getUserAvatar from './getUserAvatar.js'
import getUser from './getUser.js'
import editUserAvatar from './editUserAvatar.js'
import editUserUsername from './editUserUsername.js'
import createChat from './createChat.js'
import sendMessage from './sendMessage.js'

const logic = {
authenticateUser,
createChat,
deleteReview,
editUserAvatar,
editUserUsername,
Expand All @@ -41,6 +44,7 @@ const logic = {
registerUser,
searchGame,
searchUser,
sendMessage,
toggleAddGame,
toggleFavGame,
toggleFollowUser,
Expand Down
30 changes: 30 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/sendMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { User, Chat, Message } from '../data/models.js'

import { validate, errors } from 'com'

const { SystemError, NotFoundError } = errors

export default (userId, chatId, content) => {
validate.string(userId, 'userId')
validate.string(chatId, 'chatId')
validate.string(content, 'content')

return Promise.all([User.findById(userId).lean(), Chat.findById(chatId).lean()])
.catch(error => { throw new SystemError(error.message) })
.then(([user, chat]) => {
if (!user) throw new NotFoundError('user not found')

if (!chat) throw new NotFoundError('chat not found')

return Message.create({
author: user._id,
content
})
.catch(error => { throw new SystemError(error.message) })
})
.then(message =>
Chat.findByIdAndUpdate(chatId, { $push: { messages: message._id } })
.catch(error => { throw new SystemError(error.message) })
)
.then(() => { })
}
105 changes: 105 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/sendMessage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import { expect } from 'chai'

import sendMessage from './sendMessage.js'
import { User, Chat, Message } from '../data/models.js'
import { errors } from 'com'

const { NotFoundError, ValidationError } = errors

describe('sendMessage', () => {
before(() => mongoose.connect(process.env.MONGODB_URI))

beforeEach(() => Promise.all([User.deleteMany(), Chat.deleteMany(), Message.deleteMany()]))

it('succeeds on sending a message', () => {
return Promise.all([User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' }), User.create({ username: 'eden', email: '[email protected]', password: '123123123' })])
.then(([user1, user2]) =>
Chat.create({ participants: [user1._id, user2._id] })
.then(chat =>
sendMessage(user1.id, chat.id, 'hello')
.then(() => Message.findOne({ author: user1.id }))
.then(message => {
expect(message).to.not.be.null
expect(message.content).to.equal('hello')
return Chat.findById(chat.id)
})
.then(chat => {
expect(chat.messages).to.have.lengthOf(1)
})
)
)
})

it('fails on non-existing user', () => {
let error

return sendMessage('66c0ae22930f95c985427ece', '66c0ae22930f95c985427ece', 'hello')
.catch(_error => {
error = _error
})
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('user not found')
})
})

it('fails on non-existing chat', () => {
let error

return User.create({ username: 'monoloco', email: '[email protected]', password: '123123123' })
.then(user => sendMessage(user.id, '66c0ae22930f95c985427ece', 'hello'))
.catch(_error => {
error = _error
})
.finally(() => {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('chat not found')
})
})

it('fails on non-string userId', () => {
let error

try {
sendMessage(123, '66c0ae22930f95c985427ece', 'hello')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('userId is not a string')
}
})

it('fails on non-string chatId', () => {
let error

try {
sendMessage('66c0ae22930f95c985427ece', 123, 'hello')
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('chatId is not a string')
}
})

it('fails on non-string content', () => {
let error

try {
sendMessage('66c0ae22930f95c985427ece', '66c0ae22930f95c985427ece', 123)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('content is not a string')
}
})

afterEach(() => Promise.all([User.deleteMany(), Chat.deleteMany(), Message.deleteMany()]))

after(() => mongoose.disconnect())

})
11 changes: 11 additions & 0 deletions staff/marti-herms/project/G-HUB/core/logic/sendMessage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import sendMessage from './sendMessage.js'
import { Message } from '../data/models.js'

mongoose.connect(process.env.MONGODB_URI)
.then(() => sendMessage('66c7632d2427a0b2deea74ff', '66c780ed9ca90db154f5a1db', 'hello!'))
.then(() => Message.findOne({ content: 'hello!' }))
.then(message => console.log(message))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())

0 comments on commit f4ddbbe

Please sign in to comment.