This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
check_tests.js
137 lines (122 loc) · 3.43 KB
/
check_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
'use strict'
const mcHelper = require('../memcache-helper')
const Promise = require('bluebird')
const restify = require('restify')
const test = require('tap').test
const TestServer = require('../test_server')
const ALLOWED_EMAIL = 'test@restmail.net'
const TEST_EMAIL = 'test@example.com'
const TEST_IP = '192.0.2.1'
process.env.MAX_VERIFY_CODES = 1
var config = {
listen: {
port: 7000
}
}
var testServer = new TestServer(config)
test(
'startup',
function (t) {
testServer.start(function (err) {
t.type(testServer.server, 'object', 'test server was started')
t.notOk(err, 'no errors were returned')
t.end()
})
}
)
test(
'clear everything',
function (t) {
mcHelper.clearEverything(
function (err) {
t.notOk(err, 'no errors were returned')
t.end()
}
)
}
)
var client = restify.createJsonClient({
url: 'http://127.0.0.1:' + config.listen.port
})
Promise.promisifyAll(client, { multiArgs: true })
// NOTE: Leading semi-colon because ASI is funny.
; ['accountCreate', 'accountLogin', 'passwordChange'].forEach(function (action) {
test(
'normal ' + action,
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP, action: action },
function (err, req, res, obj) {
t.notOk(err, 'good request is successful')
t.equal(res.statusCode, 200, 'good request returns a 200')
t.ok(obj, 'got an obj, make jshint happy')
t.end()
}
)
}
)
})
test(
'missing action',
function (t) {
client.post('/check', { email: TEST_EMAIL, ip: TEST_IP },
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')
t.end()
}
)
}
)
test(
'missing email, ip and action',
function (t) {
client.post('/check', {},
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')
t.end()
}
)
}
)
test('allowed email addresses in /check do not block subsequent requests to /checkIpOnly', t => {
return client.postAsync('/check', {
email: ALLOWED_EMAIL,
ip: TEST_IP,
action: 'recoveryEmailVerifyCode'
}).spread((req, res, obj) => {
t.equal(res.statusCode, 200, '/check succeeded')
t.equal(obj.block, false, 'request was not blocked')
return client.postAsync('/check', {
email: ALLOWED_EMAIL,
ip: TEST_IP,
action: 'recoveryEmailVerifyCode'
})
})
.spread((req, res, obj) => {
t.equal(res.statusCode, 200, '/check succeeded')
t.equal(obj.block, false, 'request was not blocked')
return client.postAsync('/checkIpOnly', {
ip: TEST_IP,
action: 'consumeSigninCode'
})
})
.spread((req, res, obj) => {
t.equal(res.statusCode, 200, '/checkIpOnly succeeded')
t.equal(obj.block, false, 'request was not blocked')
})
.catch(err => t.fail(err))
.then(() => t.end())
})
test(
'teardown',
function (t) {
testServer.stop()
t.equal(testServer.server.killed, true, 'test server has been killed')
t.end()
}
)