forked from b00tc4mp/isdi-bootcamp-202405
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement sendMessage logic and testing b00tc4mp#84
- Loading branch information
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
staff/marti-herms/project/G-HUB/core/logic/sendMessage.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
staff/marti-herms/project/G-HUB/core/logic/sendMessage.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |