Working on BackgroundService for Seq Logging

This commit is contained in:
Janus C. H. Knudsen 2025-02-13 00:40:42 +01:00
parent 48578b216f
commit 7bcb7b0e66
9 changed files with 240 additions and 53 deletions

View file

@ -63,33 +63,6 @@ namespace Tests.ConfigurationTests
}
[TestMethod]
public void GetPostgresSearchPath()
{
//ALTER USER sathumper SET search_path TO ptmain, public;
var conn = Container.Resolve<IDbConnection>();
var result = conn.QuerySql("SHOW search_path;");
using (var connw = new NpgsqlConnection(conn.ConnectionString + ";Password=3911"))
{
connw.Open();
using (var cmd = new NpgsqlCommand("SHOW search_path; SELECT current_user;", connw))
{
using (var reader = cmd.ExecuteReader())
{
reader.Read();
var r1 = $"Search path: {reader.GetString(0)}";
reader.NextResult();
reader.Read();
var r2 = $"Current schema: {reader.GetString(0)}";
}
}
}
}
[TestMethod]
public void TryGetActiveConfigurations()
{

View file

@ -0,0 +1,74 @@
using Autofac;
using System.Data;
using Insight.Database;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Logging;
using Core.Telemetry;
using Microsoft.ApplicationInsights;
namespace Tests
{
[TestClass]
public class MessageChannelIntegrationTests : TestFixture
{
private IMessageChannel _messageChannel;
private SeqBackgroundService _service;
private CancellationTokenSource _cts;
[TestInitialize]
public void SetupThis()
{
_messageChannel = new MessageChannel();
var telemetryClient = Container.Resolve<TelemetryClient>();
var httpClient = new HttpClient(new TestMessageHandler());
_service = new SeqBackgroundService(telemetryClient, _messageChannel, httpClient);
_cts = new CancellationTokenSource();
}
[TestMethod]
public async Task Messages_ShouldBeProcessedFromQueue()
{
// Arrange
var processedMessages = new List<HttpRequestMessage>();
// Start service
var serviceTask = _service.StartAsync(_cts.Token);
// Act
// Send nogle beskeder til køen
for (int i = 0; i < 5; i++)
{
var message = new HttpRequestMessage(HttpMethod.Post, $"http://test.com/{i}");
await _messageChannel.Writer.WriteAsync(message);
}
// Vent lidt for at sikre processing
await Task.Delay(5000);
// Stop servicen
_cts.Cancel();
await _service.StopAsync(CancellationToken.None);
// Assert
// Check at køen er tom
bool hasMoreMessages = await _messageChannel.Reader.WaitToReadAsync();
Assert.IsFalse(hasMoreMessages, "Køen burde være tom");
}
private class TestMessageHandler : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
}
}
[TestCleanup]
public void Cleanup()
{
_cts?.Dispose();
}
}
}

View file

@ -0,0 +1,72 @@
using Autofac;
using System.Data;
using Insight.Database;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Logging;
using Core.Telemetry;
using Core.Entities.Users;
namespace Tests
{
[TestClass]
public class PasswordHasherTests : TestFixture
{
[TestMethod]
public void HashPassword_ShouldCreateValidHashFormat()
{
// Arrange
string password = "TestPassword123";
// Act
string hashedPassword = PasswordHasher.HashPassword(password);
string[] parts = hashedPassword.Split('.');
// Assert
Assert.AreEqual(3, parts.Length);
Assert.AreEqual("100000", parts[0]);
}
[TestMethod]
public void VerifyPassword_WithCorrectPassword_ShouldReturnTrue()
{
// Arrange
string password = "TestPassword123";
string hashedPassword = PasswordHasher.HashPassword(password);
// Act
bool result = PasswordHasher.VerifyPassword(hashedPassword, password);
// Assert
Assert.IsTrue(result);
}
[TestMethod]
public void VerifyPassword_WithWrongPassword_ShouldReturnFalse()
{
// Arrange
string correctPassword = "TestPassword123";
string wrongPassword = "WrongPassword123";
string hashedPassword = PasswordHasher.HashPassword(correctPassword);
// Act
bool result = PasswordHasher.VerifyPassword(hashedPassword, wrongPassword);
// Assert
Assert.IsFalse(result);
}
[TestMethod]
public void VerifyPassword_WithInvalidHashFormat_ShouldReturnFalse()
{
// Arrange
string password = "TestPassword123";
string invalidHash = "InvalidHash";
// Act
bool result = PasswordHasher.VerifyPassword(invalidHash, password);
// Assert
Assert.IsFalse(result);
}
}
}