100 lines
1.9 KiB
Go
100 lines
1.9 KiB
Go
package 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")
|
|
}
|
|
}
|