51 lines
2.0 KiB
Go
51 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAdminRequiresLoginAndCreatesSession(t *testing.T) {
|
|
t.Setenv("LANDWELLE_ADMIN_USER", "admin")
|
|
t.Setenv("LANDWELLE_ADMIN_PASSWORD", "Ein-sicheres-Testpasswort-2026")
|
|
t.Setenv("LANDWELLE_ADMIN_PASSWORD_HASH", "")
|
|
|
|
settings := &settingsStore{path: t.TempDir() + "/settings.json", settings: defaultRuntimeSettings(defaultStreamURL)}
|
|
runtime := settings.Get()
|
|
runtime.AzuraCast.Enabled = false
|
|
settings.settings = runtime
|
|
now := newNowPlayingService(settings, log.New(io.Discard, "", 0))
|
|
downloads := &downloadStore{dir: t.TempDir(), meta: make(map[string]downloadMetadata)}
|
|
handler, err := newAdminHandler(log.New(io.Discard, "", 0), NewWeatherService(), pageData(), settings, now, &programStore{path: t.TempDir() + "/program.json", items: defaultProgram()}, downloads)
|
|
if err != nil {
|
|
t.Fatalf("newAdminHandler: %v", err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
handler.register(mux)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "http://example.test/admin", nil)
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/admin/login" {
|
|
t.Fatalf("expected login redirect, got %d %q", rec.Code, rec.Header().Get("Location"))
|
|
}
|
|
|
|
form := url.Values{"username": {"admin"}, "password": {"Ein-sicheres-Testpasswort-2026"}}
|
|
req = httptest.NewRequest(http.MethodPost, "http://example.test/admin/login", strings.NewReader(form.Encode()))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
rec = httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, req)
|
|
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/admin" {
|
|
t.Fatalf("expected dashboard redirect, got %d %q", rec.Code, rec.Header().Get("Location"))
|
|
}
|
|
cookies := rec.Result().Cookies()
|
|
if len(cookies) == 0 || cookies[0].Name != adminCookieName || !cookies[0].HttpOnly {
|
|
t.Fatalf("secure admin session cookie missing: %#v", cookies)
|
|
}
|
|
}
|