added u2f login tests

This commit is contained in:
Alex Charles 2016-11-24 21:55:48 -08:00
parent 88f4a89272
commit b937b9d1a0
2 changed files with 55 additions and 6 deletions

View file

@ -38,7 +38,7 @@ describe('auth', function () {
afterEach(function () {
expect.restoreSpies();
})
});
describe('login(username, password, token)', function () {
it('should successfully login and put user data in the session', function () {
@ -59,6 +59,47 @@ describe('auth', function () {
});
});
describe('u2flogin(name, password)', function () {
var u2fSample = { type: 2, signRequests: -2, timeoutSeconds: -599, requestId: 2 };
it('should successfully login and put user data in the session', function () {
window.u2f = {
sign: function(appId, challenge, registeredKeys, callback) {
u2fSample.errorCode = 0;
callback(u2fSample);
}
};
var token = null;
api.post.andReturn($.Deferred().resolve(u2fSample));
auth.u2fLogin('user', 'password').done(()=>{ token = u2fSample; });
expect(token).toEqual(u2fSample);
expect(auth._startTokenRefresher.calls.length).toEqual(1);
expect(getCallArgs(session.setUserData).token, u2fSample);
});
it('should return rejected promise if failed to log in', function () {
var wasCalled = false;
api.post.andReturn($.Deferred().reject());
auth.u2fLogin('user', 'password').fail(()=> { wasCalled = true });
expect(wasCalled).toEqual(true);
});
it('should return rejected promise if u2f api throws an error', function() {
window.u2f = {
sign: function(appId, challenge, registeredKeys, callback) {
callback({errorCode: 1});
}
};
var wasCalled = false;-
api.post.andReturn($.Deferred().resolve(u2fSample));
auth.u2fLogin('user', 'password').fail(()=> { wasCalled = true });
expect(wasCalled).toEqual(true);
});
});
describe('ensureUser()', function () {
describe('when token is valid', function () {
it('should be resolved', function () {

View file

@ -101,24 +101,32 @@ var auth = {
var data = {
user: name,
pass: password
}
};
return api.post(cfg.api.u2fSessionChallengePath, data, false).then(data=>{
// u2f.sign doesn't return a value, so this lets us handle done()/fail() nicely
var u2fSignRet = null;
api.post(cfg.api.u2fSessionChallengePath, data, false).then(data=>{
window.u2f.sign(data.appId, data.challenge, [data], function(res){
if(res.errorCode) {
let err = JSON.stringify(res);
return $.Deferred().reject(err);
u2fSignRet = $.Deferred().reject(err);
return;
}
var response = {
user: data.user,
u2f_sign_response: res
}
api.post(cfg.api.u2fSessionPath, response, false).then(data=>{
};
u2fSignRet = api.post(cfg.api.u2fSessionPath, response, false).then(data=>{
session.setUserData(data);
auth._startTokenRefresher();
return data;
});
});
return u2fSignRet;
});
},