Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Test refactor #506

Merged
merged 7 commits into from Jan 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -20,7 +20,6 @@ notifications:
skip_join: false

env:
- DB_BACKEND=memory
- DB_BACKEND=mysql REDIS_HOST=localhost
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets give travis a break on this one


before_script:
Expand Down
2 changes: 1 addition & 1 deletion client/api.js
Expand Up @@ -57,7 +57,7 @@ ClientApi.prototype.doRequest = function (method, url, token, payload, headers)
}

this.emit('endRequest', options, err, res)
if (err || body.error) {
if (err || body.error || res.statusCode !== 200) {
d.reject(err || body)
}
else {
Expand Down
19 changes: 19 additions & 0 deletions client/index.js
Expand Up @@ -88,6 +88,25 @@ Client.changePassword = function (origin, email, oldPassword, newPassword) {
)
}

Client.createAndVerify = function (origin, email, password, mailbox, options) {
return Client.create(origin, email, password, options)
.then(
function (client) {
return mailbox.waitForCode(email)
.then(
function (code) {
return client.verifyEmail(code)
}
)
.then(
function () {
return client
}
)
}
)
}

Client.prototype.create = function () {
return this.api.accountCreate(
this.email,
Expand Down
2 changes: 1 addition & 1 deletion db/mysql.js
Expand Up @@ -542,7 +542,7 @@ var KEY_FETCH_TOKEN = 'SELECT t.authKey, t.uid, t.keyBundle, t.createdAt,' +
function (err, results) {
con.release()
if (err) return d.reject(err)
if (!results.length) return d.reject(error.unknownAccount())
if (!results.length) return d.reject(error.unknownAccount(email))
var result = results[0]
return d.resolve({
uid: result.uid,
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -9,11 +9,12 @@
"test": "test"
},
"scripts": {
"test": "./scripts/tap-coverage.js ./test/run",
"test": "./scripts/tap-coverage.js test/local test/remote",
"start": "scripts/start-local.sh",
"test-mysql": "DB_BACKEND=mysql npm test",
"test-all": "npm test && npm run test-mysql",
"test-remote": "MAILER_HOST=restmail.net MAILER_PORT=80 tap --timeout=180 --tap test/run/verification_tests.js"
"test-all": "npm run test-quick && npm run test-mysql && grunt",
"test-quick": "tap test/local test/remote",
"test-remote": "MAILER_HOST=restmail.net MAILER_PORT=80 tap --timeout=300 --tap test/remote"
},
"repository": {
"type": "git",
Expand Down
3 changes: 0 additions & 3 deletions routes/account.js
Expand Up @@ -131,9 +131,6 @@ module.exports = function (
db.emailRecord(form.email)
.then(
function (emailRecord) {
if (!emailRecord) {
throw error.unknownAccount(form.email)
}
return password.stretch(authPW, emailRecord.authSalt)
.then(
function (stretched) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was unreachable

Expand Down
3 changes: 0 additions & 3 deletions routes/password.js
Expand Up @@ -34,9 +34,6 @@ module.exports = function (log, isA, error, db, redirectDomain, mailer) {
db.emailRecord(form.email)
.then(
function (emailRecord) {
if (!emailRecord) {
throw error.unknownAccount(form.email)
}
return password.stretch(oldAuthPW, emailRecord.authSalt)
.then(
function (oldStretched) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unreachable

Expand Down
48 changes: 48 additions & 0 deletions test/local/butil_tests.js
@@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

require('ass')
var test = require('tap').test
var butil = require('../../crypto/butil')

test(
'buffersAreEqual returns false if lengths are different',
function (t) {
t.equal(butil.buffersAreEqual(Buffer(2), Buffer(4)), false)
t.end()
}
)

test(
'buffersAreEqual returns true if buffers have same bytes',
function (t) {
var b1 = Buffer('abcd', 'hex')
var b2 = Buffer('abcd', 'hex')
t.equal(butil.buffersAreEqual(b1, b2), true)
t.end()
}
)

test(
'xorBuffers throws an Error if lengths are different',
function (t) {
try {
butil.xorBuffers(Buffer(2), Buffer(4))
}
catch (e) {
return t.end()
}
t.fail('did not throw')
}
)

test(
'xorBuffers works',
function (t) {
var b1 = Buffer('e5', 'hex')
var b2 = Buffer('5e', 'hex')
t.deepEqual(butil.xorBuffers(b1, b2), Buffer('bb', 'hex'))
t.end()
}
)
1 change: 1 addition & 0 deletions test/run/db_tests.js → test/local/db_tests.js
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

require('ass')
var test = require('../ptaptest')
var uuid = require('uuid')
var log = { trace: console.log }
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 46 additions & 0 deletions test/local/token_expiry_tests.js
@@ -0,0 +1,46 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var test = require('../ptaptest')
var TestServer = require('../test_server')
var path = require('path')
var Client = require('../../client')

process.env.CONFIG_FILES = path.join(__dirname, '../config/api_error.json')
var config = require('../../config').root()

function fail() { throw new Error() }

TestServer.start(config)
.then(function main(server) {

test(
'token expiry',
function (t) {
// FYI config.tokenLifetimes.passwordChangeToken = -1
var email = Math.random() + "@example.com"
var password = 'ok'
return Client.create(config.publicUrl, email, password, { preVerified: true })
.then(
function (c) {
return c.changePassword('hello')
}
)
.then(
fail,
function (err) {
t.equal(err.errno, 110, 'invalid token')
}
)
}
)

test(
'teardown',
function (t) {
server.stop()
t.end()
}
)
})
64 changes: 64 additions & 0 deletions test/mailbox.js
@@ -0,0 +1,64 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var P = require('p-promise')
var request = require('request')

module.exports = function (host, port) {

host = host || '127.0.0.1'
port = port || 9001

function waitForCode(email) {
return waitForEmail(email)
.then(
function (emailData) {
return emailData.headers['x-verify-code'] || emailData.headers['x-recovery-code']
}
)
}

function loop(name, tries, cb) {
var url = 'http://' + host + ':' + port + '/mail/' + name
console.log('checking mail', url)
request({ url: url, method: 'GET' },
function (err, res, body) {
console.log('mail status', res && res.statusCode, 'tries', tries)
var json = null
try {
json = JSON.parse(body)[0]
}
catch (e) {
return cb(e)
}

if(!json) {
if (tries === 0) {
return cb(new Error('could not get mail for ' + url))
}
return setTimeout(loop.bind(null, name, --tries, cb), 1000)
}
console.log('deleting mail', url)
request({ url: url, method: 'DELETE' },
function (err, res, body) {
cb(err, json)
}
)
}
)
}

function waitForEmail(email) {
var d = P.defer()
loop(email.split('@')[0], 20, function (err, json) {
return err ? d.reject(err) : d.resolve(json)
})
return d.promise
}

return {
waitForEmail: waitForEmail,
waitForCode: waitForCode
}
}