package cabana_test import ( "net/http" "testing" "time" "git.golem15.com/golem15/summercms/modules/cabana" "git.golem15.com/golem15/summercms/modules/cooler" "git.golem15.com/golem15/summercms/modules/postcard" ) type fakeAttempter struct { max int hits map[string]int } func (f *fakeAttempter) Attempt(key string, max int, _ time.Duration) (bool, int, time.Duration) { if f.hits == nil { f.hits = map[string]int{} } limit := f.max if max > 0 { limit = max } f.hits[key]++ if f.hits[key] > limit { return false, f.hits[key], 42 * time.Second } return true, f.hits[key], 0 } func TestMailTestSend(t *testing.T) { env := newMailScreensEnv(t, nil) tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome) layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout) values := map[string]any{"subject": "s", "body": "Hello {{ .name }}", "layout": layoutID} t.Run("recipient_is_admin_only", func(t *testing.T) { user, tok := env.admin(t, "mail-unit-send", true, "") before := len(env.driver.messages()) got := env.expect(t, http.StatusOK, http.MethodPost, "/mail/test-send", tok, map[string]any{ "mode": "template", "id": tmplID, "values": values, }) if got["data"].(map[string]any)["email"] != user.Email { t.Fatalf("email = %#v", got["data"]) } msgs := env.driver.messages() last := msgs[len(msgs)-1] if len(msgs) != before+1 || len(last.To) != 1 || last.To[0] != user.Email || len(last.Cc) != 0 || len(last.Bcc) != 0 { t.Fatalf("recipients To=%v Cc=%v Bcc=%v count=%d", last.To, last.Cc, last.Bcc, len(msgs)-before) } }) t.Run("recipient_keys_refused", func(t *testing.T) { _, tok := env.admin(t, "mail-unit-recip", true, "") for _, key := range []string{"to", "email", "recipient"} { body := map[string]any{"mode": "template", "id": tmplID, "values": values, key: "other@example.test"} rec := env.call(t, http.MethodPost, "/mail/test-send", tok, body) if rec.Code != http.StatusUnprocessableEntity { t.Fatalf("%s status=%d want 422", key, rec.Code) } } }) t.Run("token_principal_refused", func(t *testing.T) { user, _ := env.admin(t, "mail-unit-token", true, "") plain, err := cabana.IssueTestAPIToken(env.gdb, user.ID, []string{"*"}, nil) if err != nil { t.Fatal(err) } rec := env.call(t, http.MethodPost, "/mail/test-send", plain, map[string]any{ "mode": "template", "id": tmplID, "values": values, }) if rec.Code != http.StatusForbidden { t.Fatalf("status=%d want 403", rec.Code) } }) t.Run("rate_limit_and_independence", func(t *testing.T) { fake := &fakeAttempter{max: 5} if err := env.app.Publish[cooler.Attempter](fake); err != nil { t.Logf("publish Attempter: %v (using in-process fallback)", err) } _, tokA := env.admin(t, "mail-unit-lim-a", true, "") _, tokB := env.admin(t, "mail-unit-lim-b", true, "") body := map[string]any{"mode": "template", "id": tmplID, "values": values} for i := 0; i < 5; i++ { env.expect(t, http.StatusOK, http.MethodPost, "/mail/test-send", tokA, body) } rec := env.call(t, http.MethodPost, "/mail/test-send", tokA, body) if rec.Code != http.StatusTooManyRequests { t.Fatalf("sixth status=%d want 429 body=%s", rec.Code, rec.Body.String()) } if rec.Header().Get("Retry-After") == "" { t.Fatal("missing Retry-After") } env.expect(t, http.StatusOK, http.MethodPost, "/mail/test-send", tokB, body) }) t.Run("driver_failure_502", func(t *testing.T) { _, tok := env.admin(t, "mail-unit-fail", true, "") env.driver.fail.Store(true) t.Cleanup(func() { env.driver.fail.Store(false) }) rec := env.call(t, http.MethodPost, "/mail/test-send", tok, map[string]any{ "mode": "template", "id": tmplID, "values": values, }) if rec.Code != http.StatusBadGateway { t.Fatalf("status=%d want 502", rec.Code) } }) }