package cabana_test import ( "fmt" "net/http" "strings" "testing" "testing/fstest" "git.golem15.com/golem15/summercms/modules/postcard" ) func TestMailSave(t *testing.T) { env := newMailScreensEnv(t, nil) _, tok := env.admin(t, "mail-save", true, "") tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome) layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout) path := fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID) before := env.expect(t, http.StatusOK, http.MethodGet, path, tok, nil) beforeSub := fmt.Sprint(before["data"].(map[string]any)["subject"]) cases := []struct { name string body map[string]any want string }{ {"parse", map[string]any{"subject": "Hi", "body": "Hello {{ .name", "layout": layoutID}, "body"}, {"render", map[string]any{"subject": "Hi", "body": "{{ len 5 }}", "layout": layoutID}, "body"}, {"unsafe", map[string]any{"subject": "Hi", "body": "Hi\n\n", "layout": layoutID}, "body"}, {"subject_line", map[string]any{"subject": "Hi\nthere", "body": "Fine.", "layout": layoutID}, "subject"}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { rec := env.call(t, http.MethodPut, path, tok, tc.body) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("status=%d want 422 body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), tc.want) { t.Fatalf("422 body missing field %q: %s", tc.want, rec.Body.String()) } after := env.expect(t, http.StatusOK, http.MethodGet, path, tok, nil) if fmt.Sprint(after["data"].(map[string]any)["subject"]) != beforeSub { t.Fatal("row changed after refused save") } }) } t.Run("brand_colour", func(t *testing.T) { rec := env.call(t, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"button_primary_bg": "red"}) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("brand 422 = %d %s", rec.Code, rec.Body.String()) } }) t.Run("logo_missing", func(t *testing.T) { rec := env.call(t, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"logo_file_id": 999999}) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("status=%d want 422", rec.Code) } }) } func TestMailDrift(t *testing.T) { env := newMailScreensEnv(t, nil) _, tok := env.admin(t, "mail-drift", true, "") tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcomeEn) layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout) path := fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID) env.expect(t, http.StatusOK, http.MethodPut, path, tok, map[string]any{ "subject": "Custom {{ .name }}", "body": "Custom body for {{ .name }}", "layout": layoutID, }) // A second process boots with a changed file and syncs the index. files := mailBlogFiles() files["views/mail/welcome-en.htm"] = &fstest.MapFile{Data: []byte(`subject = "Welcome, {{ .name }}" description = "Sent after registration (English)" layout = "blog" sample.name = "Ada" == Hi **{{ .name }}**, thanks for joining the Acme blog. `)} next := newMailScreensEnvWith(t, mailEnvOptions{files: files, keep: true}) list := next.expect(t, http.StatusOK, http.MethodGet, mailTemplatesPath+"?filter[status]=outdated", tok, nil) rows := listRows(t, list) if rows[blogWelcomeEn] == nil { t.Fatalf("outdated filter missing row: %#v", list["data"]) } if got := fmt.Sprint(rowStates(next.expect(t, http.StatusOK, http.MethodGet, mailTemplatesPath, tok, nil), tmplID)); got != "[customized outdated]" { t.Fatalf("drifted row states = %s", got) } next.expect(t, http.StatusOK, http.MethodPut, path, tok, map[string]any{ "subject": "Custom again {{ .name }}", "body": "Custom body for {{ .name }}", "layout": layoutID, }) if got := fmt.Sprint(rowStates(next.expect(t, http.StatusOK, http.MethodGet, mailTemplatesPath, tok, nil), tmplID)); got != "[customized]" { t.Fatalf("reviewed row states = %s", got) } } func TestMailReset(t *testing.T) { env := newMailScreensEnv(t, nil) _, tok := env.admin(t, "mail-reset", true, "") tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome) layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout) path := fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID) env.expect(t, http.StatusOK, http.MethodPut, path, tok, map[string]any{ "subject": "Custom", "body": "Body", "layout": layoutID, }) env.expect(t, http.StatusOK, http.MethodPost, path+"/actions/reset", tok, map[string]any{}) got := env.expect(t, http.StatusOK, http.MethodGet, path, tok, nil) if fmt.Sprint(got["data"].(map[string]any)["subject"]) == "Custom" { t.Fatal("subject still custom after reset") } } func TestMailLayoutDelete(t *testing.T) { env := newMailScreensEnv(t, nil) _, tok := env.admin(t, "mail-laydel", true, "") if err := env.gdb.Exec(`INSERT INTO ` + postcard.LayoutsTable + ` (code, source, name, content_html, content_text, is_custom) VALUES ('promo-del', NULL, 'Promo Del', '
{{ .Content }}
', '{{ .Content }}', TRUE)`).Error; err != nil { t.Fatal(err) } id := env.mailRowID(t, postcard.LayoutsTable, "promo-del") tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome) if err := env.gdb.Exec(`UPDATE `+postcard.TemplatesTable+` SET layout_id = ?, is_custom = TRUE WHERE id = ?`, id, tmplID).Error; err != nil { t.Fatal(err) } rec := env.call(t, http.MethodDelete, fmt.Sprintf("%s/%d", mailLayoutsPath, id), tok, nil) if rec.Code != http.StatusForbidden { t.Fatalf("in-use delete status=%d want 403 body=%s", rec.Code, rec.Body.String()) } if !strings.Contains(rec.Body.String(), "Promo Del") && !strings.Contains(rec.Body.String(), "1") { t.Fatalf("403 missing name/count: %s", rec.Body.String()) } fileID := env.mailRowID(t, postcard.LayoutsTable, blogLayout) form := env.expect(t, http.StatusOK, http.MethodGet, fmt.Sprintf("%s/%d", mailLayoutsPath, fileID), tok, nil) meta, _ := form["meta"].(map[string]any) if deletable, ok := meta["deletable"].(bool); ok && deletable { t.Fatal("file layout marked deletable") } } func TestMailBrand(t *testing.T) { env := newMailScreensEnv(t, nil) _, tok := env.admin(t, "mail-brand-unit", true, "") env.expect(t, http.StatusOK, http.MethodGet, "/settings/mail_brand", tok, nil) env.expect(t, http.StatusOK, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"button_primary_bg": "#112233"}) env.expect(t, http.StatusUnprocessableEntity, http.MethodPut, "/settings/mail_brand", tok, map[string]any{"button_primary_bg": "nope"}) }