package web
import (
"html"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// W21 (rehearsal 2026-10-20): „switch on" with no household SMB password answered „Beállítás mentve. A megosztási
// szolgáltatás előkészítése folyamatban…" — but reconcile never deploys without the password („deploy skipped —
// household SMB password not set yet") and the page stayed „Állapot: áll" for ever. The answer and the page now ask
// for the password at once and say why the service waits; with a password set, the bring-up is unchanged.
func TestW21_SwitchOnWithoutPasswordAsksForItAtOnce(t *testing.T) {
s := i18nTestServer(t)
enable := func() string {
r := httptest.NewRequest(http.MethodPost, "/sharing/enable", strings.NewReader(url.Values{"enabled": {"on"}}.Encode()))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
s.sharingEnableHandler(w, r)
if w.Code != http.StatusSeeOther {
t.Fatalf("enable: %d", w.Code)
}
return w.Header().Get("Location")
}
loc := enable()
if !strings.Contains(loc, "flash.sharing.saved_needs_password") {
t.Fatalf("switch-on without a password must say the service waits for it: %q", loc)
}
for lang, want := range map[string][2]string{
"hu": {"A megosztás akkor indul el, amikor beállítod a megosztási jelszót.", "Állíts be egy megosztási jelszót — enélkül a megosztás nem indul el."},
"en": {"Sharing starts when you set the sharing password.", "Set a sharing password. Without it, sharing does not start."},
} {
r := httptest.NewRequest(http.MethodGet, loc, nil)
r.AddCookie(&http.Cookie{Name: langCookieName, Value: lang})
w := httptest.NewRecorder()
s.sharingPageHandler(w, r)
page := html.UnescapeString(w.Body.String())
if !strings.Contains(page, want[0]) || !strings.Contains(page, want[1]) || !strings.Contains(page, `id="smb-needs-password"`) {
t.Errorf("%s: the page does not ask for the password at once", lang)
}
if strings.Contains(page, "szolgáltatás előkészítése folyamatban") || strings.Contains(page, "is being prepared") {
t.Errorf("%s: the page still says the service is being prepared", lang)
}
}
// with the password set the page asks for nothing (the bring-up path is the existing one, pinned elsewhere)
if err := s.settings.SetSMBUserSet(true); err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
s.sharingPageHandler(w, httptest.NewRequest(http.MethodGet, "/sharing", nil))
if strings.Contains(w.Body.String(), `id="smb-needs-password"`) {
t.Fatal("the needs-password alert shows although a password is set")
}
}