Add UnitTests and made code testable with DI
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using TinfoilVibeServer.Authentication;
|
||||
using TinfoilVibeServer.Services;
|
||||
|
||||
// <-- adjust namespace
|
||||
|
||||
namespace TinfoilVibeServerTest.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class AuthStoreTests
|
||||
{
|
||||
private Mock<ILogger<AuthStore>> _loggerMock;
|
||||
private AuthStore _authStore;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_loggerMock = new Mock<ILogger<AuthStore>>();
|
||||
// Assume Settings is static and can be patched for tests
|
||||
MockConfigManager = new Mock<ConfigManager>();
|
||||
_authStore = new AuthStore(_loggerMock.Object, MockConfigManager.Object);
|
||||
}
|
||||
|
||||
public Mock<ConfigManager> MockConfigManager { get; set; }
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_authStore.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LoadAll_ShouldPopulateCollections()
|
||||
{
|
||||
// Act
|
||||
var users = _authStore.Credentials.Count;
|
||||
var fprs = _authStore.Fingerprints.Count;
|
||||
|
||||
// Assert
|
||||
Assert.That(users, Is.GreaterThan(0), "At least one user must be loaded");
|
||||
Assert.That(fprs, Is.GreaterThanOrEqualTo(0));
|
||||
|
||||
_loggerMock.Verify(
|
||||
x => x.Log(
|
||||
LogLevel.Information,
|
||||
It.IsAny<EventId>(),
|
||||
It.Is<It.IsAnyType>((v, t) => v.ToString().Contains("Loaded")),
|
||||
null,
|
||||
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
|
||||
Times.AtLeastOnce);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TryValidate_NewUser_ShouldCreateAndVerify()
|
||||
{
|
||||
// Arrange
|
||||
var newUser = "newuser";
|
||||
var ip = "127.0.0.1";
|
||||
var password = "";
|
||||
var uid = null as int?;
|
||||
// Act
|
||||
var result = _authStore.TryValidate(newUser, password, uid, ip, out var cred);
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.False, "New user should be not be verified automatically");
|
||||
Assert.That(_authStore.Credentials[newUser], Is.Not.Null);
|
||||
Assert.That(_authStore.Credentials[newUser].Verified, Is.False);
|
||||
|
||||
// New user should now exist
|
||||
Assert.That(_authStore.Credentials.Any(u => u.Value.Username == newUser), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IncrementFailed_BeforeBlacklist_ShouldNotBlacklist()
|
||||
{
|
||||
// Arrange
|
||||
var ip = "203.0.113.5";
|
||||
var cred = new Credential("dummy", "hash", 1, false);
|
||||
_authStore.UnbanIp(ip); // ensure clean
|
||||
|
||||
// Act
|
||||
var counter = _authStore.IncrementFailed(cred.Username, ip);
|
||||
|
||||
// Assert
|
||||
Assert.That(counter, Is.EqualTo(1));
|
||||
Assert.That(_authStore.IsIPBlacklisted(ip), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IncrementFailed_ExceedingThreshold_ShouldBlacklist()
|
||||
{
|
||||
// Arrange
|
||||
var ip = "203.0.113.5";
|
||||
var cred = new Credential("dummy", "hash", MockConfigManager.Object.Settings.MaxFailedAttempts, false);
|
||||
int threshold = MockConfigManager.Object.Settings.MaxFailedAttempts;
|
||||
|
||||
// Simulate threshold failures
|
||||
for (int i = 0; i < threshold; i++)
|
||||
_authStore.IncrementFailed(cred.Username, ip);
|
||||
|
||||
// Act
|
||||
int final = _authStore.IncrementFailed(cred.Username, ip);
|
||||
|
||||
// Assert
|
||||
Assert.That(final, Is.EqualTo(threshold + 1));
|
||||
Assert.That(_authStore.IsIPBlacklisted(ip), Is.True);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user