refactor(test): Modify test cases to use promises instead of callbacks (#123) r=vladikoff

fixes #97
This commit is contained in:
Larissa Gaulia 2016-08-05 18:52:04 -03:00 коммит произвёл Vlad Filippov
Родитель 3a254c414f
Коммит 6fadc52bfa
8 изменённых файлов: 347 добавлений и 329 удалений

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

@ -86,21 +86,17 @@ function blockedIpCheck(cb) {
module.exports.blockedIpCheck = blockedIpCheck module.exports.blockedIpCheck = blockedIpCheck
function badLoginCheck(cb) { function badLoginCheck() {
setTimeout( // give memcache time to flush the writes return P.all([
function () { mc.getAsync(TEST_IP + TEST_EMAIL),
P.all([ mc.getAsync(TEST_IP)
mc.getAsync(TEST_IP + TEST_EMAIL), ])
mc.getAsync(TEST_IP) .spread(function (d1, d2) {
]) var ipEmailRecord = IpEmailRecord.parse(d1)
.spread(function (d1, d2) { var ipRecord = IpRecord.parse(d2)
var ier = IpEmailRecord.parse(d1) mc.end()
var ir = IpRecord.parse(d2) return {ipEmailRecord: ipEmailRecord, ipRecord: ipRecord}
mc.end() })
cb(ier.isOverBadLogins(), false, ir.isOverBadLogins())
})
}
)
} }
module.exports.badLoginCheck = badLoginCheck module.exports.badLoginCheck = badLoginCheck

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

@ -4,6 +4,7 @@
var test = require('tap').test var test = require('tap').test
var restify = require('restify') var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -15,8 +16,15 @@ var config = {
port: 7000 port: 7000
} }
} }
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -40,77 +48,73 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'missing email', 'missing email',
function (t) { function (t) {
client.post('/blockEmail', {}, return client.postAsync('/blockEmail', {})
function (err, req, res, obj) { .then(function (req, res, obj) {
t.equal(res.statusCode, 400, 'bad request returns a 400') //missing parameters
t.type(obj.code, 'string', 'bad request returns an error code') }, function(err){
t.type(obj.message, 'string', 'bad request returns an error message') t.equal(err.statusCode, 400, 'bad request returns a 400')
t.type(err.restCode, 'string', 'bad request returns an error code')
t.type(err.message, 'string', 'bad request returns an error message')
t.end() t.end()
} })
)
} }
) )
test( test(
'well-formed request', 'well-formed request',
function (t) { function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked') t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked') t.equal(obj.block, false, 'request was not blocked')
client.post('/blockEmail', { email: TEST_EMAIL }, return client.postAsync('/blockEmail', { email: TEST_EMAIL })
function (err, req, res, obj) { })
t.notOk(err, 'block request is successful') .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200') t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'check worked') .spread(function (req, res, obj) {
t.equal(obj.block, true, 'request was blocked') t.equal(res.statusCode, 200, 'check worked')
t.end() t.equal(obj.block, true, 'request was blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
} t.end()
) })
} }
) )
test( test(
'allowed email is not blocked', 'allowed email is not blocked',
function (t) { function (t) {
client.post('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked') t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked') t.equal(obj.block, false, 'request was not blocked')
client.post('/blockEmail', { email: ALLOWED_EMAIL }, return client.postAsync('/blockEmail', { email: ALLOWED_EMAIL })
function (err, req, res, obj) { })
t.notOk(err, 'block request is successful') .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200') t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'check worked') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'request was still not blocked') t.equal(res.statusCode, 200, 'check worked')
t.end() t.equal(obj.block, false, 'request was still not blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
} t.end()
) })
} }
) )

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

@ -2,8 +2,9 @@
* http://creativecommons.org/publicdomain/zero/1.0/ */ * http://creativecommons.org/publicdomain/zero/1.0/ */
var test = require('tap').test var test = require('tap').test
var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var restify = require('restify')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var config = {
} }
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -40,77 +47,74 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'missing ip', 'missing ip',
function (t) { function (t) {
client.post('/blockIp', {}, return client.postAsync('/blockIp', {})
function (err, req, res, obj) { .then(function (req, res, obj) {
t.equal(res.statusCode, 400, 'bad request returns a 400') //missing parameters
t.type(obj.code, 'string', 'bad request returns an error code') }, function(err){
t.type(obj.message, 'string', 'bad request returns an error message') t.equal(err.statusCode, 400, 'bad request returns a 400')
t.type(err.restCode, 'string', 'bad request returns an error code')
t.type(err.message, 'string', 'bad request returns an error message')
t.end() t.end()
} })
)
} }
) )
test( test(
'well-formed request', 'well-formed request',
function (t) { function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked') t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked') t.equal(obj.block, false, 'request was not blocked')
client.post('/blockIp', { ip: TEST_IP }, return client.postAsync('/blockIp', { ip: TEST_IP })
function (err, req, res, obj) { })
t.notOk(err, 'block request is successful') .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200') t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'check worked') .spread(function (req, res, obj) {
t.equal(obj.block, true, 'request was blocked') t.equal(res.statusCode, 200, 'check worked')
t.end() t.equal(obj.block, true, 'request was blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
} t.end()
) })
} }
) )
test( test(
'allowed ip is not blocked', 'allowed ip is not blocked',
function (t) { function (t) {
client.post('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked') t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked') t.equal(obj.block, false, 'request was not blocked')
client.post('/blockIp', { ip: ALLOWED_IP }, return client.postAsync('/blockIp', { ip: ALLOWED_IP })
function (err, req, res, obj) { })
t.notOk(err, 'block request is successful') .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200') t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'check worked') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'request was still not blocked') t.equal(res.statusCode, 200, 'check worked')
t.end() t.equal(obj.block, false, 'request was still not blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
} t.end()
) })
} }
) )

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

@ -4,6 +4,7 @@
var test = require('tap').test var test = require('tap').test
var restify = require('restify') var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var config = {
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -40,90 +47,87 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'too many failed logins using different capitalizations', 'too many failed logins using different capitalizations',
function (t) { function (t) {
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first login attempt noted') t.equal(res.statusCode, 200, 'first login attempt noted')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: 'TEST@example.com', ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: 'TEST@example.com', ip: TEST_IP })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'second login attempt noted') .spread(function (req, res, obj) {
t.ok(obj, 'got an obj, make jshint happy') t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: 'test@Example.Com', ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: 'test@Example.Com', ip: TEST_IP })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'third login attempt noted') .spread(function (req, res, obj) {
t.ok(obj, 'got an obj, make jshint happy') t.equal(res.statusCode, 200, 'third login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'login check succeeds') .spread(function (req, res, obj) {
t.equal(obj.block, true, 'login with exact email address is blocked') t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, true, 'login with exact email address is blocked')
client.post('/check', { email: 'tEST@eXaMpLe.CoM', ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: 'tEST@eXaMpLe.CoM', ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'login check succeeds') .spread(function (req, res, obj) {
t.equal(obj.block, true, 'login with weird caps is blocked') t.equal(res.statusCode, 200, 'login check succeeds')
t.end() t.equal(obj.block, true, 'login with weird caps is blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
} t.end()
) })
}
)
}
)
} }
) )
test( test(
'failed logins are cleared', 'failed logins are cleared',
function (t) { function (t) {
client.post('/passwordReset', { email: 'tEst@example.com' }, return client.postAsync('/passwordReset', { email: 'tEst@example.com' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.notOk(err, 'request is successful')
t.equal(res.statusCode, 200, 'request returns a 200') t.equal(res.statusCode, 200, 'request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'login check succeeds') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'login is no longer blocked') t.equal(res.statusCode, 200, 'login check succeeds')
t.end() t.equal(obj.block, false, 'login is no longer blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
t.end()
})
} }
) )
test( test(
'blocking an email using weird caps', 'blocking an email using weird caps',
function (t) { function (t) {
client.post('/blockEmail', { email: 'test@EXAMPLE.COM' }, return client.postAsync('/blockEmail', { email: 'test@EXAMPLE.COM' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.notOk(err, 'block request is successful')
t.equal(res.statusCode, 200, 'block request returns a 200') t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'check worked') .spread(function (req, res, obj) {
t.equal(obj.block, true, 'request was blocked') t.equal(res.statusCode, 200, 'check worked')
t.end() t.equal(obj.block, true, 'request was blocked')
} t.end()
) })
} .catch(function(err){
) t.fail(err)
t.end()
})
} }
) )

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

@ -4,6 +4,7 @@
var test = require('tap').test var test = require('tap').test
var restify = require('restify') var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var config = {
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -40,73 +47,72 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'maximum number of emails', 'maximum number of emails',
function (t) { function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first email attempt') t.equal(res.statusCode, 200, 'first email attempt')
t.equal(obj.block, false, 'creating the account') t.equal(obj.block, false, 'creating the account')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'second email attempt') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'resending the code') t.equal(res.statusCode, 200, 'second email attempt')
t.equal(obj.block, false, 'resending the code')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'third email attempt') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'resending the code') t.equal(res.statusCode, 200, 'third email attempt')
t.equal(obj.block, false, 'resending the code')
mcHelper.blockedEmailCheck( return new Promise(function (resolve, reject) {
function (isBlocked) { mcHelper.blockedEmailCheck(
t.equal(isBlocked, false, 'account is still not blocked') function (isBlocked) {
t.end() t.equal(isBlocked, false, 'account is still not blocked')
} resolve()
) }
} )
) })
} })
) .catch(function(err){
} t.fail(err)
) t.end()
})
} }
) )
test( test(
'maximum failed logins', 'maximum failed logins',
function (t) { function (t) {
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP },
function (err, req, res, obj) { return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first login attempt noted') t.equal(res.statusCode, 200, 'first login attempt noted')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'second login attempt noted') .spread(function (req, res, obj) {
t.ok(obj, 'got an obj, make jshint happy') t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
mcHelper.badLoginCheck( return mcHelper.badLoginCheck()
function (isOverBadLogins) { })
t.equal(isOverBadLogins, false, 'is still not over bad logins') .then(function (records) {
t.equal(records.ipEmailRecord.isOverBadLogins(), false, 'is still not over bad logins')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'attempting to login') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'login is not blocked') t.equal(res.statusCode, 200, 'attempting to login')
t.end() t.equal(obj.block, false, 'login is not blocked')
} })
) .catch(function(err){
} t.fail(err)
) t.end()
} })
)
}
)
} }
) )

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

@ -4,6 +4,7 @@
var test = require('tap').test var test = require('tap').test
var restify = require('restify') var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var config = {
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -40,62 +47,57 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'too many failed logins', 'too many failed logins',
function (t) { function (t) {
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first login attempt noted') t.equal(res.statusCode, 200, 'first login attempt noted')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'second login attempt noted') .spread( function (req, res, obj) {
t.ok(obj, 'got an obj, make jshint happy') t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'third login attempt noted') .spread( function (req, res, obj) {
t.ok(obj, 'got an obj, make jshint happy') t.equal(res.statusCode, 200, 'third login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'login check succeeds') .spread( function (req, res, obj) {
t.equal(obj.block, true, 'login is blocked') t.equal(res.statusCode, 200, 'login check succeeds')
t.end() t.equal(obj.block, true, 'login is blocked')
} })
) .catch(function(err){
} t.fail(err)
) t.end()
} })
)
}
)
} }
) )
test( test(
'failed logins are cleared', 'failed logins are cleared',
function (t) { function (t) {
client.post('/passwordReset', { email: TEST_EMAIL }, return client.postAsync('/passwordReset', { email: TEST_EMAIL })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.notOk(err, 'request is successful')
t.equal(res.statusCode, 200, 'request returns a 200') t.equal(res.statusCode, 200, 'request returns a 200')
t.ok(obj, 'got an obj, make jshint happy') t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'login check succeeds') .spread( function (req, res, obj) {
t.equal(obj.block, false, 'login is no longer blocked') t.equal(res.statusCode, 200, 'login check succeeds')
t.end() t.equal(obj.block, false, 'login is no longer blocked')
} })
) .catch(function(err){
} t.fail(err)
) t.end()
})
} }
) )

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

@ -4,6 +4,7 @@
var test = require('tap').test var test = require('tap').test
var restify = require('restify') var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -20,6 +21,12 @@ var config = {
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -43,60 +50,50 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'too many failed logins from the same IP', 'too many failed logins from the same IP',
function (t) { function (t) {
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first login attempt noted') t.equal(res.statusCode, 200, 'first login attempt noted')
return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'second login attempt noted')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return mcHelper.badLoginCheck()
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'second login attempt noted') .then(function(records){
t.equal(records.ipEmailRecord.isOverBadLogins(), false, 'is not yet over bad logins')
mcHelper.badLoginCheck( return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
function (isOverBadLogins) { })
t.equal(isOverBadLogins, false, 'is not yet over bad logins') .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'third login attempt noted')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP }, return mcHelper.badLoginCheck()
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'third login attempt noted') .then(function (records) {
t.equal(records.ipEmailRecord.isOverBadLogins(), true, 'is now over bad logins')
mcHelper.badLoginCheck( })
function (isOverBadLogins) { .catch(function(err){
t.equal(isOverBadLogins, true, 'is now over bad logins') t.fail(err)
t.end() t.end()
} })
)
}
)
}
)
}
)
}
)
} }
) )
test( test(
'failed logins expire', 'failed logins expire',
function (t) { function (t) {
setTimeout( return Promise.delay(config.limits.rateLimitIntervalSeconds * 1000)
function () { .then(function () {
mcHelper.badLoginCheck( return mcHelper.badLoginCheck()
function (isOverBadLogins) { })
t.equal(isOverBadLogins, false, 'is no longer over bad logins') .then(function (records) {
t.end() t.equal(records.ipEmailRecord.isOverBadLogins(), false, 'is no longer over bad logins')
} t.end()
) })
},
config.limits.rateLimitIntervalSeconds * 1000
)
} }
) )

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

@ -4,6 +4,7 @@
var test = require('tap').test var test = require('tap').test
var restify = require('restify') var restify = require('restify')
var TestServer = require('../test_server') var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper') var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com' var TEST_EMAIL = 'test@example.com'
@ -20,6 +21,12 @@ var config = {
var testServer = new TestServer(config) var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test( test(
'startup', 'startup',
function (t) { function (t) {
@ -43,47 +50,45 @@ test(
} }
) )
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test( test(
'too many sent emails', 'too many sent emails',
function (t) { function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
function (err, req, res, obj) { .spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first email attempt') t.equal(res.statusCode, 200, 'first email attempt')
t.equal(obj.block, false, 'resending the code') t.equal(obj.block, false, 'resending the code')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'second email attempt') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'resending the code') t.equal(res.statusCode, 200, 'second email attempt')
t.equal(obj.block, false, 'resending the code')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'third email attempt') .spread(function (req, res, obj) {
t.equal(obj.block, false, 'resending the code') t.equal(res.statusCode, 200, 'third email attempt')
t.equal(obj.block, false, 'resending the code')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' }, return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
function (err, req, res, obj) { })
t.equal(res.statusCode, 200, 'fourth email attempt') .spread(function (req, res, obj) {
t.equal(obj.block, true, 'operation blocked') t.equal(res.statusCode, 200, 'fourth email attempt')
t.equal(obj.block, true, 'operation blocked')
mcHelper.blockedEmailCheck( return new Promise(function (resolve, reject) {
function (isBlocked) { mcHelper.blockedEmailCheck(
t.equal(isBlocked, true, 'account is blocked') function (isBlocked) {
t.end() t.equal(isBlocked, true, 'account is blocked')
} resolve()
) }
} )
) })
} })
) .catch(function(err){
} t.fail(err)
) t.end()
} })
)
} }
) )