Skip to content

Commit

Permalink
solution 4.12*: Blog list tests, step5
Browse files Browse the repository at this point in the history
  • Loading branch information
patchamama committed Sep 10, 2023
1 parent 379490b commit fb25691
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ npm test
## b. Testing the backend

- [Exercises 4.8.-4.12.](https://fullstackopen.com/es/part4/probando_el_backend#ejercicios-4-8-4-12)
_Details solutions: [4.8](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/f4361d4a4973e98d2368596056f9257e1487565d) | [4.9](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/6b9903229f75ed7def60d4f81a386e61997a9341) | [4.10](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/d9fe5ba9dffc9e2d5c1b74bb88028436dd4629eb) | [4.11]() | [4.12]()_
_Details solutions: [4.8](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/f4361d4a4973e98d2368596056f9257e1487565d) | [4.9](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/6b9903229f75ed7def60d4f81a386e61997a9341) | [4.10](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/d9fe5ba9dffc9e2d5c1b74bb88028436dd4629eb) | [4.11](https://github.com/patchamama/fullstackopen-part4-bloglist/commit/379490b06d3469078cc9d2463910ae6b6f2270a3) | [4.12]()_

## c. User administration

Expand Down
10 changes: 7 additions & 3 deletions controllers/blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ blogsRouter.get('/', async (request, response) => {

blogsRouter.post('/', async (request, response) => {
request.body.likes = request.body.likes || 0
const blog = new Blog(request.body)
const result = blog.save()
response.status(201).json(result)
if (!request.body.title || !request.body.url) {
response.status(400).end()
} else {
const blog = new Blog(request.body)
const result = await blog.save()
response.status(201).json(result)
}
})

module.exports = blogsRouter
25 changes: 25 additions & 0 deletions tests/note_api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ test('if the likes property is missing from the request, it will default to the
}
})

test('if the title or url are missing from the request data, the backend responds 400 Bad Request', async () => {
// url is missing
let response = await api.post('/api/blogs').send({
title: 'test title',
author: 'A. Pacheco',
likes: 4,
})
expect(response.status).toBe(400)

// title is missing
response = await api.post('/api/blogs').send({
author: 'A. Pacheco',
likes: 4,
url: 'https://unlibroenmimochila.blogspot.com/2017/12/fugas-o-la-ansiedad-de-sentirse-vivo.html',
})
expect(response.status).toBe(400)

// both are missing (title and url)
response = await api.post('/api/blogs').send({
author: 'A. Pacheco',
likes: 4,
})
expect(response.status).toBe(400)
})

// test('blog without title is not added', async () => {
// const newBlog = {
// author: 'A. Pacheco',
Expand Down

0 comments on commit fb25691

Please sign in to comment.