Files
TinfoilVibeServer/TinfoilVibeServerTest/Tests/AuthStoreTests.cs
T
ecenshu a1ea34bc01
Build & Push Docker image / build-and-push (push) Has been cancelled
ci / build_linux (push) Has been cancelled
feature/ci (#1)
Consolidate data and config into separate folders that will be expected to be mapped in the container

Reviewed-on: #1
Co-authored-by: Huy Nguyen <ecenshu@gmail.com>
Co-committed-by: Huy Nguyen <ecenshu@gmail.com>
2025-11-13 09:11:21 +00:00

113 lines
3.7 KiB
C#

using Microsoft.Extensions.Hosting.Internal;
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>();
var env = new Mock<HostingEnvironment>();
_authStore = new AuthStore(_loggerMock.Object, MockConfigManager.Object, env.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 = "";
// 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);
}
}
}