From 28ce3e4b70fb22c983f35d37c6fe0b073c3ea12e Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Tue, 25 Oct 2016 09:32:50 +0300 Subject: [PATCH] Fix bug with infinite loader, when page refreshed on /login with oauth state restoration active --- src/index.js | 6 +++--- src/services/authFlow/AuthFlow.js | 3 ++- tests/services/authFlow/AuthFlow.test.js | 11 ++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 4dc57ae..a5d083b 100644 --- a/src/index.js +++ b/src/index.js @@ -89,9 +89,9 @@ function restoreScroll() { /* global process: false */ if (process.env.NODE_ENV !== 'production') { // some shortcuts for testing on localhost - window.testOAuth = () => location.href = '/oauth2/v1/ely?client_id=ely&redirect_uri=http%3A%2F%2Fely.by&response_type=code&scope=minecraft_server_session'; - window.testOAuthStatic = () => location.href = '/oauth2/v1/ely?client_id=ely&redirect_uri=static_page_with_code&response_type=code&scope=minecraft_server_session'; - window.testOAuthStaticCode = () => location.href = '/oauth2/v1/ely?client_id=ely&redirect_uri=static_page&response_type=code&scope=minecraft_server_session'; + window.testOAuth = () => location.href = '/oauth2/v1/ely?client_id=ely&redirect_uri=http%3A%2F%2Fely.by%2Fauthorization%2Foauth&response_type=code&scope=account_info%2Caccount_email'; + window.testOAuthStatic = () => location.href = '/oauth2/v1/ely?client_id=ely&redirect_uri=static_page_with_code&response_type=code&scope=account_info%2Caccount_email'; + window.testOAuthStaticCode = () => location.href = '/oauth2/v1/ely?client_id=ely&redirect_uri=static_page&response_type=code&scope=account_info%2Caccount_email'; // expose Perf window.Perf = require('react-addons-perf'); diff --git a/src/services/authFlow/AuthFlow.js b/src/services/authFlow/AuthFlow.js index 4c2b897..f23779a 100644 --- a/src/services/authFlow/AuthFlow.js +++ b/src/services/authFlow/AuthFlow.js @@ -202,7 +202,8 @@ export default class AuthFlow { if (Date.now() - data.timestamp < expirationTime) { this.run('oAuthValidate', data.payload) - .then(() => this.setState(new CompleteState())); + .then(() => this.setState(new CompleteState())) + .then(() => this.onReady()); return true; } diff --git a/tests/services/authFlow/AuthFlow.test.js b/tests/services/authFlow/AuthFlow.test.js index b2b8d01..3c3fe82 100644 --- a/tests/services/authFlow/AuthFlow.test.js +++ b/tests/services/authFlow/AuthFlow.test.js @@ -58,7 +58,8 @@ describe('AuthFlow', () => { })); sinon.stub(flow, 'run').named('flow.run'); - flow.run.returns({then: (fn) => fn()}); + const promiseLike = {then: (fn) => fn() || promiseLike}; + flow.run.returns(promiseLike); sinon.stub(flow, 'setState').named('flow.setState'); }); @@ -96,6 +97,14 @@ describe('AuthFlow', () => { expect(flow.setState, 'was called once'); }); + it('should call onReady after state restoration', () => { + const onReady = sinon.stub().named('onReady'); + + flow.handleRequest({path: '/login'}, null, onReady); + + expect(onReady, 'was called'); + }); + it('should not restore oauth state for /register route', () => { flow.handleRequest({path: '/register'});