|
| 1 | +package sync |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-git/go-git/v5/plumbing/transport/http" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNewAuthResolver(t *testing.T) { |
| 12 | + ctx := context.Background() |
| 13 | + |
| 14 | + t.Run("token credential returns BasicAuth with default username", func(t *testing.T) { |
| 15 | + lookup := func(_ context.Context, name string) (map[string]string, error) { |
| 16 | + return map[string]string{"token": "ghp_secret123"}, nil |
| 17 | + } |
| 18 | + fn := newAuthResolverFromLookup(ctx, lookup) |
| 19 | + auth, err := fn("my-cred") |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("unexpected error: %v", err) |
| 22 | + } |
| 23 | + basic, ok := auth.(*http.BasicAuth) |
| 24 | + if !ok { |
| 25 | + t.Fatalf("expected *http.BasicAuth, got %T", auth) |
| 26 | + } |
| 27 | + if basic.Password != "ghp_secret123" { |
| 28 | + t.Errorf("password = %q, want %q", basic.Password, "ghp_secret123") |
| 29 | + } |
| 30 | + if basic.Username != "x-token-auth" { |
| 31 | + t.Errorf("username = %q, want %q (default)", basic.Username, "x-token-auth") |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("token credential with explicit username preserves it", func(t *testing.T) { |
| 36 | + lookup := func(_ context.Context, name string) (map[string]string, error) { |
| 37 | + return map[string]string{"token": "ghp_secret123", "username": "mybot"}, nil |
| 38 | + } |
| 39 | + fn := newAuthResolverFromLookup(ctx, lookup) |
| 40 | + auth, err := fn("my-cred") |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("unexpected error: %v", err) |
| 43 | + } |
| 44 | + basic, ok := auth.(*http.BasicAuth) |
| 45 | + if !ok { |
| 46 | + t.Fatalf("expected *http.BasicAuth, got %T", auth) |
| 47 | + } |
| 48 | + if basic.Username != "mybot" { |
| 49 | + t.Errorf("username = %q, want %q", basic.Username, "mybot") |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("ssh_key credential returns unsupported error", func(t *testing.T) { |
| 54 | + lookup := func(_ context.Context, name string) (map[string]string, error) { |
| 55 | + return map[string]string{"ssh_key": "-----BEGIN OPENSSH PRIVATE KEY-----"}, nil |
| 56 | + } |
| 57 | + fn := newAuthResolverFromLookup(ctx, lookup) |
| 58 | + _, err := fn("my-ssh-cred") |
| 59 | + if err == nil { |
| 60 | + t.Fatal("expected error for ssh_key credential, got nil") |
| 61 | + } |
| 62 | + if !containsString(err.Error(), "ssh_key credentials are not supported") { |
| 63 | + t.Errorf("error = %q, want SSH not supported message", err.Error()) |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + t.Run("credential with no token or ssh_key returns error", func(t *testing.T) { |
| 68 | + lookup := func(_ context.Context, name string) (map[string]string, error) { |
| 69 | + return map[string]string{"username": "someone"}, nil |
| 70 | + } |
| 71 | + fn := newAuthResolverFromLookup(ctx, lookup) |
| 72 | + _, err := fn("empty-cred") |
| 73 | + if err == nil { |
| 74 | + t.Fatal("expected error for empty credential, got nil") |
| 75 | + } |
| 76 | + if !containsString(err.Error(), "no token or ssh_key") { |
| 77 | + t.Errorf("error = %q, want 'no token or ssh_key' message", err.Error()) |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("nil lookup returns resolver-not-configured error", func(t *testing.T) { |
| 82 | + fn := newAuthResolverFromLookup(ctx, nil) |
| 83 | + _, err := fn("any-cred") |
| 84 | + if err == nil { |
| 85 | + t.Fatal("expected error for nil resolver, got nil") |
| 86 | + } |
| 87 | + if !containsString(err.Error(), "secret resolver not configured") { |
| 88 | + t.Errorf("error = %q, want 'secret resolver not configured'", err.Error()) |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("lookup error is wrapped and returned", func(t *testing.T) { |
| 93 | + lookup := func(_ context.Context, name string) (map[string]string, error) { |
| 94 | + return nil, errors.New("credential not found") |
| 95 | + } |
| 96 | + fn := newAuthResolverFromLookup(ctx, lookup) |
| 97 | + _, err := fn("missing-cred") |
| 98 | + if err == nil { |
| 99 | + t.Fatal("expected error, got nil") |
| 100 | + } |
| 101 | + if !containsString(err.Error(), "resolving credential") { |
| 102 | + t.Errorf("error = %q, want wrapping 'resolving credential'", err.Error()) |
| 103 | + } |
| 104 | + if !containsString(err.Error(), "credential not found") { |
| 105 | + t.Errorf("error = %q, want wrapped 'credential not found'", err.Error()) |
| 106 | + } |
| 107 | + }) |
| 108 | +} |
| 109 | + |
| 110 | +// containsString is a small helper to avoid importing strings in test assertions. |
| 111 | +func containsString(s, substr string) bool { |
| 112 | + return len(s) >= len(substr) && (s == substr || len(substr) == 0 || |
| 113 | + func() bool { |
| 114 | + for i := 0; i+len(substr) <= len(s); i++ { |
| 115 | + if s[i:i+len(substr)] == substr { |
| 116 | + return true |
| 117 | + } |
| 118 | + } |
| 119 | + return false |
| 120 | + }()) |
| 121 | +} |
0 commit comments