Skip to content

Add test tools to make it easier to test Network/reauthentication logic - #8713

Merged
marcochavezf merged 4 commits into
mainfrom
marcaaron-reauthenticationTools
Apr 25, 2022
Merged

marcochavezf merged 4 commits into
mainfrom
marcaaron-reauthenticationTools

Conversation

@marcaaron

@marcaaron marcaaron commented Apr 21, 2022 •

Copy link
Copy Markdown
Contributor

cc @marcochavezf

Details

Test Tools

A few testing tools are being added here so we can test conditions

  • Invalid credentials
  • Invalid authToken
  • All requests are failing when run (e.g. auth down scenario)

Fixed Issues

https://github.com/Expensify/Expensify/issues/207534

Tests

  1. Build the app in staging mode

Note: for native platforms this diff needed to be applied to get "staging"

diff --git a/src/libs/Environment/getEnvironment/index.native.js b/src/libs/Environment/getEnvironment/index.native.js
index 89547e0ee..1cfd0e0dd 100644
--- a/src/libs/Environment/getEnvironment/index.native.js
+++ b/src/libs/Environment/getEnvironment/index.native.js
@@ -13,8 +13,8 @@ let environment = null;
 function getEnvironment() {
     return new Promise((resolve) => {
         // If we've already set the environment, use the current value
-        if (environment) {
-            return resolve(environment);
+        if (true) {
+            return resolve(CONST.ENVIRONMENT.STAGING);
         }

         if (lodashGet(Config, 'ENVIRONMENT', CONST.ENVIRONMENT.DEV) === CONST.ENVIRONMENT.DEV) {
  1. Navigate to Preferences and verify there are new testing preferences available
  2. Toggle on "Simulate failing network requests"
    2022-04-21_11-41-56
  3. Proceed to do something in the app that triggers a network request (navigate to chat)
  4. Verify requests are shown as retrying in the logs and eventually fail
    2022-04-21_11-51-15
  5. Go back to preferences and untoggle this option
  6. Invalidate the authToken via the button that says "Invalidate"
    2022-04-21_11-41-56
  7. Do something in the app that triggers a network request
  8. Verify that a call to Authenticate appears in the Chrome Dev Tools network tab and you are reauthenticated
    2022-04-21_11-53-45
  9. Go back to preferences and invalidate the authToken and destroy the device credentials
    2022-04-21_11-41-56
  10. Do something in the app that triggers a network request
  11. Verify you are logged out
  • Verify that no errors appear in the JS console

PR Review Checklist

PR Reviewer Checklist

  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • iOS / native
    • Android / native
    • iOS / Safari
    • Android / Chrome
    • MacOS / Chrome
    • MacOS / Desktop
  • I verified there are no console errors (if there’s a console error not related to the PR, report it or open an issue for it to be fixed)
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained “why” the code was doing something instead of only explaining “what” the code was doing.
    • I verified any copy / text shown in the product was added in all src/languages/* files
    • I verified any copy / text that was added to the app is correct English and approved by marketing by tagging the marketing team on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named “index.js”. All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • Any functional components have the displayName property
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn’t already exist
    • The style can’t be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.

QA Steps

  1. Navigate to Preferences and verify there are new testing preferences available
  2. Toggle on "Simulate failing network requests"
  3. Proceed to do something in the app that will trigger a network request
  4. Verify requests are shown as retrying in the logs and eventually fail
  5. Go back to preferences and untoggle the "Simulate failing network requests" option
  6. Invalidate the authToken via the button that says "Invalidate"
  7. Do something in the app that triggers a network request
  8. Verify that a call to Authenticate appears in the Chrome Dev Tools network tab and you are reauthenticated (Web Only)
  9. Go back to preferences and invalidate the authToken and press the "Destroy" button to corrupt the device credentials
  10. Do something in the app that triggers a network request
  11. Verify you are logged out
  • Verify that no errors appear in the JS console

Screenshots

Web

2022-04-21_11-41-56

Mobile Web

2022-04-21_12-08-20

Desktop

2022-04-21_12-15-40

iOS

2022-04-21_12-05-10

Android

2022-04-21_12-14-46

@marcaaron marcaaron self-assigned this Apr 21, 2022
@marcaaron marcaaron changed the title Add test tools. Preserve order of SequentialQueue requests when reauthenticating. Add test tools to make it easier to test reauthentication logic Apr 21, 2022
@marcaaron marcaaron changed the title Add test tools to make it easier to test reauthentication logic Add test tools to make it easier to test Network/reauthentication logic Apr 21, 2022
…etries.

Handle reauthentication in Network.onResponse()

undo changes

comments

Improve reauthentication logic. Make it work with sequential queue.

revert network changes

Rename TestToolMenu

rename
@marcaaron
marcaaron force-pushed the marcaaron-reauthenticationTools branch from df0c003 to d999192 Compare April 21, 2022 21:22
Comment thread src/libs/Log.js
function serverLoggingCallback(logger, params) {
const requestParams = params;
requestParams.shouldProcessImmediately = false;
requestParams.shouldRetry = false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One weird side effect of trigger the option where all the requests should fail is that some iteration of a Log command is getting retried forever. I suspect it's something like:

  1. Log fails
  2. We run out of retries for a Log command
  3. We log that we ran out of retries and log again
  4. etc.

But not 100% sure and need to debug some more. I think Log is a "persistable" request (but isn't today) so once we have a backoff logic in place we can retry it forever - but at some rate of backoff. For now, we would be making many requests over and over again if we had a situation like "Auth down" so I am making Log not retryable.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah, I think we need more debugging to find out that

@marcaaron

Copy link
Copy Markdown
Contributor Author

Still testing on some platforms but taking out of draft.

@marcaaron
marcaaron marked this pull request as ready for review April 21, 2022 21:48
@marcaaron
marcaaron requested a review from a team as a code owner April 21, 2022 21:48
@melvin-bot
melvin-bot Bot requested review from Justicea83 and removed request for a team April 21, 2022 21:49

@Justicea83 Justicea83 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good overall.

Comment thread src/components/TestToolMenu.js Resolved
Comment thread src/components/TestToolMenu.js Resolved
Comment thread src/libs/HttpUtils.js Resolved
Comment thread src/libs/Log.js
function serverLoggingCallback(logger, params) {
const requestParams = params;
requestParams.shouldProcessImmediately = false;
requestParams.shouldRetry = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yeah, I think we need more debugging to find out that

Justicea83
Justicea83 previously approved these changes Apr 22, 2022

@Justicea83 Justicea83 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTM!

@Justicea83

Copy link
Copy Markdown

all you @marcaaron

Comment thread src/components/TestToolMenu.js Outdated
This enables QA and internal testers to take advantage of sandbox environments for 3rd party services like Plaid and Onfido. */}
<TestToolRow title="Use Secure Staging Server">
<Switch
isOn={props.user.shouldUseSecureStaging}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When I tested locally, I got this console warning:

Screen Shot 2022-04-22 at 16 34 55

I think we can fix that by adding the default values to Onyx.init, no?

App/src/setup/index.js

Lines 26 to 36 in 5b8b75c

initialKeyStates: {
// Clear any loading and error messages so they do not appear on app startup
[ONYXKEYS.SESSION]: {loading: false, shouldShowComposeInput: true},
[ONYXKEYS.ACCOUNT]: CONST.DEFAULT_ACCOUNT_DATA,
[ONYXKEYS.NETWORK]: {isOffline: false},
[ONYXKEYS.IOU]: {
loading: false, error: false, creatingIOUTransaction: false, isRetrievingCurrency: false,
},
[ONYXKEYS.IS_SIDEBAR_LOADED]: false,
},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh hmm I guess if there is a user object then it won't use the defaults. Sorry @Justicea83 I am going to put the short circuit back in here. @marcochavezf not sure if this is something for initialKeyStates- the code was fine before so I'm going to revert that last change.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sure that's fine, we look good now!

@Justicea83 Justicea83 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All you @marcaaron

@marcochavezf marcochavezf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, this test tool is cool!

@marcochavezf
marcochavezf merged commit be7fd88 into main Apr 25, 2022
@marcochavezf
marcochavezf deleted the marcaaron-reauthenticationTools branch April 25, 2022 17:23
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by @marcochavezf in version: 1.1.57-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by @chiragsalian in version: 1.1.57-17 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants