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 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. CD into the solution folder (LazyStackTutorial\PetStore) and use dotnet to add the new project to the solution.
    dotnet new mstest -o PetStoreTests
    Windows:
    dotnet sln add PetStoreTests\PetStoreTests.csproj
    MacOS/Linux:
    dotnet sln add PetStoreTests/PetStoreTests.csproj
    The new PetStoreTests project is added to your solution.

8.3 Generate User-Secrets GUID

  • CD to the solution folder (LazyStackTutorial\PetStore) and install the dotnet GUID tool if it is not already installed:
    dotnet tool install -g dotnet-guid
  • Use the guid tool to generate a new GUID:
    guid
    A guid is printed to the console. Make a note of this GUID as you will need it in the next step.

8.4 Configure PetStoreTests Project

  1. Copy this content into the PetStoreTests.csproj file, then replace the UserSecretisId value with the guid you generated in the previous step:
    <Project Sdk="Microsoft.NET.Sdk">
    
        <PropertyGroup>
            <TargetFramework>net6.0</TargetFramework>
            <Nullable>enable</Nullable>
            <IsPackable>false</IsPackable>    
            <UserSecretsId>66025cbf-84df-4bb8-b739-175a8d130e36</UserSecretsId>
        </PropertyGroup>
        
        <ItemGroup>
            <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
            <PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
            <PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
            <PackageReference Include="coverlet.collector" Version="3.1.2" />
        </ItemGroup>
        
        <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>
        
        </Project>
        
  2. Save the file.
  3. Build the project.
    dotnet build
  4. Set User Secret Values
    1. Open a Terminal Window
    2. CD into the LazyStackTutorial/PetStore folder.
    3. Set EmailAccount:Domain user secret value (edit the region if necessary):
      dotnet user-secrets -p PetStoreTests set "EmailAccount:Domain" "imap.mai.us-east-1.awsapps.com" 
    4. Set EmailAccount:Port user secret value:
      dotnet user-secrets -p PetStoreTests set "EmailAccount:Port" "993" 
    5. Set EmailAccount:UseSSL user secret value:
      dotnet user-secrets -p PetStoreTests set "EmailAccount:UseSSL" "true" 
    6. Set EmailAccount:Email user secret value (edit the alias value):
      dotnet user-secrets -p PetStoreTests set "EmailAccount:Email" "test@alias.awsapps.com" 
    7. Set EmailAccount:Password user secret value:
      dotnet user-secrets -p PetStoreTests set "EmailAccount:Password" "maypassword" 

8.5 Implement Tests

  1. Delete the PetStoreTests UnitTest1.cs file.
  2. Download PetStoreIntegrationTest.cs and then move it into the PetStoreTests project.
  3. CD into the solution folder (LazyStackTutorial\PetStore) and build the project.
    dotnet build

8.6 Run Tests

  1. CD into the solution folder (LazyStackTutorial\PetStore) and run the tests:
    dotnet test PetStoreTests
  2. The integration test completes successfully.

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 PetStoreentSDK.
  • 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.
  • 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 solution on GitHub. Also read the LazyStackAuth documentation for more details.
  • 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.