Skip to content

Commit

Permalink
add logic and tests in API for retrieving all posts b00tc4mp#482
Browse files Browse the repository at this point in the history
  • Loading branch information
juditta99 committed Apr 29, 2024
1 parent 62567e7 commit 3379ead
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default (req, res) => {
}
res.status(status).json({
error: error.constructor.name,
message: error.constructor.message,
message: error.message,
})
}
}
2 changes: 2 additions & 0 deletions staff/judy-grayland/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import registerUserHandler from './registerUserHandler.js'
import authenticateUserHandler from './authenticateUserHandler.js'
import createResourceHandler from './createResourceHandler.js'
import createActivityHandler from './createActivityHandler.js'
import retrieveResourcesHandler from './retrieveResourcesHandler.js'

export {
registerUserHandler,
authenticateUserHandler,
createResourceHandler,
createActivityHandler,
retrieveResourcesHandler,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logic from '../logic/index.js'
import { errors } from 'shared'
const { NotFoundError } = errors

export default (req, res) => {
try {
logic
.retrieveResources()
.then((resources) => res.json(resources))
.catch((error) => {
let status = 500
if (error instanceof NotFoundError) {
status = 404
}
res.status(status).json({
error: error.constructor.name,
message: error.constructor.message,
})
return
})
} catch (error) {
let status = 500

if (error instanceof ContentError || error instanceof TypeError) {
status = 406
}
res.status(status).json({
error: error.constructor.name,
message: error.message,
})
}
}
4 changes: 4 additions & 0 deletions staff/judy-grayland/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
authenticateUserHandler,
createActivityHandler,
createResourceHandler,
retrieveResourcesHandler,
} from './handlers/index.js'
import retrieveResources from './logic/retrieveResources.js'

dotenv.config()

Expand All @@ -34,6 +36,8 @@ mongoose

server.post('/resources', jsonBodyParser, createResourceHandler)

server.get('/resources', retrieveResourcesHandler)

// declaring endpoint for categories path
// server.get('/categories', (req, res) => {
// Category.find()
Expand Down
59 changes: 50 additions & 9 deletions staff/judy-grayland/project/api/logic/createResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('createResource', () => {
const title = random.title()
const description = random.description()
const resourceType = 'book'
const tag = ['lgbt+', 'diversidad funcional']
const topic = ['lgbt+', 'diversidad funcional']
const link = random.link()
const image = random.image()
const author = 'Roald Dahl'
Expand All @@ -30,7 +30,7 @@ describe('createResource', () => {
description,
resourceType,
link,
tag,
topic,
image,
author,
ageRange,
Expand All @@ -40,7 +40,7 @@ describe('createResource', () => {
expect(resource.description).to.equal(description)
expect(resource.resourceType).to.equal(resourceType)
// In Mongoose, when you retrieve an array field from a document, it returns a JS array object, not a plain array. This means that direct comparison using expect(resource.tag).to.equal(tag) may fail because they are two different array objects, even though they contain the same elements. To fix this, you can use Chai's deep equality assertion deep.equal() instead of equal() to compare arrays, because it looks manually through each element and compares them:
expect(resource.tag).to.deep.equal(tag)
expect(resource.topic).to.deep.equal(topic)
expect(resource.link).to.equal(link)
expect(resource.image).to.equal(image)
expect(resource.author).to.equal(author)
Expand All @@ -54,7 +54,7 @@ describe('createResource', () => {
const title = random.title()
const description = random.description()
const resourceType = 'activity'
const tag = ['igualdad de genero']
const topic = ['igualdad de genero']
const link = random.link()
const image = random.image()
const author = ''
Expand All @@ -64,7 +64,7 @@ describe('createResource', () => {
title,
description,
resourceType,
tag,
topic,
link,
image,
author,
Expand All @@ -75,7 +75,7 @@ describe('createResource', () => {
expect(resource.description).to.equal(description)
expect(resource.link).to.equal(link)
expect(resource.resourceType).to.equal(resourceType)
expect(resource.tag).to.deep.equal(tag)
expect(resource.topic).to.deep.equal(topic)
expect(resource.image).to.equal(image)
expect(resource.author).to.equal('')
expect(resource.ageRange).to.deep.equal([''])
Expand All @@ -88,7 +88,7 @@ describe('createResource', () => {
const title = random.title()
const description = random.description()
const resourceType = ''
const tag = ['igualdad de genero']
const topic = ['igualdad de genero']
const link = random.link()
const image = random.image()
const author = ''
Expand All @@ -98,7 +98,7 @@ describe('createResource', () => {
title,
description,
resourceType,
tag,
topic,
link,
image,
author,
Expand All @@ -108,8 +108,49 @@ describe('createResource', () => {
throw new Error('should not reach this point')
})
.catch((error) => {
expect(error).to.be.instanceof(ContentError)
expect(error).to.be.instanceOf(ContentError)
expect(error.message).to.equal('resourceType is empty')
})
})
})

// UNHAPPY path - book on already existing resources
it('fails on already existing resource', () => {
const title = random.title()
const description = random.description()
const resourceType = ''
const topic = ['igualdad de genero']
const link = random.link()
const image = random.image()
const author = ''
const ageRange = ''

return Resource.create({
title,
description,
resourceType,
topic,
link,
image,
author,
ageRange,
}).then((resource) => {
return createResource(
title,
description,
resourceType,
topic,
link,
image,
author,
ageRange
)
.then(() => {
throw new Error('should not reach this point')
})
.catch((error) => {
expect(error).to.be.instanceOf(DuplicityError)
expect(error.message).to.equal('resource already exists')
})
})
})
2 changes: 2 additions & 0 deletions staff/judy-grayland/project/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import registerUser from './registerUser.js'
import authenticateUser from './authenticateUser.js'
import createActivity from './createActivity.js'
import createResource from './createResource.js'
import retrieveResources from './retrieveResources.js'

const logic = {
registerUser,
authenticateUser,
createActivity,
createResource,
retrieveResources,
}

export default logic
18 changes: 18 additions & 0 deletions staff/judy-grayland/project/api/logic/retrieveResources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { errors } from 'shared'
const { SystemError } = errors

import { Resource } from '../data/models.js'

function retrieveResources() {
return Resource.find()
.select('-__v')
.lean()
.catch((error) => {
throw new SystemError(error.message)
})
.then((resources) => {
return resources
})
}

export default retrieveResources
17 changes: 17 additions & 0 deletions staff/judy-grayland/project/api/logic/retrieveResources.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import { expect } from 'chai'

import retrieveResources from './retrieveResources.js'

dotenv.config()

describe('retrieveResources', () => {
before(() => mongoose.connect(process.env.MONGODB_URL_TEST))

it('succeeds', () => {
return retrieveResources().then(() => {
expect()
})
})
})
17 changes: 17 additions & 0 deletions staff/judy-grayland/project/api/logic/retrieveResources.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import dotenv from 'dotenv'
import mongoose from 'mongoose'
import retrieveResources from './retrieveResources.js'

dotenv.config()

mongoose.connect(process.env.MONGODB_URL_TEST).then(() => {
try {
retrieveResources()
.then(() => {
console.log('resources retrieved correctly')
})
.catch((error) => console.error(error))
} catch (error) {
console.error(error)
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ curl 'http://localhost:9000/resources' \
-d '{ "title": "Taller de prevención del acoso 2",
"description": "Charlas de 22 horas de duración para niños de 4,5 y 6 de primaria, para el profesorado y las familias. Impartido en horario escolar. Fundación Anar",
"resourceType": "activity",
"tag": ["bullying", "diversidad cultural"],
"topic": ["bullying", "diversidad cultural"],
"image": "https://www.anar.org/wp-content/uploads/2024/02/Logo-ANAR.png",
"link": "https://www.anar.org/colegios-e-institutos/"
}' \
Expand All @@ -20,7 +20,7 @@ curl 'http://localhost:9000/resources' \
-d '{ "title": "Por cuatro esquinitas de nada",
"description": "Un cuadrado nunca va a dejar de ser un cuadrado, por mucho que intenten cambiarlo los circulitos. Una historia de aceptación e inclusión",
"resourceType": "book",
"tag": ["diversidad funcional", "diversidad cultural", "lgtb+"],
"topic": ["diversidad funcional", "diversidad cultural", "lgtb+"],
"author": "Jerome Ruillier",
"image": "https://www.editorialjuventud.es/wp-content/uploads/cover-por-cuatro-esquinitas.jpg",
"ageRange": ["infantil", "5-6"]
Expand All @@ -35,7 +35,7 @@ curl 'http://localhost:9000/resources' \
-d '{ "title": "Taller de prevención del acoso 2",
"description": "Charlas de 22 horas de duración para niños de 4,5 y 6 de primaria, para el profesorado y las familias. Impartido en horario escolar. Fundación Anar",
"resourceType": "activity",
"tag": ["bullying", "diversidad cultural"],
"topic": ["bullying", "diversidad cultural"],
"image": "https://www.anar.org/wp-content/uploads/2024/02/Logo-ANAR.png",
"link": "https://www.anar.org/colegios-e-institutos/"
}' \
Expand All @@ -47,7 +47,7 @@ curl 'http://localhost:9000/resources' \
-H 'Content-Type: application/json' \
-d '{ "title": "Por cuatro esquinitas de nada",
"description": "Cuadradito quiere jugar en casa de sus amigos Redonditos, pero no pasa por la puerta porque? ¡La puerta es redonda como sus amigos! ¡Tendremos que recortarte las esquinas! le dicen los redonditos. ¡Oh, no! dice Cuadradito ¡Me dolería mucho! ¿Qué podemos hacer? Cuadradito es diferente. Nunca será redondo.",
"tag": ["diversidad funcional", "diversidad cultural", "lgtb+"],
"topic": ["diversidad funcional", "diversidad cultural", "lgtb+"],
"author": "Jerome Ruillier",
"image": "https://www.editorialjuventud.es/wp-content/uploads/cover-por-cuatro-esquinitas.jpg"
}' \
Expand All @@ -60,7 +60,7 @@ curl 'http://localhost:9000/resources' \
-d '{ "title": "Por cuatro esquinitas de nada2",
"description": "Cuadradito quiere jugar en casa de sus amigos Redonditos, pero no pasa por la puerta porque? ¡La puerta es redonda como sus amigos! ¡Tendremos que recortarte las esquinas! le dicen los redonditos. ¡Oh, no! dice Cuadradito ¡Me dolería mucho! ¿Qué podemos hacer? Cuadradito es diferente. Nunca será redondo.",
"resourceType": "book",
"tag": ["diversidad funcional", "diversidad cultural", "lgtb+"],
"topic": ["diversidad funcional", "diversidad cultural", "lgtb+"],
"author": "Jerome Ruillier",
"image": "https://www.editorialjuventud.es/wp-content/uploads/cover-por-cuatro-esquinitas.jpg"
}' \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TEST 'retrieve-resources'

CASE 'resources retrieved correctly'

curl 'http://localhost:9000/resources' \


0 comments on commit 3379ead

Please sign in to comment.