forked from b00tc4mp/isdi-parttime-202403
-
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.
update validation logic and add tests; fix miscellaneous bugs. WIP b0…
- Loading branch information
Showing
31 changed files
with
554 additions
and
124 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
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
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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import 'dotenv/config' | ||
import mongoose from 'mongoose' | ||
import mongoose, { Types } from 'mongoose' | ||
import bcrypt from 'bcryptjs' | ||
|
||
import { expect } from 'chai' | ||
|
@@ -8,7 +8,9 @@ import { Recipe, User } from '../data/index.js' | |
|
||
import createRecipe from './createRecipe.js' | ||
|
||
import { ContentError } from 'com/errors.js' | ||
import { ContentError, NotFoundError } from 'com/errors.js' | ||
|
||
const { ObjectId } = Types | ||
|
||
const { MONGODB_URL_TEST } = process.env | ||
|
||
|
@@ -24,41 +26,51 @@ describe('createRecipe', () => { | |
bcrypt.hash('123123123', 8) | ||
.then(hash => User.create({ name: 'Super', surname: 'Chef', email: '[email protected]', username: 'Superchef', password: hash })) | ||
.then(user => | ||
createRecipe(user.id, 'Hello Recipe', 'sugar', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, ['sugar', 'chocolate'], 'funcionará?', 1) | ||
createRecipe(user.id, 'Hello Recipe', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 'funcionará?', 1) | ||
.then(() => Recipe.findOne()) | ||
.then(recipe => { | ||
|
||
// Convertir cada ingrediente a un objeto simple usando map y toObject() | ||
const ingredients = recipe.ingredients.map(ingredient => ingredient.toObject ? ingredient.toObject() : ingredient) | ||
|
||
// Eliminar propiedades adicionales de Mongoose (_id, __v) | ||
ingredients.forEach(ingredient => { | ||
delete ingredient._id | ||
delete ingredient.__v | ||
}) | ||
|
||
expect(recipe.author.toString()).to.equal(user.id) | ||
expect(recipe.title).to.equal('Hello Recipe') | ||
expect(recipe.source).to.equal('sugar') | ||
expect(recipe.thumbnail).to.equal("https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g") | ||
expect(recipe.cookTime).to.equal(2) | ||
expect(recipe.ingredients).to.equal(['sugar', 'chocolate']) | ||
expect(recipe.rating).to.equal(1) | ||
expect(ingredients).to.deep.equal([{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }]) | ||
expect(recipe.description).to.equal('funcionará?') | ||
expect(recipe.rating).to.equal(1) | ||
|
||
|
||
}) | ||
) | ||
) | ||
it('fails on non-existing user', () => { | ||
let errorThrown | ||
|
||
return createRecipe('Juanito', 'Hello Recipe', 'sugar', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, '100gr sugar, 50gr chocolate', 'funcionará?', 1) | ||
return createRecipe(new ObjectId().toString(), 'Hello Recipe', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 'funcionará?', 1) | ||
.catch(error => errorThrown = error) | ||
.finally(() => { | ||
expect(errorThrown).to.be.instanceOf(NotFoundError); | ||
expect(errorThrown.message).to.equal('user not found'); | ||
expect(errorThrown).to.be.an.instanceOf(NotFoundError) | ||
expect(errorThrown.message).to.equal('user not found') | ||
}) | ||
}) | ||
|
||
it('fails on invalid userId', () => { | ||
let errorThrown | ||
|
||
try { | ||
createRecipe(123456, 'Hello Recipe', 'sugar, chocolate', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, '100gr sugar, 50gr chococale', 'funcionará?', 1) | ||
createRecipe(123456, 'Hello Recipe', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 'funcionará?', 1) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.instanceOf(ContentError) | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('userId is not valid') | ||
} | ||
}) | ||
|
@@ -67,11 +79,11 @@ describe('createRecipe', () => { | |
let errorThrown | ||
|
||
try { | ||
createRecipe(new ObjectId().toString(), 123456789, 'sugar, chocolate', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, '100gr sugar, 50gr chocolate', 'funcionará?', 1) | ||
createRecipe(new ObjectId().toString(), 123456789, "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 'funcionará?', 1) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.instanceOf(ContentError) | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('title is not valid') | ||
} | ||
}) | ||
|
@@ -80,11 +92,11 @@ describe('createRecipe', () => { | |
let errorThrown | ||
|
||
try { | ||
createRecipe(new ObjectId().toString(), 'Hello Recipe', 'sugar, chocolate', 123456789, 2, '100gr sugar, 50gr chocolate', 'funcionará?', 1) | ||
createRecipe(new ObjectId().toString(), 'Hello Recipe', 123456789, 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 'funcionará?', 1) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.instanceOf(ContentError) | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('image is not valid') | ||
} | ||
}) | ||
|
@@ -93,15 +105,42 @@ describe('createRecipe', () => { | |
let errorThrown | ||
|
||
try { | ||
createRecipe(new ObjectId().toString(), 'Hello Recipe', 'sugar, chocolate', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, '100gr sugar, 50gr chocolate', 123456789, 1) | ||
createRecipe(new ObjectId().toString(), 'Hello Recipe', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 123456789, 1) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.instanceOf(ContentError) | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('description is not valid') | ||
} | ||
}) | ||
|
||
it('fails on invalid rating', () => { | ||
let errorThrown | ||
|
||
try { | ||
createRecipe(new ObjectId().toString(), 'Hello Recipe', "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], 'funcionará?', 9) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('rating must be a number between 1 and 5.') | ||
} | ||
}) | ||
|
||
|
||
it('fails on missing title', () => { | ||
let errorThrown | ||
|
||
try { | ||
createRecipe(new ObjectId().toString(), '', [{ name: 'milk', quantity: 20, unit: 'ml' }, { name: 'chocolate', quantity: 1, unit: 'gr' }], "https://media.giphy.com/media/2kXOYTdyGPbIBISFn5/giphy.gif?cid=6c09b9525munegsuq607a67vn2oks57tip5c8ptumlx95ba7&ep=v1_gifs_trending&rid=giphy.gif&ct=g", 2, 'funcionará?', 1) | ||
} catch (error) { | ||
errorThrown = error | ||
} finally { | ||
expect(errorThrown).to.be.an.instanceOf(ContentError) | ||
expect(errorThrown.message).to.equal('title is not valid') | ||
} | ||
}) | ||
|
||
after(() => Promise.all([Recipe.deleteMany(), User.deleteMany()]).then(() => mongoose.disconnect())) | ||
}) | ||
|
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
Oops, something went wrong.