Files
at-container-registry/pkg/auth/oauth/client_test.go
2025-10-29 12:06:47 -05:00

124 lines
2.7 KiB
Go

package oauth
import (
"testing"
)
func TestNewApp(t *testing.T) {
tmpDir := t.TempDir()
storePath := tmpDir + "/oauth-test.json"
keyPath := tmpDir + "/oauth-key.bin"
store, err := NewFileStore(storePath)
if err != nil {
t.Fatalf("NewFileStore() error = %v", err)
}
baseURL := "http://localhost:5000"
holdDID := "did:web:hold.example.com"
app, err := NewApp(baseURL, store, holdDID, keyPath, "AT Container Registry")
if err != nil {
t.Fatalf("NewApp() error = %v", err)
}
if app == nil {
t.Fatal("Expected non-nil app")
}
if app.baseURL != baseURL {
t.Errorf("Expected baseURL %q, got %q", baseURL, app.baseURL)
}
}
func TestNewAppWithScopes(t *testing.T) {
tmpDir := t.TempDir()
storePath := tmpDir + "/oauth-test.json"
keyPath := tmpDir + "/oauth-key.bin"
store, err := NewFileStore(storePath)
if err != nil {
t.Fatalf("NewFileStore() error = %v", err)
}
baseURL := "http://localhost:5000"
scopes := []string{"atproto", "custom:scope"}
app, err := NewAppWithScopes(baseURL, store, scopes, keyPath, "AT Container Registry")
if err != nil {
t.Fatalf("NewAppWithScopes() error = %v", err)
}
if app == nil {
t.Fatal("Expected non-nil app")
}
// Verify scopes are set in config
config := app.GetConfig()
if len(config.Scopes) != len(scopes) {
t.Errorf("Expected %d scopes, got %d", len(scopes), len(config.Scopes))
}
}
func TestScopesMatch(t *testing.T) {
tests := []struct {
name string
stored []string
desired []string
expected bool
}{
{
name: "exact match",
stored: []string{"atproto", "blob:image/png"},
desired: []string{"atproto", "blob:image/png"},
expected: true,
},
{
name: "different order",
stored: []string{"blob:image/png", "atproto"},
desired: []string{"atproto", "blob:image/png"},
expected: true,
},
{
name: "missing scope in stored",
stored: []string{"atproto"},
desired: []string{"atproto", "blob:image/png"},
expected: false,
},
{
name: "extra scope in stored",
stored: []string{"atproto", "blob:image/png", "extra"},
desired: []string{"atproto", "blob:image/png"},
expected: false,
},
{
name: "both empty",
stored: []string{},
desired: []string{},
expected: true,
},
{
name: "nil vs empty",
stored: nil,
desired: []string{},
expected: true,
},
{
name: "completely different",
stored: []string{"foo", "bar"},
desired: []string{"baz", "qux"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ScopesMatch(tt.stored, tt.desired)
if result != tt.expected {
t.Errorf("ScopesMatch(%v, %v) = %v, want %v",
tt.stored, tt.desired, result, tt.expected)
}
})
}
}