From 0ef64b1a49e9ae06eb6e1fb20f7a3ebcfdd0d421 Mon Sep 17 00:00:00 2001 From: Marti Herms Date: Wed, 28 Aug 2024 10:21:26 +0200 Subject: [PATCH] implement validate id; add tests to account for id validation #84 --- .../G-HUB/api/middleware/errorHandler.js | 5 +- .../project/G-HUB/app/logic/getUserAvatar.js | 2 +- .../project/G-HUB/app/src/common/Alert.jsx | 2 +- staff/marti-herms/project/G-HUB/com/errors.js | 11 +--- .../marti-herms/project/G-HUB/com/validate.js | 8 ++- .../project/G-HUB/core/logic/deleteReview.js | 4 +- .../G-HUB/core/logic/deleteReview.spec.js | 26 ++++++++++ .../G-HUB/core/logic/editUserAvatar.js | 2 +- .../G-HUB/core/logic/editUserAvatar.spec.js | 15 +++++- .../G-HUB/core/logic/editUserUsername.js | 2 +- .../G-HUB/core/logic/editUserUsername.spec.js | 18 +++++-- .../G-HUB/core/logic/getChatMessages.js | 4 +- .../G-HUB/core/logic/getChatMessages.spec.js | 26 ++++++++++ .../G-HUB/core/logic/getDevUserGames.js | 4 +- .../G-HUB/core/logic/getDevUserGames.spec.js | 26 ++++++++++ .../project/G-HUB/core/logic/getGameById.js | 4 +- .../G-HUB/core/logic/getGameById.spec.js | 26 ++++++++++ .../G-HUB/core/logic/getGameReviews.js | 4 +- .../G-HUB/core/logic/getGameReviews.spec.js | 26 ++++++++++ .../project/G-HUB/core/logic/getUser.js | 4 +- .../project/G-HUB/core/logic/getUser.spec.js | 26 ++++++++++ .../project/G-HUB/core/logic/getUserAvatar.js | 4 +- .../G-HUB/core/logic/getUserAvatar.spec.js | 4 +- .../project/G-HUB/core/logic/getUserChats.js | 4 +- .../G-HUB/core/logic/getUserChats.spec.js | 24 +++++++++ .../project/G-HUB/core/logic/getUserFavs.js | 4 +- .../G-HUB/core/logic/getUserFavs.spec.js | 26 ++++++++++ .../G-HUB/core/logic/getUserFollowers.js | 4 +- .../G-HUB/core/logic/getUserFollowers.spec.js | 26 ++++++++++ .../G-HUB/core/logic/getUserFollowing.js | 4 +- .../G-HUB/core/logic/getUserFollowing.spec.js | 26 ++++++++++ .../G-HUB/core/logic/getUserLibrary.js | 4 +- .../G-HUB/core/logic/getUserLibrary.spec.js | 26 ++++++++++ .../G-HUB/core/logic/getUserUsername.js | 4 +- .../G-HUB/core/logic/getUserUsername.spec.js | 50 +++++++++---------- .../project/G-HUB/core/logic/makeReview.js | 4 +- .../G-HUB/core/logic/makeReview.spec.js | 26 ++++++++++ .../project/G-HUB/core/logic/openChat.js | 4 +- .../project/G-HUB/core/logic/openChat.spec.js | 24 +++++++++ .../project/G-HUB/core/logic/registerGame.js | 2 +- .../G-HUB/core/logic/registerGame.spec.js | 13 +++++ .../project/G-HUB/core/logic/searchGame.js | 2 +- .../G-HUB/core/logic/searchGame.spec.js | 13 +++++ .../project/G-HUB/core/logic/searchUser.js | 2 +- .../G-HUB/core/logic/searchUser.spec.js | 13 +++++ .../project/G-HUB/core/logic/sendMessage.js | 4 +- .../G-HUB/core/logic/sendMessage.spec.js | 26 ++++++++++ .../project/G-HUB/core/logic/toggleAddGame.js | 4 +- .../G-HUB/core/logic/toggleAddGame.spec.js | 26 ++++++++++ .../project/G-HUB/core/logic/toggleFavGame.js | 4 +- .../G-HUB/core/logic/toggleFavGame.spec.js | 26 ++++++++++ .../G-HUB/core/logic/toggleFollowUser.js | 4 +- .../G-HUB/core/logic/toggleFollowUser.spec.js | 26 ++++++++++ staff/marti-herms/project/G-HUB/doc/README.md | 28 +++++++++-- 54 files changed, 608 insertions(+), 98 deletions(-) diff --git a/staff/marti-herms/project/G-HUB/api/middleware/errorHandler.js b/staff/marti-herms/project/G-HUB/api/middleware/errorHandler.js index 1415b7bce..7136eb0c5 100644 --- a/staff/marti-herms/project/G-HUB/api/middleware/errorHandler.js +++ b/staff/marti-herms/project/G-HUB/api/middleware/errorHandler.js @@ -1,6 +1,6 @@ import { errors } from 'com' -const { NotFoundError, CredentialsError, DuplicityError, SessionError, ValidationError, OwnershipError } = errors +const { NotFoundError, CredentialsError, DuplicityError, SessionError, ValidationError } = errors export default (error, req, res, next) => { let status = 500 @@ -11,9 +11,6 @@ export default (error, req, res, next) => { else if (error instanceof CredentialsError || error instanceof DuplicityError) status = 409 - else if (error instanceof OwnershipError) - status = 403 - else if (error instanceof ValidationError) status = 400 diff --git a/staff/marti-herms/project/G-HUB/app/logic/getUserAvatar.js b/staff/marti-herms/project/G-HUB/app/logic/getUserAvatar.js index a2c19b720..a4f0e7a33 100644 --- a/staff/marti-herms/project/G-HUB/app/logic/getUserAvatar.js +++ b/staff/marti-herms/project/G-HUB/app/logic/getUserAvatar.js @@ -12,7 +12,7 @@ export default (targetUserId = '') => { return fetch(`${import.meta.env.VITE_API_URL}/users/${targetUserId || userId}/avatar`, { headers: { Authorization: `Bearer ${sessionStorage.token}` } }) - .catch(error => { throw new SystemError }) + .catch(error => { throw new SystemError(error.message) }) .then(response => { const { status } = response diff --git a/staff/marti-herms/project/G-HUB/app/src/common/Alert.jsx b/staff/marti-herms/project/G-HUB/app/src/common/Alert.jsx index 41f5cb283..4c6e00de2 100644 --- a/staff/marti-herms/project/G-HUB/app/src/common/Alert.jsx +++ b/staff/marti-herms/project/G-HUB/app/src/common/Alert.jsx @@ -4,7 +4,7 @@ import Container from '../library/Container' export default function Alert({ message, onAccept }) { return <> - + {message} diff --git a/staff/marti-herms/project/G-HUB/com/errors.js b/staff/marti-herms/project/G-HUB/com/errors.js index 0b996f1fd..6396fc998 100644 --- a/staff/marti-herms/project/G-HUB/com/errors.js +++ b/staff/marti-herms/project/G-HUB/com/errors.js @@ -46,22 +46,13 @@ class SessionError extends Error { } } -class OwnershipError extends Error { - constructor(message) { - super(message) - - this.name = this.constructor.name - } -} - const errors = { ValidationError, SystemError, DuplicityError, NotFoundError, CredentialsError, - SessionError, - OwnershipError + SessionError } export default errors \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/com/validate.js b/staff/marti-herms/project/G-HUB/com/validate.js index 6c94f38c4..116b025c0 100644 --- a/staff/marti-herms/project/G-HUB/com/validate.js +++ b/staff/marti-herms/project/G-HUB/com/validate.js @@ -37,6 +37,11 @@ const validateBoolean = (boolean, explain = 'value') => { if (typeof boolean !== 'boolean') throw new ValidationError(`${explain} is not a boolean`) } +const validateId = (id, explain = 'value') => { + validateString(id, explain) + if (id.length < 24) throw new ValidationError(`invalid ${explain}`) +} + const validate = { string: validateString, object: validateObject, @@ -44,7 +49,8 @@ const validate = { email: validateEmail, password: validatePassword, boolean: validateBoolean, - number: validateNumber + number: validateNumber, + id: validateId } export default validate \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/core/logic/deleteReview.js b/staff/marti-herms/project/G-HUB/core/logic/deleteReview.js index 6014fbc55..649fa440c 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/deleteReview.js +++ b/staff/marti-herms/project/G-HUB/core/logic/deleteReview.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, reviewId) => { - validate.string(userId, 'userId') - validate.string(reviewId, 'reviewId') + validate.id(userId, 'userId') + validate.id(reviewId, 'reviewId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/deleteReview.spec.js b/staff/marti-herms/project/G-HUB/core/logic/deleteReview.spec.js index 873595aa2..3fb2b8cbb 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/deleteReview.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/deleteReview.spec.js @@ -64,6 +64,19 @@ describe('deleteReview', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + deleteReview('ewjfn', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string reviewId', () => { let error @@ -77,6 +90,19 @@ describe('deleteReview', () => { } }) + it('fails on invalid reviewId', () => { + let error + + try { + deleteReview('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid reviewId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany(), Review.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.js b/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.js index 277245ca2..45da74c09 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.js +++ b/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.js @@ -5,7 +5,7 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, newAvatar) => { - validate.string(userId, 'userId') + validate.id(userId, 'userId') validate.string(newAvatar, 'avatar') return User.findById(userId).lean() diff --git a/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.spec.js b/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.spec.js index f8a81e18b..f03122873 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/editUserAvatar.spec.js @@ -48,11 +48,24 @@ describe('editUserAvatar', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + editUserAvatar('123', newAvatar) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string avatar', () => { let error try { - editUserAvatar('monoloco', 123) + editUserAvatar('66ba007f874aa7b84ec54491', 123) } catch (_error) { error = _error } finally { diff --git a/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.js b/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.js index 7fa6c3177..4d9c3b05b 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.js +++ b/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.js @@ -5,7 +5,7 @@ import { validate, errors } from 'com' const { DuplicityError, NotFoundError, SystemError } = errors export default (userId, newUsername) => { - validate.string(userId, 'userId') + validate.id(userId, 'userId') validate.username(newUsername, 'newUsername') return User.findOne({ username: newUsername }).lean() diff --git a/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.spec.js b/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.spec.js index afc6507ca..4281cc97c 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/editUserUsername.spec.js @@ -1,9 +1,6 @@ import 'dotenv/config' import editUserUsername from './editUserUsername.js' -import mongoose, { Types } from 'mongoose' -import bcrypt from 'bcryptjs' - -const { ObjectId } = Types +import mongoose from 'mongoose' import { expect } from 'chai' import { User } from '../data/models.js' @@ -63,6 +60,19 @@ describe('editUserUsername', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + editUserUsername('123', 'eden') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string newUsername', () => { let error diff --git a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js index d597adf02..c38382286 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, chatId) => { - validate.string(userId, 'userId') - validate.string(chatId, 'chatId') + validate.id(userId, 'userId') + validate.id(chatId, 'chatId') return Promise.all([User.findById(userId).lean(), Chat.findById(chatId).lean()]) .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js index a2761b94c..8fd84756a 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getChatMessages.spec.js @@ -82,6 +82,19 @@ describe('getChatMessages', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getChatMessages('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string chatId', () => { let error @@ -95,6 +108,19 @@ describe('getChatMessages', () => { } }) + it('fails on invalid chatId', () => { + let error + + try { + getChatMessages('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid chatId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Chat.deleteMany(), Message.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.js b/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.js index e271b9e09..481f54a95 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js index 961900ef4..fc36ddb35 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getDevUserGames.spec.js @@ -85,6 +85,19 @@ describe('getDevUserGames', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getDevUserGames('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -98,6 +111,19 @@ describe('getDevUserGames', () => { } }) + it('fails on invalid targetUserId', () => { + let error + + try { + getDevUserGames('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getGameById.js b/staff/marti-herms/project/G-HUB/core/logic/getGameById.js index 5870c8ff8..84708b588 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getGameById.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getGameById.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, gameId) => { - validate.string(userId, 'userId') - validate.string(gameId, 'gameId') + validate.id(userId, 'userId') + validate.id(gameId, 'gameId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.mesage) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getGameById.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getGameById.spec.js index ea3d6ebad..6780d8982 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getGameById.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getGameById.spec.js @@ -89,6 +89,19 @@ describe('getGameById', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getGameById('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string gameId', () => { let error @@ -102,6 +115,19 @@ describe('getGameById', () => { } }) + it('fails on invalid gameId', () => { + let error + + try { + getGameById('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid gameId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.js b/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.js index 3e8c8ae35..1e7e447fc 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, gameId) => { - validate.string(userId, 'userId') - validate.string(gameId, 'gameId') + validate.id(userId, 'userId') + validate.id(gameId, 'gameId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.spec.js index c218e1905..0ba7c09c8 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getGameReviews.spec.js @@ -92,6 +92,19 @@ describe('getGameReviews', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getGameReviews('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string gameId', () => { let error @@ -105,6 +118,19 @@ describe('getGameReviews', () => { } }) + it('fails on invalid gameId', () => { + let error + + try { + getGameReviews('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid gameId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany(), Review.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUser.js b/staff/marti-herms/project/G-HUB/core/logic/getUser.js index 0f5e48907..cc1dc31a7 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUser.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUser.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUser.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUser.spec.js index 02d626e9e..b722f9b28 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUser.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUser.spec.js @@ -59,6 +59,19 @@ describe('getUser', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getUser('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -72,6 +85,19 @@ describe('getUser', () => { } }) + it('fails on invalid targetUserId', () => { + let error + + try { + getUser('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => User.deleteMany()) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.js b/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.js index f4b53ce61..37a7b67bf 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.spec.js index 4d74959f9..5aa521d2b 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserAvatar.spec.js @@ -1,8 +1,6 @@ import 'dotenv/config' import getUserAvatar from './getUserAvatar.js' -import mongoose, { Types } from 'mongoose' - -const { ObjectId } = Types +import mongoose from 'mongoose' import { expect } from 'chai' import { User } from '../data/models.js' diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserChats.js b/staff/marti-herms/project/G-HUB/core/logic/getUserChats.js index 629d5e4dc..fdc17fba6 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserChats.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserChats.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return Promise.all([User.findById(userId).lean(), User.findById(targetUserId).lean()]) .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserChats.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserChats.spec.js index d68eae91c..a25fd417c 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserChats.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserChats.spec.js @@ -65,6 +65,18 @@ describe('getUserChats', () => { } }) + it('fails on invalid userId', () => { + let error + try { + getUserChats('123', '66c0ae22930f95c985427ece') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error try { @@ -77,6 +89,18 @@ describe('getUserChats', () => { } }) + it('fails on invalid targetUserId', () => { + let error + try { + getUserChats('66c0ae22930f95c985427ece', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Chat.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.js b/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.js index 14622d616..01d22d084 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.spec.js index 08c5204d4..5d0097bba 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserFavs.spec.js @@ -85,6 +85,19 @@ describe('getUserFavs', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getUserFavs('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -98,6 +111,19 @@ describe('getUserFavs', () => { } }) + it('fails on invalid targetUserId', () => { + let error + + try { + getUserFavs('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.js b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.js index ada21caa7..0b069aba8 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.spec.js index 89de037d6..2541f585c 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowers.spec.js @@ -77,6 +77,19 @@ describe('getUserFollowers', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getUserFollowers('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -90,6 +103,19 @@ describe('getUserFollowers', () => { } }) + it('fails on invalid targetUserId', () => { + let error + + try { + getUserFollowers('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => User.deleteMany()) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.js b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.js index b3d17bebf..2b6fba2cb 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.spec.js index 19890c213..8ff41a0fb 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserFollowing.spec.js @@ -77,6 +77,19 @@ describe('getUserFollowing', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getUserFollowing('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -90,6 +103,19 @@ describe('getUserFollowing', () => { } }) + it('fails on invalid targetUserId', () => { + let error + + try { + getUserFollowing('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => User.deleteMany()) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.js b/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.js index 95333573e..431eb5919 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.spec.js index a4d450137..d4987770f 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserLibrary.spec.js @@ -85,6 +85,19 @@ describe('getUserLibrary', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + getUserLibrary('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -98,6 +111,19 @@ describe('getUserLibrary', () => { } }) + it('fails on non-string targetUserId', () => { + let error + + try { + getUserLibrary('66ba007f874aa7b84ec54491', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.js b/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.js index df4c2a835..b829bb6b7 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.spec.js b/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.spec.js index 1676f6f6c..74edc4899 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/getUserUsername.spec.js @@ -50,7 +50,7 @@ describe('getUserUsername', () => { let error try { - getUserUsername(123, 'eden') + getUserUsername(123, '66b941110938786955ecf3b5') } catch (_error) { error = _error } finally { @@ -59,24 +59,24 @@ describe('getUserUsername', () => { } }) - // it('fails on invalid userId', () => { - // let error + it('fails on invalid userId', () => { + let error - // try { - // getUserUsername('', 'eden') - // } catch (_error) { - // error = _error - // } finally { - // expect(error).to.be.instanceOf(ValidationError) - // expect(error.message).to.equal('invalid userId') - // } - // }) + try { + getUserUsername('415164352', '66b941110938786955ecf3b5') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) it('fails on non-string targetUserId', () => { let error try { - getUserUsername('eden', 123) + getUserUsername('66b941110938786955ecf3b5', 123) } catch (_error) { error = _error } finally { @@ -85,18 +85,18 @@ describe('getUserUsername', () => { } }) - // it('fails on invalid targetUserId', () => { - // let error - - // try { - // getUserUsername('eden', '') - // } catch (_error) { - // error = _error - // } finally { - // expect(error).to.be.instanceOf(ValidationError) - // expect(error.message).to.equal('invalid targetUserId') - // } - // }) + it('fails on invalid targetUserId', () => { + let error + + try { + getUserUsername('66b941110938786955ecf3b5', '156461') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) afterEach(() => User.deleteMany()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/makeReview.js b/staff/marti-herms/project/G-HUB/core/logic/makeReview.js index 87bcf780c..0c208c0a6 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/makeReview.js +++ b/staff/marti-herms/project/G-HUB/core/logic/makeReview.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, gameId, comment, rate = 0) => { - validate.string(userId, 'userId') - validate.string(gameId, 'gameId') + validate.id(userId, 'userId') + validate.id(gameId, 'gameId') validate.string(comment, 'comment') validate.number(rate, 'rate') diff --git a/staff/marti-herms/project/G-HUB/core/logic/makeReview.spec.js b/staff/marti-herms/project/G-HUB/core/logic/makeReview.spec.js index b2f8df8b2..d20f4505f 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/makeReview.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/makeReview.spec.js @@ -82,6 +82,19 @@ describe('makeReview', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + makeReview('123', '66b9e687cfa7ce5e041652b3', 'great game', 3) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string gameId', () => { let error @@ -95,6 +108,19 @@ describe('makeReview', () => { } }) + it('fails on invalid gameId', () => { + let error + + try { + makeReview('66b9e687cfa7ce5e041652b3', '132', 'great game', 3) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid gameId') + } + }) + it('fails on non-string comment', () => { let error diff --git a/staff/marti-herms/project/G-HUB/core/logic/openChat.js b/staff/marti-herms/project/G-HUB/core/logic/openChat.js index 9293ce191..7668cad5c 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/openChat.js +++ b/staff/marti-herms/project/G-HUB/core/logic/openChat.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return Promise.all([User.findById(userId).lean(), User.findById(targetUserId).lean(), Chat.findOne({ participants: { $all: [userId, targetUserId] } }).lean()]) .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js b/staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js index 916e54600..d5f72a004 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/openChat.spec.js @@ -78,6 +78,18 @@ describe('openChat', () => { } }) + it('fails on invalid userId', () => { + let error + try { + openChat('123', '66c0ae22930f95c985427ece') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error try { @@ -90,6 +102,18 @@ describe('openChat', () => { } }) + it('fails on invalid targetUserId', () => { + let error + try { + openChat('66c0ae22930f95c985427ece', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Chat.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/registerGame.js b/staff/marti-herms/project/G-HUB/core/logic/registerGame.js index 8758e59d3..7c7e651f2 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/registerGame.js +++ b/staff/marti-herms/project/G-HUB/core/logic/registerGame.js @@ -5,7 +5,7 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, name, image, description, link) => { - validate.string(userId, 'userId') + validate.id(userId, 'userId') validate.string(name, 'name') validate.string(image, 'image') validate.string(description, 'description') diff --git a/staff/marti-herms/project/G-HUB/core/logic/registerGame.spec.js b/staff/marti-herms/project/G-HUB/core/logic/registerGame.spec.js index 4da90e865..769899c73 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/registerGame.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/registerGame.spec.js @@ -59,6 +59,19 @@ describe('registerGame', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + registerGame('123', 'candy crush', img, 'candy crush game broh', link) + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string name', () => { let error diff --git a/staff/marti-herms/project/G-HUB/core/logic/searchGame.js b/staff/marti-herms/project/G-HUB/core/logic/searchGame.js index eb1ad5671..6d52dbd4c 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/searchGame.js +++ b/staff/marti-herms/project/G-HUB/core/logic/searchGame.js @@ -5,7 +5,7 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, query) => { - validate.string(userId, 'userId') + validate.id(userId, 'userId') validate.string(query, 'query') return User.findById(userId).lean() diff --git a/staff/marti-herms/project/G-HUB/core/logic/searchGame.spec.js b/staff/marti-herms/project/G-HUB/core/logic/searchGame.spec.js index 565752769..f0a920530 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/searchGame.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/searchGame.spec.js @@ -53,6 +53,19 @@ describe('searchGame', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + searchGame('123', 'candy crush') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string query', () => { let error diff --git a/staff/marti-herms/project/G-HUB/core/logic/searchUser.js b/staff/marti-herms/project/G-HUB/core/logic/searchUser.js index bfd0e8007..cbe71d4cd 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/searchUser.js +++ b/staff/marti-herms/project/G-HUB/core/logic/searchUser.js @@ -5,7 +5,7 @@ import { validate, errors } from 'com' const { NotFoundError, SystemError } = errors export default (userId, query) => { - validate.string(userId, 'userId') + validate.id(userId, 'userId') validate.string(query, 'query') return User.findById(userId).lean() diff --git a/staff/marti-herms/project/G-HUB/core/logic/searchUser.spec.js b/staff/marti-herms/project/G-HUB/core/logic/searchUser.spec.js index fb81640bf..4dede3f47 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/searchUser.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/searchUser.spec.js @@ -49,6 +49,19 @@ describe('searchUser', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + searchUser('123', 'candy crush') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string query', () => { let error diff --git a/staff/marti-herms/project/G-HUB/core/logic/sendMessage.js b/staff/marti-herms/project/G-HUB/core/logic/sendMessage.js index 3a724068d..7dfdc1794 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/sendMessage.js +++ b/staff/marti-herms/project/G-HUB/core/logic/sendMessage.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, chatId, content) => { - validate.string(userId, 'userId') - validate.string(chatId, 'chatId') + validate.id(userId, 'userId') + validate.id(chatId, 'chatId') validate.string(content, 'content') return Promise.all([User.findById(userId).lean(), Chat.findById(chatId).lean()]) diff --git a/staff/marti-herms/project/G-HUB/core/logic/sendMessage.spec.js b/staff/marti-herms/project/G-HUB/core/logic/sendMessage.spec.js index ea040cfdd..271d1af76 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/sendMessage.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/sendMessage.spec.js @@ -72,6 +72,19 @@ describe('sendMessage', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + sendMessage('123', '66c0ae22930f95c985427ece', 'hello') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string chatId', () => { let error @@ -85,6 +98,19 @@ describe('sendMessage', () => { } }) + it('fails on invalid chatId', () => { + let error + + try { + sendMessage('66c0ae22930f95c985427ece', '123', 'hello') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid chatId') + } + }) + it('fails on non-string content', () => { let error diff --git a/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.js b/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.js index 137c12e94..b19dc7130 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.js +++ b/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, gameId) => { - validate.string(userId, 'userId') - validate.string(gameId, 'gameId') + validate.id(userId, 'userId') + validate.id(gameId, 'gameId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.spec.js b/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.spec.js index d04ec0d24..89f796649 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/toggleAddGame.spec.js @@ -84,6 +84,19 @@ describe('toggleAddGame', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + toggleAddGame('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string gameId', () => { let error @@ -97,6 +110,19 @@ describe('toggleAddGame', () => { } }) + it('fails on invalid gameId', () => { + let error + + try { + toggleAddGame('66ba313a881fabd96394b179', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid gameId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.js b/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.js index a0406f791..da051e9ce 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.js +++ b/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, gameId) => { - validate.string(userId, 'userId') - validate.string(gameId, 'gameId') + validate.id(userId, 'userId') + validate.id(gameId, 'gameId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.spec.js b/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.spec.js index 3c7274c27..5ef839e19 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/toggleFavGame.spec.js @@ -84,6 +84,19 @@ describe('toggleFavGame', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + toggleFavGame('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string gameId', () => { let error @@ -97,6 +110,19 @@ describe('toggleFavGame', () => { } }) + it('fails on invalid gameId', () => { + let error + + try { + toggleFavGame('66ba313a881fabd96394b179', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid gameId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.js b/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.js index d3d510129..316a6b320 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.js +++ b/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.js @@ -5,8 +5,8 @@ import { validate, errors } from 'com' const { SystemError, NotFoundError } = errors export default (userId, targetUserId) => { - validate.string(userId, 'userId') - validate.string(targetUserId, 'targetUserId') + validate.id(userId, 'userId') + validate.id(targetUserId, 'targetUserId') return User.findById(userId).lean() .catch(error => { throw new SystemError(error.message) }) diff --git a/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.spec.js b/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.spec.js index 04eed4bbb..18c83e095 100644 --- a/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.spec.js +++ b/staff/marti-herms/project/G-HUB/core/logic/toggleFollowUser.spec.js @@ -80,6 +80,19 @@ describe('toggleFollowUser', () => { } }) + it('fails on invalid userId', () => { + let error + + try { + toggleFollowUser('123', '66ba007f874aa7b84ec54491') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid userId') + } + }) + it('fails on non-string targetUserId', () => { let error @@ -93,6 +106,19 @@ describe('toggleFollowUser', () => { } }) + it('fails on invalid targetUserId', () => { + let error + + try { + toggleFollowUser('66ba313a881fabd96394b179', '123') + } catch (_error) { + error = _error + } finally { + expect(error).to.be.instanceOf(ValidationError) + expect(error.message).to.equal('invalid targetUserId') + } + }) + afterEach(() => Promise.all([User.deleteMany(), Game.deleteMany()])) after(() => mongoose.disconnect()) diff --git a/staff/marti-herms/project/G-HUB/doc/README.md b/staff/marti-herms/project/G-HUB/doc/README.md index 8b74deb79..0dda1f750 100644 --- a/staff/marti-herms/project/G-HUB/doc/README.md +++ b/staff/marti-herms/project/G-HUB/doc/README.md @@ -13,16 +13,19 @@ A hub for gamers and developers to play or upload videogames - list favourites - add game to library - add game to favourites -- search games (by name, date, genre...) +- search games (by name) +- search users (by name) - view game details - play/download game - add review - delete review +- follow user +- chat with user +- view user profile **Dev User** - register game to site - list own game -- update own game ### UIUX Design @@ -50,7 +53,11 @@ A hub for gamers and developers to play or upload videogames - Node - Express - React -- Mongo +- Mongodb +- Mongoose +- JWT +- Bcrypt +- Tailwindcss ### Data Model @@ -64,6 +71,8 @@ A hub for gamers and developers to play or upload videogames - library ([Game.id]) - favs ([Game.id]) - games ([Game.id], optional) +- following ([User.id]) +- followers ([User.id]) **Game** - id (auto) @@ -73,8 +82,6 @@ A hub for gamers and developers to play or upload videogames - image (string) - description (string) - link (string) -- downloads (number) -- rating (number) - date (Date) **Review** @@ -85,3 +92,14 @@ A hub for gamers and developers to play or upload videogames - comment (string) - date (Date) +**Chat** +- id (auto) +- participants ([User.id]) +- messages (Message.id) +- date (Date) + +**Message** +- id (auto) +- author (User.id) +- content (string) +- date (Date) \ No newline at end of file