From 1e593019be20dba0b68e2dbd0ceb1aa41afc1152 Mon Sep 17 00:00:00 2001 From: SleepWalker Date: Wed, 23 Nov 2016 08:05:28 +0200 Subject: [PATCH] #251: fix forgot password panel --- src/services/authFlow/ForgotPasswordState.js | 14 ++--- src/services/authFlow/RecoverPasswordState.js | 4 +- .../authFlow/ForgotPasswordState.test.js | 61 ++++--------------- .../authFlow/RecoverPasswordState.test.js | 6 +- 4 files changed, 20 insertions(+), 65 deletions(-) diff --git a/src/services/authFlow/ForgotPasswordState.js b/src/services/authFlow/ForgotPasswordState.js index 187fa90..ce94582 100644 --- a/src/services/authFlow/ForgotPasswordState.js +++ b/src/services/authFlow/ForgotPasswordState.js @@ -5,14 +5,8 @@ import RecoverPasswordState from './RecoverPasswordState'; export default class ForgotPasswordState extends AbstractState { enter(context) { - const {user} = context.getState(); - - if (user.isGuest) { - if (this.getLogin(context)) { - context.navigate('/forgot-password'); - } else { - context.setState(new LoginState()); - } + if (this.getLogin(context)) { + context.navigate('/forgot-password'); } else { context.setState(new CompleteState()); } @@ -32,8 +26,8 @@ export default class ForgotPasswordState extends AbstractState { } getLogin(context) { - const {user} = context.getState(); + const {auth} = context.getState(); - return user.email || user.username; + return auth.login; } } diff --git a/src/services/authFlow/RecoverPasswordState.js b/src/services/authFlow/RecoverPasswordState.js index 008fdd3..5d935c0 100644 --- a/src/services/authFlow/RecoverPasswordState.js +++ b/src/services/authFlow/RecoverPasswordState.js @@ -4,9 +4,9 @@ import CompleteState from './CompleteState'; export default class RecoverPasswordState extends AbstractState { enter(context) { - const {user} = context.getState(); + const {auth} = context.getState(); - if (user.isGuest) { + if (auth.login) { const url = context.getRequest().path.includes('/recover-password') ? context.getRequest().path : '/recover-password'; diff --git a/tests/services/authFlow/ForgotPasswordState.test.js b/tests/services/authFlow/ForgotPasswordState.test.js index eb27d58..17a2af8 100644 --- a/tests/services/authFlow/ForgotPasswordState.test.js +++ b/tests/services/authFlow/ForgotPasswordState.test.js @@ -23,9 +23,9 @@ describe('ForgotPasswordState', () => { }); describe('#enter', () => { - it('should navigate to /forgot-password if email set', () => { + it('should navigate to /forgot-password if login set', () => { context.getState.returns({ - user: {isGuest: true, email: 'foo@bar.com'} + auth: {login: 'foo@bar.com'} }); expectNavigate(mock, '/forgot-password'); @@ -33,43 +33,23 @@ describe('ForgotPasswordState', () => { state.enter(context); }); - it('should navigate to /forgot-password if username set', () => { + it('should transition to complete if no login', () => { context.getState.returns({ - user: {isGuest: true, username: 'foobar'} - }); - - expectNavigate(mock, '/forgot-password'); - - state.enter(context); - }); - - it('should transition to complete if not guest', () => { - context.getState.returns({ - user: {isGuest: false} + auth: {} }); expectState(mock, CompleteState); state.enter(context); }); - - it('should transition to login if no identity', () => { - context.getState.returns({ - user: {isGuest: true} - }); - - expectState(mock, LoginState); - - state.enter(context); - }); }); describe('#resolve', () => { - it('should call forgotPassword with email', () => { + it('should call forgotPassword with login', () => { const expectedLogin = 'foo@bar.com'; context.getState.returns({ - user: { - email: expectedLogin + auth: { + login: expectedLogin } }); @@ -87,8 +67,8 @@ describe('ForgotPasswordState', () => { it('should call forgotPassword with email from payload if any', () => { const expectedLogin = 'foo@bar.com'; context.getState.returns({ - user: { - email: 'should.not@be.used' + auth: { + login: 'should.not@be.used' } }); @@ -103,31 +83,12 @@ describe('ForgotPasswordState', () => { state.resolve(context, {email: expectedLogin}); }); - it('should call forgotPassword with username', () => { - const expectedLogin = 'foobar'; - context.getState.returns({ - user: { - username: expectedLogin - } - }); - - expectRun( - mock, - 'forgotPassword', - sinon.match({ - login: expectedLogin - }) - ).returns({then() {}}); - - state.resolve(context, {}); - }); - it('should transition to recoverPassword state on success', () => { const promise = Promise.resolve(); const expectedLogin = 'foo@bar.com'; context.getState.returns({ - user: { - email: expectedLogin + auth: { + login: expectedLogin } }); diff --git a/tests/services/authFlow/RecoverPasswordState.test.js b/tests/services/authFlow/RecoverPasswordState.test.js index a4930e8..dc2e9f7 100644 --- a/tests/services/authFlow/RecoverPasswordState.test.js +++ b/tests/services/authFlow/RecoverPasswordState.test.js @@ -25,7 +25,7 @@ describe('RecoverPasswordState', () => { it('should navigate to /recover-password', () => { const expectedPath = '/recover-password'; context.getState.returns({ - user: {isGuest: true} + auth: {login: 'foo'} }); context.getRequest.returns({path: expectedPath}); @@ -38,7 +38,7 @@ describe('RecoverPasswordState', () => { it('should navigate to /recover-password/key', () => { const expectedPath = '/recover-password/sasx5AS4d61'; context.getState.returns({ - user: {isGuest: true} + auth: {login: 'foo'} }); context.getRequest.returns({path: expectedPath}); @@ -50,7 +50,7 @@ describe('RecoverPasswordState', () => { it('should transition to complete if not guest', () => { context.getState.returns({ - user: {isGuest: false} + auth: {} }); expectState(mock, CompleteState);