From c7b45d8c9bb3d735eed7587a2a95f84a8278301e Mon Sep 17 00:00:00 2001 From: mhelander Date: Mon, 18 Sep 2017 15:41:02 +0300 Subject: [PATCH] Improved recall test cases & dropped dead code from sea.js --- sea.js | 10 +--------- test/sea.js | 45 ++++++++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/sea.js b/sea.js index 81f12ded..e3fc7dd2 100644 --- a/sea.js +++ b/sea.js @@ -186,14 +186,6 @@ return SEA.write(JSON.stringify(remember), priv).then(function(signed){ sessionStorage.setItem('user', props.alias); sessionStorage.setItem('remember', signed); - if(!persist){ - return new Promise(function(resolve){ - SEA._callonstore_(function(store) { - var act = store.clear(); // Wipes whole IndexedDB - act.onsuccess = function(){}; - }, function(){ resolve() }); - }); - } }).then(function(){ return !persist || SEA.enc(persist, pin).then(function(encrypted){ return encrypted && SEA.write(encrypted, priv).then(function(signed){ @@ -212,7 +204,7 @@ }).then(function(){ resolve(props) }) .catch(function(e){ reject({err: 'Session persisting failed!'}) }); } - // WIping IndexedDB completely when using random PIN + // Wiping IndexedDB completely when using random PIN return new Promise(function(resolve){ SEA._callonstore_(function(store) { var act = store.clear(); diff --git a/test/sea.js b/test/sea.js index 7a007f3a..ab563602 100644 --- a/test/sea.js +++ b/test/sea.js @@ -312,9 +312,8 @@ Gun().user && describe('Gun', function(){ return function(){ var checkValue = function(data, val){ if(notStored){ - if(typeof data !== 'undefined' && data !== null && data !== ''){ - expect(data).to.not.be(undefined); - } + expect(typeof data !== 'undefined' && data !== null && data !== '') + .to.not.eql(true); } else { expect(data).to.not.be(undefined); expect(data).to.not.be(''); @@ -592,7 +591,7 @@ Gun().user && describe('Gun', function(){ }); }); - describe('recall', function(){ + describe('recall (from IndexedDB)', function(){ var doCheck = function(done, hasPin, wantAck){ expect(typeof done).to.be('function'); return function(ack){ @@ -658,7 +657,7 @@ Gun().user && describe('Gun', function(){ }); }; - it('with PIN auth session stored to IndexedDB', function(done){ + it('with PIN auth session stores', function(done){ var doAction = function(){ user.auth(alias+type, pass+' new', {pin: 'PIN'}) .then(doCheck(done, true)).catch(done); @@ -670,7 +669,7 @@ Gun().user && describe('Gun', function(){ } }); - it('without PIN auth session stored to IndexedDB', function(done){ + it('without PIN auth session stores', function(done){ var doAction = function(){ user.auth(alias+type, pass+' new').then(doCheck(done)); }; @@ -694,7 +693,7 @@ Gun().user && describe('Gun', function(){ } }); - it('validity but no PIN stored to IndexedDB using random PIN', function(done){ + it('with validity but no PIN stores using random PIN', function(done){ var doAction = function(){ user.auth(alias+type, pass+' new').then(doCheck(done)).catch(done); }; @@ -706,7 +705,7 @@ Gun().user && describe('Gun', function(){ } }); - it('valid sessionStorage session', function(done){ + it('validity and auth with PIN but storage empty', function(done){ user.auth(alias+type, pass+' new').then(function(usr){ var sUser; var sRemember; @@ -730,17 +729,26 @@ Gun().user && describe('Gun', function(){ expect(root.sessionStorage.getItem('user')).to.not.be(sUser); expect(root.sessionStorage.getItem('remember')).to.not.be(sRemember); }catch(e){ done(e); return } - + // Restore but leave IndexedDB empty root.sessionStorage.setItem('user', sUser); root.sessionStorage.setItem('remember', sRemember); - user.recall(12 * 60).then(doCheck(done)) + user.recall(12 * 60).then( + doCheck(function(ack){ + expect(ack).to.have.key('err'); + expect(ack.err.toLowerCase().indexOf('no authentication')).to.not.be(-1); + checkIndexedDB(alias+type, 'auth', function(auth){ + expect((typeof auth !== 'undefined' && auth !== null && auth !== '')) + .to.not.eql(true); + done(); + }); + }, false, true)) .catch(done); }).catch(done); }).catch(done); }); - it('valid IndexedDB session bootstrap', function(done){ + it('valid session bootstrap', function(done){ var sUser; var sRemember; var iAuth; @@ -790,7 +798,7 @@ Gun().user && describe('Gun', function(){ }).catch(done); }); - it('valid IndexedDB session bootstraps using PIN', function(done){ + it('valid session bootstrap using alias & PIN', function(done){ user.recall(12 * 60).then(function(){ return user.auth(alias+type, pass+' new', {pin: 'PIN'}); }).then(doCheck(function(ack){ @@ -813,7 +821,7 @@ Gun().user && describe('Gun', function(){ return new Promise(function(resolve){ checkIndexedDB(sUser, 'auth', function(auth){ try{ expect(auth).to.not.be(iAuth) }catch(e){ done(e) } - // Then restore IndexedDB auth data, skip sessionStorage + // Then restore IndexedDB but skip sessionStorage remember setIndexedDB(sUser, iAuth, function(){ root.sessionStorage.setItem('user', sUser); resolve(ack); @@ -847,7 +855,7 @@ Gun().user && describe('Gun', function(){ }, true, true)).catch(done); }); - it('valid IndexedDB session fails to bootstrap using wrong PIN', + it('valid session fails to bootstrap with alias & wrong PIN', function(done){ user.recall(12 * 60).then(function(){ return user.auth(alias+type, pass+' new', {pin: 'PIN'}); @@ -963,10 +971,10 @@ Gun().user && describe('Gun', function(){ }); }).then(function(){ // Simulate browser reload - // Call back previous remember data + // Call back pre-update remember... root.sessionStorage.setItem('user', sUser); root.sessionStorage.setItem('remember', sRemember); - + // ... and IndexedDB auth return new Promise(function(resolve){ setIndexedDB(sUser, iAuth, resolve); }); @@ -989,16 +997,15 @@ Gun().user && describe('Gun', function(){ var pin = 'PIN'; var exp; var hookFunc = function(props){ - exp = props.exp * 2; + exp = props.exp * 2; // Doubles session expiration time var ret = Object.assign({}, props, {exp: exp}); return (type === 'callback' && ret) || new Promise(function(resolve){ - resolve(ret); + resolve(ret); // Both callback & Promise methods here }); }; user.recall(60, {hook: hookFunc}).then(function(){ return user.auth(alias+type, pass, {pin: pin}); }).then(function(){ - // Storage data OK, let's back up time of auth 65 minutes return manipulateStorage(function(props){ expect(props).to.not.be(undefined); expect(props).to.have.key('exp');