vrcauthproxy/Tests/Unit/ConfigTests.cs
MiscFrizzy 319f1071bf feat(ci): Add GitHub Actions workflows for test automation and status badges
Add comprehensive test automation setup with GitHub Actions:
- Create test.yml for running tests on main/develop branches
- Add pr-test.yml for PR validation with test results comments
- Add update-badges.yml for dynamic test status badge updates
- Configure code coverage reporting with Codecov integration

Documentation:
- Add BADGE_SETUP.md with instructions for configuring test status badges
- Add WORKFLOWS_GUIDE.md explaining CI/CD workflow setup
- Update README.md with build and test status badges

Test Framework:
- Configure test project to use .NET 9.0
- Set up test coverage reporting with coverlet
- Add integration tests with WireMock for API mocking
- Add unit tests for configuration and HTTP client components
- Document testing strategy in TestingStrategy.md

Build:
- Add Dockerfile.test for containerized testing
- Update .gitignore for test artifacts
- Configure test dependencies in VRCAuthProxy.Tests.csproj

This change enables automated testing on PRs and branches, with visual status indicators and detailed test results in PR comments.
2025-04-07 06:30:31 -04:00

125 lines
No EOL
4.2 KiB
C#

using System.Text.Json;
using FluentAssertions;
using Xunit;
namespace VRCAuthProxy.Tests.Unit
{
public class ConfigTests
{
[Fact]
public void Config_LoadsAccountsFromJsonFile()
{
// Arrange
var tempFile = Path.GetTempFileName();
var configData = new
{
accounts = new object[]
{
new { username = "user1", password = "pass1", totpSecret = "secret1" },
new { username = "user2", password = "pass2" }
}
};
File.WriteAllText(tempFile, JsonSerializer.Serialize(configData));
try
{
// Override the default config path for testing
string originalConfigPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
string backupPath = originalConfigPath + ".bak";
if (File.Exists(originalConfigPath))
{
File.Move(originalConfigPath, backupPath, true);
}
File.Copy(tempFile, originalConfigPath, true);
// Act
var config = Config.Load();
// Assert
config.Should().NotBeNull();
config.Accounts.Should().HaveCount(2);
config.Accounts[0].username.Should().Be("user1");
config.Accounts[0].password.Should().Be("pass1");
config.Accounts[0].totpSecret.Should().Be("secret1");
config.Accounts[1].username.Should().Be("user2");
config.Accounts[1].password.Should().Be("pass2");
config.Accounts[1].totpSecret.Should().BeNull();
// Restore original config
if (File.Exists(backupPath))
{
File.Move(backupPath, originalConfigPath, true);
}
}
finally
{
// Clean up the temp file
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
[Fact]
public void Config_Save_WritesConfigToFile()
{
// Arrange
var tempFile = Path.GetTempFileName();
var originalConfigPath = Path.Combine(AppContext.BaseDirectory, "appsettings.json");
var backupPath = originalConfigPath + ".bak";
if (File.Exists(originalConfigPath))
{
File.Move(originalConfigPath, backupPath, true);
}
File.Copy(tempFile, originalConfigPath, true);
try
{
var config = new Config
{
Accounts = new List<ConfigAccount>
{
new ConfigAccount { username = "test1", password = "pass1", totpSecret = "secret1" }
}
};
// Act
config.Save();
// Assert
File.Exists(originalConfigPath).Should().BeTrue();
var loadedConfig = JsonSerializer.Deserialize<iConfig>(File.ReadAllText(originalConfigPath));
if (loadedConfig != null)
{
loadedConfig.accounts.Should().HaveCount(1);
loadedConfig.accounts[0].username.Should().Be("test1");
loadedConfig.accounts[0].password.Should().Be("pass1");
loadedConfig.accounts[0].totpSecret.Should().Be("secret1");
}
// Restore original config
if (File.Exists(backupPath))
{
File.Move(backupPath, originalConfigPath, true);
}
}
finally
{
// Clean up the temp file
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
}
}
}
}