Skip to content

Commit

Permalink
test for email change done
Browse files Browse the repository at this point in the history
  • Loading branch information
rook2pawn committed May 9, 2014
1 parent 6255f9c commit b397ef2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
103 changes: 103 additions & 0 deletions test/test-email-change-resend.js
Original file line number Diff line number Diff line change
@@ -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:'[email protected]',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('[email protected]',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();
});
}
]);
})
4 changes: 2 additions & 2 deletions test/test-email-verification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
},
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b397ef2

Please sign in to comment.