mirror of
				https://github.com/elyby/accounts-frontend.git
				synced 2025-05-31 14:11:58 +05:30 
			
		
		
		
	#327: fix forgot password behaviour for logged in user
This commit is contained in:
		@@ -46,7 +46,10 @@ export default class BaseAuthBody extends Component {
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    bindField = this.form.bindField.bind(this.form);
 | 
			
		||||
    serialize = this.form.serialize.bind(this.form);
 | 
			
		||||
 | 
			
		||||
    serialize() {
 | 
			
		||||
        return this.form.serialize();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    autoFocus() {
 | 
			
		||||
        const fieldId = this.autoFocusField;
 | 
			
		||||
 
 | 
			
		||||
@@ -15,14 +15,13 @@ export default class ForgotPasswordBody extends BaseAuthBody {
 | 
			
		||||
    static hasGoBack = true;
 | 
			
		||||
 | 
			
		||||
    state = {
 | 
			
		||||
        isLoginEdit: !(this.context.user.email || this.context.user.username)
 | 
			
		||||
        isLoginEdit: !this.getLogin()
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    autoFocusField = this.state.isLoginEdit ? 'email' : null;
 | 
			
		||||
 | 
			
		||||
    render() {
 | 
			
		||||
        const { user } = this.context;
 | 
			
		||||
        const login = user.email || user.username || '';
 | 
			
		||||
        const login = this.getLogin();
 | 
			
		||||
        const isLoginEditShown = this.state.isLoginEdit;
 | 
			
		||||
 | 
			
		||||
        return (
 | 
			
		||||
@@ -61,6 +60,22 @@ export default class ForgotPasswordBody extends BaseAuthBody {
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    serialize() {
 | 
			
		||||
        const data = super.serialize();
 | 
			
		||||
 | 
			
		||||
        if (!data.email) {
 | 
			
		||||
            data.email = this.getLogin();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return data;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getLogin() {
 | 
			
		||||
        const { user, auth } = this.context;
 | 
			
		||||
 | 
			
		||||
        return auth.login || user.username || user.email || '';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    onClickEdit = () => {
 | 
			
		||||
        this.setState({
 | 
			
		||||
            isLoginEdit: true
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ export default class ForgotPasswordState extends AbstractState {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    resolve(context, payload = {}) {
 | 
			
		||||
        const login = payload.email || this.getLogin(context);
 | 
			
		||||
        const login = payload.email;
 | 
			
		||||
 | 
			
		||||
        context.run('forgotPassword', {login})
 | 
			
		||||
            .then(() => {
 | 
			
		||||
@@ -29,10 +29,4 @@ export default class ForgotPasswordState extends AbstractState {
 | 
			
		||||
    reject(context) {
 | 
			
		||||
        context.setState(new RecoverPasswordState());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    getLogin(context) {
 | 
			
		||||
        const {auth} = context.getState();
 | 
			
		||||
 | 
			
		||||
        return auth.login;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -32,32 +32,8 @@ describe('ForgotPasswordState', () => {
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    describe('#resolve', () => {
 | 
			
		||||
        it('should call forgotPassword with login', () => {
 | 
			
		||||
        it('should call forgotPassword with email from payload', () => {
 | 
			
		||||
            const expectedLogin = 'foo@bar.com';
 | 
			
		||||
            context.getState.returns({
 | 
			
		||||
                auth: {
 | 
			
		||||
                    login: expectedLogin
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            expectRun(
 | 
			
		||||
                mock,
 | 
			
		||||
                'forgotPassword',
 | 
			
		||||
                sinon.match({
 | 
			
		||||
                    login: expectedLogin
 | 
			
		||||
                })
 | 
			
		||||
            ).returns(Promise.resolve());
 | 
			
		||||
 | 
			
		||||
            state.resolve(context, {});
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        it('should call forgotPassword with email from payload if any', () => {
 | 
			
		||||
            const expectedLogin = 'foo@bar.com';
 | 
			
		||||
            context.getState.returns({
 | 
			
		||||
                auth: {
 | 
			
		||||
                    login: 'should.not@be.used'
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            expectRun(
 | 
			
		||||
                mock,
 | 
			
		||||
@@ -73,16 +49,11 @@ describe('ForgotPasswordState', () => {
 | 
			
		||||
        it('should transition to recoverPassword state on success', () => {
 | 
			
		||||
            const promise = Promise.resolve();
 | 
			
		||||
            const expectedLogin = 'foo@bar.com';
 | 
			
		||||
            context.getState.returns({
 | 
			
		||||
                auth: {
 | 
			
		||||
                    login: expectedLogin
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            mock.expects('run').twice().returns(promise);
 | 
			
		||||
            expectState(mock, RecoverPasswordState);
 | 
			
		||||
 | 
			
		||||
            state.resolve(context, {});
 | 
			
		||||
            state.resolve(context, {email: expectedLogin});
 | 
			
		||||
 | 
			
		||||
            return promise;
 | 
			
		||||
        });
 | 
			
		||||
@@ -90,17 +61,12 @@ describe('ForgotPasswordState', () => {
 | 
			
		||||
        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, {});
 | 
			
		||||
            state.resolve(context, {email: expectedLogin});
 | 
			
		||||
 | 
			
		||||
            return promise;
 | 
			
		||||
        });
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user