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:
rcourtman 2025-12-02 01:25:11 +00:00
parent f86ea20069
commit 1ec59037b9

View file

@ -7,6 +7,7 @@ import (
"math"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
@ -1384,3 +1385,53 @@ func TestGetNodes_InvalidJSON(t *testing.T) {
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)
}
})
}
}