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
function badLoginCheck(cb) {
setTimeout( // give memcache time to flush the writes
function () {
P.all([
mc.getAsync(TEST_IP + TEST_EMAIL),
mc.getAsync(TEST_IP)
])
.spread(function (d1, d2) {
var ier = IpEmailRecord.parse(d1)
var ir = IpRecord.parse(d2)
mc.end()
cb(ier.isOverBadLogins(), false, ir.isOverBadLogins())
})
}
)
function badLoginCheck() {
return P.all([
mc.getAsync(TEST_IP + TEST_EMAIL),
mc.getAsync(TEST_IP)
])
.spread(function (d1, d2) {
var ipEmailRecord = IpEmailRecord.parse(d1)
var ipRecord = IpRecord.parse(d2)
mc.end()
return {ipEmailRecord: ipEmailRecord, ipRecord: ipRecord}
})
}
module.exports.badLoginCheck = badLoginCheck

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

@ -4,6 +4,7 @@
var test = require('tap').test
var restify = require('restify')
var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com'
@ -15,8 +16,15 @@ var config = {
port: 7000
}
}
var testServer = new TestServer(config)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
test(
'startup',
function (t) {
@ -40,77 +48,73 @@ test(
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test(
'missing email',
function (t) {
client.post('/blockEmail', {},
function (err, req, res, obj) {
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')
return client.postAsync('/blockEmail', {})
.then(function (req, res, obj) {
//missing parameters
}, function(err){
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()
}
)
})
}
)
test(
'well-formed request',
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' },
function (err, req, res, obj) {
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked')
client.post('/blockEmail', { email: TEST_EMAIL },
function (err, req, res, obj) {
t.notOk(err, 'block request is successful')
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/blockEmail', { email: TEST_EMAIL })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, true, 'request was blocked')
t.end()
}
)
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, true, 'request was blocked')
t.end()
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'allowed email is not blocked',
function (t) {
client.post('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' },
function (err, req, res, obj) {
return client.postAsync('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked')
client.post('/blockEmail', { email: ALLOWED_EMAIL },
function (err, req, res, obj) {
t.notOk(err, 'block request is successful')
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/blockEmail', { email: ALLOWED_EMAIL })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was still not blocked')
t.end()
}
)
}
)
}
)
return client.postAsync('/check', { email: ALLOWED_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
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/ */
var test = require('tap').test
var restify = require('restify')
var TestServer = require('../test_server')
var Promise = require('bluebird')
var restify = require('restify')
var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var 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(
'startup',
function (t) {
@ -40,77 +47,74 @@ test(
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test(
'missing ip',
function (t) {
client.post('/blockIp', {},
function (err, req, res, obj) {
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')
return client.postAsync('/blockIp', {})
.then(function (req, res, obj) {
//missing parameters
}, function(err){
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()
}
)
})
}
)
test(
'well-formed request',
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' },
function (err, req, res, obj) {
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked')
client.post('/blockIp', { ip: TEST_IP },
function (err, req, res, obj) {
t.notOk(err, 'block request is successful')
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/blockIp', { ip: TEST_IP })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, true, 'request was blocked')
t.end()
}
)
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, true, 'request was blocked')
t.end()
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'allowed ip is not blocked',
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' },
function (err, req, res, obj) {
return client.postAsync('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was not blocked')
client.post('/blockIp', { ip: ALLOWED_IP },
function (err, req, res, obj) {
t.notOk(err, 'block request is successful')
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/blockIp', { ip: ALLOWED_IP })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, false, 'request was still not blocked')
t.end()
}
)
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: ALLOWED_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
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 restify = require('restify')
var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var 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(
'startup',
function (t) {
@ -40,90 +47,87 @@ test(
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test(
'too many failed logins using different capitalizations',
'too many failed logins using different capitalizations',
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.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: 'TEST@example.com', ip: TEST_IP },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/failedLoginAttempt', { email: 'TEST@example.com', ip: TEST_IP })
})
.spread(function (req, res, obj) {
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 },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'third login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/failedLoginAttempt', { email: 'test@Example.Com', ip: TEST_IP })
})
.spread(function (req, res, obj) {
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' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, true, 'login with exact email address is blocked')
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
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' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, true, 'login with weird caps is blocked')
t.end()
}
)
}
)
}
)
}
)
}
)
return client.postAsync('/check', { email: 'tEST@eXaMpLe.CoM', ip: TEST_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, true, 'login with weird caps is blocked')
t.end()
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'failed logins are cleared',
function (t) {
client.post('/passwordReset', { email: 'tEst@example.com' },
function (err, req, res, obj) {
t.notOk(err, 'request is successful')
return client.postAsync('/passwordReset', { email: 'tEst@example.com' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, false, 'login is no longer blocked')
t.end()
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, false, 'login is no longer blocked')
t.end()
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'blocking an email using weird caps',
function (t) {
client.post('/blockEmail', { email: 'test@EXAMPLE.COM' },
function (err, req, res, obj) {
t.notOk(err, 'block request is successful')
return client.postAsync('/blockEmail', { email: 'test@EXAMPLE.COM' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'block request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
t.equal(obj.block, true, 'request was blocked')
t.end()
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'check worked')
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 restify = require('restify')
var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var 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(
'startup',
function (t) {
@ -40,73 +47,72 @@ test(
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test(
'maximum number of emails',
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' },
function (err, req, res, obj) {
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountCreate' })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'first email attempt')
t.equal(obj.block, false, 'creating the account')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'second email attempt')
t.equal(obj.block, false, 'resending the code')
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
})
.spread(function (req, res, obj) {
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' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'third email attempt')
t.equal(obj.block, false, 'resending the code')
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'recoveryEmailResendCode' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'third email attempt')
t.equal(obj.block, false, 'resending the code')
mcHelper.blockedEmailCheck(
function (isBlocked) {
t.equal(isBlocked, false, 'account is still not blocked')
t.end()
}
)
}
)
}
)
}
)
return new Promise(function (resolve, reject) {
mcHelper.blockedEmailCheck(
function (isBlocked) {
t.equal(isBlocked, false, 'account is still not blocked')
resolve()
}
)
})
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'maximum failed logins',
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.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
mcHelper.badLoginCheck(
function (isOverBadLogins) {
t.equal(isOverBadLogins, false, 'is still not over bad logins')
return mcHelper.badLoginCheck()
})
.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' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'attempting to login')
t.equal(obj.block, false, 'login is not blocked')
t.end()
}
)
}
)
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'attempting to login')
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 restify = require('restify')
var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com'
@ -17,6 +18,12 @@ var 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(
'startup',
function (t) {
@ -40,62 +47,57 @@ test(
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test(
'too many failed logins',
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.ok(obj, 'got an obj, make jshint happy')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'second login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
})
.spread( function (req, res, obj) {
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 },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'third login attempt noted')
t.ok(obj, 'got an obj, make jshint happy')
return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
})
.spread( function (req, res, obj) {
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' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, true, 'login is blocked')
t.end()
}
)
}
)
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread( function (req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, true, 'login is blocked')
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'failed logins are cleared',
function (t) {
client.post('/passwordReset', { email: TEST_EMAIL },
function (err, req, res, obj) {
t.notOk(err, 'request is successful')
return client.postAsync('/passwordReset', { email: TEST_EMAIL })
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
t.equal(obj.block, false, 'login is no longer blocked')
t.end()
}
)
}
)
return client.postAsync('/check', { email: TEST_EMAIL, ip: TEST_IP, action: 'accountLogin' })
})
.spread( function (req, res, obj) {
t.equal(res.statusCode, 200, 'login check succeeds')
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 restify = require('restify')
var TestServer = require('../test_server')
var Promise = require('bluebird')
var mcHelper = require('../memcache-helper')
var TEST_EMAIL = 'test@example.com'
@ -20,6 +21,12 @@ var 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(
'startup',
function (t) {
@ -43,60 +50,50 @@ test(
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
test(
'too many failed logins from the same IP',
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')
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 },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'second login attempt noted')
return mcHelper.badLoginCheck()
})
.then(function(records){
t.equal(records.ipEmailRecord.isOverBadLogins(), false, 'is not yet over bad logins')
mcHelper.badLoginCheck(
function (isOverBadLogins) {
t.equal(isOverBadLogins, false, 'is not yet over bad logins')
return client.postAsync('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP })
})
.spread(function (req, res, obj) {
t.equal(res.statusCode, 200, 'third login attempt noted')
client.post('/failedLoginAttempt', { email: TEST_EMAIL, ip: TEST_IP },
function (err, req, res, obj) {
t.equal(res.statusCode, 200, 'third login attempt noted')
mcHelper.badLoginCheck(
function (isOverBadLogins) {
t.equal(isOverBadLogins, true, 'is now over bad logins')
t.end()
}
)
}
)
}
)
}
)
}
)
return mcHelper.badLoginCheck()
})
.then(function (records) {
t.equal(records.ipEmailRecord.isOverBadLogins(), true, 'is now over bad logins')
})
.catch(function(err){
t.fail(err)
t.end()
})
}
)
test(
'failed logins expire',
function (t) {
setTimeout(
function () {
mcHelper.badLoginCheck(
function (isOverBadLogins) {
t.equal(isOverBadLogins, false, 'is no longer over bad logins')
t.end()
}
)
},
config.limits.rateLimitIntervalSeconds * 1000
)
return Promise.delay(config.limits.rateLimitIntervalSeconds * 1000)
.then(function () {
return mcHelper.badLoginCheck()
})
.then(function (records) {
t.equal(records.ipEmailRecord.isOverBadLogins(), false, 'is no longer over bad logins')
t.end()
})
}
)

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

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