1. Version Versendung
This commit is contained in:
1 parent
d7113afa5f
commit
781aa20253
31 files changed
+4023
-10
No files matched your search
@@ -0,0 +1,309 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 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 st.TenantSetting(ctx, tenant.ID, "smtp.host") == "smtp.example.org" {
|
||||
found = true
|
||||
if got := st.TenantSetting(ctx, tenant.ID, "smtp.port"); got != "587" {
|
||||
t.Fatalf("smtp port = %q", got)
|
||||
}
|
||||
if got := st.TenantSetting(ctx, tenant.ID, "smtp.password"); got != "secret" {
|
||||
t.Fatalf("smtp password = %q", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("SMTP host was not stored as a tenant setting")
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
|
||||
// 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 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user