vrcauthproxy/Tests/Unit/RedisServiceTests.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

144 lines
No EOL
4.8 KiB
C#

using System.Text.Json;
using FluentAssertions;
using VRCAuthProxy.Services;
using Xunit;
namespace VRCAuthProxy.Tests.Unit
{
public class RedisServiceTests
{
// Using a minimal implementation for testing
private class InMemoryRedisService
{
private readonly Dictionary<string, Dictionary<string, string>> _store = new();
public Task SaveAuthToken(string username, Dictionary<string, string> cookies)
{
_store[username] = new Dictionary<string, string>(cookies);
return Task.CompletedTask;
}
public Task<Dictionary<string, string>?> GetAuthToken(string username)
{
if (_store.TryGetValue(username, out var cookies))
return Task.FromResult<Dictionary<string, string>?>(new Dictionary<string, string>(cookies));
return Task.FromResult<Dictionary<string, string>?>(null);
}
public Task<Dictionary<string, Dictionary<string, string>>> GetAllAuthTokens()
{
var result = new Dictionary<string, Dictionary<string, string>>();
foreach (var pair in _store)
{
result[pair.Key] = new Dictionary<string, string>(pair.Value);
}
return Task.FromResult(result);
}
public Task RemoveAuthToken(string username)
{
_store.Remove(username);
return Task.CompletedTask;
}
}
private readonly InMemoryRedisService _redisService;
public RedisServiceTests()
{
_redisService = new InMemoryRedisService();
}
[Fact]
public async Task SaveAndGetAuthToken_ShouldStoreAndRetrieveTokens()
{
// Arrange
var username = "testuser";
var cookies = new Dictionary<string, string>
{
{ "auth", "test-auth-token" },
{ "session", "test-session-token" }
};
// Act - Save token
await _redisService.SaveAuthToken(username, cookies);
// Act - Retrieve token
var retrievedCookies = await _redisService.GetAuthToken(username);
// Assert
retrievedCookies.Should().NotBeNull();
if (retrievedCookies != null)
{
retrievedCookies.Should().ContainKey("auth");
retrievedCookies.Should().ContainKey("session");
retrievedCookies["auth"].Should().Be("test-auth-token");
retrievedCookies["session"].Should().Be("test-session-token");
}
}
[Fact]
public async Task GetAuthToken_WhenTokenDoesNotExist_ShouldReturnNull()
{
// Arrange
var username = "nonexistentuser";
// Act
var result = await _redisService.GetAuthToken(username);
// Assert
result.Should().BeNull();
}
[Fact]
public async Task GetAllAuthTokens_ShouldReturnAllStoredTokens()
{
// Arrange
var user1 = "testuser1";
var user2 = "testuser2";
// Clear any existing tokens
await _redisService.RemoveAuthToken(user1);
await _redisService.RemoveAuthToken(user2);
// Add test data
await _redisService.SaveAuthToken(user1, new Dictionary<string, string> { { "auth", "token1" } });
await _redisService.SaveAuthToken(user2, new Dictionary<string, string> { { "auth", "token2" } });
// Act
var allTokens = await _redisService.GetAllAuthTokens();
// Assert
allTokens.Should().ContainKey(user1);
allTokens.Should().ContainKey(user2);
allTokens[user1]["auth"].Should().Be("token1");
allTokens[user2]["auth"].Should().Be("token2");
}
[Fact]
public async Task RemoveAuthToken_ShouldDeleteToken()
{
// Arrange
var username = "testuser";
var cookies = new Dictionary<string, string>
{
{ "auth", "token-to-delete" }
};
await _redisService.SaveAuthToken(username, cookies);
// Verify token exists
var tokenBeforeDelete = await _redisService.GetAuthToken(username);
tokenBeforeDelete.Should().NotBeNull();
// Act
await _redisService.RemoveAuthToken(username);
// Assert
var tokenAfterDelete = await _redisService.GetAuthToken(username);
tokenAfterDelete.Should().BeNull();
}
}
}