[Beta Tester] Add tool to force wccom endpoint errors or timeout (#50654)

This commit is contained in:
Paul Sealock 2024-08-19 20:35:59 +12:00 committed by GitHub
parent db9623395d
commit 17c6fb9b0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 219 additions and 0 deletions

View File

@ -64,3 +64,4 @@ require 'live-branches/install.php';
require 'remote-spec-validator/class-wca-test-helper-remote-spec-validator.php';
require 'remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php';
require 'remote-logging/remote-logging.php';
require 'tools/wccom-request-errors.php';

View File

@ -0,0 +1,48 @@
<?php
defined( 'ABSPATH' ) || exit;
register_woocommerce_admin_test_helper_rest_route(
'/tools/set-wccom-request-errors/v1',
'tools_set_wccom_request_errors',
array(
'methods' => 'POST',
'args' => array(
'mode' => array(
'description' => 'wccom request error mode',
'type' => 'enum',
'enum' => array( 'timeout', 'error', 'disabled' ),
),
),
)
);
register_woocommerce_admin_test_helper_rest_route(
'/tools/get-wccom-request-errors/v1',
'tools_get_wccom_request_errors',
array(
'methods' => 'GET',
)
);
/**
* A tool to set wccom request errors mode.
*
* @param WP_REST_Request $request Request object.
*/
function tools_set_wccom_request_errors( $request ) {
$mode = $request->get_param( 'mode' );
update_option( 'wc_admin_test_helper_modify_wccom_request_responses', $mode );
return new WP_REST_Response( $mode, 200 );
}
/**
* A tool to get wccom request mode.
*/
function tools_get_wccom_request_errors() {
$mode = get_option( 'wc_admin_test_helper_modify_wccom_request_responses', 'disabled' );
return new WP_REST_Response( $mode, 200 );
}

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add a tool to simulate server problems with woocommerce.com

View File

@ -0,0 +1,74 @@
<?php
/**
* Modify wccom request responses.
* @package WC_Beta_Tester
*/
defined( 'ABSPATH' ) || exit;
/**
* WC_Beta_Tester Modify wccom request responses.
*/
class WC_Beta_Tester_WCCOM_Requests {
/**
* Constructor.
*/
public function __construct() {
$this->wc_beta_tester_modify_wccom_responses();
}
/**
* Modify responses based on option value.
*/
public function wc_beta_tester_modify_wccom_responses() {
$mode = get_option( 'wc_admin_test_helper_modify_wccom_request_responses', 'disabled' );
if ( 'disabled' === $mode ) {
return;
}
if ( 'timeout' === $mode ) {
add_filter( 'pre_http_request', array( $this, 'override_http_request_timeout' ), 10, 3 );
}
if ( 'error' === $mode ) {
add_filter( 'pre_http_request', array( $this, 'override_http_request_error' ), 10, 3 );
}
}
/**
* Override the http request with a timeout.
*/
public function override_http_request_timeout( $response, $args, $url ) {
if ( strpos( $url, 'https://woocommerce.com/wp-json/' ) !== false || strpos( $url, 'woocommerce.test/wp-json/' ) !== false ) {
sleep( 6 ); // 6 seconds
return new WP_Error( 'http_request_timeout', 'Mock timeout error' );
}
return false;
}
/**
* Override the http request with an error.
*/
public function override_http_request_error( $response, $args, $url ) {
if ( strpos( $url, 'https://woocommerce.com/wp-json/' ) !== false ) {
return array(
'response' => array(
'code' => 500,
'message' => 'Internal Server Error',
),
'body' => 'Server Error',
'headers' => array(),
'cookies' => array(),
'filename' => null,
);
}
return false;
}
}
new WC_Beta_Tester_WCCOM_Requests();

View File

@ -16,6 +16,11 @@ import {
UPDATE_COMING_SOON_MODE_ACTION_NAME,
} from './set-coming-soon-mode';
import {
UPDATE_WCCOM_REQUEST_ERRORS_MODE,
SetWccomRequestErrros,
} from './set-wccom-request-errors';
export default [
{
command: 'Trigger WCA Install',
@ -87,4 +92,9 @@ export default [
description: <SetComingSoonMode />,
action: UPDATE_COMING_SOON_MODE_ACTION_NAME,
},
{
command: 'Force errors on woocommerce.com requests',
description: <SetWccomRequestErrros />,
action: UPDATE_WCCOM_REQUEST_ERRORS_MODE,
},
];

View File

@ -0,0 +1,48 @@
/**
* External dependencies
*/
import { SelectControl } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import { STORE_KEY } from '../data/constants';
export const UPDATE_WCCOM_REQUEST_ERRORS_MODE = 'updateWccomRequestErrorsMode';
const OPTIONS = [
{ label: 'Timeout after 6 seconds', value: 'timeout' },
{ label: '500', value: 'error' },
{ label: 'Disabled', value: 'disabled' },
];
export const SetWccomRequestErrros = () => {
const errorsMode = useSelect(
( select ) => select( STORE_KEY ).getWccomRequestErrorsMode(),
[]
);
const { updateCommandParams } = useDispatch( STORE_KEY );
function onChange( mode ) {
updateCommandParams( UPDATE_WCCOM_REQUEST_ERRORS_MODE, {
mode,
} );
}
return (
<div className="select-description">
{ ! errorsMode ? (
<p>Loading ...</p>
) : (
<SelectControl
label="Error Mode"
labelPosition="side"
value={ errorsMode }
onChange={ onChange }
options={ OPTIONS }
/>
) }
</div>
);
};

View File

@ -272,3 +272,13 @@ export function* updateComingSoonMode( params ) {
} );
} );
}
export function* updateWccomRequestErrorsMode( params ) {
yield runCommand( 'Update wccom request errors mode', function* () {
yield apiFetch( {
path: API_NAMESPACE + '/tools/set-wccom-request-errors/v1',
method: 'POST',
data: params,
} );
} );
}

View File

@ -13,6 +13,7 @@ const DEFAULT_STATE = {
updateComingSoonMode: {},
updateBlockTemplateLoggingThreshold: {},
runSelectedUpdateCallbacks: {},
updateWccomRequestErrorsMode: {},
},
status: '',
dbUpdateVersions: [],

View File

@ -17,6 +17,7 @@ import {
import { UPDATE_BLOCK_TEMPLATE_LOGGING_THRESHOLD_ACTION_NAME } from '../commands/update-block-template-logging-threshold';
import { UPDATE_COMING_SOON_MODE_ACTION_NAME } from '../commands/set-coming-soon-mode';
import { TRIGGER_UPDATE_CALLBACKS_ACTION_NAME } from '../commands/trigger-update-callbacks';
import { UPDATE_WCCOM_REQUEST_ERRORS_MODE } from '../commands/set-wccom-request-errors';
export function* getCronJobs() {
const path = `${ API_NAMESPACE }/tools/get-cron-list/v1`;
@ -117,3 +118,20 @@ export function* getComingSoonMode() {
throw new Error( error );
}
}
export function* getWccomRequestErrorsMode() {
const path = `${ API_NAMESPACE }/tools/get-wccom-request-errors/v1`;
try {
const mode = yield apiFetch( {
path,
method: 'GET',
} );
yield updateCommandParams( UPDATE_WCCOM_REQUEST_ERRORS_MODE, {
mode: mode || 'disabled',
} );
} catch ( error ) {
throw new Error( error );
}
}

View File

@ -37,3 +37,7 @@ export function getBlockTemplateLoggingThreshold( state ) {
export function getComingSoonMode( state ) {
return state.params.updateComingSoonMode.mode;
}
export function getWccomRequestErrorsMode( state ) {
return state.params.updateWccomRequestErrorsMode.mode;
}

View File

@ -64,6 +64,7 @@ function _wc_beta_tester_bootstrap() {
// Tools.
include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-version-picker.php';
include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-override-coming-soon-options.php';
include dirname( __FILE__ ) . '/includes/class-wc-beta-tester-wccom-requests.php';
register_activation_hook( __FILE__, array( 'WC_Beta_Tester', 'activate' ) );