Skip to content

Commit

Permalink
[#3] better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
b051 committed Mar 21, 2017
1 parent 4bceb47 commit dc644d7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/person/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ router.get('/', async ctx => {

router.post('/login', async ctx => {
const { email, password } = ctx.request.body
if (!email || !password) {
throw Boom.expectationFailed('require email and password', { email, password })
}
const person = await Person.unscoped().findOne({ where: { email } })
if (person) {
if (person.matchPassword(password)) {
Expand All @@ -22,7 +25,11 @@ router.post('/login', async ctx => {
token,
person: person.toSafeJSON()
}
} else {
throw Boom.forbidden('wrong password')
}
} else {
throw Boom.notFound('user not found')
}
})

Expand Down
25 changes: 24 additions & 1 deletion test/person/person.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,32 @@ describe("router - people", () => {
})

describe('/login', () => {
it('should throw on missing infomation', async () => {
const res = await app.post('/people/login')
expect(res.status).to.equal(417)
expect(res.body.message).to.equal('require email and password')
})

it('should throw on not existing user', async () => {
const res = await app.post('/people/login').send({
email: 'notexist@gmail.com',
password: 'password'
})
expect(res.status).to.equal(404)
expect(res.body.message).to.equal('user not found')
})

it('should throw on not matching password', async () => {
const res = await app.post('/people/login').send({
email: 'shengning@gmail.com',
password: 'wrong password'
})
expect(res.status).to.equal(403)
expect(res.body.message).to.equal('wrong password')
})

it('should login', async () => {
let res = await app.post('/people/login').send({
const res = await app.post('/people/login').send({
email: 'shengning@gmail.com',
password: 'password'
})
Expand Down

0 comments on commit dc644d7

Please sign in to comment.