Skip to content

Instantly share code, notes, and snippets.

@warner
Created March 14, 2014 22:43
Show Gist options
  • Select an option

  • Save warner/9558560 to your computer and use it in GitHub Desktop.

Select an option

Save warner/9558560 to your computer and use it in GitHub Desktop.
/* 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/. */
define([
'intern!tdd',
'intern/chai!assert',
'sjcl',
'client/lib/credentials',
'tests/addons/environment'
], function (tdd, assert, sjcl, credentials, Environment) {
with (tdd) {
suite('passwordChange', function () {
var accountHelper;
var respond;
var mail;
var client;
var RequestMocks;
var ErrorMocks;
var requests;
beforeEach(function () {
var env = new Environment();
accountHelper = env.accountHelper;
respond = env.respond;
mail = env.mail;
client = env.client;
RequestMocks = env.RequestMocks;
ErrorMocks = env.ErrorMocks;
requests = env.requests;
});
test('#basic', function () {
var user = 'test7' + Date.now();
var email = user + '@restmail.net';
var password = 'iliketurtles';
var newPassword = 'ilikefoxes';
var uid;
var oldCreds, kB, newUnwrapBKey;
// compute oldwrapKB and newwrapKB from email+passwords. The
// submitted newWrapKB should equal (kB XOR newwrapkb). This way we
// don't need to know what the server will return for wrapKB: handy,
// since sometimes we're using a mock (with a fixed response), but
// sometimes we're using a real server (which randomly creates
// wrapKB)
return credentials.setup(email, newPassword)
.then(function (newCreds) {
newUnwrapBKey = newCreds.unwrapBKey;
return respond(client.signUp(email, password), RequestMocks.signUp);
})
.then(function (result) {
uid = result.uid;
return respond(mail.wait(user), RequestMocks.mail);
})
.then(function (emails) {
var code = emails[0].html.match(/code=([A-Za-z0-9]+)/)[1];
return respond(client.verifyCode(uid, code), RequestMocks.verifyCode);
})
.then(function() {
return respond(client.signIn(email, password, {keys: true}), RequestMocks.signInWithKeys);
})
.then(function(result) {
console.log(result);
return respond(client.accountKeys(result.keyFetchToken, result.unwrapBKey), RequestMocks.accountKeys);
})
.then(function(keys) {
console.log("=====");
console.log(keys);
kB = keys.kB;
})
.then(function () {
return respond(client._passwordChangeStart(email, password), RequestMocks.passwordChangeStart);
})
.then(function (credentials) {
oldCreds = credentials;
return respond(client._passwordChangeKeys(oldCreds), RequestMocks.accountKeys);
})
.then(function (keys) {
return respond(client._passwordChangeFinish(email, newPassword, oldCreds, keys), RequestMocks.passwordChangeFinish);
})
.then(function (result) {
console.log("===00");
var req = requests[requests.length-1];
console.log(req);
var args = JSON.parse(req.requestBody);
console.log(args.wrapKb);
var expectedNewWrapKB = sjcl.codec.hex.fromBits(
credentials.xor(sjcl.codec.hex.toBits(kB),
sjcl.codec.hex.toBits(newUnwrapBKey)));
assert.equal(args.wrapKb, expectedNewWrapKB);
assert.ok(result, '{}');
return respond(client.signIn(email, newPassword), RequestMocks.signIn);
})
.then(
function (res) {
assert.property(res, 'sessionToken');
},
function (err) {
throw err;
}
)
});
test('#with incorrect case', function () {
var newPassword = 'ilikefoxes';
var account;
var oldCreds;
return accountHelper.newVerifiedAccount()
.then(function (acc) {
account = acc;
var incorrectCaseEmail = account.input.email.charAt(0).toUpperCase() + account.input.email.slice(1);
return respond(client._passwordChangeStart(incorrectCaseEmail, account.input.password), RequestMocks.passwordChangeStart);
})
.then(function (credentials) {
oldCreds = credentials;
return respond(client._passwordChangeKeys(oldCreds), RequestMocks.accountKeys);
})
.then(function (keys) {
return respond(client._passwordChangeFinish(account.input.email, newPassword, oldCreds, keys), RequestMocks.passwordChangeFinish);
})
.then(function (result) {
assert.ok(result, '{}');
return respond(client.signIn(account.input.email, newPassword), RequestMocks.signIn);
})
.then(
function (res) {
assert.property(res, 'sessionToken');
},
function (err) {
throw err;
}
)
});
test('#with incorrect case with skipCaseError', function () {
var account;
return accountHelper.newVerifiedAccount()
.then(function (acc) {
account = acc;
var incorrectCaseEmail = account.input.email.charAt(0).toUpperCase() + account.input.email.slice(1);
return respond(client._passwordChangeStart(incorrectCaseEmail, account.input.password, {skipCaseError: true}),
ErrorMocks.incorrectEmailCase);
})
.then(
function () {
assert.fail();
},
function (res) {
assert.equal(res.code, 400);
assert.equal(res.errno, 120);
}
);
});
/**
* Changing the Password failure
*/
test('#changeFailure', function () {
var user = 'test8' + Date.now();
var email = user + '@restmail.net';
var password = 'iliketurtles';
var newPassword = 'ilikefoxes';
var wrongPassword = '12345678';
var uid;
var oldCreds;
return respond(client.signUp(email, password), RequestMocks.signUp)
.then(function (result) {
uid = result.uid;
return respond(mail.wait(user), RequestMocks.mail);
})
.then(function (emails) {
var code = emails[0].html.match(/code=([A-Za-z0-9]+)/)[1];
return respond(client.verifyCode(uid, code), RequestMocks.verifyCode);
})
.then(function () {
return respond(client._passwordChangeStart(email, password), RequestMocks.passwordChangeStart);
})
.then(function (credentials) {
oldCreds = credentials
return respond(client._passwordChangeKeys(oldCreds), RequestMocks.accountKeys);
})
.then(function (keys) {
return respond(client._passwordChangeFinish(email, newPassword, oldCreds, keys), RequestMocks.passwordChangeFinish);
})
.then(function (result) {
assert.ok(result);
return respond(client.signIn(email, wrongPassword), ErrorMocks.accountIncorrectPassword);
})
.then(
function () {
assert.fail();
},
function (error) {
assert.ok(error);
assert.equal(error.message, 'Incorrect password', '== Password is incorrect');
assert.equal(error.code, 400, '== Correct status code');
}
)
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment