Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable native staging apps to use Staging API #13029

Merged
merged 10 commits into from
Dec 5, 2022
5 changes: 4 additions & 1 deletion src/components/TestToolMenu.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import styles from '../styles/styles';
Expand All @@ -14,6 +15,8 @@ import TestToolRow from './TestToolRow';
import networkPropTypes from './networkPropTypes';
import compose from '../libs/compose';
import {withNetwork} from './OnyxProvider';
import getPlatform from '../libs/getPlatform';
import CONST from '../CONST';

const propTypes = {
/** User object in Onyx */
Expand Down Expand Up @@ -42,7 +45,7 @@ const TestToolMenu = props => (
This enables QA and internal testers to take advantage of sandbox environments for 3rd party services like Plaid and Onfido. */}
<TestToolRow title="Use Staging Server">
<Switch
isOn={lodashGet(props, 'user.shouldUseStagingServer', true)}
isOn={lodashGet(props, 'user.shouldUseStagingServer', _.contains([CONST.PLATFORM.WEB, CONST.PLATFORM.DESKTOP], getPlatform()))}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only default to true if we are in web or desktop which can read the correct config

onToggle={() => User.setShouldUseStagingServer(!lodashGet(props, 'user.shouldUseStagingServer', true))}
/>
</TestToolRow>
Expand Down
10 changes: 7 additions & 3 deletions src/libs/HttpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import CONFIG from '../CONFIG';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';
import HttpsError from './Errors/HttpsError';
import shouldUseStagingServer from './shouldUseStagingServer';
import getPlatform from './getPlatform';

let shouldUseStagingServer = false;
// Desktop and web use staging config too so we we should default to staging API endpoint if on those platforms
const shouldDefaultToStaging = _.contains([CONST.PLATFORM.WEB, CONST.PLATFORM.DESKTOP], getPlatform());
let stagingServerToggleState = false;
Onyx.connect({
key: ONYXKEYS.USER,
callback: val => shouldUseStagingServer = lodashGet(val, 'shouldUseStagingServer', true),
callback: val => stagingServerToggleState = lodashGet(val, 'shouldUseStagingServer', shouldDefaultToStaging),
});

let shouldFailAllRequests = false;
Expand Down Expand Up @@ -105,7 +109,7 @@ function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure =

let apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.URL_API_ROOT;

if (CONFIG.IS_IN_STAGING && shouldUseStagingServer) {
if (shouldUseStagingServer(stagingServerToggleState)) {
apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.STAGING_SECURE_EXPENSIFY_URL : CONFIG.EXPENSIFY.STAGING_EXPENSIFY_URL;
}

Expand Down
13 changes: 13 additions & 0 deletions src/libs/shouldUseStagingServer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import CONFIG from '../../CONFIG';

/**
* Helper method used to decide which API endpoint to call
*
* @param {Boolean} stagingServerToggleState
* @returns {Boolean}
*/
function shouldUseStagingServer(stagingServerToggleState) {
return CONFIG.IS_IN_STAGING && stagingServerToggleState;
}

export default shouldUseStagingServer;
13 changes: 13 additions & 0 deletions src/libs/shouldUseStagingServer/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Environment from '../Environment/Environment';

/**
* Helper method used to decide which API endpoint to call in the Native apps.
* We build the staging native apps with production env config so we cannot rely on those values,
* hence we will decide solely on the value of the shouldUseStagingServer value (always false in production).
*
* @param {Boolean} stagingServerToggleState
* @returns {Boolean}
*/
export default function shouldUseStagingServer(stagingServerToggleState) {
return !Environment.isDevelopment() && stagingServerToggleState;
}