Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Cognito module #13

Merged
merged 3 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CDK.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sagittaras.CDK.Tests.CodeDe
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sagittaras.CDK.Framework.Cognito", "Sagittaras.CDK.Framework.Cognito\Sagittaras.CDK.Framework.Cognito.csproj", "{84CEB00D-5C47-4FC6-93A0-5EEA858351D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sagittaras.CDK.Testing.Cognito", "Sagittaras.CDK.Testing.Cognito\Sagittaras.CDK.Testing.Cognito.csproj", "{C327A6EB-BED1-46D1-9629-7898D609FC11}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sagittaras.CDK.Tests.Cognito", "Sagittaras.CDK.Tests.Cognito\Sagittaras.CDK.Tests.Cognito.csproj", "{65BF0B5F-2F99-4371-B038-AE27F937DF3F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -105,6 +109,8 @@ Global
{0155B525-1934-4085-9F7A-308802DE166C} = {3A01C380-5151-4039-A00C-A33687E00861}
{F4979529-5489-46DC-86DA-BEE653511C3A} = {F2C4D978-97A6-4451-AAD2-16EDCA158D97}
{84CEB00D-5C47-4FC6-93A0-5EEA858351D7} = {506BD5FC-1E9D-47D4-A609-F977EDF69184}
{C327A6EB-BED1-46D1-9629-7898D609FC11} = {3A01C380-5151-4039-A00C-A33687E00861}
{65BF0B5F-2F99-4371-B038-AE27F937DF3F} = {F2C4D978-97A6-4451-AAD2-16EDCA158D97}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{940EEEA3-A068-4793-A44A-D2FCE1143E49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -203,5 +209,13 @@ Global
{84CEB00D-5C47-4FC6-93A0-5EEA858351D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84CEB00D-5C47-4FC6-93A0-5EEA858351D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84CEB00D-5C47-4FC6-93A0-5EEA858351D7}.Release|Any CPU.Build.0 = Release|Any CPU
{C327A6EB-BED1-46D1-9629-7898D609FC11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C327A6EB-BED1-46D1-9629-7898D609FC11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C327A6EB-BED1-46D1-9629-7898D609FC11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C327A6EB-BED1-46D1-9629-7898D609FC11}.Release|Any CPU.Build.0 = Release|Any CPU
{65BF0B5F-2F99-4371-B038-AE27F937DF3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{65BF0B5F-2F99-4371-B038-AE27F937DF3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65BF0B5F-2F99-4371-B038-AE27F937DF3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65BF0B5F-2F99-4371-B038-AE27F937DF3F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
16 changes: 16 additions & 0 deletions Sagittaras.CDK.Framework/Extensions/EnumExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ public static string GetCdkValue(this Enum value)
CdkValueAttribute? attribute = field.GetCustomAttribute<CdkValueAttribute>();
return attribute?.Value ?? value.ToString();
}

/// <summary>
/// Gets the translated CDK values from Enum which is marked as flags.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string[] GetCdkFlagValues(this Enum value)
{
Type type = value.GetType();

return (
from Enum flag in Enum.GetValues(type)
where value.HasFlag(flag) && !flag.Equals(default(Enum))
select GetCdkValue(flag)
).ToArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0-preview-01</Version>
<Title>CDK Cognito testing library</Title>
<Authors>Sagittaras Games</Authors>
<PackageProjectUrl>https://github.com/sagittaras/cdk.framework</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/sagittaras/cdk.framework/blob/main/LICENSE</PackageLicenseUrl>
<RepositoryUrl>https://github.com/sagittaras/cdk.framework</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Sagittaras.CDK.Testing\Sagittaras.CDK.Testing.csproj"/>
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Sagittaras.CDK.Testing.Cognito/UserPool/AliasAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Sagittaras.CDK.Framework.Enums;

namespace Sagittaras.CDK.Testing.Cognito.UserPool;

[Flags]
public enum AliasAttributes
{
[CdkValue("email")]
Email,

[CdkValue("phone_number")]
PhoneNumber,

[CdkValue("preferred_username")]
PreferredUsername
}
13 changes: 13 additions & 0 deletions Sagittaras.CDK.Testing.Cognito/UserPool/AutoVerifiedAttributes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Sagittaras.CDK.Framework.Enums;

namespace Sagittaras.CDK.Testing.Cognito.UserPool;

[Flags]
public enum AutoVerifiedAttributes
{
[CdkValue("email")]
Email,

[CdkValue("phone_number")]
PhoneNumber
}
46 changes: 46 additions & 0 deletions Sagittaras.CDK.Testing.Cognito/UserPool/UserPoolAssertion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Sagittaras.CDK.Framework;
using Sagittaras.CDK.Testing.Resources;

namespace Sagittaras.CDK.Testing.Cognito.UserPool;

/// <summary>
/// Assertion for AWS::Cognito::UserPool.
/// </summary>
public class UserPoolAssertion : ResourceAssertion<UserPoolProperties>
{
/// <inheritdoc />
public override string Type => "AWS::Cognito::UserPool";

/// <summary>
/// Sets the expected user pool name.
/// </summary>
/// <param name="userPoolName"></param>
/// <returns></returns>
public UserPoolAssertion WithUserPoolName(string userPoolName)
{
SetProperty(x => x.UserPoolName = Cloudspace.ResourceName(userPoolName));
return this;
}

/// <summary>
/// Sets the expected flag of auto verified attribute.
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public UserPoolAssertion WithAutoVerifiedAttribute(AutoVerifiedAttributes attribute)
{
SetProperty(x => x.AutoVerifiedAttributes |= attribute);
return this;
}

/// <summary>
/// Sets expected flag of alias attribute.
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public UserPoolAssertion AllowsAlias(AliasAttributes attribute)
{
SetProperty(x => x.AliasAttributes |= attribute);
return this;
}
}
13 changes: 13 additions & 0 deletions Sagittaras.CDK.Testing.Cognito/UserPool/UserPoolProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Sagittaras.CDK.Testing.Resources;

namespace Sagittaras.CDK.Testing.Cognito.UserPool;

/// <summary>
/// Properties for AWS::Cognito::UserPool.
/// </summary>
public class UserPoolProperties : ResourceProperties
{
public string? UserPoolName { get; set; }
public AutoVerifiedAttributes? AutoVerifiedAttributes { get; set; }
public AliasAttributes? AliasAttributes { get; set; }
}
11 changes: 9 additions & 2 deletions Sagittaras.CDK.Testing/Resources/ResourceProperties.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections;
using System.Reflection;
using Sagittaras.CDK.Framework.Extensions;

Expand Down Expand Up @@ -36,7 +35,15 @@ public IDictionary<string, object> ToDictionary()
dict[property.Name] = propsCollection.Select(x => x.ToDictionary() as Dictionary<string, object>).ToArray();
break;
case Enum @enum:
dict[property.Name] = @enum.GetCdkValue();
if (Attribute.IsDefined(@enum.GetType(), typeof(FlagsAttribute)))
{
dict[property.Name] = @enum.GetCdkFlagValues();
}
else
{
dict[property.Name] = @enum.GetCdkValue();
}

break;
default:
dict[property.Name] = value;
Expand Down
2 changes: 1 addition & 1 deletion Sagittaras.CDK.Testing/Sagittaras.CDK.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>1.0.0-preview-01</Version>
<Version>1.0.0-preview-02</Version>
<Title>Core test library for AWS CDK</Title>
<Authors>Sagittaras Games</Authors>
<PackageProjectUrl>https://github.com/sagittaras/cdk.framework</PackageProjectUrl>
Expand Down
29 changes: 29 additions & 0 deletions Sagittaras.CDK.Tests.Cognito/Sagittaras.CDK.Tests.Cognito.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Sagittaras.CDK.Framework.Cognito\Sagittaras.CDK.Framework.Cognito.csproj"/>
<ProjectReference Include="..\Sagittaras.CDK.Testing.Cognito\Sagittaras.CDK.Testing.Cognito.csproj"/>
<ProjectReference Include="..\Sagittaras.CDK.Tests\Sagittaras.CDK.Tests.csproj"/>
</ItemGroup>

</Project>
59 changes: 59 additions & 0 deletions Sagittaras.CDK.Tests.Cognito/UserPoolTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Amazon.CDK.Assertions;
using Sagittaras.CDK.Framework.Cognito.UserPools;
using Sagittaras.CDK.Testing.Cognito.UserPool;
using Xunit;

namespace Sagittaras.CDK.Tests.Cognito;

public class UserPoolTest : ConstructTest
{
private const string PoolName = "TestPool";

/// <summary>
/// Tests most basic creation of the user pool.
/// </summary>
[Fact]
public void Test_BaseConstruct()
{
new UserPoolFactory(Stack, PoolName)
.Construct();

Template template = StackTemplate;

new UserPoolAssertion()
.WithUserPoolName(PoolName)
.Assert(template);
}

/// <summary>
/// Tests setting of automatically verified attributes.
/// </summary>
[Fact]
public void Test_AutoVerifiedAttribute()
{
const string singleVerify = "SingleVerify";
const string multipleVerify = "MultipleVerify";

new UserPoolFactory(Stack, singleVerify)
.AutomaticallyVerify(x => x.Email)
.Construct();

new UserPoolFactory(Stack, multipleVerify)
.AutomaticallyVerify(x => x.Email)
.AutomaticallyVerify(x => x.Phone)
.Construct();

Template template = StackTemplate;

new UserPoolAssertion()
.WithUserPoolName(singleVerify)
.WithAutoVerifiedAttribute(AutoVerifiedAttributes.Email)
.Assert(template);

new UserPoolAssertion()
.WithUserPoolName(multipleVerify)
.WithAutoVerifiedAttribute(AutoVerifiedAttributes.Email)
.WithAutoVerifiedAttribute(AutoVerifiedAttributes.PhoneNumber)
.Assert(template);
}
}
Loading