From b0433c2d10e17a89b4a35b2fcb572beb451e305f Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Thu, 18 Jan 2024 10:56:29 +0800 Subject: [PATCH] Fix isPartnerSignupQuery URIError (#86514) --- client/state/login/test/utils.js | 44 ++++++++++++++++++++++++++++++++ client/state/login/utils.js | 31 ++++++++++++++-------- 2 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 client/state/login/test/utils.js diff --git a/client/state/login/test/utils.js b/client/state/login/test/utils.js new file mode 100644 index 00000000000000..f03d0eba2fa824 --- /dev/null +++ b/client/state/login/test/utils.js @@ -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; + } ); +} ); diff --git a/client/state/login/utils.js b/client/state/login/utils.js index f12c932ce1ca40..52ca29ffc1e757 100644 --- a/client/state/login/utils.js +++ b/client/state/login/utils.js @@ -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;