Merge pull request #68 from fmarier/use_400_error_code

Use 400 error code for missing parameters
This commit is contained in:
Francois Marier 2014-09-29 17:00:11 -07:00
Родитель 00b584e3be 3fd870899c
Коммит 4297a4d5c9
6 изменённых файлов: 24 добавлений и 20 удалений

Просмотреть файл

@ -90,16 +90,17 @@ function normalizedEmail(rawEmail) {
api.post(
'/check',
function (req, res, next) {
var email = normalizedEmail(req.body.email)
var email = req.body.email
var ip = req.body.ip
var action = req.body.action
if (!email || !ip || !action) {
var err = {code: 'MissingParameters', message: 'email, ip and action are all required'}
log.error({ op: 'request.failedLoginAttempt', email: email, ip: ip, action: action, err: err })
res.send(500, err)
res.send(400, err)
return next()
}
email = normalizedEmail(email)
fetchRecords(email, ip)
.spread(
@ -141,14 +142,15 @@ api.post(
api.post(
'/failedLoginAttempt',
function (req, res, next) {
var email = normalizedEmail(req.body.email)
var email = req.body.email
var ip = req.body.ip
if (!email || !ip) {
var err = {code: 'MissingParameters', message: 'email and ip are both required'}
log.error({ op: 'request.failedLoginAttempt', email: email, ip: ip, err: err })
res.send(500, err)
next()
res.send(400, err)
return next()
}
email = normalizedEmail(email)
fetchRecords(email, ip)
.spread(
@ -182,13 +184,14 @@ api.post(
api.post(
'/passwordReset',
function (req, res, next) {
var email = normalizedEmail(req.body.email)
var email = req.body.email
if (!email) {
var err = {code: 'MissingParameters', message: 'email is required'}
log.error({ op: 'request.passwordReset', email: email, err: err })
res.send(500, err)
next()
res.send(400, err)
return next()
}
email = normalizedEmail(email)
mc.getAsync(email)
.then(EmailRecord.parse, EmailRecord.parse)
@ -215,13 +218,14 @@ api.post(
api.post(
'/blockEmail',
function (req, res, next) {
var email = normalizedEmail(req.body.email)
var email = req.body.email
if (!email) {
var err = {code: 'MissingParameters', message: 'email is required'}
log.error({ op: 'request.blockEmail', email: email, err: err })
res.send(500, err)
next()
res.send(400, err)
return next()
}
email = normalizedEmail(email)
handleBan({ ban: { email: email } })
.then(
@ -247,8 +251,8 @@ api.post(
if (!ip) {
var err = {code: 'MissingParameters', message: 'ip is required'}
log.error({ op: 'request.blockIp', ip: ip, err: err })
res.send(500, err)
next()
res.send(400, err)
return next()
}
handleBan({ ban: { ip: ip } })

Просмотреть файл

@ -48,7 +48,7 @@ test(
function (t) {
client.post('/blockEmail', {},
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()

Просмотреть файл

@ -48,7 +48,7 @@ test(
function (t) {
client.post('/blockIp', {},
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()

Просмотреть файл

@ -50,7 +50,7 @@ test(
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP },
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()
@ -64,7 +64,7 @@ test(
function (t) {
client.post('/check', {},
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()

Просмотреть файл

@ -48,7 +48,7 @@ test(
function (t) {
client.post('/failedLoginAttempt', { email: TEST_EMAIL },
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()
@ -62,7 +62,7 @@ test(
function (t) {
client.post('/failedLoginAttempt', {},
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()

Просмотреть файл

@ -47,7 +47,7 @@ test(
function (t) {
client.post('/passwordReset', {},
function (err, req, res, obj) {
t.equal(res.statusCode, 500, 'bad request returns a 500')
t.equal(res.statusCode, 400, 'bad request returns a 400')
t.type(obj.code, 'string', 'bad request returns an error code')
t.type(obj.message, 'string', 'bad request returns an error message')
t.end()