Skip to content

Commit

Permalink
implement register Game logic and test in core b00tc4mp#84
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden23 committed Aug 12, 2024
1 parent aacbb11 commit cec8aef
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 1 deletion.
Empty file.
Empty file.
2 changes: 1 addition & 1 deletion staff/marti-herms/project/V-HUB/core/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const user = new Schema({
const game = new Schema({
enabled: {
type: Boolean,
required: true
default: false
},
author: {
type: ObjectId,
Expand Down
33 changes: 33 additions & 0 deletions staff/marti-herms/project/V-HUB/core/logic/registerGame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { User, Game } from '../data/models.js'

import { validate, errors } from 'com'

const { SystemError, NotFoundError } = errors

export default (userId, name, image, description, link) => {
validate.string(userId, 'userId')
validate.string(name, 'name')
validate.string(image, 'image')
validate.string(description, 'description')
validate.string(link, 'link')

return User.findById(userId).lean()
.catch(error => { throw new SystemError(error.message) })
.then(author => {
if (!author) throw new NotFoundError('user not found')

return Game.create({
author,
name,
image,
description,
link
})
.catch(error => { throw new SystemError(error.message) })
})
.then(game =>
User.findByIdAndUpdate(userId, { $push: { games: game.id } })
.catch(error => { throw new SystemError(error.message) })
)
.then(() => { })
}
117 changes: 117 additions & 0 deletions staff/marti-herms/project/V-HUB/core/logic/registerGame.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import { expect } from 'chai'

import registerGame from './registerGame.js'
import { User, Game } from '../data/models.js'

import { errors } from 'com'

const { ValidationError, NotFoundError } = errors

const img = 'https://store-images.s-microsoft.com/image/apps.54354.13510798882606697.7a42c472-75d7-487e-9538-ebb5ce1657e6.372723d8-dd1a-450a-9fed-d420e7705e4e?mode=scale&q=90&h=300&w=200'

const link = 'https://www.microsoft.com/en-us/p/candy-crush-saga/9nblggh18846?activetab=pivot:overviewtab'

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

beforeEach(() => User.deleteMany())

it('succeds on new game', () =>
User.create({ username: 'monoloco', email: '[email protected]', password: '12312323' })
.then(user => {
return registerGame(user.id, 'candy crush', img, 'candy crush game broh', link)
.then(() => User.findById(user.id))
})
.then(user => {
Game.findOne({ name: 'candy crush' })
.then(game => {
expect(user.games[0]).to.equal(game.id)
expect(game.img).to.equal(img)
expect(game.description).to.equal('candy crush game broh')
expect(game.link).to.equal(link)
})
})
)

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

return registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', img, 'candy crush game broh', link)
.catch(_error => error = _error)
.finally(() => {
expect(error).to.be.instanceof(NotFoundError)
expect(error.message).to.equal('user not found')
})
})

it('fails on non-string 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('userId is not a string')
}
})

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

try {
registerGame('66b9e687cfa7ce5e041652b3', 213, img, 'candy crush game broh', link)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('name is not a string')
}
})

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

try {
registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', 123, 'candy crush game broh', link)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('image is not a string')
}
})

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

try {
registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', img, 123, link)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('description is not a string')
}
})

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

try {
registerGame('66b9e687cfa7ce5e041652b3', 'candy crush', img, 'candy crush game broh', 6123)
} catch (_error) {
error = _error
} finally {
expect(error).to.be.instanceOf(ValidationError)
expect(error.message).to.equal('link is not a string')
}
})

afterEach(() => User.deleteMany())

after(() => mongoose.disconnect())
})
17 changes: 17 additions & 0 deletions staff/marti-herms/project/V-HUB/core/logic/registerGame.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dotenv/config'
import { User, Game } from '../data/models.js'

import registerGame from './registerGame.js'

import mongoose from 'mongoose'

const img = 'https://store-images.s-microsoft.com/image/apps.54354.13510798882606697.7a42c472-75d7-487e-9538-ebb5ce1657e6.372723d8-dd1a-450a-9fed-d420e7705e4e?mode=scale&q=90&h=300&w=200'

const link = 'https://www.microsoft.com/en-us/p/candy-crush-saga/9nblggh18846?activetab=pivot:overviewtab'

mongoose.connect(process.env.MONGODB_URI)
.then(() => User.create({ username: 'monoloco', email: '[email protected]', password: '12312323' }))
.then(user => registerGame(user.id, 'candy crush', img, 'candy crush game broh', link))
.then(() => console.log('game registered'))
.catch(error => console.error(error))
.finally(() => mongoose.disconnect())

0 comments on commit cec8aef

Please sign in to comment.