diff --git a/api/user.js b/api/user.js index f27f0e7..5477527 100644 --- a/api/user.js +++ b/api/user.js @@ -152,7 +152,7 @@ var email_change = function(req,res) { } var token = libutils.generateToken(); exports.store.update_where({set:{email:req.body.email,email_token:token},where:{key:'id',value:req.body.blob_id}},function(resp) { - if ((resp.result) && (result.success)) { + if ((resp.result) && (resp.result == 'success')) { email.send({email:req.body.email,hostlink:req.body.hostlink,token:token,name:req.body.username}); res.writeHead(200, { 'Content-Type' : 'application/json', diff --git a/test.sh b/test.sh index 0291a88..f74c498 100755 --- a/test.sh +++ b/test.sh @@ -7,3 +7,6 @@ node_modules/.bin/mocha --ui tdd -R spec test/test-signature.js node_modules/.bin/mocha --ui tdd -R spec test/test-usercap.js node_modules/.bin/mocha --ui tdd -R spec test/test-quota.js node_modules/.bin/mocha --ui tdd -R spec test/test-normalize-reserved.js + + +node_modules/.bin/mocha --ui tdd -R spec test/test-email-change-resend.js diff --git a/test/test-email-change-resend.js b/test/test-email-change-resend.js new file mode 100644 index 0000000..069c5f9 --- /dev/null +++ b/test/test-email-change-resend.js @@ -0,0 +1,103 @@ +console.log(__filename); +var config = require('../config'); +var request = require('request'); +var http = require('http'); +var api = require('../api'); +var hmac = require('../lib/hmac'); +var store = require('../lib/store')(config.dbtype); +api.setStore(store); +hmac.setStore(store); +var util = require('util'); +var queuelib = require('queuelib'); +var express = require('express'); +var testutils = require('./utils') +var assert = require('chai').assert; +var lib = require('../lib'); +var ecdsa = require('../lib/ecdsa'); +var limiter = lib.limiter.resend_email(); +var q = new queuelib; + +var log = function(obj) { + console.log(util.inspect(obj, { showHidden: true, depth: null })); +} + +var app = express(); + +app.use(function(req,res,next) { + next(); +}); +app.use(express.json()); +app.use(express.urlencoded()); + +app.post('/v1/user/email', ecdsa.middleware, api.user.emailChange); +app.post('/v1/user/email/resend', limiter.check, api.user.emailResend); +app.get('/v1/user/:username',api.user.get); +app.get('/v1/user/:username/verify/:token',api.user.verify); +app.post('/v1/user',api.blob.create); +app.delete('/v1/user',hmac.middleware, api.blob.delete); +// we just test that the token secret is created +test('email verification', function(done) { + var server = http.createServer(app); + var GLOBALS = {}; + q.series([ + function(lib) { + server.listen(5050,function() { + lib.set({old_email:testutils.person.email}) + lib.done(); + }); + }, + // create the user + function(lib) { + request.post({ + url:'http://localhost:5050/v1/user', + json: testutils.person + }, + function(err, resp, body) { + assert.equal(resp.statusCode,201,'after proper create request, status code should be 201'); + lib.done(); + } + ); + }, + // change email address + function(lib) { + request.post({ + url:'http://localhost:5050/v1/user/email', + json: {email:'bit@bit.com',blob_id:testutils.person.blob_id, hostlink:'asdf', username:testutils.person.username} + }, + function(err, resp, body) { + console.log("Changed email address:", body) + lib.done(); + } + ); + }, + // inspect it + function(lib) { + store.read_where({key:'id',value:testutils.person.blob_id},function(rows) { + console.log(rows); + if (rows.length) { + var email = rows[0].email; + assert.equal('bit@bit.com',email); + } + lib.done(); + }); + }, + // delete user after + function(lib) { + var sig = testutils.createSignature({method:'DELETE',url:'/v1/user',secret:testutils.person.auth_secret,date:testutils.person.date}); + var url = 'http://localhost:5050/v1/user?signature=' + sig + '&signature_date='+testutils.person.date + '&signature_blob_id='+ testutils.person.blob_id; + request.del({ + url:url, + json:true + },function(err, resp, body) { + assert.equal(resp.statusCode,200,'after delete request, status code should be 200'); + lib.done(); + }); + }, + function(lib) { + server.close(function() { + lib.done(); + done(); + }); + } + ]); +}) diff --git a/test/test-email-verification.js b/test/test-email-verification.js index 669c7e8..4511fe6 100644 --- a/test/test-email-verification.js +++ b/test/test-email-verification.js @@ -55,7 +55,7 @@ test('email verification', function(done) { function(lib) { store.readall({username:testutils.person.username}, function(resp) { if (resp.length) - GLOBALS.token = resp[0].email_token; + lib.set({token:resp[0].email_token}); lib.done(); }); }, @@ -83,7 +83,7 @@ test('email verification', function(done) { }, function(lib) { request.get({ - url:'http://localhost:5050/v1/user/'+testutils.person.username+'/verify/'+GLOBALS.token, + url:'http://localhost:5050/v1/user/'+testutils.person.username+'/verify/'+lib.get('token'), json:true }, function(err, resp, body) {