package syncsocket import ( "net/http" "net/http/httptest" "strings" "testing" "github.com/go-chi/chi/v5" sharedauth "repos.gfranz.ch/Gabriel20xx/shared/backend/auth" ) // TestSyncWriteRequiresLogin checks that an anonymous PUT /sync is refused before it can change // shared playback state. func TestSyncWriteRequiresLogin(t *testing.T) { auth := sharedauth.NewWithStore(loginRequiredStore{}, "session", false, "", "testproject") router := chi.NewRouter() MountRoutes(router, NewHub(), auth.Optional, auth.Require) request := httptest.NewRequest(http.MethodPut, "/sync", strings.NewReader(`{"clientId":"c1","mediaId":"m1"}`)) response := httptest.NewRecorder() router.ServeHTTP(response, request) if response.Code != http.StatusUnauthorized { t.Errorf("PUT /sync status = %d, want %d", response.Code, http.StatusUnauthorized) } } // TestSyncScalarKeepsOnlyScalars checks that sync values are limited to null, numbers and // bounded strings, so objects, arrays and booleans are never stored as session state. func TestSyncScalarKeepsOnlyScalars(t *testing.T) { if value, ok := syncScalar(nil); !ok || value != nil { t.Errorf("syncScalar(nil) = %v, %v; want nil, true", value, ok) } if value, ok := syncScalar(float64(7)); !ok || value != float64(7) { t.Errorf("syncScalar(7) = %v, %v; want 7, true", value, ok) } if value, ok := syncScalar(" media-1 "); !ok || value != "media-1" { t.Errorf("syncScalar(string) = %v, %v; want media-1, true", value, ok) } long, ok := syncScalar(strings.Repeat("x", 500)) if text, isText := long.(string); !ok || !isText || len(text) != 128 { t.Errorf("syncScalar(long string) = %v, %v; want a 128-byte string, true", long, ok) } for _, refused := range []any{map[string]any{"a": 1}, []any{1}, true} { if _, ok := syncScalar(refused); ok { t.Errorf("syncScalar(%#v) accepted a non-scalar value", refused) } } }