Skip to content

Commit

Permalink
Update the logic to use state functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenDufresne committed Jan 16, 2025
1 parent a128300 commit ad61f86
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 34 deletions.
23 changes: 14 additions & 9 deletions client/login/controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import config from '@automattic/calypso-config';
import page from '@automattic/calypso-router';
import { getUrlParts } from '@automattic/calypso-url';
import { isGravPoweredOAuth2Client } from 'calypso/lib/oauth2-clients';
import { isGravPoweredOAuth2Client, isWooOAuth2Client } from 'calypso/lib/oauth2-clients';
import { SOCIAL_HANDOFF_CONNECT_ACCOUNT } from 'calypso/state/action-types';
import { isUserLoggedIn, getCurrentUserLocale } from 'calypso/state/current-user/selectors';
import { fetchOAuth2ClientData } from 'calypso/state/oauth2-clients/actions';
import { getOAuth2Client } from 'calypso/state/oauth2-clients/selectors';
import { getCurrentOAuth2Client } from 'calypso/state/oauth2-clients/ui/selectors';
import getIsBlazePro from 'calypso/state/selectors/get-is-blaze-pro';
import getIsWooPasswordless from 'calypso/state/selectors/get-is-woo-passwordless';
import isWooPasswordlessJPCFlow from 'calypso/state/selectors/is-woo-passwordless-jpc-flow';
import MagicLogin from './magic-login';
import HandleEmailedLinkForm from './magic-login/handle-emailed-link-form';
import HandleEmailedLinkFormJetpackConnect from './magic-login/handle-emailed-link-form-jetpack-connect';
Expand Down Expand Up @@ -304,26 +308,27 @@ export function redirectJetpack( context, next ) {
/**
* Redirect clients to use PHP lost password. Excludes WooCommerce and Tumblr Blaze Pro.
* @param {Object} context - The context object containing request parameters and query strings.
* @param {Object} context.params - The parameters from the URL, including the action.
* @param {Object} context.query - The query string from the URL, including the `redirect_to` URL.
* @param {Function} next - The next middleware function to call if conditions are met.
* @returns {void} Either redirects the user or invokes the `next()` middleware function.
*/
export function redirectLostPassword( context, next ) {
const { action } = context.params;
const { redirect_to } = context.query;

if ( action !== 'lostpassword' ) {
next();
return;
}

const exclusions = [ 'blazepro.tumblr.com', 'woocommerce.com' ];
const state = context.store.getState();
const oauth2Client = getCurrentOAuth2Client( state );

if (
redirect_to === undefined ||
! exclusions.some( ( exclusion ) => redirect_to.includes( exclusion ) )
) {
const shouldRedirectToLostPassword = () =>
! getIsBlazePro( state ) &&
! getIsWooPasswordless( state ) &&
! isWooOAuth2Client( oauth2Client ) &&
! isWooPasswordlessJPCFlow( state );

if ( shouldRedirectToLostPassword() ) {
return context.redirect( 301, '/wp-login.php?action=lostpassword' );
}

Expand Down
73 changes: 48 additions & 25 deletions client/login/test/controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import page from '@automattic/calypso-router';
import { isWooOAuth2Client } from 'calypso/lib/oauth2-clients';
import { getOAuth2Client } from 'calypso/state/oauth2-clients/selectors';
import { getCurrentOAuth2Client } from 'calypso/state/oauth2-clients/ui/selectors';
import getIsBlazePro from 'calypso/state/selectors/get-is-blaze-pro';
import getIsWooPasswordless from 'calypso/state/selectors/get-is-woo-passwordless';
import isWooPasswordlessJPCFlow from 'calypso/state/selectors/is-woo-passwordless-jpc-flow';
import { redirectJetpack, redirectLostPassword, login } from '../controller';

jest.mock( 'calypso/state/oauth2-clients/actions', () => ( {
Expand All @@ -12,6 +17,31 @@ jest.mock( 'calypso/state/oauth2-clients/selectors', () => ( {
getOAuth2Client: jest.fn(),
} ) );

jest.mock( 'calypso/lib/oauth2-clients', () => ( {
...jest.requireActual( 'calypso/lib/oauth2-clients' ),
isWooOAuth2Client: jest.fn(),
} ) );

jest.mock( 'calypso/state/oauth2-clients/ui/selectors', () => ( {
...jest.requireActual( 'calypso/state/oauth2-clients/ui/selectors' ),
getCurrentOAuth2Client: jest.fn(),
} ) );

jest.mock( 'calypso/state/selectors/get-is-blaze-pro', () => ( {
__esModule: true,
default: jest.fn(),
} ) );

jest.mock( 'calypso/state/selectors/get-is-woo-passwordless', () => ( {
__esModule: true,
default: jest.fn(),
} ) );

jest.mock( 'calypso/state/selectors/is-woo-passwordless-jpc-flow', () => ( {
__esModule: true,
default: jest.fn(),
} ) );

jest.mock( '@automattic/calypso-router' );

describe( 'redirectJetpack', () => {
Expand Down Expand Up @@ -114,9 +144,15 @@ describe( 'redirectLostPassword', () => {
context = {
params: {},
query: {},
store: {
getState: jest.fn(),
},
redirect: jest.fn(),
};
next = jest.fn();

// Reset all mocks before each test
jest.clearAllMocks();
} );

it( 'should call next() if action is not "lostpassword"', () => {
Expand All @@ -128,42 +164,29 @@ describe( 'redirectLostPassword', () => {
expect( context.redirect ).not.toHaveBeenCalled();
} );

it( 'should redirect to "/wp-login.php?action=lostpassword" if redirect_to is undefined', () => {
it( 'should call next() if isWooOAuth2Client returns true', () => {
context.params.action = 'lostpassword';

redirectLostPassword( context, next );

expect( context.redirect ).toHaveBeenCalledWith( 301, '/wp-login.php?action=lostpassword' );
} );

it( 'should redirect to "/wp-login.php?action=lostpassword" if redirect_to does not include the WooCommerce URI', () => {
context.params.action = 'lostpassword';
context.query.redirect_to = 'https://example.com/some-other-uri';

redirectLostPassword( context, next );

expect( context.redirect ).toHaveBeenCalledWith( 301, '/wp-login.php?action=lostpassword' );
} );

it( 'should not redirect if redirect_to includes the WooCommerce URI', () => {
context.params.action = 'lostpassword';
context.query.redirect_to =
'https://example.com?redirect_uri=https://woocommerce.com/wc-api/wpcom-signin';
context.store.getState.mockReturnValueOnce( {} );
getCurrentOAuth2Client.mockReturnValueOnce( 'mocked-client' );
isWooOAuth2Client.mockReturnValueOnce( true );

redirectLostPassword( context, next );

expect( context.redirect ).not.toHaveBeenCalled();
expect( next ).toHaveBeenCalled();
} );

it( 'should not redirect if redirect_to includes the Blaze Pro URI', () => {
it( 'should redirect to "/wp-login.php?action=lostpassword" if none of the conditions are true', () => {
context.params.action = 'lostpassword';
context.query.redirect_to =
'https://example.com?redirect_uri=https://blazepro.tumblr.com/auth/connected';
context.store.getState.mockReturnValueOnce( {} );
getIsBlazePro.mockReturnValueOnce( false );
getIsWooPasswordless.mockReturnValueOnce( false );
isWooOAuth2Client.mockReturnValueOnce( false );
isWooPasswordlessJPCFlow.mockReturnValueOnce( false );

redirectLostPassword( context, next );

expect( context.redirect ).not.toHaveBeenCalled();
expect( next ).toHaveBeenCalled();
expect( context.redirect ).toHaveBeenCalledWith( 301, '/wp-login.php?action=lostpassword' );
expect( next ).not.toHaveBeenCalled();
} );
} );

0 comments on commit ad61f86

Please sign in to comment.