Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add unittests for TotpGenerator logic (#70)
Leendert de Borst0df0b2c3ffd4
diff --git a/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj b/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csprojindex 14e1ed894..48194f5f5 100644--- a/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj+++ b/src/Tests/AliasVault.UnitTests/AliasVault.UnitTests.csproj@@ -55,6 +55,7 @@<ProjectReference Include="..\..\Utilities\Cryptography\Cryptography.csproj" /><ProjectReference Include="..\..\Utilities\CsvImportExport\CsvImportExport.csproj" /><ProjectReference Include="..\..\Utilities\FaviconExtractor\FaviconExtractor.csproj" />+ <ProjectReference Include="..\..\Utilities\TotpGenerator\TotpGenerator.csproj" /></ItemGroup></Project>diff --git a/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs b/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.csindex f62b49fb6..c2734e81d 100644--- a/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs+++ b/src/Tests/AliasVault.UnitTests/Utilities/FaviconExtractorTests.cs@@ -17,7 +17,7 @@ public class FaviconExtractorTests/// </summary>/// <returns>Task.</returns>[Test]- public async Task ExtractFaviconSpamOK()+ public async Task ExtractFaviconSpamOk(){var faviconBytes = await FaviconExtractor.FaviconExtractor.GetFaviconAsync("https://spamok.com");Assert.That(faviconBytes, Is.Not.Null);diff --git a/src/Tests/AliasVault.UnitTests/Utilities/TotpGeneratorTests.cs b/src/Tests/AliasVault.UnitTests/Utilities/TotpGeneratorTests.csnew file mode 100644index 000000000..b276f3520--- /dev/null+++ b/src/Tests/AliasVault.UnitTests/Utilities/TotpGeneratorTests.cs@@ -0,0 +1,82 @@+//-----------------------------------------------------------------------+// <copyright file="TotpGeneratorTests.cs" company="lanedirt">+// Copyright (c) lanedirt. All rights reserved.+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.+// </copyright>+//-----------------------------------------------------------------------++namespace AliasVault.Tests.Utilities;++/// <summary>+/// Tests for the TotpGeneratorTests class.+/// </summary>+public class TotpGeneratorTests+{+ private const string TestSecretKey = "JBSWY3DPEHPK3PXP";++ /// <summary>+ /// Tests if the GenerateTotpCode method returns a valid code.+ /// </summary>+ [Test]+ public void GenerateTotpCode_ReturnsValidCode()+ {+ string code = TotpGenerator.TotpGenerator.GenerateTotpCode(TestSecretKey);+ Assert.That(code, Has.Length.EqualTo(6));+ Assert.That(code, Does.Match(@"^\d{6}$"));+ }++ /// <summary>+ /// Tests if the GenerateTotpCode method returns a code with the correct length.+ /// </summary>+ [Test]+ public void GenerateTotpCode_WithCustomDigits_ReturnsCodeWithCorrectLength()+ {+ string code = TotpGenerator.TotpGenerator.GenerateTotpCode(TestSecretKey, digits: 8);+ Assert.That(code, Has.Length.EqualTo(8));+ Assert.That(code, Does.Match(@"^\d{8}$"));+ }++ /// <summary>+ /// Tests if the GenerateTotpCode method returns a code when the secret key contains spaces and hyphens.+ /// </summary>+ [Test]+ public void GenerateTotpCode_WithSpacesAndHyphens_ReturnsValidCode()+ {+ string secretWithSpacesAndHyphens = "JBSW Y3DP-EHPK 3PXP";+ string code = TotpGenerator.TotpGenerator.GenerateTotpCode(secretWithSpacesAndHyphens);+ Assert.That(code, Has.Length.EqualTo(6));+ Assert.That(code, Does.Match(@"^\d{6}$"));+ }++ /// <summary>+ /// Tests if the VerifyTotpCode method returns true for a valid code.+ /// </summary>+ [Test]+ public void VerifyTotpCode_WithValidCode_ReturnsTrue()+ {+ string code = TotpGenerator.TotpGenerator.GenerateTotpCode(TestSecretKey);+ bool isValid = TotpGenerator.TotpGenerator.VerifyTotpCode(TestSecretKey, code);+ Assert.That(isValid, Is.True);+ }++ /// <summary>+ /// Tests if the VerifyTotpCode method returns false for an invalid code.+ /// </summary>+ [Test]+ public void VerifyTotpCode_WithInvalidCode_ReturnsFalse()+ {+ string invalidCode = "000000";+ bool isValid = TotpGenerator.TotpGenerator.VerifyTotpCode(TestSecretKey, invalidCode);+ Assert.That(isValid, Is.False);+ }++ /// <summary>+ /// Tests if the VerifyTotpCode method throws an exception for an invalid secret key.+ /// </summary>+ [Test]+ public void GenerateTotpCode_WithInvalidSecretKey_ThrowsException()+ {+ string invalidSecret = "INVALID!@#";+ Assert.Throws<ArgumentException>(() => TotpGenerator.TotpGenerator.GenerateTotpCode(invalidSecret));+ }+}diff --git a/src/Utilities/TotpGenerator/TotpGenerator.cs b/src/Utilities/TotpGenerator/TotpGenerator.csindex 7d74357c6..ec11b7d75 100644--- a/src/Utilities/TotpGenerator/TotpGenerator.cs+++ b/src/Utilities/TotpGenerator/TotpGenerator.cs@@ -14,7 +14,7 @@ using OtpNet;/// </summary>public class TotpGenerator{- /// <summary>+ /// <summary>/// Generates a Time-based One-Time Password (TOTP) for the given secret key./// </summary>/// <param name="secretKey">The secret key in Base32 encoding.</param>diff --git a/src/Utilities/TotpGenerator/TotpGenerator.csproj b/src/Utilities/TotpGenerator/TotpGenerator.csprojindex ea4f23bc5..ecc045c64 100644--- a/src/Utilities/TotpGenerator/TotpGenerator.csproj+++ b/src/Utilities/TotpGenerator/TotpGenerator.csproj@@ -8,10 +8,12 @@<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "><TreatWarningsAsErrors>true</TreatWarningsAsErrors>+ <DocumentationFile>bin\Debug\net8.0\TotpGenerator.xml</DocumentationFile></PropertyGroup><PropertyGroup Condition=" '$(Configuration)' == 'Release' "><TreatWarningsAsErrors>true</TreatWarningsAsErrors>+ <DocumentationFile>bin\Release\net8.0\TotpGenerator.xml</DocumentationFile></PropertyGroup><ItemGroup>