Files
CollabTable/shared/backend/authclient/client_test.go
T
Gabriel20xxandClaude Sonnet 5 40578f5392 Auto-detect scheme for AUTHGUARD_URL instead of always assuming https
A bare host:port (e.g. a LAN AuthGuard reachable only over plain HTTP)
was always defaulting to https, so a TLS handshake against a plain-HTTP
AuthGuard failed at the transport level. Now a loopback, RFC1918/RFC4193
private, or link-local host (or a .local name) defaults to http instead;
an explicit http:// or https:// is still always honored as-is.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-03 21:48:08 +02:00

86 lines
3.5 KiB
Go

package authclient
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestNewNormalizesBaseURL(t *testing.T) {
cases := []struct {
name string
input string
want string
}{
{name: "bare public host gets https", input: "auth.example.com", want: "https://auth.example.com"},
{name: "bare private host:port gets http", input: "192.168.1.50:8080", want: "http://192.168.1.50:8080"},
{name: "bare public host:port gets https", input: "auth.example.com:8080", want: "https://auth.example.com:8080"},
{name: "loopback IPv4 gets http", input: "127.0.0.1:8080", want: "http://127.0.0.1:8080"},
{name: "10/8 private range gets http", input: "10.1.2.3:8080", want: "http://10.1.2.3:8080"},
{name: "172.16/12 private range gets http", input: "172.20.0.5:8080", want: "http://172.20.0.5:8080"},
{name: "172.x outside the 16-31 private range gets https", input: "172.32.0.5:8080", want: "https://172.32.0.5:8080"},
{name: "link-local IPv4 gets http", input: "169.254.1.1:8080", want: "http://169.254.1.1:8080"},
{name: "localhost name gets http", input: "localhost:8080", want: "http://localhost:8080"},
{name: ".local mDNS name gets http", input: "authguard.local:8080", want: "http://authguard.local:8080"},
{name: "loopback IPv6 gets http", input: "[::1]:8080", want: "http://[::1]:8080"},
{name: "unique-local IPv6 gets http", input: "[fd00::1]:8080", want: "http://[fd00::1]:8080"},
{name: "explicit http is left alone even for a public host", input: "http://auth.example.com", want: "http://auth.example.com"},
{name: "explicit https is left alone even for a private host", input: "https://192.168.1.50:8080", want: "https://192.168.1.50:8080"},
{name: "scheme match is case-insensitive", input: "HTTP://auth.example.com", want: "HTTP://auth.example.com"},
{name: "trailing slash stripped before scheme check", input: "auth.example.com/", want: "https://auth.example.com"},
{name: "surrounding whitespace trimmed", input: " auth.example.com ", want: "https://auth.example.com"},
{name: "empty stays empty", input: "", want: ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
client := New(tc.input)
if got := client.baseURL; got != tc.want {
t.Fatalf("New(%q).baseURL = %q, want %q", tc.input, got, tc.want)
}
})
}
}
func TestSetupStatus(t *testing.T) {
cases := []struct {
name string
response any
want bool
}{
{name: "no accounts yet", response: map[string]bool{"setup_required": true}, want: true},
{name: "an account already exists", response: map[string]bool{"setup_required": false}, want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/api/setup-status" {
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(tc.response)
}))
defer server.Close()
client := New(server.URL)
got, err := client.SetupStatus(context.Background())
if err != nil {
t.Fatalf("SetupStatus() error = %v", err)
}
if got != tc.want {
t.Fatalf("SetupStatus() = %v, want %v", got, tc.want)
}
})
}
}
func TestSetupStatusPropagatesTransportError(t *testing.T) {
client := New("http://127.0.0.1:0")
if _, err := client.SetupStatus(context.Background()); err == nil {
t.Fatal("SetupStatus() expected an error against an unreachable server, got nil")
}
}