forked from
evan.jarrett.net/at-container-registry
A container registry that uses the AT Protocol for manifest storage and S3 for blob storage.
1package auth
2
3import (
4 "testing"
5 "time"
6)
7
8func TestTokenCache_SetAndGet(t *testing.T) {
9 cache := &TokenCache{
10 tokens: make(map[string]*TokenCacheEntry),
11 }
12
13 did := "did:plc:test123"
14 token := "test_token_abc"
15
16 // Set token with 1 hour TTL
17 cache.Set(did, token, time.Hour)
18
19 // Get token - should exist
20 retrieved, ok := cache.Get(did)
21 if !ok {
22 t.Fatal("Expected token to be cached")
23 }
24
25 if retrieved != token {
26 t.Errorf("Expected token %q, got %q", token, retrieved)
27 }
28}
29
30func TestTokenCache_GetNonExistent(t *testing.T) {
31 cache := &TokenCache{
32 tokens: make(map[string]*TokenCacheEntry),
33 }
34
35 // Try to get non-existent token
36 _, ok := cache.Get("did:plc:nonexistent")
37 if ok {
38 t.Error("Expected cache miss for non-existent DID")
39 }
40}
41
42func TestTokenCache_Expiration(t *testing.T) {
43 cache := &TokenCache{
44 tokens: make(map[string]*TokenCacheEntry),
45 }
46
47 did := "did:plc:test123"
48 token := "test_token_abc"
49
50 // Set token with very short TTL
51 cache.Set(did, token, 1*time.Millisecond)
52
53 // Wait for expiration
54 time.Sleep(10 * time.Millisecond)
55
56 // Get token - should be expired
57 _, ok := cache.Get(did)
58 if ok {
59 t.Error("Expected token to be expired")
60 }
61}
62
63func TestTokenCache_Delete(t *testing.T) {
64 cache := &TokenCache{
65 tokens: make(map[string]*TokenCacheEntry),
66 }
67
68 did := "did:plc:test123"
69 token := "test_token_abc"
70
71 // Set and verify
72 cache.Set(did, token, time.Hour)
73 _, ok := cache.Get(did)
74 if !ok {
75 t.Fatal("Expected token to be cached")
76 }
77
78 // Delete
79 cache.Delete(did)
80
81 // Verify deleted
82 _, ok = cache.Get(did)
83 if ok {
84 t.Error("Expected token to be deleted")
85 }
86}
87
88func TestGetGlobalTokenCache(t *testing.T) {
89 cache := GetGlobalTokenCache()
90 if cache == nil {
91 t.Fatal("Expected global cache to be initialized")
92 }
93
94 // Test that we get the same instance
95 cache2 := GetGlobalTokenCache()
96 if cache != cache2 {
97 t.Error("Expected same global cache instance")
98 }
99}