Reload page when refreshed credentials differ

When credentials are refreshed, but the new credentials are for a
different account than before, the entire page is reloaded. In this way,
the available UI actions will correspond to the new user's capabilities.

Bug: Issue 5820
Change-Id: I48ff608b2e68d284b6bbcf4f843f64d3390136a8
This commit is contained in:
Wyatt Allen
2017-03-21 15:23:43 -07:00
parent a7ad680e55
commit 99b3b045cd
4 changed files with 82 additions and 8 deletions

View File

@@ -170,10 +170,19 @@
},
_checkSignedIn: function() {
this.$.restAPI.checkCredentials().then(function(isLoggedIn) {
this.$.restAPI.checkCredentials().then(function(account) {
var isLoggedIn = !!account;
this._lastCredentialCheck = Date.now();
if (this._refreshingCredentials) {
if (isLoggedIn) {
// If the credentials were refreshed but the account is different
// then reload the page completely.
if (account._account_id !== this.knownAccountId) {
this._reloadPage();
return;
}
this._handleCredentialRefreshed();
} else {
this._requestCheckLoggedIn();
@@ -182,6 +191,10 @@
}.bind(this));
},
_reloadPage: function() {
window.location.reload();
},
_createLoginPopup: function() {
var left = window.screenLeft + (window.outerWidth - SIGN_IN_WIDTH_PX) / 2;
var top = window.screenTop + (window.outerHeight - SIGN_IN_HEIGHT_PX) / 2;

View File

@@ -164,5 +164,67 @@ limitations under the License.
assert.isTrue(refreshStub.called);
assert.equal(element._lastCredentialCheck, 999999);
});
test('refresh loop continues on credential fail', function(done) {
var accountPromise = Promise.resolve(null);
sandbox.stub(element.$.restAPI, 'checkCredentials')
.returns(accountPromise);
var requestCheckStub = sandbox.stub(element, '_requestCheckLoggedIn');
var handleRefreshStub = sandbox.stub(element,
'_handleCredentialRefreshed');
var reloadStub = sandbox.stub(element, '_reloadPage');
element._refreshingCredentials = true;
element._checkSignedIn();
accountPromise.then(function() {
assert.isTrue(requestCheckStub.called);
assert.isFalse(handleRefreshStub.called);
assert.isFalse(reloadStub.called);
done();
});
});
test('refreshes with same credentials', function(done) {
var accountPromise = Promise.resolve({_account_id: 1234});
sandbox.stub(element.$.restAPI, 'checkCredentials')
.returns(accountPromise);
var requestCheckStub = sandbox.stub(element, '_requestCheckLoggedIn');
var handleRefreshStub = sandbox.stub(element,
'_handleCredentialRefreshed');
var reloadStub = sandbox.stub(element, '_reloadPage');
element.knownAccountId = 1234;
element._refreshingCredentials = true;
element._checkSignedIn();
accountPromise.then(function() {
assert.isFalse(requestCheckStub.called);
assert.isTrue(handleRefreshStub.called);
assert.isFalse(reloadStub.called);
done();
});
});
test('reloads when refreshed credentials differ', function(done) {
var accountPromise = Promise.resolve({_account_id: 1234});
sandbox.stub(element.$.restAPI, 'checkCredentials')
.returns(accountPromise);
var requestCheckStub = sandbox.stub(element, '_requestCheckLoggedIn');
var handleRefreshStub = sandbox.stub(element,
'_handleCredentialRefreshed');
var reloadStub = sandbox.stub(element, '_reloadPage');
element.knownAccountId = 4321; // Different from 1234
element._refreshingCredentials = true;
element._checkSignedIn();
accountPromise.then(function() {
assert.isFalse(requestCheckStub.called);
assert.isFalse(handleRefreshStub.called);
assert.isTrue(reloadStub.called);
done();
});
});
});
</script>

View File

@@ -336,9 +336,7 @@
checkCredentials: function() {
// Skip the REST response cache.
return this.fetchJSON('/accounts/self/detail').then(function(account) {
return account != null;
});
return this.fetchJSON('/accounts/self/detail');
},
getPreferences: function() {

View File

@@ -451,10 +451,11 @@ limitations under the License.
return Promise.resolve(responses.shift());
}
});
element.getLoggedIn().then(function(isLoggedIn) {
assert.isFalse(isLoggedIn);
element.checkCredentials().then(function(isRefreshed) {
assert.isTrue(isRefreshed);
element.getLoggedIn().then(function(account) {
assert.isNotOk(account);
element.checkCredentials().then(function(account) {
assert.isOk(account);
done();
});
});