Skip to content

Commit

Permalink
fix: endpoint responses as specified in docs/endpoint.md (#18)
Browse files Browse the repository at this point in the history
Fix #17
  • Loading branch information
matheuspiment authored Oct 9, 2019
1 parent 3a34566 commit d866fcd
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
15 changes: 13 additions & 2 deletions __tests__/integration/controllers/PostController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ describe('PostController', () => {
.delete(`/post/${createPostBody.post._id}`)
.set('Authorization', `Bearer ${loginBody.token}`)

expect(response.status).toBe(200)
expect(response.status).toBe(204)
})

it('should not delete a post with a non-existent postId', async () => {
Expand All @@ -161,8 +161,19 @@ describe('PostController', () => {
password: user.password
})

const post = await factory.attrs('Post')

const { body: createPostBody } = await request(app)
.post('/create/post')
.set('Authorization', `Bearer ${loginBody.token}`)
.send(post)

await request(app)
.delete(`/post/${createPostBody.post._id}`)
.set('Authorization', `Bearer ${loginBody.token}`)

const response = await request(app)
.delete(`/post/a${5}`)
.delete(`/post/${createPostBody.post._id}`)
.set('Authorization', `Bearer ${loginBody.token}`)

expect(response.status).toBe(400)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/integration/controllers/UserController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('UserController', () => {
.post('/register')
.send(user)

expect(response.body).toHaveProperty('_id')
expect(response.body).toHaveProperty('user')
})

it('should not be able to register with duplicated email', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/AuthController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AuthController {
})

if (!(await schema.isValid(req.body))) {
return res.status(400).json({ error: 'Validation fails' })
return res.status(400).json({ error: 'Invalid argument(s)' })
}

try {
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/CityController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CityController {
const schema = Yup.string().required()

if (!(await schema.isValid(req.query.search))) {
return res.status(400).json({ error: 'Validation fails' })
return res.status(400).json({ error: 'Invalid argument(s)' })
}

try {
Expand All @@ -35,7 +35,7 @@ class CityController {

return res.status(200).json({ cities: normalizedCities })
} catch (error) {
return res.status(400).json({ error: 'Unable to search cities' })
return res.status(400).json({ error: 'Failed to search cities' })
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/controllers/PostController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class PostController {
})

if (!(await schema.isValid(req.body))) {
return res.status(400).json({ error: 'Validation fails' })
return res.status(400).json({ error: 'Invalid argument(s)' })
}

try {
const post = await Post.create({ ...req.body, user: req.userId })

return res.json({ post })
return res.status(201).json({ post })
} catch (error) {
return res.status(400).json({ error: 'Creation failed' })
return res.status(400).json({ error: 'Failed to create post' })
}
}

Expand All @@ -29,17 +29,21 @@ class PostController {

return res.json({ posts })
} catch (error) {
return res.status(400).json({ error: 'Listation failed' })
return res.status(400).json({ error: 'Failed to list posts' })
}
}

async delete (req: AuthRequest, res: Response): Promise<Response> {
try {
await Post.findByIdAndRemove(req.params.postId)
const result = await Post.findByIdAndRemove(req.params.postId)

return res.status(200).send()
if (!result) {
return res.status(400).json({ error: 'Post not existent' })
}

return res.status(204).send()
} catch (error) {
return res.status(400).json({ error: 'Deletation failed' })
return res.status(400).json({ error: 'Failed to delete post' })
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express'
import * as Yup from 'yup'
import pick from 'lodash/pick'
import omit from 'lodash/omit'

import User from '../schemas/User'
import CheckUserEmail from '../services/CheckUserEmail'
Expand All @@ -14,7 +14,7 @@ class UserController {
})

if (!(await schema.isValid(req.body))) {
return res.status(400).json({ error: 'Validation fails' })
return res.status(400).json({ error: 'Invalid argument(s)' })
}

try {
Expand All @@ -25,9 +25,9 @@ class UserController {
}

const user = await User.create(req.body)
return res.json(pick(user.toObject(), ['_id', 'name', 'email']))
return res.status(201).json({ user: omit(user.toObject(), ['password']) })
} catch (err) {
return res.status(400).json({ error: 'Registration failed' })
return res.status(400).json({ error: 'Failed to register user' })
}
}
}
Expand Down

0 comments on commit d866fcd

Please sign in to comment.