vrcauthproxy/Tests/Helpers/RedisFixture.cs
MiscFrizzy 4936481ffc ### Commit Message Summary
**Enhancements to Redis Integration and Testing**

- **RedisService.cs**:
  - Implemented a Redis service for storing and retrieving authentication tokens.
  - Added methods for saving, retrieving, and removing tokens using Redis.
  - Introduced a constructor for dependency injection to facilitate testing.

- **RedisFixture.cs**:
  - Created a test fixture to ensure a Redis instance is available for integration tests.
  - Implemented a check to verify if Redis is running before executing tests.

- **ProgramRedisIntegrationTests.cs**:
  - Added integration tests to validate the login process and token storage in Redis.
  - Implemented tests to check the reuse of stored tokens and the correct handling of authentication.

- **RedisIntegrationTests.cs**:
  - Developed integration tests for saving, retrieving, and deleting authentication tokens in Redis.
  - Ensured that all tokens are correctly stored and can be retrieved as expected.

- **RedisServiceTests.cs**:
  - Created unit tests using an in-memory implementation of the Redis service for isolated testing.
  - Validated the functionality of saving, retrieving, and removing tokens without a real Redis connection.

### Notes
- All tests are designed to ensure the reliability of the Redis integration and the overall functionality of the VRCAuthProxy service.
- Integration tests are marked to skip execution unless a Redis instance is available.
2025-04-07 08:04:52 -04:00

78 lines
No EOL
2.4 KiB
C#

using System.Diagnostics;
using Xunit;
namespace VRCAuthProxy.Tests.Helpers
{
/// <summary>
/// Collection definition for Redis tests
/// </summary>
[CollectionDefinition("Redis")]
public class RedisCollection : ICollectionFixture<RedisFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
}
/// <summary>
/// Redis test fixture that ensures a Redis instance is available for tests
/// </summary>
public class RedisFixture : IDisposable
{
private Process? _redisProcess; // Kept for future implementation where we might start Redis
private bool _disposedValue;
public RedisFixture()
{
// For CI environments, this would start Redis using Docker
// For local development, we assume Redis is already running
// Check if Redis is available
if (!IsRedisRunning())
{
// In a real implementation, we could start Redis here if needed
Console.WriteLine("Warning: Redis is not running. Redis integration tests will be skipped.");
}
}
/// <summary>
/// Check if Redis is available on localhost:6379
/// </summary>
private bool IsRedisRunning()
{
try
{
using var client = new System.Net.Sockets.TcpClient();
var result = client.BeginConnect("localhost", 6379, null, null);
var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
client.EndConnect(result);
return success;
}
catch
{
return false;
}
}
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// Stop Redis if we started it
_redisProcess?.Kill();
_redisProcess?.Dispose();
}
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}