package handlers import ( "testing" "time" ) func TestDownloadDebouncer(t *testing.T) { d := NewDownloadDebouncer(time.Minute) if !d.ShouldAllow("user", "content") { t.Fatal("first request denied") } if d.ShouldAllow("user", "content") { t.Fatal("duplicate request allowed") } if !d.ShouldAllow("other", "content") { t.Fatal("different user denied") } } func TestTokenStoreCreateConsume(t *testing.T) { store := newTokenStore[SingleDownloadRequest]() req := SingleDownloadRequest{Image: "nginx:latest", Platform: "linux/amd64", UseCompressedLayers: true} token, err := store.create(req, "127.0.0.1", "ua") if err != nil { t.Fatal(err) } got, ok := store.consume(token, "127.0.0.1", "ua") if !ok { t.Fatal("token not consumed") } if got != req { t.Fatalf("request = %#v, want %#v", got, req) } if _, ok := store.consume(token, "127.0.0.1", "ua"); ok { t.Fatal("token consumed twice") } } func TestTokenStoreRejectsDifferentClient(t *testing.T) { store := newTokenStore[SingleDownloadRequest]() token, err := store.create(SingleDownloadRequest{Image: "nginx:latest"}, "127.0.0.1", "ua") if err != nil { t.Fatal(err) } if _, ok := store.consume(token, "127.0.0.2", "ua"); ok { t.Fatal("token accepted for different IP") } } func TestGenerateContentFingerprintStable(t *testing.T) { a := generateContentFingerprint([]string{"b:1", "a:1"}, "linux/amd64") b := generateContentFingerprint([]string{"a:1", "b:1"}, "linux/amd64") c := generateContentFingerprint([]string{"a:1", "b:1"}, "linux/arm64") if a != b || a == c { t.Fatalf("unexpected fingerprints: %q %q %q", a, b, c) } }