Merge media-webui/media-backend into the general shared component/backend system
Extracts a new backend/authproxy package for the AuthGuard-proxy helper
functions (session cookie handling, input normalization, error
translation) that Bitmagnet/GameStream/media-backend/AutoCode/
CollabTable/PubliSafe/SimpleCollection/SyncServer/UnraidMonitor/
OutdoorGames were each independently reimplementing, and repoints all
of them onto it.
Extends packages/web-ui's Button/Badge with the variants media-webui
needed (outline, icon size, primary badge) and promotes EmptyState/
SectionTitle/Metric/StatusWidget/StatusDialog into the shared library,
then migrates media-webui off its own diverged ui/{button,card,badge,
metric} components and local duplicates of the promoted components
onto @gabriel20xx/web-ui, so media-webui is a proper consumer of the
same design system every other project already uses instead of a
parallel fork.
Also removes the dead `desktop`/MediaDesktop service variant from
media-backend and media-webui (MediaDesktop is an Electron client that
never actually runs this stack).
This commit is contained in:
1 parent
9589d07baf
commit
7b5a4e6c2c
49 files changed
+872
-726
No files matched your search
@@ -0,0 +1,150 @@
|
||||
package authproxy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
authclient "repos.gfranz.ch/Gabriel20xx/shared/backend/authclient"
|
||||
)
|
||||
|
||||
func TestSessionToken(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
cookie *http.Cookie
|
||||
want string
|
||||
}{
|
||||
{name: "no cookie", want: ""},
|
||||
{name: "present", cookie: &http.Cookie{Name: authclient.DefaultCookieName, Value: "tok123"}, want: "tok123"},
|
||||
{name: "empty value", cookie: &http.Cookie{Name: authclient.DefaultCookieName, Value: ""}, want: ""},
|
||||
{name: "wrong cookie name", cookie: &http.Cookie{Name: "other", Value: "tok123"}, want: ""},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
if tc.cookie != nil {
|
||||
r.AddCookie(tc.cookie)
|
||||
}
|
||||
if got := SessionToken(r); got != tc.want {
|
||||
t.Fatalf("SessionToken() = %q, want %q", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearSessionCookies(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
ClearSessionCookies(w, "legacy_session", true)
|
||||
|
||||
resp := w.Result()
|
||||
cookies := resp.Cookies()
|
||||
if len(cookies) != 2 {
|
||||
t.Fatalf("got %d Set-Cookie headers, want 2: %+v", len(cookies), cookies)
|
||||
}
|
||||
byName := map[string]*http.Cookie{}
|
||||
for _, c := range cookies {
|
||||
byName[c.Name] = c
|
||||
}
|
||||
for _, name := range []string{authclient.DefaultCookieName, "legacy_session"} {
|
||||
c, ok := byName[name]
|
||||
if !ok {
|
||||
t.Fatalf("missing Set-Cookie for %q", name)
|
||||
}
|
||||
if c.Value != "" || c.MaxAge != -1 || !c.HttpOnly || !c.Secure {
|
||||
t.Fatalf("cookie %q not cleared correctly: %+v", name, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearSessionCookiesNoLegacyName(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
ClearSessionCookies(w, "", false)
|
||||
|
||||
if got := len(w.Result().Cookies()); got != 1 {
|
||||
t.Fatalf("got %d Set-Cookie headers, want 1 (no legacy name given)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClearSessionCookiesLegacyEqualsDefault(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
ClearSessionCookies(w, authclient.DefaultCookieName, false)
|
||||
|
||||
if got := len(w.Result().Cookies()); got != 1 {
|
||||
t.Fatalf("got %d Set-Cookie headers, want 1 (legacy name equals AuthGuard's own)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeUsername(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
ok bool
|
||||
}{
|
||||
{name: "valid", input: " gabriel ", want: "gabriel", ok: true},
|
||||
{name: "empty", input: " ", want: "", ok: false},
|
||||
{name: "too long", input: string(make([]byte, 65)), want: string(make([]byte, 65)), ok: false},
|
||||
{name: "exactly max", input: "1234567890", want: "1234567890", ok: true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, ok := NormalizeUsername(tc.input, 10)
|
||||
if got != tc.want || ok != tc.ok {
|
||||
t.Fatalf("NormalizeUsername(%q, 10) = (%q, %v), want (%q, %v)", tc.input, got, ok, tc.want, tc.ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeEmail(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
ok bool
|
||||
}{
|
||||
{name: "valid", input: " Gabriel@Example.COM ", want: "gabriel@example.com", ok: true},
|
||||
{name: "empty", input: "", want: "", ok: false},
|
||||
{name: "no at", input: "gabriel.example.com", want: "gabriel.example.com", ok: false},
|
||||
{name: "two ats", input: "a@b@c.com", want: "a@b@c.com", ok: false},
|
||||
{name: "empty local part", input: "@example.com", want: "@example.com", ok: false},
|
||||
{name: "empty domain", input: "gabriel@", want: "gabriel@", ok: false},
|
||||
{name: "domain without dot", input: "gabriel@localhost", want: "gabriel@localhost", ok: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, ok := NormalizeEmail(tc.input, 254)
|
||||
if got != tc.want || ok != tc.ok {
|
||||
t.Fatalf("NormalizeEmail(%q, 254) = (%q, %v), want (%q, %v)", tc.input, got, ok, tc.want, tc.ok)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidPassword(t *testing.T) {
|
||||
if ValidPassword("short", 8) {
|
||||
t.Fatal("expected 5-char password to fail an 8-char minimum")
|
||||
}
|
||||
if !ValidPassword("longenough", 8) {
|
||||
t.Fatal("expected 10-char password to pass an 8-char minimum")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIErrorResponse(t *testing.T) {
|
||||
t.Run("api error", func(t *testing.T) {
|
||||
apiErr := &authclient.APIError{StatusCode: http.StatusConflict, Message: "already exists"}
|
||||
status, message, ok := APIErrorResponse(apiErr)
|
||||
if !ok || status != http.StatusConflict || message != "already exists" {
|
||||
t.Fatalf("APIErrorResponse() = (%d, %q, %v), want (409, %q, true)", status, message, ok, "already exists")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("non-api error", func(t *testing.T) {
|
||||
_, _, ok := APIErrorResponse(errors.New("transport failure"))
|
||||
if ok {
|
||||
t.Fatal("expected ok=false for a non-APIError")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user