test: Add invalid user format tests for NewClient
Test error handling for password authentication user format validation: - Missing realm separator (no @) - Empty user string - Multiple @ symbols Improves NewClient coverage from 74.2% to 83.9%.
This commit is contained in:
parent
f86ea20069
commit
1ec59037b9
1 changed files with 51 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -1384,3 +1385,53 @@ func TestGetNodes_InvalidJSON(t *testing.T) {
|
||||||
t.Fatal("expected JSON decode error, got nil")
|
t.Fatal("expected JSON decode error, got nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewClient_InvalidUserFormat(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
user string
|
||||||
|
wantErr bool
|
||||||
|
errContains string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "missing realm separator",
|
||||||
|
user: "userwithoutrealm",
|
||||||
|
wantErr: true,
|
||||||
|
errContains: "invalid user format",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty user",
|
||||||
|
user: "",
|
||||||
|
wantErr: true,
|
||||||
|
errContains: "invalid user format",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multiple @ symbols",
|
||||||
|
user: "user@realm@extra",
|
||||||
|
wantErr: true,
|
||||||
|
errContains: "invalid user format",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
cfg := ClientConfig{
|
||||||
|
Host: "https://localhost:8006",
|
||||||
|
User: tc.user,
|
||||||
|
Password: "testpass",
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := NewClient(cfg)
|
||||||
|
if tc.wantErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected error for user %q, got nil", tc.user)
|
||||||
|
} else if !strings.Contains(err.Error(), tc.errContains) {
|
||||||
|
t.Errorf("expected error containing %q, got: %v", tc.errContains, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !tc.wantErr && err != nil {
|
||||||
|
t.Errorf("unexpected error for user %q: %v", tc.user, err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue