Added (working) unit tests!

This commit is contained in:
mskor 2024-12-09 23:47:55 +00:00
parent b8afed8ac1
commit f14ac4953d
3 changed files with 115 additions and 115 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component> </component>
</project> </project>

View file

@ -1,35 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject> <IsTestProject>true</IsTestProject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageReference Include="xunit" Version="2.4.2" /> <PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0"> <PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BackEnd\BackEnd.csproj" /> <ProjectReference Include="..\BackEnd\BackEnd.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="common-passwords.txt"> <None Update="common-passwords.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,76 +1,76 @@
using back_end.Services; using back_end.Services;
namespace Backend.Tests; namespace Backend.Tests;
public class PasswordTests public class PasswordTests
{ {
private readonly PasswordService _passwordService; private readonly PasswordService _passwordService;
public PasswordTests() public PasswordTests()
{ {
_passwordService = new PasswordService(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"common-passwords.txt")); _passwordService = new PasswordService(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"common-passwords.txt"));
} }
[Theory] [Theory]
[InlineData("abcdefg",true)] [InlineData("abcdefg",true)]
[InlineData("abcdef",false)] [InlineData("abcdef",false)]
[InlineData("abcdefghijklmn",true)] [InlineData("abcdefghijklmn",true)]
[InlineData("abcdefghijklmno",false)] [InlineData("abcdefghijklmno",false)]
public void PasswordLengthValid(string password, bool valid) public void PasswordLengthValid(string password, bool valid)
{ {
var check = _passwordService.IsPasswordLengthValid(password); var check = _passwordService.IsPasswordLengthValid(password);
Assert.Equal(valid, check); Assert.Equal(valid, check);
} }
[Theory] [Theory]
[InlineData("password", false)] [InlineData("password", false)]
[InlineData("password1", false)] [InlineData("password1", false)]
[InlineData("password!", false)] [InlineData("password!", false)]
[InlineData("password1!", true)] [InlineData("password1!", true)]
public void PasswordContainsMinimumCharacters(string password, bool valid) public void PasswordContainsMinimumCharacters(string password, bool valid)
{ {
var check = _passwordService.IsPasswordContainingMinimumCharacters(password); var check = _passwordService.IsPasswordContainingMinimumCharacters(password);
Assert.Equal(valid, check); Assert.Equal(valid, check);
} }
[Theory] [Theory]
[InlineData("password",true)] [InlineData("password",true)]
[InlineData("password(",false)] [InlineData("password(",false)]
[InlineData("password\u00a9",false)] [InlineData("password\u00a9",false)]
public void PasswordContainsOnlyLegalCharacters(string password, bool valid) public void PasswordContainsOnlyLegalCharacters(string password, bool valid)
{ {
var check = _passwordService.IsPasswordContainingOnlyLegalCharacters(password); var check = _passwordService.IsPasswordContainingOnlyLegalCharacters(password);
Assert.Equal(valid, check); Assert.Equal(valid, check);
} }
// not sure why, however, 'password' on it's own is not considered a 'common' password // not sure why, however, 'password' on it's own is not considered a 'common' password
// As explained in the comments for the PasswordTree, for the '123!passwords' test, I feel like it should count // As explained in the comments for the PasswordTree, for the '123!passwords' test, I feel like it should count
// as a common password but doesnt in this implementation! // as a common password but doesnt in this implementation!
[Theory] [Theory]
[InlineData("123!passwords",false)] [InlineData("123!passwords",false)]
[InlineData("password123!",true)] [InlineData("password123!",true)]
[InlineData("123!haslo",false)] [InlineData("123!haslo",false)]
[InlineData("haslo123!",false)] [InlineData("haslo123!",false)]
[InlineData("passwords",true)] [InlineData("passwords",true)]
[InlineData("haslo",false)] [InlineData("haslo",false)]
public void PasswordIsCommon(string password, bool valid) public void PasswordIsCommon(string password, bool valid)
{ {
var check = _passwordService.IsPasswordCommon(password); var check = _passwordService.IsPasswordCommon(password);
Assert.Equal(valid, check); Assert.Equal(valid, check);
} }
[Theory] [Theory]
[InlineData("123!passwords",true)] [InlineData("123!passwords",true)]
[InlineData("password123!",true)] // this is a valid password, regardless of it being common [InlineData("password123!",true)] // this is a valid password, regardless of it being common
[InlineData("123!haslo",true)] [InlineData("123!haslo",true)]
[InlineData("haslo123!",true)] [InlineData("haslo123!",true)]
[InlineData("passwords",false)] [InlineData("passwords",false)]
[InlineData("haslo",false)] [InlineData("haslo",false)]
[InlineData("password\u00a9",false)] [InlineData("password\u00a9",false)]
public void PasswordIsValid(string password, bool valid) public void PasswordIsValid(string password, bool valid)
{ {
// The logic here is hard to read, the test checks whether I am passing in a VALID password // The logic here is hard to read, the test checks whether I am passing in a VALID password
var check = !(_passwordService.IsPasswordInvalid(password)); var check = !(_passwordService.IsPasswordInvalid(password));
Assert.Equal(valid, check); Assert.Equal(valid, check);
} }
} }