Something went wrong. Try again.
A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
Something went wrong. Try again.
1.9 kB · 99 lines
Go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100package auth
import ( "testing" "time")
func TestTokenCache_SetAndGet(t *testing.T) { cache := &TokenCache{ tokens: make(map[string]*TokenCacheEntry), }
did := "did:plc:test123" token := "test_token_abc"
// Set token with 1 hour TTL cache.Set(did, token, time.Hour)
// Get token - should exist retrieved, ok := cache.Get(did) if !ok { t.Fatal("Expected token to be cached") }
if retrieved != token { t.Errorf("Expected token %q, got %q", token, retrieved) }}
func TestTokenCache_GetNonExistent(t *testing.T) { cache := &TokenCache{ tokens: make(map[string]*TokenCacheEntry), }
// Try to get non-existent token _, ok := cache.Get("did:plc:nonexistent") if ok { t.Error("Expected cache miss for non-existent DID") }}
func TestTokenCache_Expiration(t *testing.T) { cache := &TokenCache{ tokens: make(map[string]*TokenCacheEntry), }
did := "did:plc:test123" token := "test_token_abc"
// Set token with very short TTL cache.Set(did, token, 1*time.Millisecond)
// Wait for expiration time.Sleep(10 * time.Millisecond)
// Get token - should be expired _, ok := cache.Get(did) if ok { t.Error("Expected token to be expired") }}
func TestTokenCache_Delete(t *testing.T) { cache := &TokenCache{ tokens: make(map[string]*TokenCacheEntry), }
did := "did:plc:test123" token := "test_token_abc"
// Set and verify cache.Set(did, token, time.Hour) _, ok := cache.Get(did) if !ok { t.Fatal("Expected token to be cached") }
// Delete cache.Delete(did)
// Verify deleted _, ok = cache.Get(did) if ok { t.Error("Expected token to be deleted") }}
func TestGetGlobalTokenCache(t *testing.T) { cache := GetGlobalTokenCache() if cache == nil { t.Fatal("Expected global cache to be initialized") }
// Test that we get the same instance cache2 := GetGlobalTokenCache() if cache != cache2 { t.Error("Expected same global cache instance") }}