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 register Game logic and test in core b00tc4mp#84
- Loading branch information
Showing
6 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
Empty file.
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
33 changes: 33 additions & 0 deletions
33
staff/marti-herms/project/V-HUB/core/logic/registerGame.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,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
117
staff/marti-herms/project/V-HUB/core/logic/registerGame.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,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
17
staff/marti-herms/project/V-HUB/core/logic/registerGame.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,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()) |