Skip to content

Commit

Permalink
more test
Browse files Browse the repository at this point in the history
  • Loading branch information
mezlet committed Mar 19, 2019
1 parent a8ffd71 commit 1584c02
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
22 changes: 7 additions & 15 deletions server/controllers/recipe-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,13 @@ class RecipeController {
static async updateRecipe(req, res) {
const { id } = req.user;
const { slug } = req.params;
const {
title,
ingredients,
steps,
cookingTime,
preparationTime
} = req.body;
const { ...newRecipe } = req.body;
try {
await validate(req.body, recipeUpdateSchema);
await validate(newRecipe, recipeUpdateSchema);
const recipe = await Recipe.findOne({ where: { slug } });

if (!recipe) {
return errorResponse(res, 'recipe not found', 404);
}

if (recipe.userId !== id) {
return errorResponse(
res,
Expand All @@ -92,11 +84,11 @@ class RecipeController {
}
const data = await recipe.update(
{
title: title || recipe.title,
ingredients: ingredients || recipe.ingredients,
cookingTime: cookingTime || recipe.cookingTime,
preparationTime: preparationTime || recipe.preparationTime,
steps: steps || recipe.steps
title: newRecipe.title || recipe.title,
ingredients: newRecipe.ingredients || recipe.ingredients,
cookingTime: newRecipe.cookingTime || recipe.cookingTime,
preparationTime: newRecipe.preparationTime || recipe.preparationTime,
steps: newRecipe.steps || recipe.steps
},
{ returning: true }
);
Expand Down
11 changes: 11 additions & 0 deletions test/recipes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ describe('Recipes', () => {
});
});

it('should return an error if recipe does not exist', done => {
chai
.request(server)
.put(`${baseUrl}/sometext`)
.set({ authorization: token })
.end((err, res) => {
expect(res).to.have.status(404);
done(err);
});
});

it('should return an error if user does not exist', done => {
chai
.request(server)
Expand Down

0 comments on commit 1584c02

Please sign in to comment.