382 lines
12 KiB
Go
382 lines
12 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"lektorenplan/store"
|
|
)
|
|
|
|
// dispatchRange is a range with seeded demo services and liturgical days.
|
|
const dispatchRange = "start=2026-03-01&end=2026-03-31"
|
|
|
|
func postForm(t *testing.T, client *http.Client, target string, form url.Values) *http.Response {
|
|
t.Helper()
|
|
resp, err := client.PostForm(target, form)
|
|
if err != nil {
|
|
t.Fatalf("POST %s: %v", target, err)
|
|
}
|
|
return resp
|
|
}
|
|
|
|
func TestDispatchPageAndPreferences(t *testing.T) {
|
|
ts, st := newTestServer(t)
|
|
client := newClient(t)
|
|
login(t, client, ts.URL, "editor", "editor123")
|
|
|
|
body := get(t, client, ts.URL+"/dispatch?"+dispatchRange, http.StatusOK)
|
|
if !strings.Contains(body, "Versendung") {
|
|
t.Fatal("dispatch page is missing its heading")
|
|
}
|
|
csrf := extractCSRF(t, body)
|
|
|
|
ctx := context.Background()
|
|
tenants, err := st.Tenants(ctx)
|
|
if err != nil || len(tenants) == 0 {
|
|
t.Fatalf("tenants: %v", err)
|
|
}
|
|
tenantID := ""
|
|
for _, tenant := range tenants {
|
|
people, _ := st.People(ctx, tenant.ID)
|
|
for _, person := range people {
|
|
if person.ID == "p-markus" {
|
|
tenantID = tenant.ID
|
|
}
|
|
}
|
|
}
|
|
if tenantID == "" {
|
|
t.Fatal("demo tenant with p-markus not found")
|
|
}
|
|
|
|
resp := postForm(t, client, ts.URL+"/dispatch/save", url.Values{
|
|
"csrf": {csrf},
|
|
"start": {"2026-03-01"},
|
|
"end": {"2026-03-31"},
|
|
"intro": {"Liebe Leute, anbei der Plan von {start} bis {end}."},
|
|
"greeting": {"Mit Grüßen"},
|
|
"channel_p-markus": {"print"},
|
|
"variant_p-markus": {"full"},
|
|
"cover_p-markus": {"1"},
|
|
})
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("save dispatch = %d, want 303", resp.StatusCode)
|
|
}
|
|
|
|
preferences, err := st.DispatchPreferences(ctx, tenantID)
|
|
if err != nil {
|
|
t.Fatalf("preferences: %v", err)
|
|
}
|
|
preference, ok := preferences["p-markus"]
|
|
if !ok {
|
|
t.Fatal("p-markus preference was not stored")
|
|
}
|
|
if preference.Channel != "print" || preference.Variant != "full" || !preference.Cover {
|
|
t.Fatalf("unexpected preference: %+v", preference)
|
|
}
|
|
if got := st.TenantSetting(ctx, tenantID, dispatchIntroKey); !strings.Contains(got, "{start}") {
|
|
t.Fatalf("intro setting = %q", got)
|
|
}
|
|
if got := st.TenantSetting(ctx, tenantID, dispatchGreetingKey); got != "Mit Grüßen" {
|
|
t.Fatalf("greeting setting = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestDispatchRequiresEditor(t *testing.T) {
|
|
ts, _ := newTestServer(t)
|
|
reader := newClient(t)
|
|
login(t, reader, ts.URL, "reader", "reader123")
|
|
get(t, reader, ts.URL+"/dispatch", http.StatusForbidden)
|
|
|
|
anonymous := newClient(t)
|
|
resp, err := anonymous.Get(ts.URL + "/dispatch")
|
|
if err != nil {
|
|
t.Fatalf("GET /dispatch: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("anonymous /dispatch = %d, want 303", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestDispatchPrintPacketAndLog(t *testing.T) {
|
|
ts, st := newTestServer(t)
|
|
client := newClient(t)
|
|
login(t, client, ts.URL, "editor", "editor123")
|
|
|
|
body := get(t, client, ts.URL+"/dispatch?"+dispatchRange, http.StatusOK)
|
|
csrf := extractCSRF(t, body)
|
|
|
|
resp := postForm(t, client, ts.URL+"/dispatch/print", url.Values{
|
|
"csrf": {csrf},
|
|
"start": {"2026-03-01"},
|
|
"end": {"2026-03-31"},
|
|
"channel_p-maria": {"print"},
|
|
"variant_p-maria": {"compact"},
|
|
"cover_p-maria": {"1"},
|
|
})
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("print = %d, want 200\n%s", resp.StatusCode, firstBytes(raw, 300))
|
|
}
|
|
if contentType := resp.Header.Get("Content-Type"); contentType != "application/pdf" {
|
|
t.Fatalf("content type = %q", contentType)
|
|
}
|
|
raw, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatalf("read PDF: %v", err)
|
|
}
|
|
if !strings.HasPrefix(string(raw), "%PDF") {
|
|
t.Fatalf("response is not a PDF: %q", firstBytes(raw, 20))
|
|
}
|
|
|
|
ctx := context.Background()
|
|
tenants, _ := st.Tenants(ctx)
|
|
found := false
|
|
for _, tenant := range tenants {
|
|
entries, err := st.DispatchLog(ctx, tenant.ID, 20)
|
|
if err != nil {
|
|
t.Fatalf("dispatch log: %v", err)
|
|
}
|
|
for _, entry := range entries {
|
|
if entry.PersonID == "p-maria" && entry.Channel == "print" && entry.Status == "pdf" {
|
|
found = true
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("print dispatch was not logged")
|
|
}
|
|
}
|
|
|
|
func TestDispatchSendWithoutSMTPIsRejected(t *testing.T) {
|
|
ts, st := newTestServer(t)
|
|
client := newClient(t)
|
|
login(t, client, ts.URL, "editor", "editor123")
|
|
|
|
body := get(t, client, ts.URL+"/dispatch?"+dispatchRange, http.StatusOK)
|
|
csrf := extractCSRF(t, body)
|
|
|
|
resp := postForm(t, client, ts.URL+"/dispatch/send", url.Values{
|
|
"csrf": {csrf},
|
|
"start": {"2026-03-01"},
|
|
"end": {"2026-03-31"},
|
|
"channel_p-anna": {"email"},
|
|
"variant_p-anna": {"compact"},
|
|
})
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("send without SMTP = %d, want 303", resp.StatusCode)
|
|
}
|
|
if strings.Contains(resp.Header.Get("Location"), "dispatch") == false {
|
|
t.Fatalf("redirect = %q, want dispatch page", resp.Header.Get("Location"))
|
|
}
|
|
|
|
// A refused send must not be logged as successful.
|
|
ctx := context.Background()
|
|
tenants, _ := st.Tenants(ctx)
|
|
for _, tenant := range tenants {
|
|
entries, _ := st.DispatchLog(ctx, tenant.ID, 10)
|
|
for _, entry := range entries {
|
|
if entry.Status == "sent" {
|
|
t.Fatalf("unexpected sent log entry: %+v", entry)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEditorManagesTenantSMTP(t *testing.T) {
|
|
ts, st := newTestServer(t)
|
|
client := newClient(t)
|
|
login(t, client, ts.URL, "editor", "editor123")
|
|
|
|
body := get(t, client, ts.URL+"/settings", http.StatusOK)
|
|
if !strings.Contains(body, "E-Mail-Versand (SMTP)") {
|
|
t.Fatal("editor settings do not offer SMTP fields")
|
|
}
|
|
csrf := extractCSRF(t, body)
|
|
|
|
resp := postForm(t, client, ts.URL+"/settings/save", url.Values{
|
|
"csrf": {csrf},
|
|
"smtp_host": {"smtp.example.org"},
|
|
"smtp_port": {"587"},
|
|
"smtp_username": {"plan"},
|
|
"smtp_password": {"secret"},
|
|
"smtp_from_address": {"plan@example.org"},
|
|
"smtp_from_name": {"Pfarrei"},
|
|
"smtp_encryption": {"starttls"},
|
|
})
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusSeeOther {
|
|
t.Fatalf("save settings = %d, want 303", resp.StatusCode)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
tenants, _ := st.Tenants(ctx)
|
|
found := false
|
|
for _, tenant := range tenants {
|
|
if got := st.TenantSetting(ctx, tenant.ID, "smtp.host"); got == "smtp.example.org" {
|
|
t.Fatalf("SMTP host was stored as plaintext tenant setting")
|
|
}
|
|
if st.SecretExists(ctx, tenant.ID, store.ScopeTenant, tenant.ID, "smtp.host") {
|
|
found = true
|
|
if !st.SecretIsInitialized(ctx, tenant.ID) {
|
|
t.Fatal("tenant secrets are not marked as initialized")
|
|
}
|
|
editor, err := st.UserByUsername(ctx, "editor")
|
|
if err != nil {
|
|
t.Fatalf("editor account: %v", err)
|
|
}
|
|
keyring, err := st.EnsureUserKeys(ctx, editor.ID, "editor1231")
|
|
if err != nil {
|
|
t.Fatalf("unlock editor keys: %v", err)
|
|
}
|
|
if err := st.UnlockAccessibleKeys(ctx, keyring); err != nil {
|
|
t.Fatalf("unwrap data keys: %v", err)
|
|
}
|
|
secretCtx := store.WithKeyring(ctx, keyring)
|
|
if got, ok := st.SecretValue(secretCtx, tenant.ID, store.ScopeTenant, tenant.ID, "smtp.host"); !ok || got != "smtp.example.org" {
|
|
t.Fatalf("smtp host = %q, ok=%v", got, ok)
|
|
}
|
|
if got, ok := st.SecretValue(secretCtx, tenant.ID, store.ScopeTenant, tenant.ID, "smtp.port"); !ok || got != "587" {
|
|
t.Fatalf("smtp port = %q, ok=%v", got, ok)
|
|
}
|
|
if got, ok := st.SecretValue(secretCtx, tenant.ID, store.ScopeTenant, tenant.ID, "smtp.password"); !ok || got != "secret" {
|
|
t.Fatalf("smtp password = %q, ok=%v", got, ok)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("SMTP host was not stored as an encrypted secret")
|
|
}
|
|
|
|
// The dispatch page now reports a ready SMTP configuration.
|
|
page := get(t, client, ts.URL+"/dispatch", http.StatusOK)
|
|
if !strings.Contains(page, "SMTP bereit") {
|
|
t.Fatal("dispatch page does not reflect the SMTP configuration")
|
|
}
|
|
if !strings.Contains(page, `formaction="/dispatch/send"`) {
|
|
t.Fatal("dispatch page does not offer direct sending with SMTP configured")
|
|
}
|
|
if strings.Contains(page, "data-mailto-link") {
|
|
t.Fatal("dispatch page must not offer mailto drafts when SMTP is configured")
|
|
}
|
|
|
|
// Readers may not change these settings.
|
|
reader := newClient(t)
|
|
login(t, reader, ts.URL, "reader", "reader123")
|
|
readerPage := get(t, reader, ts.URL+"/settings", http.StatusOK)
|
|
if strings.Contains(readerPage, "E-Mail-Versand (SMTP)") {
|
|
t.Fatal("reader settings must not expose SMTP")
|
|
}
|
|
}
|
|
|
|
func TestDispatchMailtoEncodesDraft(t *testing.T) {
|
|
href := dispatchMailto("anna.berg@example.org", "Plan März", "Hallo & Grüße\nZeile 2")
|
|
if !strings.HasPrefix(href, "mailto:") {
|
|
t.Fatalf("mailto link = %q", href)
|
|
}
|
|
if !strings.Contains(href, "subject=Plan%20M%C3%A4rz") {
|
|
t.Fatalf("subject is not encoded: %q", href)
|
|
}
|
|
body := "body=Hallo%20%26%20Gr%C3%BC%C3%9Fe%0D%0AZeile%202"
|
|
if !strings.Contains(href, body) {
|
|
t.Fatalf("body is not encoded: %q", href)
|
|
}
|
|
if strings.Contains(href, "+") {
|
|
t.Fatalf("mailto link must not use plus signs for spaces: %q", href)
|
|
}
|
|
}
|
|
|
|
func TestDispatchOffersMailtoAndCopyWithoutSMTP(t *testing.T) {
|
|
ts, _ := newTestServer(t)
|
|
client := newClient(t)
|
|
login(t, client, ts.URL, "editor", "editor123")
|
|
|
|
body := get(t, client, ts.URL+"/dispatch?"+dispatchRange, http.StatusOK)
|
|
if !strings.Contains(body, "Kein SMTP-Konto hinterlegt") {
|
|
t.Fatal("dispatch page is missing the mailto fallback notice")
|
|
}
|
|
if strings.Contains(body, `formaction="/dispatch/send"`) {
|
|
t.Fatal("dispatch page must not offer direct sending without SMTP")
|
|
}
|
|
if !strings.Contains(body, "data-copy-person") {
|
|
t.Fatal("dispatch page is missing per-person copy controls")
|
|
}
|
|
if !strings.Contains(body, "data-copy-shared") {
|
|
t.Fatal("dispatch page is missing the shared-text copy control")
|
|
}
|
|
if !strings.Contains(body, "data-mailto-link") || !strings.Contains(body, `data-mailto-email="anna.berg@example.org"`) {
|
|
t.Fatal("dispatch page is missing the mailto fallback for recipients")
|
|
}
|
|
if !strings.Contains(body, "mailto:anna.berg@example.org?subject=") {
|
|
t.Fatal("dispatch page is missing a prepared mailto link")
|
|
}
|
|
}
|
|
|
|
func TestGuestAccessIsReadOnlyAndAnonymous(t *testing.T) {
|
|
ts, st := newTestServer(t)
|
|
client := newClient(t)
|
|
|
|
body := get(t, client, ts.URL+"/guest", http.StatusOK)
|
|
if !strings.Contains(body, "Gastzugang") {
|
|
t.Fatal("guest page is missing the guest badge")
|
|
}
|
|
if strings.Contains(body, "/settings") {
|
|
t.Fatal("guest page must not link to settings")
|
|
}
|
|
if !strings.Contains(body, "schott.erzabtei-beuron.de") {
|
|
t.Fatal("guest page should link the liturgical readings")
|
|
}
|
|
if strings.Contains(body, "Anna Berg") || strings.Contains(body, "Markus Weber") {
|
|
t.Fatal("guest page must not name assigned people")
|
|
}
|
|
if !strings.Contains(body, "Lektor") && !strings.Contains(body, "Kommunionhelfer") {
|
|
t.Fatal("guest page should still show assignment roles")
|
|
}
|
|
|
|
// Guest data must never contain person records.
|
|
ctx := context.Background()
|
|
tenants, _ := st.Tenants(ctx)
|
|
people, _ := st.People(ctx, tenants[0].ID)
|
|
for _, person := range people {
|
|
if person.Email != "" && strings.Contains(body, person.Email) {
|
|
t.Fatalf("guest page leaks e-mail address %q", person.Email)
|
|
}
|
|
}
|
|
|
|
// The guest range picker and tenant switch keep working.
|
|
get(t, client, ts.URL+"/guest?"+dispatchRange, http.StatusOK)
|
|
get(t, client, ts.URL+"/guest?tid="+url.QueryEscape(tenants[len(tenants)-1].ID), http.StatusOK)
|
|
|
|
// Write routes stay protected.
|
|
loginPage := get(t, client, ts.URL+"/login", http.StatusOK)
|
|
if !strings.Contains(loginPage, `href="/guest"`) {
|
|
t.Fatal("login page is missing the guest access button")
|
|
}
|
|
}
|
|
|
|
func TestGuestHandlerDoesNotExposePeople(t *testing.T) {
|
|
ts, _ := newTestServer(t)
|
|
resp, err := http.Get(ts.URL + "/guest?start=2025-01-01&end=2025-12-31")
|
|
if err != nil {
|
|
t.Fatalf("GET /guest: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
raw, _ := io.ReadAll(resp.Body)
|
|
body := string(raw)
|
|
if !strings.Contains(body, "Liturgie") {
|
|
t.Fatal("guest year plan is missing liturgical data")
|
|
}
|
|
for _, leaked := range []string{"mailto:", "/team", "passwort", "SMTP"} {
|
|
if strings.Contains(strings.ToLower(body), strings.ToLower(leaked)) {
|
|
t.Fatalf("guest page contains %q", leaked)
|
|
}
|
|
}
|
|
}
|