From b4bbbe3372977943a1c4ed941d1c4d2be869b25e Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Wed, 19 Apr 2017 08:11:05 +0300 Subject: [PATCH] #327: allow restore-password access for everyone. Update auth.login state, after forgot-password submit --- src/services/authFlow/ForgotPasswordState.js | 9 +++++++-- src/services/authFlow/RecoverPasswordState.js | 14 ++++--------- .../authFlow/ForgotPasswordState.test.js | 20 ++++++++++++++++++- .../authFlow/RecoverPasswordState.test.js | 16 --------------- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/services/authFlow/ForgotPasswordState.js b/src/services/authFlow/ForgotPasswordState.js index b3d10a9..d9a1fa4 100644 --- a/src/services/authFlow/ForgotPasswordState.js +++ b/src/services/authFlow/ForgotPasswordState.js @@ -10,8 +10,13 @@ export default class ForgotPasswordState extends AbstractState { } resolve(context, payload = {}) { - context.run('forgotPassword', {login: payload.email || this.getLogin(context)}) - .then(() => context.setState(new RecoverPasswordState())) + const login = payload.email || this.getLogin(context); + + context.run('forgotPassword', {login}) + .then(() => { + context.run('setLogin', login); + context.setState(new RecoverPasswordState()); + }) .catch((err = {}) => err.errors || logger.warn('Error requesting password recoverage', err) ); diff --git a/src/services/authFlow/RecoverPasswordState.js b/src/services/authFlow/RecoverPasswordState.js index 43c2a23..36588e4 100644 --- a/src/services/authFlow/RecoverPasswordState.js +++ b/src/services/authFlow/RecoverPasswordState.js @@ -6,16 +6,10 @@ import CompleteState from './CompleteState'; export default class RecoverPasswordState extends AbstractState { enter(context) { - const {auth} = context.getState(); - - if (auth.login) { - const url = context.getRequest().path.includes('/recover-password') - ? context.getRequest().path - : '/recover-password'; - context.navigate(url); - } else { - context.setState(new CompleteState()); - } + const url = context.getRequest().path.includes('/recover-password') + ? context.getRequest().path + : '/recover-password'; + context.navigate(url); } resolve(context, payload) { diff --git a/tests/services/authFlow/ForgotPasswordState.test.js b/tests/services/authFlow/ForgotPasswordState.test.js index f805862..fc371af 100644 --- a/tests/services/authFlow/ForgotPasswordState.test.js +++ b/tests/services/authFlow/ForgotPasswordState.test.js @@ -79,13 +79,31 @@ describe('ForgotPasswordState', () => { } }); - mock.expects('run').returns(promise); + mock.expects('run').twice().returns(promise); expectState(mock, RecoverPasswordState); state.resolve(context, {}); return promise; }); + + it('should run setLogin on success', () => { + const promise = Promise.resolve(); + const expectedLogin = 'foo@bar.com'; + context.getState.returns({ + auth: { + login: expectedLogin + } + }); + + mock.expects('run').withArgs('forgotPassword').returns(promise); + expectState(mock, RecoverPasswordState); + mock.expects('run').withArgs('setLogin', expectedLogin); + + state.resolve(context, {}); + + return promise; + }); }); describe('#reject', () => { diff --git a/tests/services/authFlow/RecoverPasswordState.test.js b/tests/services/authFlow/RecoverPasswordState.test.js index eb9db07..387bf4d 100644 --- a/tests/services/authFlow/RecoverPasswordState.test.js +++ b/tests/services/authFlow/RecoverPasswordState.test.js @@ -26,9 +26,6 @@ describe('RecoverPasswordState', () => { describe('#enter', () => { it('should navigate to /recover-password', () => { const expectedPath = '/recover-password'; - context.getState.returns({ - auth: {login: 'foo'} - }); context.getRequest.returns({path: expectedPath}); @@ -39,9 +36,6 @@ describe('RecoverPasswordState', () => { it('should navigate to /recover-password/key', () => { const expectedPath = '/recover-password/sasx5AS4d61'; - context.getState.returns({ - auth: {login: 'foo'} - }); context.getRequest.returns({path: expectedPath}); @@ -49,16 +43,6 @@ describe('RecoverPasswordState', () => { state.enter(context); }); - - it('should transition to complete if not guest', () => { - context.getState.returns({ - auth: {} - }); - - expectState(mock, CompleteState); - - state.enter(context); - }); }); describe('#resolve', () => {