sabisan/internal/app/test_helpers_test.go
V fac53d7b85
All checks were successful
Publish / Test, build, and push image (push) Successful in 3m38s
Refactor app and store layout
2026-05-17 00:03:50 +01:00

52 lines
1.2 KiB
Go

package app
import (
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
"archi_folio/internal/store"
)
func newTestServer(t *testing.T) *Server {
t.Helper()
dir := t.TempDir()
st, err := store.Open(filepath.Join(dir, "app.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = st.Close() })
if err := st.Migrate("admin", "changeme"); err != nil {
t.Fatal(err)
}
srv, err := New(Config{
DatabasePath: filepath.Join(dir, "app.db"),
SessionSecret: "test-secret",
AdminUsername: "admin",
AdminPassword: "changeme",
UploadDir: filepath.Join(dir, "uploads"),
Version: "test-version",
}, st)
if err != nil {
t.Fatal(err)
}
return srv
}
func loginCookie(t *testing.T, handler http.Handler) *http.Cookie {
t.Helper()
form := url.Values{"username": {"admin"}, "password": {"changeme"}}
req := httptest.NewRequest(http.MethodPost, "/admin/login", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
cookies := rec.Result().Cookies()
if len(cookies) == 0 {
t.Fatal("expected login cookie")
}
return cookies[0]
}