**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.
59 lines
No EOL
1.8 KiB
C#
59 lines
No EOL
1.8 KiB
C#
using System.Text.Json;
|
|
using StackExchange.Redis;
|
|
|
|
namespace VRCAuthProxy.Services;
|
|
|
|
public class RedisService
|
|
{
|
|
private readonly IConnectionMultiplexer _redis;
|
|
private readonly IDatabase _db;
|
|
private const string AUTH_TOKEN_KEY = "vrcauthproxy:tokens";
|
|
|
|
public RedisService(string connectionString)
|
|
{
|
|
_redis = ConnectionMultiplexer.Connect(connectionString);
|
|
_db = _redis.GetDatabase();
|
|
}
|
|
|
|
// Constructor for testing with mocked dependencies
|
|
internal RedisService(IConnectionMultiplexer redis, IDatabase database)
|
|
{
|
|
_redis = redis;
|
|
_db = database;
|
|
}
|
|
|
|
public virtual async Task SaveAuthToken(string username, Dictionary<string, string> cookies)
|
|
{
|
|
var serializedCookies = JsonSerializer.Serialize(cookies);
|
|
await _db.HashSetAsync(AUTH_TOKEN_KEY, username, serializedCookies);
|
|
}
|
|
|
|
public virtual async Task<Dictionary<string, string>?> GetAuthToken(string username)
|
|
{
|
|
var value = await _db.HashGetAsync(AUTH_TOKEN_KEY, username);
|
|
if (!value.HasValue) return null;
|
|
return JsonSerializer.Deserialize<Dictionary<string, string>>(value!);
|
|
}
|
|
|
|
public virtual async Task<Dictionary<string, Dictionary<string, string>>> GetAllAuthTokens()
|
|
{
|
|
var entries = await _db.HashGetAllAsync(AUTH_TOKEN_KEY);
|
|
var result = new Dictionary<string, Dictionary<string, string>>();
|
|
|
|
foreach (var entry in entries)
|
|
{
|
|
var cookies = JsonSerializer.Deserialize<Dictionary<string, string>>(entry.Value!);
|
|
if (cookies != null)
|
|
{
|
|
result[entry.Name!] = cookies;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public virtual async Task RemoveAuthToken(string username)
|
|
{
|
|
await _db.HashDeleteAsync(AUTH_TOKEN_KEY, username);
|
|
}
|
|
} |