Skip to content

Commit

Permalink
ci: Onboard the repo to the testing infra
Browse files Browse the repository at this point in the history
  • Loading branch information
philasmar committed Oct 15, 2024
1 parent 089a339 commit e8cef41
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 47 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/aws-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: AWS CI

on:
workflow_dispatch:
pull_request:
branches:
- master
- dev
- 'feature/**'

permissions:
id-token: write

jobs:
run-ci:
runs-on: ubuntu-latest
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 #v4
with:
role-to-assume: ${{ secrets.CI_MAIN_TESTING_ACCOUNT_ROLE_ARN }}
role-duration-seconds: 7200
aws-region: us-west-2
- name: Invoke Load Balancer Lambda
id: lambda
shell: pwsh
run: |
aws lambda invoke response.json --function-name "${{ secrets.CI_TESTING_LOAD_BALANCER_LAMBDA_NAME }}" --cli-binary-format raw-in-base64-out --payload '{"Roles": "${{ secrets.CI_TEST_RUNNER_ACCOUNT_ROLES }}", "ProjectName": "${{ secrets.CI_TESTING_CODE_BUILD_PROJECT_NAME }}", "Branch": "${{ github.sha }}"}'
$roleArn=$(cat ./response.json)
"roleArn=$($roleArn -replace '"', '')" >> $env:GITHUB_OUTPUT
- name: Configure Test Runner Credentials
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 #v4
with:
role-to-assume: ${{ steps.lambda.outputs.roleArn }}
role-duration-seconds: 7200
aws-region: us-west-2
- name: Run Tests on AWS
id: codebuild
uses: aws-actions/aws-codebuild-run-build@v1
with:
project-name: ${{ secrets.CI_TESTING_CODE_BUILD_PROJECT_NAME }}
- name: CodeBuild Link
shell: pwsh
run: |
$buildId = "${{ steps.codebuild.outputs.aws-build-id }}"
echo $buildId
16 changes: 16 additions & 0 deletions buildtools/ci.buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 0.2

phases:
install:
runtime-versions:
dotnet: 8.x
build:
commands:
- dotnet test test/Amazon.Extensions.CognitoAuthentication.UnitTests/Amazon.Extensions.CognitoAuthentication.UnitTests.csproj -c Release --logger trx --results-directory ./testresults
- dotnet test test/Amazon.Extensions.CognitoAuthentication.IntegrationTests/Amazon.Extensions.CognitoAuthentication.IntegrationTests.csproj -c Release --logger trx --results-directory ./testresults
reports:
aws-ssm-data-protection-provider-for-aspnet-tests:
file-format: VisualStudioTrx
files:
- '**/*'
base-directory: './testresults'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,9 +12,12 @@
<PackageReference Include="AWSSDK.Core" Version="3.7.303.14" />
<PackageReference Include="AWSSDK.IdentityManagement" Version="3.7.301" />
<PackageReference Include="AWSSDK.S3" Version="3.7.307.15" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public AuthenticationConfirmUserTests() : base()

//Tests SRP authentication flow for web applications
[Fact]
public async void TestGenericSrpAuthentication()
public async Task TestGenericSrpAuthentication()
{
string password = "PassWord1!";

AuthFlowResponse context =
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = password
}).ConfigureAwait(false);
});

Assert.True(user.SessionTokens.IsValid());
}
Expand All @@ -78,24 +78,24 @@ public async Task TestDeleteUser()
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = "PassWord1!"
}).ConfigureAwait(false);
});

ListUsersRequest listUsersRequest = new ListUsersRequest()
{
Limit = 60,
UserPoolId = pool.PoolID
};
ListUsersResponse listUsersReponse = await provider.ListUsersAsync(listUsersRequest).ConfigureAwait(false);
ListUsersResponse listUsersReponse = await provider.ListUsersAsync(listUsersRequest);
foreach (UserType listUser in listUsersReponse.Users)
{
users.Add(listUser.Username);
}

Assert.Contains(userID, users);

await user.DeleteUserAsync().ConfigureAwait(false);
await user.DeleteUserAsync();

listUsersReponse = await provider.ListUsersAsync(listUsersRequest).ConfigureAwait(false);
listUsersReponse = await provider.ListUsersAsync(listUsersRequest);
users.Clear();
foreach(UserType listUser in listUsersReponse.Users)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ public async Task TestNewPasswordRequiredFlow()
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = password
}).ConfigureAwait(false);
});

Assert.Equal(context.ChallengeName, ChallengeNameType.NEW_PASSWORD_REQUIRED);

context = await user.RespondToNewPasswordRequiredAsync(new RespondToNewPasswordRequiredRequest()
{
SessionID = context.SessionID,
NewPassword = "NewPassword1!"
}).ConfigureAwait(false);
});

Assert.True(user.SessionTokens.IsValid());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ public async Task TestSignUpProcess()
{ CognitoConstants.UserAttrEmail, "[email protected]"}
};

await pool.SignUpAsync(userID, password, userAttributes, validationData).ConfigureAwait(false);
await pool.SignUpAsync(userID, password, userAttributes, validationData);

ListUsersRequest listUsersRequest = new ListUsersRequest()
{
Limit = 2,
UserPoolId = pool.PoolID
};
ListUsersResponse listUsersResponse = await provider.ListUsersAsync(listUsersRequest).ConfigureAwait(false);
ListUsersResponse listUsersResponse = await provider.ListUsersAsync(listUsersRequest);
bool containsUser55 = false;

foreach (UserType user in listUsersResponse.Users)
Expand All @@ -84,9 +84,9 @@ public async Task TestSignUpProcess()

// Tests that ConfirmSignUp reaches the proper failure point with incorrect confirmation code
[Fact]
public void TestConfirmSignUpFail()
public async Task TestConfirmSignUpFail()
{
Assert.ThrowsAsync<CodeMismatchException>(() => user.ConfirmSignUpAsync("fakeConfirmationCode", false));
await Assert.ThrowsAsync<CodeMismatchException>(() => user.ConfirmSignUpAsync("fakeConfirmationCode", false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

using Amazon;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class CognitoCredentialsTests : AuthenticationConfirmUserTests

[Fact]
//Tests GetCognitoAWSCredentials
public async void TestGetCognitoAWSCredentials()
public async Task TestGetCognitoAWSCredentials()
{
string password = "PassWord1!";
string poolRegion = user.UserPool.PoolID.Substring(0, user.UserPool.PoolID.IndexOf("_"));
Expand All @@ -54,7 +55,7 @@ public async void TestGetCognitoAWSCredentials()
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = password
}).ConfigureAwait(false);
});

//Create identity pool
identityClient = new AmazonCognitoIdentityClient(clientCredentials, clientRegion);
Expand All @@ -68,37 +69,37 @@ await identityClient.CreateIdentityPoolAsync(new CreateIdentityPoolRequest()
},
IdentityPoolName = "TestIdentityPool" + DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"),

}).ConfigureAwait(false);
});
identityPoolId = poolResponse.IdentityPoolId;

//Create role for identity pool
managementClient = new AmazonIdentityManagementServiceClient(clientCredentials, clientRegion);
CreateRoleResponse roleResponse = managementClient.CreateRoleAsync(new CreateRoleRequest()
CreateRoleResponse roleResponse = await managementClient.CreateRoleAsync(new CreateRoleRequest()
{
RoleName = "_TestRole_" + DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"),
AssumeRolePolicyDocument = "{\"Version\": \"2012-10-17\",\"Statement\": [{\"Effect" +
"\": \"Allow\",\"Principal\": {\"Federated\": \"cognito-identity.amazonaws.com\"}," +
"\"Action\": \"sts:AssumeRoleWithWebIdentity\",\"Condition\": {\"StringEquals\": {" +
"\"cognito-identity.amazonaws.com:aud\": [\"" + identityPoolId + "\"]}}}]}"
}).Result;
});
roleName = roleResponse.Role.RoleName;

//Create and attach policy for role
CreatePolicyResponse policyResponse = managementClient.CreatePolicyAsync(new CreatePolicyRequest()
CreatePolicyResponse policyResponse = await managementClient.CreatePolicyAsync(new CreatePolicyRequest()
{
PolicyDocument = "{\"Version\": \"2012-10-17\",\"Statement\": " +
"[{\"Effect\": \"Allow\",\"Action\": [\"mobileanalytics:PutEvents\",\"cog" +
"nito-sync:*\",\"cognito-identity:*\",\"s3:*\"],\"Resource\": [\"*\"]}]}",
PolicyName = "_Cognito_" + DateTime.UtcNow.ToString("yyyyMMdd_HHmmss"),
}).Result;
});
policyArn = policyResponse.Policy.Arn;

AttachRolePolicyRequest attachRequest = new AttachRolePolicyRequest()
{
PolicyArn = policyArn,
RoleName = roleName
};
AttachRolePolicyResponse attachRolePolicyResponse = managementClient.AttachRolePolicyAsync(attachRequest).Result;
AttachRolePolicyResponse attachRolePolicyResponse = await managementClient.AttachRolePolicyAsync(attachRequest);

//Set the role for the identity pool
await identityClient.SetIdentityPoolRolesAsync(new SetIdentityPoolRolesRequest()
Expand All @@ -109,7 +110,7 @@ await identityClient.SetIdentityPoolRolesAsync(new SetIdentityPoolRolesRequest()
{ "authenticated", roleResponse.Role.Arn },
{ "unauthenticated", roleResponse.Role.Arn }
},
}).ConfigureAwait(false);
});

//Create and test credentials
CognitoAWSCredentials credentials = user.GetCognitoAWSCredentials(identityPoolId, clientRegion);
Expand All @@ -122,17 +123,17 @@ await identityClient.SetIdentityPoolRolesAsync(new SetIdentityPoolRolesRequest()
{
try
{
bucketsResponse = await client.ListBucketsAsync(new ListBucketsRequest()).ConfigureAwait(false);
bucketsResponse = await client.ListBucketsAsync(new ListBucketsRequest());
break;
}
catch (Exception ex)
catch (Exception)
{
System.Threading.Thread.Sleep(5000);
Thread.Sleep(5000);
}
}

Assert.True(null != bucketsResponse, "Failed to list buckets after 5 tries");
Assert.Equal(bucketsResponse.HttpStatusCode, System.Net.HttpStatusCode.OK);
Assert.Equal(System.Net.HttpStatusCode.OK, bucketsResponse.HttpStatusCode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

using Amazon.Runtime;
Expand All @@ -35,23 +36,23 @@ public class MfaAuthenticationTests : BaseAuthenticationTestClass

//Tests MFA authentication flow
[Fact]
public async void TestMfaAuthenticationFlow()
public async Task TestMfaAuthenticationFlow()
{
string password = "PassWord1!";

AuthFlowResponse context =
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = password
}).ConfigureAwait(false);
});

Assert.Equal(context.ChallengeName, ChallengeNameType.SMS_MFA);

await Assert.ThrowsAsync<CodeMismatchException>(() => user.RespondToSmsMfaAuthAsync(new RespondToSmsMfaRequest()
{
MfaCode = "fakeMfaCode",
SessionID = context.SessionID
})).ConfigureAwait(false);
}));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class SessionTests : AuthenticationConfirmUserTests
{
// Tests the ChangePassword method in CognitoUser to fail due to no valid session
[Fact]
public async void TestFailedChangePassword()
public async Task TestFailedChangePassword()
{
await Assert.ThrowsAsync<NotAuthorizedException>(() => user.ChangePasswordAsync("PassWord1!", "PassWord2!")).ConfigureAwait(false);
await Assert.ThrowsAsync<NotAuthorizedException>(() => user.ChangePasswordAsync("PassWord1!", "PassWord2!"));
}

// Tests that a CognitoUser object has a valid session object after being authenticated
Expand All @@ -40,7 +40,7 @@ public async Task TestValidSession()
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = "PassWord1!"
}).ConfigureAwait(false);
});

Assert.True(user.SessionTokens.IsValid());
}
Expand All @@ -53,10 +53,10 @@ public async Task TestGetUserDetails()
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = "PassWord1!"
}).ConfigureAwait(false);
GetUserResponse userDetails = await user.GetUserDetailsAsync().ConfigureAwait(false);
});
GetUserResponse userDetails = await user.GetUserDetailsAsync();

Assert.True(userDetails.UserAttributes.Any(x => string.Equals(x.Name, CognitoConstants.UserAttrEmail, StringComparison.Ordinal)));
Assert.Contains(userDetails.UserAttributes, x => string.Equals(x.Name, CognitoConstants.UserAttrEmail, StringComparison.Ordinal));
Assert.Empty(userDetails.MFAOptions);
}

Expand All @@ -68,9 +68,9 @@ public async Task TestGlobalSignOut()
await user.StartWithSrpAuthAsync(new InitiateSrpAuthRequest()
{
Password = "PassWord1!"
}).ConfigureAwait(false);
});

await user.GlobalSignOutAsync().ConfigureAwait(false);
await user.GlobalSignOutAsync();

Assert.Null(user.SessionTokens);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -14,9 +14,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit e8cef41

Please sign in to comment.