From 0988a44fe47a4b8ac0e7fda44784999b6b85bac8 Mon Sep 17 00:00:00 2001 From: Andrew Trieu Date: Wed, 31 May 2023 21:14:33 +0300 Subject: [PATCH] Upload 4.12 --- part4/bloglist/controllers/blogs.js | 13 ++- part4/bloglist/requests/delete_blog.rest | 1 + part4/bloglist/requests/get_by_id.rest | 1 + part4/bloglist/requests/post_blog.rest | 2 +- part4/bloglist/tests/blog_api.test.js | 115 ++++++++++++++--------- 5 files changed, 88 insertions(+), 44 deletions(-) create mode 100644 part4/bloglist/requests/delete_blog.rest create mode 100644 part4/bloglist/requests/get_by_id.rest diff --git a/part4/bloglist/controllers/blogs.js b/part4/bloglist/controllers/blogs.js index f8748e7..3f6123d 100644 --- a/part4/bloglist/controllers/blogs.js +++ b/part4/bloglist/controllers/blogs.js @@ -13,11 +13,22 @@ blogsRouter.post('/', async (request, response) => { title: body.title, author: body.author, url: body.url, - likes: body.likes + likes: body.likes || 0 })).save() response.status(201).json(blog) }) +blogsRouter.get('/:id', async (request, response) => { + const blog = await Blog.findById(request.params.id) + if (blog) response.json(blog) + else response.status(404).end() +}) + +blogsRouter.delete('/:id', async (request, response) => { + await Blog.findByIdAndRemove(request.params.id) + response.status(204).end() +}) + module.exports = blogsRouter \ No newline at end of file diff --git a/part4/bloglist/requests/delete_blog.rest b/part4/bloglist/requests/delete_blog.rest new file mode 100644 index 0000000..3902d1b --- /dev/null +++ b/part4/bloglist/requests/delete_blog.rest @@ -0,0 +1 @@ +DELETE http://localhost:3001/api/blogs/64778b41a8f175e16455b293 \ No newline at end of file diff --git a/part4/bloglist/requests/get_by_id.rest b/part4/bloglist/requests/get_by_id.rest new file mode 100644 index 0000000..2293d17 --- /dev/null +++ b/part4/bloglist/requests/get_by_id.rest @@ -0,0 +1 @@ +GET http://localhost:3001/api/blogs/647788bc1d350a17ba599541 \ No newline at end of file diff --git a/part4/bloglist/requests/post_blog.rest b/part4/bloglist/requests/post_blog.rest index 2f69a1d..ccc6420 100644 --- a/part4/bloglist/requests/post_blog.rest +++ b/part4/bloglist/requests/post_blog.rest @@ -2,6 +2,6 @@ POST http://localhost:3001/api/blogs content-type: application/json { - "title": "Another blog", "author": "Andrew", "url": "www.andrew.eu", "likes": 1000 + "title": "Last blog", "author": "Andrew", "url": "www.andrew.eu", "likes": 1000 } \ No newline at end of file diff --git a/part4/bloglist/tests/blog_api.test.js b/part4/bloglist/tests/blog_api.test.js index 712a7d5..ce909e2 100644 --- a/part4/bloglist/tests/blog_api.test.js +++ b/part4/bloglist/tests/blog_api.test.js @@ -12,53 +12,84 @@ beforeEach(async () => { await Blog.insertMany(helper.initialBlogs) }) -test('blogs are returned as json', async () => { - await api - .get('/api/blogs') - .expect(200) - .expect('Content-Type', /application\/json/) +describe('getting blogs', () => { + test('blogs are returned as json', async () => { + await api + .get('/api/blogs') + .expect(200) + .expect('Content-Type', /application\/json/) + }) + + test('all blogs are returned', async () => { + const response = await api.get('/api/blogs') + expect(response.body).toHaveLength(helper.initialBlogs.length) + }) + + test('a specific blog is within the returned blogs', async () => { + const response = await api.get('/api/blogs') + const contents = response.body.map((r) => r.title) + expect(contents).toContain('Another blog') + }) }) -test('all blogs are returned', async () => { - const response = await api.get('/api/blogs') - expect(response.body).toHaveLength(helper.initialBlogs.length) +describe('adding blog', () => { + test('a valid blog can be added ', async () => { + const newBlog = { + title: 'Test', + author: 'Tester', + url: 'http:/test.com', + likes: 999 + } + + await api + .post('/api/blogs') + .send(newBlog) + .expect(201) + .expect('Content-Type', /application\/json/) + + const blogsAtEnd = await helper.blogsInDb() + expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length + 1) + const contents = blogsAtEnd.map((n) => n.title) + expect(contents).toContain('Test') + }) + + test('blog without title or url is not added', async () => { + const newBlog = { + author: 'Tester', + likes: 999 + } + + await api.post('/api/blogs').send(newBlog).expect(400) + + const blogsAtEnd = await helper.blogsInDb() + expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length) + }) + + test('blog without likes is added with 0 likes', async () => { + const newBlog = { + title: 'Another test', + author: 'Tester', + url: 'http:/test.com' + } + + await api + .post('/api/blogs') + .send(newBlog) + .expect(201) + .expect('Content-Type', /application\/json/) + + const blogsAtEnd = await helper.blogsInDb() + const addedBlog = blogsAtEnd.find(blog => blog.title === 'Another test') + expect(addedBlog.likes).toBe(0) + }) }) -test('a specific blog is within the returned blogs', async () => { - const response = await api.get('/api/blogs') - const contents = response.body.map((r) => r.title) - expect(contents).toContain('Another blog') -}) -test('a valid blog can be added ', async () => { - const newBlog = { - title: 'Test', - author: 'Tester', - url: 'http:/test.com', - likes: 999 - } - - await api - .post('/api/blogs') - .send(newBlog) - .expect(201) - .expect('Content-Type', /application\/json/) - - const blogsAtEnd = await helper.blogsInDb() - expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length + 1) - const contents = blogsAtEnd.map((n) => n.title) - expect(contents).toContain('Test') -}) - -test('blog without content is not added', async () => { - const newBlog = { - likes: 999 - } - - await api.post('/api/blogs').send(newBlog).expect(400) - - const blogsAtEnd = await helper.blogsInDb() - expect(blogsAtEnd).toHaveLength(helper.initialBlogs.length) +describe('blog id check', () => { + test('blog id is defined', async () => { + const response = await api.get('/api/blogs') + expect(response.body[0].id).toBeDefined() + }) }) afterAll(async () => {