vrcauthproxy/Tests/Integration/RedisIntegrationTests.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

104 lines
No EOL
3.8 KiB
C#

using System.Text.Json;
using VRCAuthProxy.Services;
using Xunit;
using FluentAssertions;
namespace VRCAuthProxy.Tests.Integration
{
[Trait("Category", "Integration")]
[Collection("Redis")]
public class RedisIntegrationTests
{
private readonly RedisService _redisService;
private const string TEST_USERNAME = "testuser";
public RedisIntegrationTests()
{
// For integration tests, use the actual Redis service
// This assumes Redis is running locally or in a Docker container
// as configured in docker-compose.yml (accessible at localhost:6379)
_redisService = new RedisService("localhost:6379");
// Clean up any previous test data
_redisService.RemoveAuthToken(TEST_USERNAME).GetAwaiter().GetResult();
}
[Fact(Skip = "Requires Redis instance - for manual integration testing")]
public async Task SaveAndGetAuthToken_ShouldStoreAndRetrieveTokens()
{
// Arrange
var cookies = new Dictionary<string, string>
{
{ "auth", "integration-test-token" },
{ "session", "integration-test-session" }
};
// Act - Save token
await _redisService.SaveAuthToken(TEST_USERNAME, cookies);
// Act - Retrieve token
var retrievedCookies = await _redisService.GetAuthToken(TEST_USERNAME);
// Assert
retrievedCookies.Should().NotBeNull();
if (retrievedCookies != null)
{
retrievedCookies.Should().ContainKey("auth");
retrievedCookies.Should().ContainKey("session");
retrievedCookies["auth"].Should().Be("integration-test-token");
retrievedCookies["session"].Should().Be("integration-test-session");
}
}
[Fact(Skip = "Requires Redis instance - for manual integration testing")]
public async Task RemoveAuthToken_ShouldDeleteTokenFromRedis()
{
// Arrange
var cookies = new Dictionary<string, string>
{
{ "auth", "token-to-delete" }
};
await _redisService.SaveAuthToken(TEST_USERNAME, cookies);
// Verify token exists
var tokenBeforeDelete = await _redisService.GetAuthToken(TEST_USERNAME);
tokenBeforeDelete.Should().NotBeNull();
// Act
await _redisService.RemoveAuthToken(TEST_USERNAME);
// Assert
var tokenAfterDelete = await _redisService.GetAuthToken(TEST_USERNAME);
tokenAfterDelete.Should().BeNull();
}
[Fact(Skip = "Requires Redis instance - for manual integration testing")]
public async Task GetAllAuthTokens_ShouldReturnAllStoredTokens()
{
// Arrange
var user1 = "testuser1";
var user2 = "testuser2";
// Clean up
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");
// Cleanup
await _redisService.RemoveAuthToken(user1);
await _redisService.RemoveAuthToken(user2);
}
}
}