Skip to content

Commit

Permalink
Fix isPartnerSignupQuery URIError (#86514)
Browse files Browse the repository at this point in the history
  • Loading branch information
chihsuan authored Jan 18, 2024
1 parent 69ebfd0 commit b0433c2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
44 changes: 44 additions & 0 deletions client/state/login/test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { isPartnerSignupQuery } from '../utils';

describe( 'isPartnerSignupQuery', () => {
it( 'should return false when currentQuery is undefined', () => {
const result = isPartnerSignupQuery( undefined );
expect( result ).toBe( false );
} );

it( 'should return false when currentQuery is null', () => {
const result = isPartnerSignupQuery( null );
expect( result ).toBe( false );
} );

it( 'should return true when currentQuery has is_partner_signup', () => {
const currentQuery = { is_partner_signup: true };
const result = isPartnerSignupQuery( currentQuery );
expect( result ).toBe( true );
} );

it( 'should return true when currentQuery has redirect_to with partner-signup', () => {
const currentQuery = { redirect_to: 'https://woocommerce.com/partner-signup' };
const result = isPartnerSignupQuery( currentQuery );
expect( result ).toBe( true );
} );

it( 'should return true when currentQuery has oauth2_redirect with partner-signup', () => {
const currentQuery = { oauth2_redirect: 'https://woocommerce.com/partner-signup' };
const result = isPartnerSignupQuery( currentQuery );
expect( result ).toBe( true );
} );

it( 'should throw an error when an unexpected error occurs', () => {
const currentQuery = { redirect_to: 'https://woocommerce.com/partner-signup' };
const originalDecodeURIComponent = global.decodeURIComponent;
global.decodeURIComponent = jest.fn( () => {
throw new Error( 'Unexpected error' );
} );

expect( () => isPartnerSignupQuery( currentQuery ) ).toThrow( 'Unexpected error' );

// Restore the original decodeURIComponent function
global.decodeURIComponent = originalDecodeURIComponent;
} );
} );
31 changes: 20 additions & 11 deletions client/state/login/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,27 @@ export function isPartnerSignupQuery( currentQuery ) {
return true;
}

// Handles login through /log-in/?redirect_to=...
if ( typeof currentQuery?.redirect_to === 'string' ) {
return /woocommerce\.(?:com|test)\/partner-signup/.test(
decodeURIComponent( currentQuery.redirect_to )
);
}
try {
// Handles login through /log-in/?redirect_to=...
if ( typeof currentQuery?.redirect_to === 'string' ) {
return /woocommerce\.(?:com|test)\/partner-signup/.test(
decodeURIComponent( currentQuery.redirect_to )
);
}

// Handles user creation through /start/wpcc?oauth2_redirect=...
if ( typeof currentQuery?.oauth2_redirect === 'string' ) {
return /woocommerce\.(?:com|test)\/partner-signup/.test(
decodeURIComponent( currentQuery.oauth2_redirect )
);
// Handles user creation through /start/wpcc?oauth2_redirect=...
if ( typeof currentQuery?.oauth2_redirect === 'string' ) {
return /woocommerce\.(?:com|test)\/partner-signup/.test(
decodeURIComponent( currentQuery.oauth2_redirect )
);
}
} catch ( e ) {
if ( e instanceof URIError ) {
// Ignore the URIError
return false;
}
// Should not happen, re-throw the error
throw e;
}

return false;
Expand Down

0 comments on commit b0433c2

Please sign in to comment.