package web import ( "io" "log" "net/http" "net/http/httptest" "net/url" "path/filepath" "strings" "testing" "gitea.dooplex.hu/admin/felhom-controller/internal/config" "gitea.dooplex.hu/admin/felhom-controller/internal/settings" "gitea.dooplex.hu/admin/felhom-controller/internal/stacks" ) // testPageServer builds a Server complete enough to render full pages through ServeHTTP // (templates loaded, settings + stack manager real, agent/hub absent). func testPageServer(t *testing.T) *Server { t.Helper() lg := log.New(io.Discard, "", 0) dir := t.TempDir() cfg := &config.Config{} cfg.Customer.ID = "test-customer" cfg.Customer.Name = "Teszt Ügyfél" cfg.Customer.Domain = "example.hu" cfg.Paths.StacksDir = filepath.Join(dir, "stacks") cfg.Paths.DataDir = filepath.Join(dir, "data") cfg.Stacks.ComposeCommand = "docker compose" // skip detection (not needed for page renders) sett, err := settings.Load(filepath.Join(dir, "settings.json"), lg) if err != nil { t.Fatalf("settings: %v", err) } mgr, err := stacks.NewManager(cfg, lg) if err != nil { t.Fatalf("stacks manager: %v", err) } s := &Server{cfg: cfg, settings: sett, stackMgr: mgr, logger: lg, version: "test"} s.loadTemplates() return s } func getPage(t *testing.T, s *Server, path string) *httptest.ResponseRecorder { t.Helper() rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, path, nil) s.ServeHTTP(rec, req) return rec } // TestSettingsSplitPagesRender (D1 Scenario A skeleton): the four pages respond 200. // Per-page unique-section markers are asserted once the template split lands (Part 2). func TestSettingsSplitPagesRender(t *testing.T) { s := testPageServer(t) for _, path := range []string{"/settings", "/settings/notifications", "/settings/security", "/storage"} { rec := getPage(t, s, path) if rec.Code != 200 { t.Errorf("GET %s = %d, want 200", path, rec.Code) } } } // TestWizardRoutesMovedWith301 (D1 Scenario E): old wizard URLs permanently redirect. func TestWizardRoutesMovedWith301(t *testing.T) { s := testPageServer(t) cases := map[string]string{ "/settings/storage/init": "/storage/init", "/settings/storage/attach": "/storage/attach", } for old, want := range cases { rec := getPage(t, s, old) if rec.Code != http.StatusMovedPermanently { t.Errorf("GET %s = %d, want 301", old, rec.Code) } if loc := rec.Header().Get("Location"); loc != want { t.Errorf("GET %s Location = %q, want %q", old, loc, want) } } // and the new URLs render for _, p := range []string{"/storage/init", "/storage/attach"} { if rec := getPage(t, s, p); rec.Code != 200 { t.Errorf("GET %s = %d, want 200", p, rec.Code) } } } // TestStorageActionRedirectsToStorage (D1 Scenario D): storage POST successes land on // /storage?storage_msg=..., and the flash renders there. func TestStorageActionRedirectsToStorage(t *testing.T) { s := testPageServer(t) if err := s.settings.AddStoragePath(settings.StoragePath{Path: "/mnt/test-drive", Label: "Teszt", Schedulable: true}); err != nil { t.Fatalf("add path: %v", err) } form := url.Values{"storage_path": {"/mnt/test-drive"}, "storage_label": {"Új Név"}} rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/settings/storage/label", strings.NewReader(form.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") s.settingsStorageLabelHandler(rec, req) if rec.Code != http.StatusFound { t.Fatalf("label POST = %d, want 302", rec.Code) } loc := rec.Header().Get("Location") if !strings.HasPrefix(loc, "/storage?storage_msg=success") { t.Errorf("Location = %q, want prefix /storage?storage_msg=success", loc) } // The flash renders on /storage rec2 := getPage(t, s, loc) if rec2.Code != 200 { t.Fatalf("GET %s = %d, want 200", loc, rec2.Code) } if !strings.Contains(rec2.Body.String(), "Megnevezés módosítva") { t.Errorf("/storage did not render the storage flash message") } } // TestPasswordErrorRerendersSecurityPage (D1 Scenario D): a wrong current password // re-renders the page carrying the password form with the inline error. func TestPasswordErrorRerendersSecurityPage(t *testing.T) { s := testPageServer(t) // enable auth so the password form path is active if err := s.settings.SetPasswordHash("$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy"); err != nil { // "password" t.Fatalf("set hash: %v", err) } form := url.Values{ "current_password": {"wrong-password"}, "new_password": {"newpassword123"}, "confirm_password": {"newpassword123"}, } rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodPost, "/settings/password", strings.NewReader(form.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") s.settingsPasswordHandler(rec, req) if rec.Code != 200 { t.Fatalf("password POST = %d, want 200 (inline error re-render)", rec.Code) } body := rec.Body.String() if !strings.Contains(body, "Hibás jelenlegi jelszó") { t.Error("missing inline error 'Hibás jelenlegi jelszó'") } if !strings.Contains(body, "Jelszó módosítás") { t.Error("re-rendered page lacks the password form section") } }