Files

193 lines
6.3 KiB
Go

package coreconfig
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestStoreSetIgnoresBlankFields(t *testing.T) {
store := NewStore(Info{AuthGuardURL: "http://auth.example.com", CarrierNodeURL: "http://carrier.example.com"})
store.Set(Info{CarrierNodeURL: "http://carrier2.example.com"})
got := store.Get()
if got.AuthGuardURL != "http://auth.example.com" {
t.Fatalf("AuthGuardURL = %q, want unchanged", got.AuthGuardURL)
}
if got.CarrierNodeURL != "http://carrier2.example.com" {
t.Fatalf("CarrierNodeURL = %q, want updated", got.CarrierNodeURL)
}
}
func TestStoreSetComfyUIURL(t *testing.T) {
store := NewStore(Info{})
store.Set(Info{ComfyUIURL: "http://comfyui:8188/"})
if got := store.ComfyUIURL(); got != "http://comfyui:8188" {
t.Fatalf("ComfyUIURL = %q, want trimmed URL", got)
}
}
func TestStoreSetCarrierNodeSecret(t *testing.T) {
store := NewStore(Info{})
store.Set(Info{CarrierNodeSecret: " carrier-secret "})
if got := store.CarrierNodeSecret(); got != "carrier-secret" {
t.Fatalf("CarrierNodeSecret = %q, want trimmed pushed secret", got)
}
store.Set(Info{})
if got := store.CarrierNodeSecret(); got != "carrier-secret" {
t.Fatalf("blank push cleared CarrierNodeSecret to %q", got)
}
}
func TestStoreOnUpdateFiresWithNewInfo(t *testing.T) {
store := NewStore(Info{})
var got Info
calls := 0
store.OnUpdate(func(info Info) {
calls++
got = info
})
store.Set(Info{AuthGuardURL: "http://auth.example.com"})
if calls != 1 {
t.Fatalf("OnUpdate called %d times, want 1", calls)
}
if got.AuthGuardURL != "http://auth.example.com" {
t.Fatalf("OnUpdate saw AuthGuardURL = %q, want http://auth.example.com", got.AuthGuardURL)
}
}
func TestHandlerRejectsMissingOrWrongSecret(t *testing.T) {
store := NewStore(Info{})
handler := Handler("test-internal-secret", store)
body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com"})
cases := []string{"", "wrong"}
for _, header := range cases {
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
if header != "" {
req.Header.Set("X-Internal-Auth", header)
}
rec := httptest.NewRecorder()
handler(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Fatalf("status = %d, want 401 for X-Internal-Auth=%q", rec.Code, header)
}
}
if got := store.Get(); got.AuthGuardURL != "" {
t.Fatalf("store was updated despite auth failure: %+v", got)
}
}
func TestStoreSetPopulatesCarrierNodeURLsFromLegacyField(t *testing.T) {
store := NewStore(Info{})
store.Set(Info{CarrierNodeURL: "http://carrier.example.com"})
got := store.Get()
if want := []string{"http://carrier.example.com"}; !equalStrings(got.CarrierNodeURLs, want) {
t.Fatalf("CarrierNodeURLs = %v, want %v", got.CarrierNodeURLs, want)
}
}
func TestStoreSetCarrierNodeURLsAlsoUpdatesPrimary(t *testing.T) {
store := NewStore(Info{})
store.Set(Info{CarrierNodeURLs: []string{"http://primary.example.com", "http://replica.example.com"}})
got := store.Get()
if got.CarrierNodeURL != "http://primary.example.com" {
t.Fatalf("CarrierNodeURL = %q, want first CarrierNodeURLs entry", got.CarrierNodeURL)
}
want := []string{"http://primary.example.com", "http://replica.example.com"}
if !equalStrings(got.CarrierNodeURLs, want) {
t.Fatalf("CarrierNodeURLs = %v, want %v", got.CarrierNodeURLs, want)
}
}
func equalStrings(got, want []string) bool {
if len(got) != len(want) {
return false
}
for i := range got {
if got[i] != want[i] {
return false
}
}
return true
}
func TestHandlerAppliesPushedInfo(t *testing.T) {
store := NewStore(Info{})
handler := Handler("test-internal-secret", store)
body, _ := json.Marshal(Info{
AuthGuardURL: "http://auth.example.com", CarrierNodeURL: "http://carrier.example.com", CarrierNodeSecret: "carrier-secret",
ContentScannerURL: "http://scanner.example.com/", ContentScannerSecret: "scanner-secret",
})
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
req.Header.Set("X-Internal-Auth", "test-internal-secret")
rec := httptest.NewRecorder()
handler(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200: %s", rec.Code, rec.Body.String())
}
got := store.Get()
if got.AuthGuardURL != "http://auth.example.com" || got.CarrierNodeURL != "http://carrier.example.com" || got.CarrierNodeSecret != "carrier-secret" || got.ContentScannerURL != "http://scanner.example.com" || got.ContentScannerSecret != "scanner-secret" {
t.Fatalf("store = %+v, want pushed values applied", got)
}
if strings.Contains(rec.Body.String(), "scanner-secret") {
t.Fatal("response echoed the ContentScanner secret")
}
}
func TestHandlerRejectsNonHTTPURLsWithoutChangingStore(t *testing.T) {
store := NewStore(Info{AuthGuardURL: "http://auth.example.com"})
handler := Handler("test-internal-secret", store)
for _, bad := range []Info{
{AuthGuardURL: "javascript:alert(1)"},
{AuthGuardURL: "auth.example.com"},
{CarrierNodeURLs: []string{"http://carrier.example.com", "file:///etc/passwd"}},
{ContentScannerURL: "http://"},
} {
body, _ := json.Marshal(bad)
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
req.Header.Set("X-Internal-Auth", "test-internal-secret")
rec := httptest.NewRecorder()
handler(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d for %+v, want 400", rec.Code, bad)
}
}
if got := store.Get(); got.AuthGuardURL != "http://auth.example.com" || len(got.CarrierNodeURLs) != 0 {
t.Fatalf("store changed by a rejected push: %+v", got)
}
}
func TestHandlerRefusesAnUnusableSecret(t *testing.T) {
for _, secret := range []string{"short", "SECRET", "change-me-to-a-long-random-value"} {
store := NewStore(Info{})
handler := Handler(secret, store)
body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com"})
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
req.Header.Set("X-Internal-Auth", secret)
rec := httptest.NewRecorder()
handler(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Fatalf("secret %q: status = %d, want %d", secret, rec.Code, http.StatusUnauthorized)
}
if store.Get().AuthGuardURL != "" {
t.Fatalf("secret %q: store changed despite a refused push", secret)
}
}
}