1
0

cache_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package user
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/0xJacky/Nginx-UI/internal/cache"
  6. "github.com/0xJacky/Nginx-UI/model"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestTokenCacheOperations(t *testing.T) {
  10. if testing.Short() {
  11. t.Skip("skipping test in short mode")
  12. }
  13. // Initialize cache for testing
  14. cache.InitInMemoryCache()
  15. // Create test token data
  16. testToken := &model.AuthToken{
  17. UserID: 12345,
  18. Token: "test-jwt-token-123",
  19. ShortToken: "short-token-456",
  20. ExpiredAt: time.Now().Add(time.Hour).Unix(),
  21. }
  22. // Test caching token
  23. CacheToken(testToken)
  24. // Test retrieving token data
  25. tokenData, found := GetCachedTokenData(testToken.Token)
  26. assert.True(t, found, "Token should be found in cache")
  27. assert.Equal(t, testToken.UserID, tokenData.UserID)
  28. assert.Equal(t, testToken.Token, tokenData.Token)
  29. assert.Equal(t, testToken.ShortToken, tokenData.ShortToken)
  30. assert.Equal(t, testToken.ExpiredAt, tokenData.ExpiredAt)
  31. // Test retrieving by short token
  32. shortTokenData, found := GetCachedShortTokenData(testToken.ShortToken)
  33. assert.True(t, found, "Short token should be found in cache")
  34. assert.Equal(t, testToken.UserID, shortTokenData.UserID)
  35. // Test cache invalidation
  36. InvalidateTokenCache(testToken.Token)
  37. _, found = GetCachedTokenData(testToken.Token)
  38. assert.False(t, found, "Token should not be found after invalidation")
  39. _, found = GetCachedShortTokenData(testToken.ShortToken)
  40. assert.False(t, found, "Short token should not be found after invalidation")
  41. }
  42. func TestUserCacheOperations(t *testing.T) {
  43. if testing.Short() {
  44. t.Skip("skipping integration test in short mode")
  45. }
  46. // Initialize cache for testing
  47. cache.InitInMemoryCache()
  48. // Create test user
  49. testUser := &model.User{
  50. Name: "testuser",
  51. Status: true,
  52. Language: "en",
  53. }
  54. testUser.ID = 12345
  55. // Test caching user
  56. CacheUser(testUser)
  57. // Test retrieving user
  58. cachedUser, found := GetCachedUser(testUser.ID)
  59. assert.True(t, found, "User should be found in cache")
  60. assert.Equal(t, testUser.Name, cachedUser.Name)
  61. assert.Equal(t, testUser.ID, cachedUser.ID)
  62. assert.Equal(t, testUser.Status, cachedUser.Status)
  63. // Test cache invalidation
  64. InvalidateUserCache(testUser.ID)
  65. _, found = GetCachedUser(testUser.ID)
  66. assert.False(t, found, "User should not be found after invalidation")
  67. }
  68. func TestExpiredTokenHandling(t *testing.T) {
  69. if testing.Short() {
  70. t.Skip("skipping test in short mode")
  71. }
  72. // Initialize cache for testing
  73. cache.InitInMemoryCache()
  74. // Create expired token
  75. expiredToken := &model.AuthToken{
  76. UserID: 12345,
  77. Token: "expired-token-123",
  78. ShortToken: "expired-short-456",
  79. ExpiredAt: time.Now().Add(-time.Hour).Unix(), // Expired 1 hour ago
  80. }
  81. // Cache the expired token
  82. CacheToken(expiredToken)
  83. // Try to retrieve expired token - should return false and clean cache
  84. _, found := GetCachedTokenData(expiredToken.Token)
  85. assert.False(t, found, "Expired token should not be returned")
  86. // Try to retrieve by expired short token - should return false and clean cache
  87. _, found = GetCachedShortTokenData(expiredToken.ShortToken)
  88. assert.False(t, found, "Expired short token should not be returned")
  89. }