forked from b00tc4mp/isdi-parttime-202309
-
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.
- Loading branch information
Showing
9 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
staff/belen-ivars/project/api/handlers/deleteRecipeHandler.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,21 @@ | ||
import { ContentError, NotFoundError } from "com/errors.js" | ||
import logic from "../logic/index.js" | ||
|
||
export default async (req, res) => { | ||
const userId = req.params.id | ||
const recipeId = req.body | ||
|
||
try { | ||
await logic.deleteRecipe(userId, recipeId) | ||
|
||
res.status(204).send() | ||
} catch (error) { | ||
let status = 500 | ||
if (error instanceof NotFoundError) | ||
status = 404 | ||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 500 | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { validate } from "com" | ||
import { User, Recipe } from '../data/models.js' | ||
import { ContentError, NotFoundError } from "com/errors.js" | ||
|
||
async function deleteRecipe(userId, recipeId) { | ||
validate.id(userId, 'user id') | ||
validate.id(recipeId, 'recipe id') | ||
|
||
const user = await User.findById(userId) | ||
|
||
if (!user) | ||
throw new NotFoundError('user not found') | ||
console.log('user founded') | ||
|
||
const recipe = await Recipe.findById(recipeId) | ||
if (user.id !== recipe.author.toString()) | ||
throw new NotFoundError('recipe does not belong to that user') | ||
|
||
try { | ||
await Recipe.findByIdAndDelete(recipe.id) | ||
} catch (error) { | ||
throw new ContentError('recipe cannot be deleted') | ||
} | ||
} | ||
|
||
export default deleteRecipe |
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,80 @@ | ||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
|
||
import mongoose, { mongo } from 'mongoose' | ||
|
||
import { expect } from 'chai' | ||
import random from './helpers/random.js' | ||
import deleteRecipe from './deleteRecipe.js' | ||
import { errors } from 'com' | ||
import { User, Recipe } from '../data/models.js' | ||
import { NotFoundError } from 'com/errors.js' | ||
const { ObjectId } = mongoose.Types | ||
const { DuplicityError } = errors | ||
|
||
describe('deleteRecipe', () => { | ||
before(async () => await mongoose.connect(process.env.TEST_MONGODB_URL)) | ||
beforeEach(async () => await User.deleteMany()) | ||
|
||
it('on success', async () => { | ||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
const user = await User.create({ name, email, password }) | ||
|
||
const title = random.name() | ||
const description = random.text() | ||
const image = random.image() | ||
const author = user.id | ||
const recipe = await Recipe.create({ author, title, description, image }) | ||
|
||
try { | ||
await deleteRecipe(user.id, recipe.id) | ||
} catch (error) { | ||
expect(recipe).not.exist | ||
} | ||
|
||
}) | ||
|
||
it('fails on non existing user', async () => { | ||
|
||
const title = random.name() | ||
const description = random.text() | ||
const image = random.image() | ||
const author = new ObjectId().toString() | ||
const recipe = await Recipe.create({ author, title, description, image }) | ||
|
||
try { | ||
await deleteRecipe(recipe.id, new ObjectId().toString()) | ||
throw new Error('should not reach this point') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(NotFoundError) | ||
expect(error.message).to.equal('user not found') | ||
} | ||
}) | ||
|
||
it('fails on non author user', async () => { | ||
|
||
const name = random.name() | ||
const email = random.email() | ||
const password = random.password() | ||
const user = await User.create({ name, email, password }) | ||
|
||
const title = random.name() | ||
const description = random.text() | ||
const image = random.image() | ||
const author = new ObjectId().toString() | ||
const recipe = await Recipe.create({ author, title, description, image }) | ||
|
||
try { | ||
await deleteRecipe(user.id, recipe.id) | ||
throw new Error('should not reach this point') | ||
} catch (error) { | ||
expect(error).to.be.instanceOf(NotFoundError) | ||
expect(error.message).to.equal('recipe does not belong to that user') | ||
} | ||
}) | ||
|
||
|
||
after(async () => await 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
import mongoose from 'mongoose' | ||
|
||
import deleteRecipe from './deleteRecipe.js' | ||
|
||
(async () => { | ||
await mongoose.connect(process.env.MONGODB_URL) | ||
try { | ||
await deleteRecipe('65d656eaa92e85c9f9fa6d71', '65f97adbd699fa7945c5d178') | ||
|
||
console.log('recipe has been deleted') | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})() |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
source pepetest.sh | ||
|
||
TEST 'delete-recipe' | ||
|
||
CASE 'success on current user' | ||
|
||
curl 'http://localhost:9000/recipes/65d655fac1dd88f9aee917d6' \ | ||
-H 'Authorization: Bearer 65d655fac1dd88f9aee917d6' \ | ||
-d '{"recipeId": "ObjectId('65f97adbd699fa7945c5d178')"}' \ | ||
-X DELETE \ | ||
-v | ||
|
||
# > POST /recipes HTTP/1.1 | ||
# > Host: localhost:9000 | ||
# > User-Agent: curl/8.4.0 | ||
# > Accept: */* | ||
# > Content-Type: application/json | ||
# > Content-Length: 187 | ||
# > | ||
# < HTTP/1.1 201 Created | ||
# < X-Powered-By: Express | ||
# < Access-Control-Allow-Origin: * | ||
# < Date: Thu, 21 Mar 2024 17:45:59 GMT | ||
# < Connection: keep-alive | ||
# < Keep-Alive: timeout=5 | ||
# < Content-Length: 0 | ||
|
||
CASE "fail on non existing user" | ||
|
||
curl 'http://localhost:9000/recipes' \ | ||
-H 'Content-Type: application/json' \ | ||
-v | ||
|
||
# > POST /recipes HTTP/1.1 | ||
# > Host: localhost:9000 | ||
# > User-Agent: curl/8.4.0 | ||
# > Accept: */* | ||
# > Content-Type: application/json | ||
# > Content-Length: 187 | ||
# > | ||
# < HTTP/1.1 404 Not Found | ||
# < X-Powered-By: Express | ||
# < Access-Control-Allow-Origin: * | ||
# < Content-Type: application/json; charset=utf-8 | ||
# < Content-Length: 52 | ||
# < ETag: W/"34-Cs2INrsYwSHLSHCKVUFPEWh9NjI" | ||
# < Date: Thu, 21 Mar 2024 17:45:59 GMT | ||
# < Connection: keep-alive | ||
# < Keep-Alive: timeout=5 | ||
# < | ||
# * Connection #0 to host localhost left intact | ||
# {"error":"NotFoundError","message":"user not found"}% |