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

Commit

Permalink
fix(email): Return createdAt when calling db.emailRecord (#209), r=@…
Browse files Browse the repository at this point in the history
  • Loading branch information
vbudhram committed Feb 23, 2017
1 parent 44470ad commit 1a226cc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/db/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ module.exports = function (log, error) {
}

// Select : accounts
// Fields : uid, email, normalizedEmail, emailVerified, emailCode, kA, wrapWrapKb, verifierVersion, authSalt, verifierSetAt, lockedAt
// Fields : uid, email, normalizedEmail, emailVerified, emailCode, kA, wrapWrapKb, verifierVersion, authSalt, verifierSetAt, createdAt, lockedAt
// Where : accounts.normalizedEmail = LOWER($1)
var EMAIL_RECORD = 'CALL emailRecord_3(?)'
var EMAIL_RECORD = 'CALL emailRecord_4(?)'

MySql.prototype.emailRecord = function (emailBuffer) {
return this.readFirstResult(EMAIL_RECORD, [emailBuffer.toString('utf8')])
Expand Down
2 changes: 1 addition & 1 deletion lib/db/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

// The expected patch level of the database. Update if you add a new
// patch in the ./schema/ directory.
module.exports.level = 43
module.exports.level = 44
27 changes: 27 additions & 0 deletions lib/db/schema/patch-043-044.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Update email record to return `createdAt`
-- emailRecord v4
CREATE PROCEDURE `emailRecord_4` (
IN `inEmail` VARCHAR(255)
)
BEGIN
SELECT
a.uid,
a.email,
a.normalizedEmail,
a.emailVerified,
a.emailCode,
a.kA,
a.wrapWrapKb,
a.verifierVersion,
a.authSalt,
a.verifierSetAt,
a.lockedAt,
a.createdAt
FROM
accounts a
WHERE
a.normalizedEmail = LOWER(inEmail)
;
END;

UPDATE dbMetadata SET value = '44' WHERE name = 'schema-patch-level';
3 changes: 3 additions & 0 deletions lib/db/schema/patch-044-043.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- DROP PROCEDURE `emailRecord_4`;

-- UPDATE dbMetadata SET value = '43' WHERE name = 'schema-patch-level';
45 changes: 45 additions & 0 deletions test/local/mysql_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ var DB = require('../../lib/db/mysql')(log, dbServer.errors)
var config = require('../../config')
var test = require('tap').test
var P = require('../../lib/promise')
var crypto = require('crypto')

var zeroBuffer16 = Buffer.from('00000000000000000000000000000000', 'hex')
var zeroBuffer32 = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
var now = Date.now()

DB.connect(config)
.then(
Expand Down Expand Up @@ -332,6 +337,46 @@ DB.connect(config)
}
)

test(
'create account and read email record',
function (t) {
var uid = crypto.randomBytes(16)
var account = {
uid: uid,
email: ('' + Math.random()).substr(2) + '@bar.com',
emailCode: zeroBuffer16,
emailVerified: false,
verifierVersion: 1,
verifyHash: zeroBuffer32,
authSalt: zeroBuffer32,
kA: zeroBuffer32,
wrapWrapKb: zeroBuffer32,
verifierSetAt: now,
createdAt: now,
locale : 'en_US',
}
account.normalizedEmail = account.email.toLowerCase()

return db.createAccount(uid, account)
.then(
function(result) {
t.deepEqual(result, {}, 'Returned an empty object on account creation')
return db.emailRecord(account.email)
}
)
.then(
function(result) {
t.equal(result.createdAt, account.createdAt, 'createdAt set')
t.equal(result.email, account.email, 'email set')
t.equal(result.emailVerified, 0, 'emailVerified set')
t.equal(result.normalizedEmail, account.normalizedEmail, 'normalizedEmail set')
t.equal(result.verifierSetAt, account.verifierSetAt, 'verifierSetAt set')
t.equal(result.verifierVersion, account.verifierVersion, 'verifierVersion set')
}
)
}
)

test(
'teardown',
function (t) {
Expand Down

0 comments on commit 1a226cc

Please sign in to comment.