90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text.Json.Nodes;
|
|
using PolyTrader.Web.Configuration;
|
|
using Xunit;
|
|
|
|
namespace PolyTrader.Tests.WebUi
|
|
{
|
|
public class WebUiConfigManagerTests : IDisposable
|
|
{
|
|
private readonly string _tempFile;
|
|
|
|
public WebUiConfigManagerTests()
|
|
{
|
|
_tempFile = Path.Combine(Path.GetTempPath(), $"appsettings_test_{Guid.NewGuid():N}.json");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (File.Exists(_tempFile))
|
|
File.Delete(_tempFile);
|
|
}
|
|
|
|
[Fact]
|
|
public void GenerateSecureToken_Returns64CharHexAndIsRandom()
|
|
{
|
|
string t1 = WebUiConfigManager.GenerateSecureToken();
|
|
string t2 = WebUiConfigManager.GenerateSecureToken();
|
|
|
|
Assert.NotNull(t1);
|
|
Assert.Equal(64, t1.Length);
|
|
Assert.Matches("^[0-9a-f]{64}$", t1);
|
|
Assert.NotEqual(t1, t2);
|
|
}
|
|
|
|
[Fact]
|
|
public void SaveToLocalSettings_CreatesFileAndPreservesExistingSections()
|
|
{
|
|
// Vorhandene Sektion simulieren
|
|
File.WriteAllText(_tempFile, "{\"Database\":{\"Provider\":\"MySql\"}}");
|
|
|
|
var options = new WebUiOptions
|
|
{
|
|
Enabled = true,
|
|
Port = 6001,
|
|
LocalOnly = false,
|
|
AuthToken = "test-token-xyz"
|
|
};
|
|
|
|
WebUiConfigManager.SaveToLocalSettings(options, _tempFile);
|
|
|
|
string content = File.ReadAllText(_tempFile);
|
|
var node = JsonNode.Parse(content)?.AsObject();
|
|
|
|
Assert.NotNull(node);
|
|
Assert.Equal("MySql", node["Database"]?["Provider"]?.GetValue<string>());
|
|
|
|
var webNode = node["WebUi"]?.AsObject();
|
|
Assert.NotNull(webNode);
|
|
Assert.True(webNode["Enabled"]?.GetValue<bool>());
|
|
Assert.Equal(6001, webNode["Port"]?.GetValue<int>());
|
|
Assert.False(webNode["LocalOnly"]?.GetValue<bool>());
|
|
Assert.Equal("test-token-xyz", webNode["AuthToken"]?.GetValue<string>());
|
|
}
|
|
|
|
[Fact]
|
|
public void SaveToLocalSettings_WhenFileDoesNotExist_CreatesNewFile()
|
|
{
|
|
if (File.Exists(_tempFile)) File.Delete(_tempFile);
|
|
|
|
var options = new WebUiOptions
|
|
{
|
|
Enabled = true,
|
|
Port = 5050,
|
|
LocalOnly = true,
|
|
AuthToken = "brand-new-token"
|
|
};
|
|
|
|
WebUiConfigManager.SaveToLocalSettings(options, _tempFile);
|
|
|
|
Assert.True(File.Exists(_tempFile));
|
|
string content = File.ReadAllText(_tempFile);
|
|
var node = JsonNode.Parse(content)?.AsObject();
|
|
Assert.NotNull(node);
|
|
Assert.Equal(5050, node["WebUi"]?["Port"]?.GetValue<int>());
|
|
Assert.Equal("brand-new-token", node["WebUi"]?["AuthToken"]?.GetValue<string>());
|
|
}
|
|
}
|
|
}
|