Files
CollabTable/shared/backend/authclient/client_test.go
T

152 lines
5.8 KiB
Go
Raw Normal View History

package authclient
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
2026-09-02 20:49:29 +02:00
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"},
2026-09-02 20:49:29 +02:00
{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")
}
}
func TestSetBaseURLRetargetsSubsequentCalls(t *testing.T) {
client := New("http://127.0.0.1:0")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]bool{"setup_required": true})
}))
defer server.Close()
client.SetBaseURL(server.URL)
got, err := client.SetupStatus(context.Background())
if err != nil {
t.Fatalf("SetupStatus() after SetBaseURL error = %v", err)
}
if !got {
t.Fatalf("SetupStatus() = %v, want true", got)
}
}
func TestSetBaseURLIgnoresBlank(t *testing.T) {
client := New("https://auth.example.com")
client.SetBaseURL(" ")
if got := client.getBaseURL(); got != "https://auth.example.com" {
t.Fatalf("SetBaseURL(blank) changed baseURL to %q, want unchanged", got)
}
}
func TestWithClientIPSendsXForwardedForOnCredentialCalls(t *testing.T) {
var seen []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
seen = append(seen, r.Header.Get(ForwardedForHeader))
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"user":{"id":1,"username":"a"}}`))
}))
defer server.Close()
client := New(server.URL)
ctx := WithClientIP(context.Background(), " 203.0.113.9 ")
if _, err := client.Login(ctx, "a", "password123", ""); err != nil {
t.Fatal(err)
}
if _, err := client.Register(WithClientIP(context.Background(), "2001:db8::1"), "a", "a@b.com", "password123"); err != nil {
t.Fatal(err)
}
if err := client.TOTPVerify(ctx, "tok", "123456"); err != nil {
t.Fatal(err)
}
// No address, and values that are not IP addresses, send no header.
if _, err := client.Login(context.Background(), "a", "password123", ""); err != nil {
t.Fatal(err)
}
for _, bad := range []string{"", "not-an-ip", "203.0.113.9\r\nX-Evil: 1"} {
if _, err := client.Login(WithClientIP(context.Background(), bad), "a", "password123", ""); err != nil {
t.Fatal(err)
}
}
want := []string{"203.0.113.9", "2001:db8::1", "203.0.113.9", "", "", "", ""}
if len(seen) != len(want) {
t.Fatalf("server saw %d requests, want %d", len(seen), len(want))
}
for i := range want {
if seen[i] != want[i] {
t.Errorf("request %d X-Forwarded-For = %q, want %q", i, seen[i], want[i])
}
}
}