LazyStack

PetStoreTests Project

In this step we will create a PetStoreTest project to perform an integration test against the PetStoreDev stack. We will test:

  • Application SignUp - using the LazyStackAuth library.
  • Application SignIn - using the LazyStackAuth library.
  • Calling the stack using the PetStore - defined in the PetStoreClientSDK library.

8.1 Configure a Gmail Account

The automated tests we will create in this step require an IMAP account with subaddressing capabilities. For example, Gmail, Outlook or AWS WorkMail will work just fine. AWS Cognito will email authorization codes to an email account during the tests. For instance, the sign-up process will email the new user a authorization code. Our sign-up process test must log into the user's email account, find the email containing the authorization code and then use that code to finalize the sign-up process.

AWS Cognito requires each user to have a unique email address. We don't want to create a new email address each time we run the tests and deleting users from the Cognito User Pool is not convenient or always what we what.

Lucky for us, email subaddressing is a nifty feature that allows you to use a dynamic email address alias. For example, if you email address is myemail@gmail.com, you can send email to that address with the alias myemail+01@gmail.com. As far as AWS Cognito is concerned, this alias is a unique email address. This means we can use a single Gmail account to create and test multiple AWS Cognito accounts where each of the AWS Cognito accounts is associated with a unique Gmail address alias.

For the purposes of this demo we will use the AWS WorkMail service. AWS WorkMail accounts currently cost $4/mo but you get a free 30 day trail. One nice feature of the AWS WorkMail service is that it offers a "free" subdomain so you don't have to configure any domains or spend a lot of time setting things up.

  1. Follow the instructions here. to create a Amazon WorkMail site. Choose to use a free testing domain provided by Amazon WorkMail and select an available "alias" to use. Test domains have the form: alias.awsapps.com. Add a single user called "test".

8.2 Create PetStoreTests Project

  1. Right Click on "Solution PetStore" in the Solution Explorer pane. The Context Menu appears.
  2. Select "Add → New Project" from the context menu. The "Add new project" dialog appears.
  3. Type "MSTEST Test Project" into the search field. The project template is listed.
  4. Double Click on the template list item. The "Configure your project" dialog appears.
  5. Edit project name to read "PetStoreTests".
  6. Click the Next button. The 'Additional Information' page appears.
  7. For 'TargetFrameWork' select '.NET 6 (Long-term support).
  8. Click the Create button. The test project is added to your solution.

8.3 Add Project Dependencies

  1. Open the PetStoreTests.csproj file.
  2. Copy the following ItemGroup into the .csproj file.
    
        <ItemGroup>
            <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
            <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
            <ProjectReference Include="..\PetStoreClientSDK\PetStoreClientSDK.csproj" />
            <EmbeddedResource Include="..\Stacks\Dev\AwsSettings.json" />
        </ItemGroup>
                    
  3. Save the file.

Note the inclusion of the "..\Stacks\Dev\AwsSettings.json" file as an embedded resource. This file contains the information necessary for a client app to connect to and use the AWS Stack. We generated this file earlier with using Tools → LazyStack - Generate Stacks\Dev\AwsSettings.json.

8.4 Configure Visual Studio Secrets

  1. Right Click on PetStoreTest project in solution explorer. The Context Menu appears.
  2. Select "Manage User Secrets". A secrets.json editor tab opens.
  3. Enter your email credentials in this format:
    {
        "EmailAccount": {
        "Domain":"imap.mai.us-east-1.awsapps.com",
        "Port":993,
        "UseSSL": true,
        "Email": "test@alias.awsapps.com",
        "Password": "mypassword"
        }
    }
  4. Edit the Domain's AWS Region, Port and UseSSL settings are per the AWS docs here.
  5. Edit the "alias" portion of the email address to the value you used when setting up the AWS WorkMail service.
  6. Edit the "password" to the value used for the test email account.
  7. Save your edits.

Note: For our testing process, we don't need to "send" email from this AWS WorkMail "test" account so we skip configuring an outgoing SMTP server for this AWS WorkMail service. If you are using WorkMail for more than this simple testing scenario, you should setup an outgoing SMTP server as well.

You can use any imap compatible mail service that supports subaddressing. I like using AWS WorkMail because it is simple to setup and inexpensive to use as a dedicated testing component.

8.5 Implement Tests

  1. Delete the PetStoreTests UnitTest1.cs file.
  2. Download PetStoreIntegrationTest.cs and then move it into the PetStoreTests project.

8.6 Run Tests

  1. Select View → Test Explorer. The Test Explorer panel appears.
  2. Run Tests. The integration test completes successfully.

8.7 Step Summary

In this step we created a Integration test project and ran our first tests against the published stack.

Reviewing the contents of the PetStoreIntegrationTest.cs class may yield valuable insights into how you should use the libraries in your client apps. Take note of the following implementation elements:

  • AwsSettings.json is an embedded resource that is loaded by ConfigurationBuilder. This embedded resource references \Stacks\Dev\AwsSettings.json. In a client app, you would introduce condition logic in the csproj file to use \Stacks\Prod\AwsSettings.json for release builds or just distribute the AwsSettings.json file separately to developers using the PetStoreClientSDK.
  • AuthMessages.json is a embedded resource that is loaded by ConfigurationBuilder. Extend this file with new message sets by language to localize the messages emitted by the AuthProcess class. This test does not demonstrate the use of AuthProcess messages but later Tutorial steps will.
  • Dependency injection is used in this test to demonstrate the service registration of IAuthProcess and IAuthProvider.
  • LazyStackAuth is an important library in this project.
    • This test only exercises the simple Happy Path SignUp and SignIn process. If you want to see much more extensive integration tests for AuthProcess, view the LazyStack repo on GitHub.
    • The LzHttpClient class implements the logic necessary for your client to make secure calls to the AWS Stack ApiGateways. The LzHttpClient class uses the Aws, LoadApis and MethodMap sections in the appConfig.