aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 243 forks source link

[Docs] Provide documentation for testing with Amplify #1256

Open Jordan-Nelson opened 2 years ago

Jordan-Nelson commented 2 years ago

Context

There have been a few issues opened asking questions about testing flutter apps with Amplify (unit and integration). The amplify flutter docs currently don't offer much guidance on testing.

Request

At a minimum:

Ideally:

Related issues

94Sip commented 1 month ago

did this documentation ever get created? If so, can you point me to it? I'd like to understand how to do integration testing when using the Amplify Authenticator component and other Amplify libraries needing Auth (storage and API, in particular).

Thank you.

Jordan-Nelson commented 1 month ago

@94Sip no, I don't believe there are any public docs on integration or end to end testing. Are you looking to write tests that make network calls to AWS services (a full end to end test), or are you looking to stub/mock some portion to test your application logic?

94Sip commented 1 month ago

I am looking to do end-to-end testing - I am using the Authenticator UI component, and all my API calls are AWS_IAM authorization. @Jordan-Nelson

Jordan-Nelson commented 1 month ago

Got it. Is there a particular challenge you are facing with the tests? One common problem with Auth is creating new users, as by default that requires sending a confirmation code either via phone or email.

94Sip commented 1 month ago

nothing in general, but I would like to see how it is done. Meaning, do I just load my app (just like all the integration test scenarios show), let Amplify configure() happen and then wait for pump and settle for the Authenticator to show the login form, and then proceed as usual? I guess just seeing some working code, or even pseudo code for this type of scenario would be helpful. Do I need to do anything so that the IAM authorization works as expected in an integration testing setup?

And thank you for helping with this @Jordan-Nelson !

94Sip commented 1 month ago

Update @Jordan-Nelson - I was able to get it to work! Its probably worth saying that I had to spend a good bit of time hunting in the Widget Explorer to figure out the widgets/types that I needed to interact with that are part of the Authenticator UI component. That was easily the hardest part.

After typing in my thoughts above, I figured why not just try it out, and sure enough, it worked.

Caveat: I did not create a integration test for creating new users.

Jordan-Nelson commented 1 month ago

Got it. Most of the widgets in the Authenticator have specific Keys, which we use for our own widget tests. These are not part of the public API and could change in the future, but could be helpful for testing.

94Sip commented 1 month ago

yes! I looked thru Widget explored to identify those keys - i didn't realize they were documented in source, as you linked.

Question @Jordan-Nelson - back to one of your previous questions: how do I integration test user signup where a confirmation code is sent by Cognito?

Jordan-Nelson commented 1 month ago

@94Sip there are a couple options you could use in a testing environment. You would not want to use any of these options in a production environment.

  1. Automatically confirm the user, removing the need for confirmation via code. See this post for an example of how to do that. This is probably the easiest option if you aren't too concerned about including the confirmation flow in your test.
  2. Use a Cognito lambda trigger to capture and store the confirmation code. Cognito offers triggers (lambdas) that can be set up to run on many different events. One of those events is a Custom Email Sender or a Customer SMS Sender. These lambda triggers will run each time Cognito would normally send a email or SMS. You can use this to intercept the code and store it somewhere (for example AppSync) and then your automated test can fetch it. This is how we test the full sign up process in our end to end tests in Amplify. Setting this up is fairly involved. I would only recommend this if you had a good reason to test this flow as part of your e2e tests. You can see how we use the AWS CDK to set up these triggers in our e2e tests here: https://github.com/aws-amplify/amplify-flutter/tree/main/infra-gen2/infra-common/src/auth-user-extensions. That directory has some other utils for creating and deleting users as well.
94Sip commented 1 month ago

Extremely helpful @Jordan-Nelson thank you!